blob: fbd855bcd9f023a18d7995afb0851a6b59cabeff [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
Francois Romieuda78dbf2012-01-26 14:18:23 +0100670enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100671 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100672 RTL_FLAG_TASK_SLOW_PENDING,
673 RTL_FLAG_TASK_RESET_PENDING,
674 RTL_FLAG_TASK_PHY_PENDING,
675 RTL_FLAG_MAX
676};
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678struct rtl8169_private {
679 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200680 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000681 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700682 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200683 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700684 u16 txd_version;
685 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
687 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
688 u32 dirty_rx;
689 u32 dirty_tx;
690 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
691 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
692 dma_addr_t TxPhyAddr;
693 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000694 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 struct timer_list timer;
697 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100698
699 u16 event_slow;
françois romieuc0e45c12011-01-03 15:08:04 +0000700
701 struct mdio_ops {
702 void (*write)(void __iomem *, int, int);
703 int (*read)(void __iomem *, int);
704 } mdio_ops;
705
françois romieu065c27c2011-01-03 15:08:12 +0000706 struct pll_power_ops {
707 void (*down)(struct rtl8169_private *);
708 void (*up)(struct rtl8169_private *);
709 } pll_power_ops;
710
Francois Romieud58d46b2011-05-03 16:38:29 +0200711 struct jumbo_ops {
712 void (*enable)(struct rtl8169_private *);
713 void (*disable)(struct rtl8169_private *);
714 } jumbo_ops;
715
Oliver Neukum54405cd2011-01-06 21:55:13 +0100716 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200717 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000718 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100719 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000720 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800722 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100723
724 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100725 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
726 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100727 struct work_struct work;
728 } wk;
729
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200730 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200731
732 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800733 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000734 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400735 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000736
Francois Romieub6ffd972011-06-17 17:00:05 +0200737 struct rtl_fw {
738 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200739
740#define RTL_VER_SIZE 32
741
742 char version[RTL_VER_SIZE];
743
744 struct rtl_fw_phy_action {
745 __le32 *code;
746 size_t size;
747 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200748 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300749#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750};
751
Ralf Baechle979b6c12005-06-13 14:30:40 -0700752MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700755MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200756module_param_named(debug, debug.msg_enable, int, 0);
757MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758MODULE_LICENSE("GPL");
759MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000760MODULE_FIRMWARE(FIRMWARE_8168D_1);
761MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000762MODULE_FIRMWARE(FIRMWARE_8168E_1);
763MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400764MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800765MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800766MODULE_FIRMWARE(FIRMWARE_8168F_1);
767MODULE_FIRMWARE(FIRMWARE_8168F_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000770static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
771 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100772static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100774static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100776static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200778static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200779static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Francois Romieu99f252b2007-04-02 22:59:59 +0200780static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700781static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Francois Romieuda78dbf2012-01-26 14:18:23 +0100783static void rtl_lock_work(struct rtl8169_private *tp)
784{
785 mutex_lock(&tp->wk.mutex);
786}
787
788static void rtl_unlock_work(struct rtl8169_private *tp)
789{
790 mutex_unlock(&tp->wk.mutex);
791}
792
Francois Romieud58d46b2011-05-03 16:38:29 +0200793static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
794{
795 int cap = pci_pcie_cap(pdev);
796
797 if (cap) {
798 u16 ctl;
799
800 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
801 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
802 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
803 }
804}
805
françois romieub646d902011-01-03 15:08:21 +0000806static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
807{
808 void __iomem *ioaddr = tp->mmio_addr;
809 int i;
810
811 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
812 for (i = 0; i < 20; i++) {
813 udelay(100);
814 if (RTL_R32(OCPAR) & OCPAR_FLAG)
815 break;
816 }
817 return RTL_R32(OCPDR);
818}
819
820static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
821{
822 void __iomem *ioaddr = tp->mmio_addr;
823 int i;
824
825 RTL_W32(OCPDR, data);
826 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
827 for (i = 0; i < 20; i++) {
828 udelay(100);
829 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
830 break;
831 }
832}
833
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800834static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000835{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800836 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000837 int i;
838
839 RTL_W8(ERIDR, cmd);
840 RTL_W32(ERIAR, 0x800010e8);
841 msleep(2);
842 for (i = 0; i < 5; i++) {
843 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200844 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000845 break;
846 }
847
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800848 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000849}
850
851#define OOB_CMD_RESET 0x00
852#define OOB_CMD_DRIVER_START 0x05
853#define OOB_CMD_DRIVER_STOP 0x06
854
Francois Romieucecb5fd2011-04-01 10:21:07 +0200855static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
856{
857 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
858}
859
françois romieub646d902011-01-03 15:08:21 +0000860static void rtl8168_driver_start(struct rtl8169_private *tp)
861{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200862 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000863 int i;
864
865 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
866
Francois Romieucecb5fd2011-04-01 10:21:07 +0200867 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000868
françois romieub646d902011-01-03 15:08:21 +0000869 for (i = 0; i < 10; i++) {
870 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000871 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000872 break;
873 }
874}
875
876static void rtl8168_driver_stop(struct rtl8169_private *tp)
877{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200878 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000879 int i;
880
881 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
882
Francois Romieucecb5fd2011-04-01 10:21:07 +0200883 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000884
françois romieub646d902011-01-03 15:08:21 +0000885 for (i = 0; i < 10; i++) {
886 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000887 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000888 break;
889 }
890}
891
hayeswang4804b3b2011-03-21 01:50:29 +0000892static int r8168dp_check_dash(struct rtl8169_private *tp)
893{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200894 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000895
Francois Romieucecb5fd2011-04-01 10:21:07 +0200896 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000897}
françois romieub646d902011-01-03 15:08:21 +0000898
françois romieu4da19632011-01-03 15:07:55 +0000899static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 int i;
902
Francois Romieua6baf3a2007-11-08 23:23:21 +0100903 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Francois Romieu23714082006-01-29 00:49:09 +0100905 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100906 /*
907 * Check if the RTL8169 has completed writing to the specified
908 * MII register.
909 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200910 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 break;
Francois Romieu23714082006-01-29 00:49:09 +0100912 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700914 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700915 * According to hardware specs a 20us delay is required after write
916 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700917 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700918 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
françois romieu4da19632011-01-03 15:07:55 +0000921static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 int i, value = -1;
924
Francois Romieua6baf3a2007-11-08 23:23:21 +0100925 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Francois Romieu23714082006-01-29 00:49:09 +0100927 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100928 /*
929 * Check if the RTL8169 has completed retrieving data from
930 * the specified MII register.
931 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100933 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 break;
935 }
Francois Romieu23714082006-01-29 00:49:09 +0100936 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700938 /*
939 * According to hardware specs a 20us delay is required after read
940 * complete indication, but before sending next command.
941 */
942 udelay(20);
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return value;
945}
946
françois romieuc0e45c12011-01-03 15:08:04 +0000947static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
948{
949 int i;
950
951 RTL_W32(OCPDR, data |
952 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
953 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
954 RTL_W32(EPHY_RXER_NUM, 0);
955
956 for (i = 0; i < 100; i++) {
957 mdelay(1);
958 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
959 break;
960 }
961}
962
963static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
964{
965 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
966 (value & OCPDR_DATA_MASK));
967}
968
969static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
970{
971 int i;
972
973 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
974
975 mdelay(1);
976 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
977 RTL_W32(EPHY_RXER_NUM, 0);
978
979 for (i = 0; i < 100; i++) {
980 mdelay(1);
981 if (RTL_R32(OCPAR) & OCPAR_FLAG)
982 break;
983 }
984
985 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
986}
987
françois romieue6de30d2011-01-03 15:08:37 +0000988#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
989
990static void r8168dp_2_mdio_start(void __iomem *ioaddr)
991{
992 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
993}
994
995static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
996{
997 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
998}
999
1000static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
1001{
1002 r8168dp_2_mdio_start(ioaddr);
1003
1004 r8169_mdio_write(ioaddr, reg_addr, value);
1005
1006 r8168dp_2_mdio_stop(ioaddr);
1007}
1008
1009static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
1010{
1011 int value;
1012
1013 r8168dp_2_mdio_start(ioaddr);
1014
1015 value = r8169_mdio_read(ioaddr, reg_addr);
1016
1017 r8168dp_2_mdio_stop(ioaddr);
1018
1019 return value;
1020}
1021
françois romieu4da19632011-01-03 15:07:55 +00001022static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001023{
françois romieuc0e45c12011-01-03 15:08:04 +00001024 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001025}
1026
françois romieu4da19632011-01-03 15:07:55 +00001027static int rtl_readphy(struct rtl8169_private *tp, int location)
1028{
françois romieuc0e45c12011-01-03 15:08:04 +00001029 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +00001030}
1031
1032static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1033{
1034 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1035}
1036
1037static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001038{
1039 int val;
1040
françois romieu4da19632011-01-03 15:07:55 +00001041 val = rtl_readphy(tp, reg_addr);
1042 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001043}
1044
Francois Romieuccdffb92008-07-26 14:26:06 +02001045static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1046 int val)
1047{
1048 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001049
françois romieu4da19632011-01-03 15:07:55 +00001050 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001051}
1052
1053static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1054{
1055 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001056
françois romieu4da19632011-01-03 15:07:55 +00001057 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001058}
1059
Francois Romieudacf8152008-08-02 20:44:13 +02001060static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
1061{
1062 unsigned int i;
1063
1064 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1065 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1066
1067 for (i = 0; i < 100; i++) {
1068 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1069 break;
1070 udelay(10);
1071 }
1072}
1073
1074static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1075{
1076 u16 value = 0xffff;
1077 unsigned int i;
1078
1079 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1080
1081 for (i = 0; i < 100; i++) {
1082 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1083 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1084 break;
1085 }
1086 udelay(10);
1087 }
1088
1089 return value;
1090}
1091
1092static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1093{
1094 unsigned int i;
1095
1096 RTL_W32(CSIDR, value);
1097 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (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 break;
1103 udelay(10);
1104 }
1105}
1106
1107static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1108{
1109 u32 value = ~0x00;
1110 unsigned int i;
1111
1112 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1113 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1114
1115 for (i = 0; i < 100; i++) {
1116 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1117 value = RTL_R32(CSIDR);
1118 break;
1119 }
1120 udelay(10);
1121 }
1122
1123 return value;
1124}
1125
Hayes Wang133ac402011-07-06 15:58:05 +08001126static
1127void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1128{
1129 unsigned int i;
1130
1131 BUG_ON((addr & 3) || (mask == 0));
1132 RTL_W32(ERIDR, val);
1133 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1134
1135 for (i = 0; i < 100; i++) {
1136 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1137 break;
1138 udelay(100);
1139 }
1140}
1141
1142static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1143{
1144 u32 value = ~0x00;
1145 unsigned int i;
1146
1147 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1148
1149 for (i = 0; i < 100; i++) {
1150 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1151 value = RTL_R32(ERIDR);
1152 break;
1153 }
1154 udelay(100);
1155 }
1156
1157 return value;
1158}
1159
1160static void
1161rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1162{
1163 u32 val;
1164
1165 val = rtl_eri_read(ioaddr, addr, type);
1166 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1167}
1168
françois romieuc28aa382011-08-02 03:53:43 +00001169struct exgmac_reg {
1170 u16 addr;
1171 u16 mask;
1172 u32 val;
1173};
1174
1175static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1176 const struct exgmac_reg *r, int len)
1177{
1178 while (len-- > 0) {
1179 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1180 r++;
1181 }
1182}
1183
françois romieudaf9df62009-10-07 12:44:20 +00001184static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1185{
1186 u8 value = 0xff;
1187 unsigned int i;
1188
1189 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1190
1191 for (i = 0; i < 300; i++) {
1192 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1193 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1194 break;
1195 }
1196 udelay(100);
1197 }
1198
1199 return value;
1200}
1201
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001202static u16 rtl_get_events(struct rtl8169_private *tp)
1203{
1204 void __iomem *ioaddr = tp->mmio_addr;
1205
1206 return RTL_R16(IntrStatus);
1207}
1208
1209static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1210{
1211 void __iomem *ioaddr = tp->mmio_addr;
1212
1213 RTL_W16(IntrStatus, bits);
1214 mmiowb();
1215}
1216
1217static void rtl_irq_disable(struct rtl8169_private *tp)
1218{
1219 void __iomem *ioaddr = tp->mmio_addr;
1220
1221 RTL_W16(IntrMask, 0);
1222 mmiowb();
1223}
1224
Francois Romieu3e990ff2012-01-26 12:50:01 +01001225static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1226{
1227 void __iomem *ioaddr = tp->mmio_addr;
1228
1229 RTL_W16(IntrMask, bits);
1230}
1231
Francois Romieuda78dbf2012-01-26 14:18:23 +01001232#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1233#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1234#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1235
1236static void rtl_irq_enable_all(struct rtl8169_private *tp)
1237{
1238 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1239}
1240
françois romieu811fd302011-12-04 20:30:45 +00001241static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
françois romieu811fd302011-12-04 20:30:45 +00001243 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001245 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001246 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001247 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
françois romieu4da19632011-01-03 15:07:55 +00001250static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
françois romieu4da19632011-01-03 15:07:55 +00001252 void __iomem *ioaddr = tp->mmio_addr;
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return RTL_R32(TBICSR) & TBIReset;
1255}
1256
françois romieu4da19632011-01-03 15:07:55 +00001257static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
françois romieu4da19632011-01-03 15:07:55 +00001259 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
1261
1262static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1263{
1264 return RTL_R32(TBICSR) & TBILinkOk;
1265}
1266
1267static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1268{
1269 return RTL_R8(PHYstatus) & LinkStatus;
1270}
1271
françois romieu4da19632011-01-03 15:07:55 +00001272static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
françois romieu4da19632011-01-03 15:07:55 +00001274 void __iomem *ioaddr = tp->mmio_addr;
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1277}
1278
françois romieu4da19632011-01-03 15:07:55 +00001279static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 unsigned int val;
1282
françois romieu4da19632011-01-03 15:07:55 +00001283 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1284 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
Hayes Wang70090422011-07-06 15:58:06 +08001287static void rtl_link_chg_patch(struct rtl8169_private *tp)
1288{
1289 void __iomem *ioaddr = tp->mmio_addr;
1290 struct net_device *dev = tp->dev;
1291
1292 if (!netif_running(dev))
1293 return;
1294
1295 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1296 if (RTL_R8(PHYstatus) & _1000bpsF) {
1297 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1298 0x00000011, ERIAR_EXGMAC);
1299 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1300 0x00000005, ERIAR_EXGMAC);
1301 } else if (RTL_R8(PHYstatus) & _100bps) {
1302 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1303 0x0000001f, ERIAR_EXGMAC);
1304 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1305 0x00000005, ERIAR_EXGMAC);
1306 } else {
1307 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1308 0x0000001f, ERIAR_EXGMAC);
1309 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1310 0x0000003f, ERIAR_EXGMAC);
1311 }
1312 /* Reset packet filter */
1313 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1314 ERIAR_EXGMAC);
1315 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1316 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001317 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1318 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1319 if (RTL_R8(PHYstatus) & _1000bpsF) {
1320 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1321 0x00000011, ERIAR_EXGMAC);
1322 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1323 0x00000005, ERIAR_EXGMAC);
1324 } else {
1325 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1326 0x0000001f, ERIAR_EXGMAC);
1327 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1328 0x0000003f, ERIAR_EXGMAC);
1329 }
Hayes Wang70090422011-07-06 15:58:06 +08001330 }
1331}
1332
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001333static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001334 struct rtl8169_private *tp,
1335 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001338 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001339 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001340 if (pm)
1341 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001343 if (net_ratelimit())
1344 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001345 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001347 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001348 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001349 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
1352
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001353static void rtl8169_check_link_status(struct net_device *dev,
1354 struct rtl8169_private *tp,
1355 void __iomem *ioaddr)
1356{
1357 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1358}
1359
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001360#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1361
1362static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1363{
1364 void __iomem *ioaddr = tp->mmio_addr;
1365 u8 options;
1366 u32 wolopts = 0;
1367
1368 options = RTL_R8(Config1);
1369 if (!(options & PMEnable))
1370 return 0;
1371
1372 options = RTL_R8(Config3);
1373 if (options & LinkUp)
1374 wolopts |= WAKE_PHY;
1375 if (options & MagicPacket)
1376 wolopts |= WAKE_MAGIC;
1377
1378 options = RTL_R8(Config5);
1379 if (options & UWF)
1380 wolopts |= WAKE_UCAST;
1381 if (options & BWF)
1382 wolopts |= WAKE_BCAST;
1383 if (options & MWF)
1384 wolopts |= WAKE_MCAST;
1385
1386 return wolopts;
1387}
1388
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001389static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1390{
1391 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001392
Francois Romieuda78dbf2012-01-26 14:18:23 +01001393 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001394
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001395 wol->supported = WAKE_ANY;
1396 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001397
Francois Romieuda78dbf2012-01-26 14:18:23 +01001398 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001399}
1400
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001401static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001402{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001403 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001404 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001405 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001406 u32 opt;
1407 u16 reg;
1408 u8 mask;
1409 } cfg[] = {
1410 { WAKE_ANY, Config1, PMEnable },
1411 { WAKE_PHY, Config3, LinkUp },
1412 { WAKE_MAGIC, Config3, MagicPacket },
1413 { WAKE_UCAST, Config5, UWF },
1414 { WAKE_BCAST, Config5, BWF },
1415 { WAKE_MCAST, Config5, MWF },
1416 { WAKE_ANY, Config5, LanWake }
1417 };
1418
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001419 RTL_W8(Cfg9346, Cfg9346_Unlock);
1420
1421 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1422 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001423 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001424 options |= cfg[i].mask;
1425 RTL_W8(cfg[i].reg, options);
1426 }
1427
1428 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001429}
1430
1431static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1432{
1433 struct rtl8169_private *tp = netdev_priv(dev);
1434
Francois Romieuda78dbf2012-01-26 14:18:23 +01001435 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001436
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001437 if (wol->wolopts)
1438 tp->features |= RTL_FEATURE_WOL;
1439 else
1440 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001441 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001442
1443 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001444
françois romieuea809072010-11-08 13:23:58 +00001445 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1446
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001447 return 0;
1448}
1449
Francois Romieu31bd2042011-04-26 18:58:59 +02001450static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1451{
Francois Romieu85bffe62011-04-27 08:22:39 +02001452 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001453}
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455static void rtl8169_get_drvinfo(struct net_device *dev,
1456 struct ethtool_drvinfo *info)
1457{
1458 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001459 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Rick Jones68aad782011-11-07 13:29:27 +00001461 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1462 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1463 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001464 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001465 if (!IS_ERR_OR_NULL(rtl_fw))
1466 strlcpy(info->fw_version, rtl_fw->version,
1467 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
1470static int rtl8169_get_regs_len(struct net_device *dev)
1471{
1472 return R8169_REGS_SIZE;
1473}
1474
1475static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001476 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 struct rtl8169_private *tp = netdev_priv(dev);
1479 void __iomem *ioaddr = tp->mmio_addr;
1480 int ret = 0;
1481 u32 reg;
1482
1483 reg = RTL_R32(TBICSR);
1484 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1485 (duplex == DUPLEX_FULL)) {
1486 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1487 } else if (autoneg == AUTONEG_ENABLE)
1488 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1489 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001490 netif_warn(tp, link, dev,
1491 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 ret = -EOPNOTSUPP;
1493 }
1494
1495 return ret;
1496}
1497
1498static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001499 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
1501 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001502 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001503 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Hayes Wang716b50a2011-02-22 17:26:18 +08001505 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001508 int auto_nego;
1509
françois romieu4da19632011-01-03 15:07:55 +00001510 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001511 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1512 ADVERTISE_100HALF | ADVERTISE_100FULL);
1513
1514 if (adv & ADVERTISED_10baseT_Half)
1515 auto_nego |= ADVERTISE_10HALF;
1516 if (adv & ADVERTISED_10baseT_Full)
1517 auto_nego |= ADVERTISE_10FULL;
1518 if (adv & ADVERTISED_100baseT_Half)
1519 auto_nego |= ADVERTISE_100HALF;
1520 if (adv & ADVERTISED_100baseT_Full)
1521 auto_nego |= ADVERTISE_100FULL;
1522
françois romieu3577aa12009-05-19 10:46:48 +00001523 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1524
françois romieu4da19632011-01-03 15:07:55 +00001525 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001526 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1527
1528 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001529 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001530 if (adv & ADVERTISED_1000baseT_Half)
1531 giga_ctrl |= ADVERTISE_1000HALF;
1532 if (adv & ADVERTISED_1000baseT_Full)
1533 giga_ctrl |= ADVERTISE_1000FULL;
1534 } else if (adv & (ADVERTISED_1000baseT_Half |
1535 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001536 netif_info(tp, link, dev,
1537 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001538 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
françois romieu3577aa12009-05-19 10:46:48 +00001541 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001542
françois romieu4da19632011-01-03 15:07:55 +00001543 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1544 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001545 } else {
1546 giga_ctrl = 0;
1547
1548 if (speed == SPEED_10)
1549 bmcr = 0;
1550 else if (speed == SPEED_100)
1551 bmcr = BMCR_SPEED100;
1552 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001553 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001554
1555 if (duplex == DUPLEX_FULL)
1556 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001557 }
1558
françois romieu4da19632011-01-03 15:07:55 +00001559 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001560
Francois Romieucecb5fd2011-04-01 10:21:07 +02001561 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1562 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001563 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001564 rtl_writephy(tp, 0x17, 0x2138);
1565 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001566 } else {
françois romieu4da19632011-01-03 15:07:55 +00001567 rtl_writephy(tp, 0x17, 0x2108);
1568 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001569 }
1570 }
1571
Oliver Neukum54405cd2011-01-06 21:55:13 +01001572 rc = 0;
1573out:
1574 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
1577static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001578 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
1580 struct rtl8169_private *tp = netdev_priv(dev);
1581 int ret;
1582
Oliver Neukum54405cd2011-01-06 21:55:13 +01001583 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001584 if (ret < 0)
1585 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Francois Romieu4876cc12011-03-11 21:07:11 +01001587 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1588 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001590 }
1591out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return ret;
1593}
1594
1595static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1596{
1597 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 int ret;
1599
Francois Romieu4876cc12011-03-11 21:07:11 +01001600 del_timer_sync(&tp->timer);
1601
Francois Romieuda78dbf2012-01-26 14:18:23 +01001602 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001603 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001604 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001605 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 return ret;
1608}
1609
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001610static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1611 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Francois Romieud58d46b2011-05-03 16:38:29 +02001613 struct rtl8169_private *tp = netdev_priv(dev);
1614
Francois Romieu2b7b4312011-04-18 22:53:24 -07001615 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001616 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Francois Romieud58d46b2011-05-03 16:38:29 +02001618 if (dev->mtu > JUMBO_1K &&
1619 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1620 features &= ~NETIF_F_IP_CSUM;
1621
Michał Mirosław350fb322011-04-08 06:35:56 +00001622 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623}
1624
Francois Romieuda78dbf2012-01-26 14:18:23 +01001625static void __rtl8169_set_features(struct net_device *dev,
1626 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
1628 struct rtl8169_private *tp = netdev_priv(dev);
Ben Greear6bbe0212012-02-10 15:04:33 +00001629 netdev_features_t changed = features ^ dev->features;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001630 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Ben Greear6bbe0212012-02-10 15:04:33 +00001632 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)))
1633 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Ben Greear6bbe0212012-02-10 15:04:33 +00001635 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)) {
1636 if (features & NETIF_F_RXCSUM)
1637 tp->cp_cmd |= RxChkSum;
1638 else
1639 tp->cp_cmd &= ~RxChkSum;
Michał Mirosław350fb322011-04-08 06:35:56 +00001640
Ben Greear6bbe0212012-02-10 15:04:33 +00001641 if (dev->features & NETIF_F_HW_VLAN_RX)
1642 tp->cp_cmd |= RxVlan;
1643 else
1644 tp->cp_cmd &= ~RxVlan;
1645
1646 RTL_W16(CPlusCmd, tp->cp_cmd);
1647 RTL_R16(CPlusCmd);
1648 }
1649 if (changed & NETIF_F_RXALL) {
1650 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1651 if (features & NETIF_F_RXALL)
1652 tmp |= (AcceptErr | AcceptRunt);
1653 RTL_W32(RxConfig, tmp);
1654 }
Francois Romieuda78dbf2012-01-26 14:18:23 +01001655}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Francois Romieuda78dbf2012-01-26 14:18:23 +01001657static int rtl8169_set_features(struct net_device *dev,
1658 netdev_features_t features)
1659{
1660 struct rtl8169_private *tp = netdev_priv(dev);
1661
1662 rtl_lock_work(tp);
1663 __rtl8169_set_features(dev, features);
1664 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 return 0;
1667}
1668
Francois Romieuda78dbf2012-01-26 14:18:23 +01001669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1671 struct sk_buff *skb)
1672{
Jesse Grosseab6d182010-10-20 13:56:03 +00001673 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1675}
1676
Francois Romieu7a8fc772011-03-01 17:18:33 +01001677static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
1679 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Francois Romieu7a8fc772011-03-01 17:18:33 +01001681 if (opts2 & RxVlanTag)
1682 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
Francois Romieuccdffb92008-07-26 14:26:06 +02001687static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
1689 struct rtl8169_private *tp = netdev_priv(dev);
1690 void __iomem *ioaddr = tp->mmio_addr;
1691 u32 status;
1692
1693 cmd->supported =
1694 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1695 cmd->port = PORT_FIBRE;
1696 cmd->transceiver = XCVR_INTERNAL;
1697
1698 status = RTL_R32(TBICSR);
1699 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1700 cmd->autoneg = !!(status & TBINwEnable);
1701
David Decotigny70739492011-04-27 18:32:40 +00001702 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001704
1705 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
Francois Romieuccdffb92008-07-26 14:26:06 +02001708static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
1710 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Francois Romieuccdffb92008-07-26 14:26:06 +02001712 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713}
1714
1715static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1716{
1717 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001718 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Francois Romieuda78dbf2012-01-26 14:18:23 +01001720 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02001721 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001722 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Francois Romieuccdffb92008-07-26 14:26:06 +02001724 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
1727static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1728 void *p)
1729{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001730 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Francois Romieu5b0384f2006-08-16 16:00:01 +02001732 if (regs->len > R8169_REGS_SIZE)
1733 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Francois Romieuda78dbf2012-01-26 14:18:23 +01001735 rtl_lock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001736 memcpy_fromio(p, tp->mmio_addr, regs->len);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001737 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738}
1739
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001740static u32 rtl8169_get_msglevel(struct net_device *dev)
1741{
1742 struct rtl8169_private *tp = netdev_priv(dev);
1743
1744 return tp->msg_enable;
1745}
1746
1747static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1748{
1749 struct rtl8169_private *tp = netdev_priv(dev);
1750
1751 tp->msg_enable = value;
1752}
1753
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001754static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1755 "tx_packets",
1756 "rx_packets",
1757 "tx_errors",
1758 "rx_errors",
1759 "rx_missed",
1760 "align_errors",
1761 "tx_single_collisions",
1762 "tx_multi_collisions",
1763 "unicast",
1764 "broadcast",
1765 "multicast",
1766 "tx_aborted",
1767 "tx_underrun",
1768};
1769
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001770static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001771{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001772 switch (sset) {
1773 case ETH_SS_STATS:
1774 return ARRAY_SIZE(rtl8169_gstrings);
1775 default:
1776 return -EOPNOTSUPP;
1777 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001778}
1779
Ivan Vecera355423d2009-02-06 21:49:57 -08001780static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001781{
1782 struct rtl8169_private *tp = netdev_priv(dev);
1783 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001784 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001785 struct rtl8169_counters *counters;
1786 dma_addr_t paddr;
1787 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001788 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001789
Ivan Vecera355423d2009-02-06 21:49:57 -08001790 /*
1791 * Some chips are unable to dump tally counters when the receiver
1792 * is disabled.
1793 */
1794 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1795 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001796
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001797 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001798 if (!counters)
1799 return;
1800
1801 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001802 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001803 RTL_W32(CounterAddrLow, cmd);
1804 RTL_W32(CounterAddrLow, cmd | CounterDump);
1805
Ivan Vecera355423d2009-02-06 21:49:57 -08001806 while (wait--) {
1807 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001808 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001809 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001810 }
1811 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001812 }
1813
1814 RTL_W32(CounterAddrLow, 0);
1815 RTL_W32(CounterAddrHigh, 0);
1816
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001817 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001818}
1819
Ivan Vecera355423d2009-02-06 21:49:57 -08001820static void rtl8169_get_ethtool_stats(struct net_device *dev,
1821 struct ethtool_stats *stats, u64 *data)
1822{
1823 struct rtl8169_private *tp = netdev_priv(dev);
1824
1825 ASSERT_RTNL();
1826
1827 rtl8169_update_counters(dev);
1828
1829 data[0] = le64_to_cpu(tp->counters.tx_packets);
1830 data[1] = le64_to_cpu(tp->counters.rx_packets);
1831 data[2] = le64_to_cpu(tp->counters.tx_errors);
1832 data[3] = le32_to_cpu(tp->counters.rx_errors);
1833 data[4] = le16_to_cpu(tp->counters.rx_missed);
1834 data[5] = le16_to_cpu(tp->counters.align_errors);
1835 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1836 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1837 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1838 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1839 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1840 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1841 data[12] = le16_to_cpu(tp->counters.tx_underun);
1842}
1843
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001844static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1845{
1846 switch(stringset) {
1847 case ETH_SS_STATS:
1848 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1849 break;
1850 }
1851}
1852
Jeff Garzik7282d492006-09-13 14:30:00 -04001853static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 .get_drvinfo = rtl8169_get_drvinfo,
1855 .get_regs_len = rtl8169_get_regs_len,
1856 .get_link = ethtool_op_get_link,
1857 .get_settings = rtl8169_get_settings,
1858 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001859 .get_msglevel = rtl8169_get_msglevel,
1860 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001862 .get_wol = rtl8169_get_wol,
1863 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001864 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001865 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001866 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867};
1868
Francois Romieu07d3f512007-02-21 22:40:46 +01001869static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001870 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
Francois Romieu5d320a22011-05-08 17:47:36 +02001872 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001873 /*
1874 * The driver currently handles the 8168Bf and the 8168Be identically
1875 * but they can be identified more specifically through the test below
1876 * if needed:
1877 *
1878 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001879 *
1880 * Same thing for the 8101Eb and the 8101Ec:
1881 *
1882 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001883 */
Francois Romieu37441002011-06-17 22:58:54 +02001884 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001886 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 int mac_version;
1888 } mac_info[] = {
Hayes Wangc2218922011-09-06 16:55:18 +08001889 /* 8168F family. */
1890 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1891 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1892
hayeswang01dc7fe2011-03-21 01:50:28 +00001893 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001894 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001895 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1896 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1897 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1898
Francois Romieu5b538df2008-07-20 16:22:45 +02001899 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001900 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1901 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001902 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001903
françois romieue6de30d2011-01-03 15:08:37 +00001904 /* 8168DP family. */
1905 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1906 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001907 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001908
Francois Romieuef808d52008-06-29 13:10:54 +02001909 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001910 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001911 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001912 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001913 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001914 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1915 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001916 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001917 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001918 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001919
1920 /* 8168B family. */
1921 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1922 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1923 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1924 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1925
1926 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001927 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001928 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1929 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1930 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001931 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1932 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1933 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1934 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1935 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1936 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001937 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001938 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001939 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001940 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1941 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001942 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1943 /* FIXME: where did these entries come from ? -- FR */
1944 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1945 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1946
1947 /* 8110 family. */
1948 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1949 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1950 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1951 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1952 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1953 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1954
Jean Delvaref21b75e2009-05-26 20:54:48 -07001955 /* Catch-all */
1956 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001957 };
1958 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 u32 reg;
1960
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001961 reg = RTL_R32(TxConfig);
1962 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 p++;
1964 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001965
1966 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1967 netif_notice(tp, probe, dev,
1968 "unknown MAC, using family default\n");
1969 tp->mac_version = default_version;
1970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
1973static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1974{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001975 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
1977
Francois Romieu867763c2007-08-17 18:21:58 +02001978struct phy_reg {
1979 u16 reg;
1980 u16 val;
1981};
1982
françois romieu4da19632011-01-03 15:07:55 +00001983static void rtl_writephy_batch(struct rtl8169_private *tp,
1984 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001985{
1986 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001987 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001988 regs++;
1989 }
1990}
1991
françois romieubca03d52011-01-03 15:07:31 +00001992#define PHY_READ 0x00000000
1993#define PHY_DATA_OR 0x10000000
1994#define PHY_DATA_AND 0x20000000
1995#define PHY_BJMPN 0x30000000
1996#define PHY_READ_EFUSE 0x40000000
1997#define PHY_READ_MAC_BYTE 0x50000000
1998#define PHY_WRITE_MAC_BYTE 0x60000000
1999#define PHY_CLEAR_READCOUNT 0x70000000
2000#define PHY_WRITE 0x80000000
2001#define PHY_READCOUNT_EQ_SKIP 0x90000000
2002#define PHY_COMP_EQ_SKIPN 0xa0000000
2003#define PHY_COMP_NEQ_SKIPN 0xb0000000
2004#define PHY_WRITE_PREVIOUS 0xc0000000
2005#define PHY_SKIPN 0xd0000000
2006#define PHY_DELAY_MS 0xe0000000
2007#define PHY_WRITE_ERI_WORD 0xf0000000
2008
Hayes Wang960aee62011-06-18 11:37:48 +02002009struct fw_info {
2010 u32 magic;
2011 char version[RTL_VER_SIZE];
2012 __le32 fw_start;
2013 __le32 fw_len;
2014 u8 chksum;
2015} __packed;
2016
Francois Romieu1c361ef2011-06-17 17:16:24 +02002017#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2018
2019static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002020{
Francois Romieub6ffd972011-06-17 17:00:05 +02002021 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002022 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002023 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2024 char *version = rtl_fw->version;
2025 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002026
Francois Romieu1c361ef2011-06-17 17:16:24 +02002027 if (fw->size < FW_OPCODE_SIZE)
2028 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002029
2030 if (!fw_info->magic) {
2031 size_t i, size, start;
2032 u8 checksum = 0;
2033
2034 if (fw->size < sizeof(*fw_info))
2035 goto out;
2036
2037 for (i = 0; i < fw->size; i++)
2038 checksum += fw->data[i];
2039 if (checksum != 0)
2040 goto out;
2041
2042 start = le32_to_cpu(fw_info->fw_start);
2043 if (start > fw->size)
2044 goto out;
2045
2046 size = le32_to_cpu(fw_info->fw_len);
2047 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2048 goto out;
2049
2050 memcpy(version, fw_info->version, RTL_VER_SIZE);
2051
2052 pa->code = (__le32 *)(fw->data + start);
2053 pa->size = size;
2054 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002055 if (fw->size % FW_OPCODE_SIZE)
2056 goto out;
2057
2058 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2059
2060 pa->code = (__le32 *)fw->data;
2061 pa->size = fw->size / FW_OPCODE_SIZE;
2062 }
2063 version[RTL_VER_SIZE - 1] = 0;
2064
2065 rc = true;
2066out:
2067 return rc;
2068}
2069
Francois Romieufd112f22011-06-18 00:10:29 +02002070static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2071 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002072{
Francois Romieufd112f22011-06-18 00:10:29 +02002073 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002074 size_t index;
2075
Francois Romieu1c361ef2011-06-17 17:16:24 +02002076 for (index = 0; index < pa->size; index++) {
2077 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002078 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002079
hayeswang42b82dc2011-01-10 02:07:25 +00002080 switch(action & 0xf0000000) {
2081 case PHY_READ:
2082 case PHY_DATA_OR:
2083 case PHY_DATA_AND:
2084 case PHY_READ_EFUSE:
2085 case PHY_CLEAR_READCOUNT:
2086 case PHY_WRITE:
2087 case PHY_WRITE_PREVIOUS:
2088 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002089 break;
2090
hayeswang42b82dc2011-01-10 02:07:25 +00002091 case PHY_BJMPN:
2092 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002093 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002094 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002095 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002096 }
2097 break;
2098 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002099 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002100 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002101 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002102 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002103 }
2104 break;
2105 case PHY_COMP_EQ_SKIPN:
2106 case PHY_COMP_NEQ_SKIPN:
2107 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002108 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002109 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002110 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002111 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002112 }
2113 break;
2114
2115 case PHY_READ_MAC_BYTE:
2116 case PHY_WRITE_MAC_BYTE:
2117 case PHY_WRITE_ERI_WORD:
2118 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002119 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002120 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002121 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002122 }
2123 }
Francois Romieufd112f22011-06-18 00:10:29 +02002124 rc = true;
2125out:
2126 return rc;
2127}
françois romieubca03d52011-01-03 15:07:31 +00002128
Francois Romieufd112f22011-06-18 00:10:29 +02002129static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2130{
2131 struct net_device *dev = tp->dev;
2132 int rc = -EINVAL;
2133
2134 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2135 netif_err(tp, ifup, dev, "invalid firwmare\n");
2136 goto out;
2137 }
2138
2139 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2140 rc = 0;
2141out:
2142 return rc;
2143}
2144
2145static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2146{
2147 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2148 u32 predata, count;
2149 size_t index;
2150
2151 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002152
Francois Romieu1c361ef2011-06-17 17:16:24 +02002153 for (index = 0; index < pa->size; ) {
2154 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002155 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002156 u32 regno = (action & 0x0fff0000) >> 16;
2157
2158 if (!action)
2159 break;
françois romieubca03d52011-01-03 15:07:31 +00002160
2161 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002162 case PHY_READ:
2163 predata = rtl_readphy(tp, regno);
2164 count++;
2165 index++;
françois romieubca03d52011-01-03 15:07:31 +00002166 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002167 case PHY_DATA_OR:
2168 predata |= data;
2169 index++;
2170 break;
2171 case PHY_DATA_AND:
2172 predata &= data;
2173 index++;
2174 break;
2175 case PHY_BJMPN:
2176 index -= regno;
2177 break;
2178 case PHY_READ_EFUSE:
2179 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2180 index++;
2181 break;
2182 case PHY_CLEAR_READCOUNT:
2183 count = 0;
2184 index++;
2185 break;
2186 case PHY_WRITE:
2187 rtl_writephy(tp, regno, data);
2188 index++;
2189 break;
2190 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002191 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002192 break;
2193 case PHY_COMP_EQ_SKIPN:
2194 if (predata == data)
2195 index += regno;
2196 index++;
2197 break;
2198 case PHY_COMP_NEQ_SKIPN:
2199 if (predata != data)
2200 index += regno;
2201 index++;
2202 break;
2203 case PHY_WRITE_PREVIOUS:
2204 rtl_writephy(tp, regno, predata);
2205 index++;
2206 break;
2207 case PHY_SKIPN:
2208 index += regno + 1;
2209 break;
2210 case PHY_DELAY_MS:
2211 mdelay(data);
2212 index++;
2213 break;
2214
2215 case PHY_READ_MAC_BYTE:
2216 case PHY_WRITE_MAC_BYTE:
2217 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002218 default:
2219 BUG();
2220 }
2221 }
2222}
2223
françois romieuf1e02ed2011-01-13 13:07:53 +00002224static void rtl_release_firmware(struct rtl8169_private *tp)
2225{
Francois Romieub6ffd972011-06-17 17:00:05 +02002226 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2227 release_firmware(tp->rtl_fw->fw);
2228 kfree(tp->rtl_fw);
2229 }
2230 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002231}
2232
François Romieu953a12c2011-04-24 17:38:48 +02002233static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002234{
Francois Romieub6ffd972011-06-17 17:00:05 +02002235 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002236
2237 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002238 if (!IS_ERR_OR_NULL(rtl_fw))
2239 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002240}
2241
2242static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2243{
2244 if (rtl_readphy(tp, reg) != val)
2245 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2246 else
2247 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002248}
2249
françois romieu4da19632011-01-03 15:07:55 +00002250static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002252 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002253 { 0x1f, 0x0001 },
2254 { 0x06, 0x006e },
2255 { 0x08, 0x0708 },
2256 { 0x15, 0x4000 },
2257 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
françois romieu0b9b5712009-08-10 19:44:56 +00002259 { 0x1f, 0x0001 },
2260 { 0x03, 0x00a1 },
2261 { 0x02, 0x0008 },
2262 { 0x01, 0x0120 },
2263 { 0x00, 0x1000 },
2264 { 0x04, 0x0800 },
2265 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
françois romieu0b9b5712009-08-10 19:44:56 +00002267 { 0x03, 0xff41 },
2268 { 0x02, 0xdf60 },
2269 { 0x01, 0x0140 },
2270 { 0x00, 0x0077 },
2271 { 0x04, 0x7800 },
2272 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
françois romieu0b9b5712009-08-10 19:44:56 +00002274 { 0x03, 0x802f },
2275 { 0x02, 0x4f02 },
2276 { 0x01, 0x0409 },
2277 { 0x00, 0xf0f9 },
2278 { 0x04, 0x9800 },
2279 { 0x04, 0x9000 },
2280
2281 { 0x03, 0xdf01 },
2282 { 0x02, 0xdf20 },
2283 { 0x01, 0xff95 },
2284 { 0x00, 0xba00 },
2285 { 0x04, 0xa800 },
2286 { 0x04, 0xa000 },
2287
2288 { 0x03, 0xff41 },
2289 { 0x02, 0xdf20 },
2290 { 0x01, 0x0140 },
2291 { 0x00, 0x00bb },
2292 { 0x04, 0xb800 },
2293 { 0x04, 0xb000 },
2294
2295 { 0x03, 0xdf41 },
2296 { 0x02, 0xdc60 },
2297 { 0x01, 0x6340 },
2298 { 0x00, 0x007d },
2299 { 0x04, 0xd800 },
2300 { 0x04, 0xd000 },
2301
2302 { 0x03, 0xdf01 },
2303 { 0x02, 0xdf20 },
2304 { 0x01, 0x100a },
2305 { 0x00, 0xa0ff },
2306 { 0x04, 0xf800 },
2307 { 0x04, 0xf000 },
2308
2309 { 0x1f, 0x0000 },
2310 { 0x0b, 0x0000 },
2311 { 0x00, 0x9200 }
2312 };
2313
françois romieu4da19632011-01-03 15:07:55 +00002314 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315}
2316
françois romieu4da19632011-01-03 15:07:55 +00002317static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002318{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002319 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002320 { 0x1f, 0x0002 },
2321 { 0x01, 0x90d0 },
2322 { 0x1f, 0x0000 }
2323 };
2324
françois romieu4da19632011-01-03 15:07:55 +00002325 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002326}
2327
françois romieu4da19632011-01-03 15:07:55 +00002328static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002329{
2330 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002331
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002332 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2333 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002334 return;
2335
françois romieu4da19632011-01-03 15:07:55 +00002336 rtl_writephy(tp, 0x1f, 0x0001);
2337 rtl_writephy(tp, 0x10, 0xf01b);
2338 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002339}
2340
françois romieu4da19632011-01-03 15:07:55 +00002341static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002342{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002343 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002344 { 0x1f, 0x0001 },
2345 { 0x04, 0x0000 },
2346 { 0x03, 0x00a1 },
2347 { 0x02, 0x0008 },
2348 { 0x01, 0x0120 },
2349 { 0x00, 0x1000 },
2350 { 0x04, 0x0800 },
2351 { 0x04, 0x9000 },
2352 { 0x03, 0x802f },
2353 { 0x02, 0x4f02 },
2354 { 0x01, 0x0409 },
2355 { 0x00, 0xf099 },
2356 { 0x04, 0x9800 },
2357 { 0x04, 0xa000 },
2358 { 0x03, 0xdf01 },
2359 { 0x02, 0xdf20 },
2360 { 0x01, 0xff95 },
2361 { 0x00, 0xba00 },
2362 { 0x04, 0xa800 },
2363 { 0x04, 0xf000 },
2364 { 0x03, 0xdf01 },
2365 { 0x02, 0xdf20 },
2366 { 0x01, 0x101a },
2367 { 0x00, 0xa0ff },
2368 { 0x04, 0xf800 },
2369 { 0x04, 0x0000 },
2370 { 0x1f, 0x0000 },
2371
2372 { 0x1f, 0x0001 },
2373 { 0x10, 0xf41b },
2374 { 0x14, 0xfb54 },
2375 { 0x18, 0xf5c7 },
2376 { 0x1f, 0x0000 },
2377
2378 { 0x1f, 0x0001 },
2379 { 0x17, 0x0cc0 },
2380 { 0x1f, 0x0000 }
2381 };
2382
françois romieu4da19632011-01-03 15:07:55 +00002383 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002384
françois romieu4da19632011-01-03 15:07:55 +00002385 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002386}
2387
françois romieu4da19632011-01-03 15:07:55 +00002388static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002389{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002390 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002391 { 0x1f, 0x0001 },
2392 { 0x04, 0x0000 },
2393 { 0x03, 0x00a1 },
2394 { 0x02, 0x0008 },
2395 { 0x01, 0x0120 },
2396 { 0x00, 0x1000 },
2397 { 0x04, 0x0800 },
2398 { 0x04, 0x9000 },
2399 { 0x03, 0x802f },
2400 { 0x02, 0x4f02 },
2401 { 0x01, 0x0409 },
2402 { 0x00, 0xf099 },
2403 { 0x04, 0x9800 },
2404 { 0x04, 0xa000 },
2405 { 0x03, 0xdf01 },
2406 { 0x02, 0xdf20 },
2407 { 0x01, 0xff95 },
2408 { 0x00, 0xba00 },
2409 { 0x04, 0xa800 },
2410 { 0x04, 0xf000 },
2411 { 0x03, 0xdf01 },
2412 { 0x02, 0xdf20 },
2413 { 0x01, 0x101a },
2414 { 0x00, 0xa0ff },
2415 { 0x04, 0xf800 },
2416 { 0x04, 0x0000 },
2417 { 0x1f, 0x0000 },
2418
2419 { 0x1f, 0x0001 },
2420 { 0x0b, 0x8480 },
2421 { 0x1f, 0x0000 },
2422
2423 { 0x1f, 0x0001 },
2424 { 0x18, 0x67c7 },
2425 { 0x04, 0x2000 },
2426 { 0x03, 0x002f },
2427 { 0x02, 0x4360 },
2428 { 0x01, 0x0109 },
2429 { 0x00, 0x3022 },
2430 { 0x04, 0x2800 },
2431 { 0x1f, 0x0000 },
2432
2433 { 0x1f, 0x0001 },
2434 { 0x17, 0x0cc0 },
2435 { 0x1f, 0x0000 }
2436 };
2437
françois romieu4da19632011-01-03 15:07:55 +00002438 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002439}
2440
françois romieu4da19632011-01-03 15:07:55 +00002441static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002442{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002443 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002444 { 0x10, 0xf41b },
2445 { 0x1f, 0x0000 }
2446 };
2447
françois romieu4da19632011-01-03 15:07:55 +00002448 rtl_writephy(tp, 0x1f, 0x0001);
2449 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002450
françois romieu4da19632011-01-03 15:07:55 +00002451 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002452}
2453
françois romieu4da19632011-01-03 15:07:55 +00002454static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002455{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002456 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002457 { 0x1f, 0x0001 },
2458 { 0x10, 0xf41b },
2459 { 0x1f, 0x0000 }
2460 };
2461
françois romieu4da19632011-01-03 15:07:55 +00002462 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002463}
2464
françois romieu4da19632011-01-03 15:07:55 +00002465static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002466{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002467 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002468 { 0x1f, 0x0000 },
2469 { 0x1d, 0x0f00 },
2470 { 0x1f, 0x0002 },
2471 { 0x0c, 0x1ec8 },
2472 { 0x1f, 0x0000 }
2473 };
2474
françois romieu4da19632011-01-03 15:07:55 +00002475 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002476}
2477
françois romieu4da19632011-01-03 15:07:55 +00002478static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002479{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002480 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002481 { 0x1f, 0x0001 },
2482 { 0x1d, 0x3d98 },
2483 { 0x1f, 0x0000 }
2484 };
2485
françois romieu4da19632011-01-03 15:07:55 +00002486 rtl_writephy(tp, 0x1f, 0x0000);
2487 rtl_patchphy(tp, 0x14, 1 << 5);
2488 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002489
françois romieu4da19632011-01-03 15:07:55 +00002490 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002491}
2492
françois romieu4da19632011-01-03 15:07:55 +00002493static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002494{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002495 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002496 { 0x1f, 0x0001 },
2497 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002498 { 0x1f, 0x0002 },
2499 { 0x00, 0x88d4 },
2500 { 0x01, 0x82b1 },
2501 { 0x03, 0x7002 },
2502 { 0x08, 0x9e30 },
2503 { 0x09, 0x01f0 },
2504 { 0x0a, 0x5500 },
2505 { 0x0c, 0x00c8 },
2506 { 0x1f, 0x0003 },
2507 { 0x12, 0xc096 },
2508 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002509 { 0x1f, 0x0000 },
2510 { 0x1f, 0x0000 },
2511 { 0x09, 0x2000 },
2512 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002513 };
2514
françois romieu4da19632011-01-03 15:07:55 +00002515 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002516
françois romieu4da19632011-01-03 15:07:55 +00002517 rtl_patchphy(tp, 0x14, 1 << 5);
2518 rtl_patchphy(tp, 0x0d, 1 << 5);
2519 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002520}
2521
françois romieu4da19632011-01-03 15:07:55 +00002522static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002523{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002524 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002525 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002526 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002527 { 0x03, 0x802f },
2528 { 0x02, 0x4f02 },
2529 { 0x01, 0x0409 },
2530 { 0x00, 0xf099 },
2531 { 0x04, 0x9800 },
2532 { 0x04, 0x9000 },
2533 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002534 { 0x1f, 0x0002 },
2535 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002536 { 0x06, 0x0761 },
2537 { 0x1f, 0x0003 },
2538 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002539 { 0x1f, 0x0000 }
2540 };
2541
françois romieu4da19632011-01-03 15:07:55 +00002542 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002543
françois romieu4da19632011-01-03 15:07:55 +00002544 rtl_patchphy(tp, 0x16, 1 << 0);
2545 rtl_patchphy(tp, 0x14, 1 << 5);
2546 rtl_patchphy(tp, 0x0d, 1 << 5);
2547 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002548}
2549
françois romieu4da19632011-01-03 15:07:55 +00002550static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002551{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002552 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002553 { 0x1f, 0x0001 },
2554 { 0x12, 0x2300 },
2555 { 0x1d, 0x3d98 },
2556 { 0x1f, 0x0002 },
2557 { 0x0c, 0x7eb8 },
2558 { 0x06, 0x5461 },
2559 { 0x1f, 0x0003 },
2560 { 0x16, 0x0f0a },
2561 { 0x1f, 0x0000 }
2562 };
2563
françois romieu4da19632011-01-03 15:07:55 +00002564 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002565
françois romieu4da19632011-01-03 15:07:55 +00002566 rtl_patchphy(tp, 0x16, 1 << 0);
2567 rtl_patchphy(tp, 0x14, 1 << 5);
2568 rtl_patchphy(tp, 0x0d, 1 << 5);
2569 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002570}
2571
françois romieu4da19632011-01-03 15:07:55 +00002572static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002573{
françois romieu4da19632011-01-03 15:07:55 +00002574 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002575}
2576
françois romieubca03d52011-01-03 15:07:31 +00002577static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002578{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002579 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002580 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002581 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002582 { 0x06, 0x4064 },
2583 { 0x07, 0x2863 },
2584 { 0x08, 0x059c },
2585 { 0x09, 0x26b4 },
2586 { 0x0a, 0x6a19 },
2587 { 0x0b, 0xdcc8 },
2588 { 0x10, 0xf06d },
2589 { 0x14, 0x7f68 },
2590 { 0x18, 0x7fd9 },
2591 { 0x1c, 0xf0ff },
2592 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002593 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002594 { 0x12, 0xf49f },
2595 { 0x13, 0x070b },
2596 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002597 { 0x14, 0x94c0 },
2598
2599 /*
2600 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002601 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002602 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002603 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002604 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002605 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002606 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002607 { 0x06, 0x5561 },
2608
2609 /*
2610 * Can not link to 1Gbps with bad cable
2611 * Decrease SNR threshold form 21.07dB to 19.04dB
2612 */
2613 { 0x1f, 0x0001 },
2614 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002615
2616 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002617 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002618 };
françois romieubca03d52011-01-03 15:07:31 +00002619 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002620
françois romieu4da19632011-01-03 15:07:55 +00002621 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002622
françois romieubca03d52011-01-03 15:07:31 +00002623 /*
2624 * Rx Error Issue
2625 * Fine Tune Switching regulator parameter
2626 */
françois romieu4da19632011-01-03 15:07:55 +00002627 rtl_writephy(tp, 0x1f, 0x0002);
2628 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2629 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002630
françois romieudaf9df62009-10-07 12:44:20 +00002631 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002632 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002633 { 0x1f, 0x0002 },
2634 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002635 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002636 { 0x05, 0x8330 },
2637 { 0x06, 0x669a },
2638 { 0x1f, 0x0002 }
2639 };
2640 int val;
2641
françois romieu4da19632011-01-03 15:07:55 +00002642 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002643
françois romieu4da19632011-01-03 15:07:55 +00002644 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002645
2646 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002647 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002648 0x0065, 0x0066, 0x0067, 0x0068,
2649 0x0069, 0x006a, 0x006b, 0x006c
2650 };
2651 int i;
2652
françois romieu4da19632011-01-03 15:07:55 +00002653 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002654
2655 val &= 0xff00;
2656 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002657 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002658 }
2659 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002660 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002661 { 0x1f, 0x0002 },
2662 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002663 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002664 { 0x05, 0x8330 },
2665 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002666 };
2667
françois romieu4da19632011-01-03 15:07:55 +00002668 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002669 }
2670
françois romieubca03d52011-01-03 15:07:31 +00002671 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002672 rtl_writephy(tp, 0x1f, 0x0002);
2673 rtl_patchphy(tp, 0x0d, 0x0300);
2674 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002675
françois romieubca03d52011-01-03 15:07:31 +00002676 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002677 rtl_writephy(tp, 0x1f, 0x0002);
2678 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2679 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002680
françois romieu4da19632011-01-03 15:07:55 +00002681 rtl_writephy(tp, 0x1f, 0x0005);
2682 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002683
2684 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002685
françois romieu4da19632011-01-03 15:07:55 +00002686 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002687}
2688
françois romieubca03d52011-01-03 15:07:31 +00002689static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002690{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002691 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002692 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002693 { 0x1f, 0x0001 },
2694 { 0x06, 0x4064 },
2695 { 0x07, 0x2863 },
2696 { 0x08, 0x059c },
2697 { 0x09, 0x26b4 },
2698 { 0x0a, 0x6a19 },
2699 { 0x0b, 0xdcc8 },
2700 { 0x10, 0xf06d },
2701 { 0x14, 0x7f68 },
2702 { 0x18, 0x7fd9 },
2703 { 0x1c, 0xf0ff },
2704 { 0x1d, 0x3d9c },
2705 { 0x1f, 0x0003 },
2706 { 0x12, 0xf49f },
2707 { 0x13, 0x070b },
2708 { 0x1a, 0x05ad },
2709 { 0x14, 0x94c0 },
2710
françois romieubca03d52011-01-03 15:07:31 +00002711 /*
2712 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002713 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002714 */
françois romieudaf9df62009-10-07 12:44:20 +00002715 { 0x1f, 0x0002 },
2716 { 0x06, 0x5561 },
2717 { 0x1f, 0x0005 },
2718 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002719 { 0x06, 0x5561 },
2720
2721 /*
2722 * Can not link to 1Gbps with bad cable
2723 * Decrease SNR threshold form 21.07dB to 19.04dB
2724 */
2725 { 0x1f, 0x0001 },
2726 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002727
2728 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002729 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002730 };
françois romieubca03d52011-01-03 15:07:31 +00002731 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002732
françois romieu4da19632011-01-03 15:07:55 +00002733 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002734
2735 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002736 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002737 { 0x1f, 0x0002 },
2738 { 0x05, 0x669a },
2739 { 0x1f, 0x0005 },
2740 { 0x05, 0x8330 },
2741 { 0x06, 0x669a },
2742
2743 { 0x1f, 0x0002 }
2744 };
2745 int val;
2746
françois romieu4da19632011-01-03 15:07:55 +00002747 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002748
françois romieu4da19632011-01-03 15:07:55 +00002749 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002750 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002751 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002752 0x0065, 0x0066, 0x0067, 0x0068,
2753 0x0069, 0x006a, 0x006b, 0x006c
2754 };
2755 int i;
2756
françois romieu4da19632011-01-03 15:07:55 +00002757 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002758
2759 val &= 0xff00;
2760 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002761 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002762 }
2763 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002764 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002765 { 0x1f, 0x0002 },
2766 { 0x05, 0x2642 },
2767 { 0x1f, 0x0005 },
2768 { 0x05, 0x8330 },
2769 { 0x06, 0x2642 }
2770 };
2771
françois romieu4da19632011-01-03 15:07:55 +00002772 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002773 }
2774
françois romieubca03d52011-01-03 15:07:31 +00002775 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002776 rtl_writephy(tp, 0x1f, 0x0002);
2777 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2778 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002779
françois romieubca03d52011-01-03 15:07:31 +00002780 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002781 rtl_writephy(tp, 0x1f, 0x0002);
2782 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002783
françois romieu4da19632011-01-03 15:07:55 +00002784 rtl_writephy(tp, 0x1f, 0x0005);
2785 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002786
2787 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002788
françois romieu4da19632011-01-03 15:07:55 +00002789 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002790}
2791
françois romieu4da19632011-01-03 15:07:55 +00002792static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002793{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002794 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002795 { 0x1f, 0x0002 },
2796 { 0x10, 0x0008 },
2797 { 0x0d, 0x006c },
2798
2799 { 0x1f, 0x0000 },
2800 { 0x0d, 0xf880 },
2801
2802 { 0x1f, 0x0001 },
2803 { 0x17, 0x0cc0 },
2804
2805 { 0x1f, 0x0001 },
2806 { 0x0b, 0xa4d8 },
2807 { 0x09, 0x281c },
2808 { 0x07, 0x2883 },
2809 { 0x0a, 0x6b35 },
2810 { 0x1d, 0x3da4 },
2811 { 0x1c, 0xeffd },
2812 { 0x14, 0x7f52 },
2813 { 0x18, 0x7fc6 },
2814 { 0x08, 0x0601 },
2815 { 0x06, 0x4063 },
2816 { 0x10, 0xf074 },
2817 { 0x1f, 0x0003 },
2818 { 0x13, 0x0789 },
2819 { 0x12, 0xf4bd },
2820 { 0x1a, 0x04fd },
2821 { 0x14, 0x84b0 },
2822 { 0x1f, 0x0000 },
2823 { 0x00, 0x9200 },
2824
2825 { 0x1f, 0x0005 },
2826 { 0x01, 0x0340 },
2827 { 0x1f, 0x0001 },
2828 { 0x04, 0x4000 },
2829 { 0x03, 0x1d21 },
2830 { 0x02, 0x0c32 },
2831 { 0x01, 0x0200 },
2832 { 0x00, 0x5554 },
2833 { 0x04, 0x4800 },
2834 { 0x04, 0x4000 },
2835 { 0x04, 0xf000 },
2836 { 0x03, 0xdf01 },
2837 { 0x02, 0xdf20 },
2838 { 0x01, 0x101a },
2839 { 0x00, 0xa0ff },
2840 { 0x04, 0xf800 },
2841 { 0x04, 0xf000 },
2842 { 0x1f, 0x0000 },
2843
2844 { 0x1f, 0x0007 },
2845 { 0x1e, 0x0023 },
2846 { 0x16, 0x0000 },
2847 { 0x1f, 0x0000 }
2848 };
2849
françois romieu4da19632011-01-03 15:07:55 +00002850 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002851}
2852
françois romieue6de30d2011-01-03 15:08:37 +00002853static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2854{
2855 static const struct phy_reg phy_reg_init[] = {
2856 { 0x1f, 0x0001 },
2857 { 0x17, 0x0cc0 },
2858
2859 { 0x1f, 0x0007 },
2860 { 0x1e, 0x002d },
2861 { 0x18, 0x0040 },
2862 { 0x1f, 0x0000 }
2863 };
2864
2865 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2866 rtl_patchphy(tp, 0x0d, 1 << 5);
2867}
2868
Hayes Wang70090422011-07-06 15:58:06 +08002869static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002870{
2871 static const struct phy_reg phy_reg_init[] = {
2872 /* Enable Delay cap */
2873 { 0x1f, 0x0005 },
2874 { 0x05, 0x8b80 },
2875 { 0x06, 0xc896 },
2876 { 0x1f, 0x0000 },
2877
2878 /* Channel estimation fine tune */
2879 { 0x1f, 0x0001 },
2880 { 0x0b, 0x6c20 },
2881 { 0x07, 0x2872 },
2882 { 0x1c, 0xefff },
2883 { 0x1f, 0x0003 },
2884 { 0x14, 0x6420 },
2885 { 0x1f, 0x0000 },
2886
2887 /* Update PFM & 10M TX idle timer */
2888 { 0x1f, 0x0007 },
2889 { 0x1e, 0x002f },
2890 { 0x15, 0x1919 },
2891 { 0x1f, 0x0000 },
2892
2893 { 0x1f, 0x0007 },
2894 { 0x1e, 0x00ac },
2895 { 0x18, 0x0006 },
2896 { 0x1f, 0x0000 }
2897 };
2898
Francois Romieu15ecd032011-04-27 13:52:22 -07002899 rtl_apply_firmware(tp);
2900
hayeswang01dc7fe2011-03-21 01:50:28 +00002901 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2902
2903 /* DCO enable for 10M IDLE Power */
2904 rtl_writephy(tp, 0x1f, 0x0007);
2905 rtl_writephy(tp, 0x1e, 0x0023);
2906 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2907 rtl_writephy(tp, 0x1f, 0x0000);
2908
2909 /* For impedance matching */
2910 rtl_writephy(tp, 0x1f, 0x0002);
2911 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002912 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002913
2914 /* PHY auto speed down */
2915 rtl_writephy(tp, 0x1f, 0x0007);
2916 rtl_writephy(tp, 0x1e, 0x002d);
2917 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2918 rtl_writephy(tp, 0x1f, 0x0000);
2919 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2920
2921 rtl_writephy(tp, 0x1f, 0x0005);
2922 rtl_writephy(tp, 0x05, 0x8b86);
2923 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2924 rtl_writephy(tp, 0x1f, 0x0000);
2925
2926 rtl_writephy(tp, 0x1f, 0x0005);
2927 rtl_writephy(tp, 0x05, 0x8b85);
2928 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2929 rtl_writephy(tp, 0x1f, 0x0007);
2930 rtl_writephy(tp, 0x1e, 0x0020);
2931 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2932 rtl_writephy(tp, 0x1f, 0x0006);
2933 rtl_writephy(tp, 0x00, 0x5a00);
2934 rtl_writephy(tp, 0x1f, 0x0000);
2935 rtl_writephy(tp, 0x0d, 0x0007);
2936 rtl_writephy(tp, 0x0e, 0x003c);
2937 rtl_writephy(tp, 0x0d, 0x4007);
2938 rtl_writephy(tp, 0x0e, 0x0000);
2939 rtl_writephy(tp, 0x0d, 0x0000);
2940}
2941
Hayes Wang70090422011-07-06 15:58:06 +08002942static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2943{
2944 static const struct phy_reg phy_reg_init[] = {
2945 /* Enable Delay cap */
2946 { 0x1f, 0x0004 },
2947 { 0x1f, 0x0007 },
2948 { 0x1e, 0x00ac },
2949 { 0x18, 0x0006 },
2950 { 0x1f, 0x0002 },
2951 { 0x1f, 0x0000 },
2952 { 0x1f, 0x0000 },
2953
2954 /* Channel estimation fine tune */
2955 { 0x1f, 0x0003 },
2956 { 0x09, 0xa20f },
2957 { 0x1f, 0x0000 },
2958 { 0x1f, 0x0000 },
2959
2960 /* Green Setting */
2961 { 0x1f, 0x0005 },
2962 { 0x05, 0x8b5b },
2963 { 0x06, 0x9222 },
2964 { 0x05, 0x8b6d },
2965 { 0x06, 0x8000 },
2966 { 0x05, 0x8b76 },
2967 { 0x06, 0x8000 },
2968 { 0x1f, 0x0000 }
2969 };
2970
2971 rtl_apply_firmware(tp);
2972
2973 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2974
2975 /* For 4-corner performance improve */
2976 rtl_writephy(tp, 0x1f, 0x0005);
2977 rtl_writephy(tp, 0x05, 0x8b80);
2978 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2979 rtl_writephy(tp, 0x1f, 0x0000);
2980
2981 /* PHY auto speed down */
2982 rtl_writephy(tp, 0x1f, 0x0004);
2983 rtl_writephy(tp, 0x1f, 0x0007);
2984 rtl_writephy(tp, 0x1e, 0x002d);
2985 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2986 rtl_writephy(tp, 0x1f, 0x0002);
2987 rtl_writephy(tp, 0x1f, 0x0000);
2988 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2989
2990 /* improve 10M EEE waveform */
2991 rtl_writephy(tp, 0x1f, 0x0005);
2992 rtl_writephy(tp, 0x05, 0x8b86);
2993 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2994 rtl_writephy(tp, 0x1f, 0x0000);
2995
2996 /* Improve 2-pair detection performance */
2997 rtl_writephy(tp, 0x1f, 0x0005);
2998 rtl_writephy(tp, 0x05, 0x8b85);
2999 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3000 rtl_writephy(tp, 0x1f, 0x0000);
3001
3002 /* EEE setting */
3003 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
3004 ERIAR_EXGMAC);
3005 rtl_writephy(tp, 0x1f, 0x0005);
3006 rtl_writephy(tp, 0x05, 0x8b85);
3007 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3008 rtl_writephy(tp, 0x1f, 0x0004);
3009 rtl_writephy(tp, 0x1f, 0x0007);
3010 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04003011 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003012 rtl_writephy(tp, 0x1f, 0x0002);
3013 rtl_writephy(tp, 0x1f, 0x0000);
3014 rtl_writephy(tp, 0x0d, 0x0007);
3015 rtl_writephy(tp, 0x0e, 0x003c);
3016 rtl_writephy(tp, 0x0d, 0x4007);
3017 rtl_writephy(tp, 0x0e, 0x0000);
3018 rtl_writephy(tp, 0x0d, 0x0000);
3019
3020 /* Green feature */
3021 rtl_writephy(tp, 0x1f, 0x0003);
3022 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3023 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3024 rtl_writephy(tp, 0x1f, 0x0000);
3025}
3026
Hayes Wangc2218922011-09-06 16:55:18 +08003027static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3028{
3029 static const struct phy_reg phy_reg_init[] = {
3030 /* Channel estimation fine tune */
3031 { 0x1f, 0x0003 },
3032 { 0x09, 0xa20f },
3033 { 0x1f, 0x0000 },
3034
3035 /* Modify green table for giga & fnet */
3036 { 0x1f, 0x0005 },
3037 { 0x05, 0x8b55 },
3038 { 0x06, 0x0000 },
3039 { 0x05, 0x8b5e },
3040 { 0x06, 0x0000 },
3041 { 0x05, 0x8b67 },
3042 { 0x06, 0x0000 },
3043 { 0x05, 0x8b70 },
3044 { 0x06, 0x0000 },
3045 { 0x1f, 0x0000 },
3046 { 0x1f, 0x0007 },
3047 { 0x1e, 0x0078 },
3048 { 0x17, 0x0000 },
3049 { 0x19, 0x00fb },
3050 { 0x1f, 0x0000 },
3051
3052 /* Modify green table for 10M */
3053 { 0x1f, 0x0005 },
3054 { 0x05, 0x8b79 },
3055 { 0x06, 0xaa00 },
3056 { 0x1f, 0x0000 },
3057
3058 /* Disable hiimpedance detection (RTCT) */
3059 { 0x1f, 0x0003 },
3060 { 0x01, 0x328a },
3061 { 0x1f, 0x0000 }
3062 };
3063
3064 rtl_apply_firmware(tp);
3065
3066 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3067
3068 /* For 4-corner performance improve */
3069 rtl_writephy(tp, 0x1f, 0x0005);
3070 rtl_writephy(tp, 0x05, 0x8b80);
3071 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3072 rtl_writephy(tp, 0x1f, 0x0000);
3073
3074 /* PHY auto speed down */
3075 rtl_writephy(tp, 0x1f, 0x0007);
3076 rtl_writephy(tp, 0x1e, 0x002d);
3077 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3078 rtl_writephy(tp, 0x1f, 0x0000);
3079 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3080
3081 /* Improve 10M EEE waveform */
3082 rtl_writephy(tp, 0x1f, 0x0005);
3083 rtl_writephy(tp, 0x05, 0x8b86);
3084 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3085 rtl_writephy(tp, 0x1f, 0x0000);
3086
3087 /* Improve 2-pair detection performance */
3088 rtl_writephy(tp, 0x1f, 0x0005);
3089 rtl_writephy(tp, 0x05, 0x8b85);
3090 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3091 rtl_writephy(tp, 0x1f, 0x0000);
3092}
3093
3094static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3095{
3096 rtl_apply_firmware(tp);
3097
3098 /* For 4-corner performance improve */
3099 rtl_writephy(tp, 0x1f, 0x0005);
3100 rtl_writephy(tp, 0x05, 0x8b80);
3101 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3102 rtl_writephy(tp, 0x1f, 0x0000);
3103
3104 /* PHY auto speed down */
3105 rtl_writephy(tp, 0x1f, 0x0007);
3106 rtl_writephy(tp, 0x1e, 0x002d);
3107 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3108 rtl_writephy(tp, 0x1f, 0x0000);
3109 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3110
3111 /* Improve 10M EEE waveform */
3112 rtl_writephy(tp, 0x1f, 0x0005);
3113 rtl_writephy(tp, 0x05, 0x8b86);
3114 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3115 rtl_writephy(tp, 0x1f, 0x0000);
3116}
3117
françois romieu4da19632011-01-03 15:07:55 +00003118static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003119{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003120 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003121 { 0x1f, 0x0003 },
3122 { 0x08, 0x441d },
3123 { 0x01, 0x9100 },
3124 { 0x1f, 0x0000 }
3125 };
3126
françois romieu4da19632011-01-03 15:07:55 +00003127 rtl_writephy(tp, 0x1f, 0x0000);
3128 rtl_patchphy(tp, 0x11, 1 << 12);
3129 rtl_patchphy(tp, 0x19, 1 << 13);
3130 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003131
françois romieu4da19632011-01-03 15:07:55 +00003132 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003133}
3134
Hayes Wang5a5e4442011-02-22 17:26:21 +08003135static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3136{
3137 static const struct phy_reg phy_reg_init[] = {
3138 { 0x1f, 0x0005 },
3139 { 0x1a, 0x0000 },
3140 { 0x1f, 0x0000 },
3141
3142 { 0x1f, 0x0004 },
3143 { 0x1c, 0x0000 },
3144 { 0x1f, 0x0000 },
3145
3146 { 0x1f, 0x0001 },
3147 { 0x15, 0x7701 },
3148 { 0x1f, 0x0000 }
3149 };
3150
3151 /* Disable ALDPS before ram code */
3152 rtl_writephy(tp, 0x1f, 0x0000);
3153 rtl_writephy(tp, 0x18, 0x0310);
3154 msleep(100);
3155
François Romieu953a12c2011-04-24 17:38:48 +02003156 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003157
3158 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3159}
3160
Francois Romieu5615d9f2007-08-17 17:50:46 +02003161static void rtl_hw_phy_config(struct net_device *dev)
3162{
3163 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003164
3165 rtl8169_print_mac_version(tp);
3166
3167 switch (tp->mac_version) {
3168 case RTL_GIGA_MAC_VER_01:
3169 break;
3170 case RTL_GIGA_MAC_VER_02:
3171 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003172 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003173 break;
3174 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003175 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003176 break;
françois romieu2e9558562009-08-10 19:44:19 +00003177 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003178 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003179 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003180 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003181 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003182 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003183 case RTL_GIGA_MAC_VER_07:
3184 case RTL_GIGA_MAC_VER_08:
3185 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003186 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003187 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003188 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003189 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003190 break;
3191 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003192 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003193 break;
3194 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003195 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003196 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003197 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003198 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003199 break;
3200 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003201 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003202 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003203 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003204 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003205 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003206 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003207 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003208 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003209 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003210 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003211 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003212 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003213 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003214 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003215 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003216 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003217 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003218 break;
3219 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003220 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003221 break;
3222 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003223 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003224 break;
françois romieue6de30d2011-01-03 15:08:37 +00003225 case RTL_GIGA_MAC_VER_28:
3226 rtl8168d_4_hw_phy_config(tp);
3227 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003228 case RTL_GIGA_MAC_VER_29:
3229 case RTL_GIGA_MAC_VER_30:
3230 rtl8105e_hw_phy_config(tp);
3231 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003232 case RTL_GIGA_MAC_VER_31:
3233 /* None. */
3234 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003235 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003236 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003237 rtl8168e_1_hw_phy_config(tp);
3238 break;
3239 case RTL_GIGA_MAC_VER_34:
3240 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003241 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003242 case RTL_GIGA_MAC_VER_35:
3243 rtl8168f_1_hw_phy_config(tp);
3244 break;
3245 case RTL_GIGA_MAC_VER_36:
3246 rtl8168f_2_hw_phy_config(tp);
3247 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003248
Francois Romieu5615d9f2007-08-17 17:50:46 +02003249 default:
3250 break;
3251 }
3252}
3253
Francois Romieuda78dbf2012-01-26 14:18:23 +01003254static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 struct timer_list *timer = &tp->timer;
3257 void __iomem *ioaddr = tp->mmio_addr;
3258 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3259
Francois Romieubcf0bf92006-07-26 23:14:13 +02003260 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
françois romieu4da19632011-01-03 15:07:55 +00003262 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003263 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 * A busy loop could burn quite a few cycles on nowadays CPU.
3265 * Let's delay the execution of the timer for a few ticks.
3266 */
3267 timeout = HZ/10;
3268 goto out_mod_timer;
3269 }
3270
3271 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01003272 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
Francois Romieuda78dbf2012-01-26 14:18:23 +01003274 netif_warn(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
françois romieu4da19632011-01-03 15:07:55 +00003276 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
3278out_mod_timer:
3279 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003280}
3281
3282static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3283{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003284 if (!test_and_set_bit(flag, tp->wk.flags))
3285 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003286}
3287
3288static void rtl8169_phy_timer(unsigned long __opaque)
3289{
3290 struct net_device *dev = (struct net_device *)__opaque;
3291 struct rtl8169_private *tp = netdev_priv(dev);
3292
Francois Romieu98ddf982012-01-31 10:47:34 +01003293 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294}
3295
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296#ifdef CONFIG_NET_POLL_CONTROLLER
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297static void rtl8169_netpoll(struct net_device *dev)
3298{
3299 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300
Francois Romieuda78dbf2012-01-26 14:18:23 +01003301 rtl8169_interrupt(tp->pci_dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302}
3303#endif
3304
3305static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3306 void __iomem *ioaddr)
3307{
3308 iounmap(ioaddr);
3309 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003310 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 pci_disable_device(pdev);
3312 free_netdev(dev);
3313}
3314
Francois Romieubf793292006-11-01 00:53:05 +01003315static void rtl8169_phy_reset(struct net_device *dev,
3316 struct rtl8169_private *tp)
3317{
Francois Romieu07d3f512007-02-21 22:40:46 +01003318 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003319
françois romieu4da19632011-01-03 15:07:55 +00003320 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003321 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003322 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003323 return;
3324 msleep(1);
3325 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003326 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003327}
3328
David S. Miller8decf862011-09-22 03:23:13 -04003329static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3330{
3331 void __iomem *ioaddr = tp->mmio_addr;
3332
3333 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3334 (RTL_R8(PHYstatus) & TBI_Enable);
3335}
3336
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003337static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003339 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003340
Francois Romieu5615d9f2007-08-17 17:50:46 +02003341 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003342
Marcus Sundberg773328942008-07-10 21:28:08 +02003343 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3344 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3345 RTL_W8(0x82, 0x01);
3346 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003347
Francois Romieu6dccd162007-02-13 23:38:05 +01003348 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3349
3350 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3351 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003352
Francois Romieubcf0bf92006-07-26 23:14:13 +02003353 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003354 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3355 RTL_W8(0x82, 0x01);
3356 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003357 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003358 }
3359
Francois Romieubf793292006-11-01 00:53:05 +01003360 rtl8169_phy_reset(dev, tp);
3361
Oliver Neukum54405cd2011-01-06 21:55:13 +01003362 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003363 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3364 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3365 (tp->mii.supports_gmii ?
3366 ADVERTISED_1000baseT_Half |
3367 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003368
David S. Miller8decf862011-09-22 03:23:13 -04003369 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003370 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003371}
3372
Francois Romieu773d2022007-01-31 23:47:43 +01003373static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3374{
3375 void __iomem *ioaddr = tp->mmio_addr;
3376 u32 high;
3377 u32 low;
3378
3379 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3380 high = addr[4] | (addr[5] << 8);
3381
Francois Romieuda78dbf2012-01-26 14:18:23 +01003382 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003383
3384 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003385
Francois Romieu773d2022007-01-31 23:47:43 +01003386 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00003387 RTL_R32(MAC4);
3388
Francois Romieu78f1cd02010-03-27 19:35:46 -07003389 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00003390 RTL_R32(MAC0);
3391
françois romieuc28aa382011-08-02 03:53:43 +00003392 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3393 const struct exgmac_reg e[] = {
3394 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3395 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3396 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3397 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3398 low >> 16 },
3399 };
3400
3401 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3402 }
3403
Francois Romieu773d2022007-01-31 23:47:43 +01003404 RTL_W8(Cfg9346, Cfg9346_Lock);
3405
Francois Romieuda78dbf2012-01-26 14:18:23 +01003406 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003407}
3408
3409static int rtl_set_mac_address(struct net_device *dev, void *p)
3410{
3411 struct rtl8169_private *tp = netdev_priv(dev);
3412 struct sockaddr *addr = p;
3413
3414 if (!is_valid_ether_addr(addr->sa_data))
3415 return -EADDRNOTAVAIL;
3416
3417 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3418
3419 rtl_rar_set(tp, dev->dev_addr);
3420
3421 return 0;
3422}
3423
Francois Romieu5f787a12006-08-17 13:02:36 +02003424static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3425{
3426 struct rtl8169_private *tp = netdev_priv(dev);
3427 struct mii_ioctl_data *data = if_mii(ifr);
3428
Francois Romieu8b4ab282008-11-19 22:05:25 -08003429 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3430}
Francois Romieu5f787a12006-08-17 13:02:36 +02003431
Francois Romieucecb5fd2011-04-01 10:21:07 +02003432static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3433 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003434{
Francois Romieu5f787a12006-08-17 13:02:36 +02003435 switch (cmd) {
3436 case SIOCGMIIPHY:
3437 data->phy_id = 32; /* Internal PHY */
3438 return 0;
3439
3440 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003441 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003442 return 0;
3443
3444 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003445 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003446 return 0;
3447 }
3448 return -EOPNOTSUPP;
3449}
3450
Francois Romieu8b4ab282008-11-19 22:05:25 -08003451static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3452{
3453 return -EOPNOTSUPP;
3454}
3455
Francois Romieu0e485152007-02-20 00:00:26 +01003456static const struct rtl_cfg_info {
3457 void (*hw_start)(struct net_device *);
3458 unsigned int region;
3459 unsigned int align;
Francois Romieuda78dbf2012-01-26 14:18:23 +01003460 u16 event_slow;
Francois Romieuccdffb92008-07-26 14:26:06 +02003461 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07003462 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01003463} rtl_cfg_infos [] = {
3464 [RTL_CFG_0] = {
3465 .hw_start = rtl_hw_start_8169,
3466 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01003467 .align = 0,
Francois Romieuda78dbf2012-01-26 14:18:23 +01003468 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003469 .features = RTL_FEATURE_GMII,
3470 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01003471 },
3472 [RTL_CFG_1] = {
3473 .hw_start = rtl_hw_start_8168,
3474 .region = 2,
3475 .align = 8,
Francois Romieuda78dbf2012-01-26 14:18:23 +01003476 .event_slow = SYSErr | LinkChg | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003477 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3478 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01003479 },
3480 [RTL_CFG_2] = {
3481 .hw_start = rtl_hw_start_8101,
3482 .region = 2,
3483 .align = 8,
Francois Romieuda78dbf2012-01-26 14:18:23 +01003484 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
3485 PCSTimeout,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003486 .features = RTL_FEATURE_MSI,
3487 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01003488 }
3489};
3490
Francois Romieufbac58f2007-10-04 22:51:38 +02003491/* Cfg9346_Unlock assumed. */
françois romieu2ca6cf02011-12-15 08:37:43 +00003492static unsigned rtl_try_msi(struct rtl8169_private *tp,
Francois Romieufbac58f2007-10-04 22:51:38 +02003493 const struct rtl_cfg_info *cfg)
3494{
françois romieu2ca6cf02011-12-15 08:37:43 +00003495 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieufbac58f2007-10-04 22:51:38 +02003496 unsigned msi = 0;
3497 u8 cfg2;
3498
3499 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02003500 if (cfg->features & RTL_FEATURE_MSI) {
françois romieu2ca6cf02011-12-15 08:37:43 +00003501 if (pci_enable_msi(tp->pci_dev)) {
3502 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
Francois Romieufbac58f2007-10-04 22:51:38 +02003503 } else {
3504 cfg2 |= MSIEnable;
3505 msi = RTL_FEATURE_MSI;
3506 }
3507 }
françois romieu2ca6cf02011-12-15 08:37:43 +00003508 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3509 RTL_W8(Config2, cfg2);
Francois Romieufbac58f2007-10-04 22:51:38 +02003510 return msi;
3511}
3512
3513static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3514{
3515 if (tp->features & RTL_FEATURE_MSI) {
3516 pci_disable_msi(pdev);
3517 tp->features &= ~RTL_FEATURE_MSI;
3518 }
3519}
3520
Francois Romieu8b4ab282008-11-19 22:05:25 -08003521static const struct net_device_ops rtl8169_netdev_ops = {
3522 .ndo_open = rtl8169_open,
3523 .ndo_stop = rtl8169_close,
3524 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08003525 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003526 .ndo_tx_timeout = rtl8169_tx_timeout,
3527 .ndo_validate_addr = eth_validate_addr,
3528 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00003529 .ndo_fix_features = rtl8169_fix_features,
3530 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003531 .ndo_set_mac_address = rtl_set_mac_address,
3532 .ndo_do_ioctl = rtl8169_ioctl,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003533 .ndo_set_rx_mode = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003534#ifdef CONFIG_NET_POLL_CONTROLLER
3535 .ndo_poll_controller = rtl8169_netpoll,
3536#endif
3537
3538};
3539
françois romieuc0e45c12011-01-03 15:08:04 +00003540static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3541{
3542 struct mdio_ops *ops = &tp->mdio_ops;
3543
3544 switch (tp->mac_version) {
3545 case RTL_GIGA_MAC_VER_27:
3546 ops->write = r8168dp_1_mdio_write;
3547 ops->read = r8168dp_1_mdio_read;
3548 break;
françois romieue6de30d2011-01-03 15:08:37 +00003549 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003550 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003551 ops->write = r8168dp_2_mdio_write;
3552 ops->read = r8168dp_2_mdio_read;
3553 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003554 default:
3555 ops->write = r8169_mdio_write;
3556 ops->read = r8169_mdio_read;
3557 break;
3558 }
3559}
3560
David S. Miller1805b2f2011-10-24 18:18:09 -04003561static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3562{
3563 void __iomem *ioaddr = tp->mmio_addr;
3564
3565 switch (tp->mac_version) {
3566 case RTL_GIGA_MAC_VER_29:
3567 case RTL_GIGA_MAC_VER_30:
3568 case RTL_GIGA_MAC_VER_32:
3569 case RTL_GIGA_MAC_VER_33:
3570 case RTL_GIGA_MAC_VER_34:
3571 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3572 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3573 break;
3574 default:
3575 break;
3576 }
3577}
3578
3579static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3580{
3581 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3582 return false;
3583
3584 rtl_writephy(tp, 0x1f, 0x0000);
3585 rtl_writephy(tp, MII_BMCR, 0x0000);
3586
3587 rtl_wol_suspend_quirk(tp);
3588
3589 return true;
3590}
3591
françois romieu065c27c2011-01-03 15:08:12 +00003592static void r810x_phy_power_down(struct rtl8169_private *tp)
3593{
3594 rtl_writephy(tp, 0x1f, 0x0000);
3595 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3596}
3597
3598static void r810x_phy_power_up(struct rtl8169_private *tp)
3599{
3600 rtl_writephy(tp, 0x1f, 0x0000);
3601 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3602}
3603
3604static void r810x_pll_power_down(struct rtl8169_private *tp)
3605{
David S. Miller1805b2f2011-10-24 18:18:09 -04003606 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003607 return;
françois romieu065c27c2011-01-03 15:08:12 +00003608
3609 r810x_phy_power_down(tp);
3610}
3611
3612static void r810x_pll_power_up(struct rtl8169_private *tp)
3613{
3614 r810x_phy_power_up(tp);
3615}
3616
3617static void r8168_phy_power_up(struct rtl8169_private *tp)
3618{
3619 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003620 switch (tp->mac_version) {
3621 case RTL_GIGA_MAC_VER_11:
3622 case RTL_GIGA_MAC_VER_12:
3623 case RTL_GIGA_MAC_VER_17:
3624 case RTL_GIGA_MAC_VER_18:
3625 case RTL_GIGA_MAC_VER_19:
3626 case RTL_GIGA_MAC_VER_20:
3627 case RTL_GIGA_MAC_VER_21:
3628 case RTL_GIGA_MAC_VER_22:
3629 case RTL_GIGA_MAC_VER_23:
3630 case RTL_GIGA_MAC_VER_24:
3631 case RTL_GIGA_MAC_VER_25:
3632 case RTL_GIGA_MAC_VER_26:
3633 case RTL_GIGA_MAC_VER_27:
3634 case RTL_GIGA_MAC_VER_28:
3635 case RTL_GIGA_MAC_VER_31:
3636 rtl_writephy(tp, 0x0e, 0x0000);
3637 break;
3638 default:
3639 break;
3640 }
françois romieu065c27c2011-01-03 15:08:12 +00003641 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3642}
3643
3644static void r8168_phy_power_down(struct rtl8169_private *tp)
3645{
3646 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003647 switch (tp->mac_version) {
3648 case RTL_GIGA_MAC_VER_32:
3649 case RTL_GIGA_MAC_VER_33:
3650 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3651 break;
3652
3653 case RTL_GIGA_MAC_VER_11:
3654 case RTL_GIGA_MAC_VER_12:
3655 case RTL_GIGA_MAC_VER_17:
3656 case RTL_GIGA_MAC_VER_18:
3657 case RTL_GIGA_MAC_VER_19:
3658 case RTL_GIGA_MAC_VER_20:
3659 case RTL_GIGA_MAC_VER_21:
3660 case RTL_GIGA_MAC_VER_22:
3661 case RTL_GIGA_MAC_VER_23:
3662 case RTL_GIGA_MAC_VER_24:
3663 case RTL_GIGA_MAC_VER_25:
3664 case RTL_GIGA_MAC_VER_26:
3665 case RTL_GIGA_MAC_VER_27:
3666 case RTL_GIGA_MAC_VER_28:
3667 case RTL_GIGA_MAC_VER_31:
3668 rtl_writephy(tp, 0x0e, 0x0200);
3669 default:
3670 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3671 break;
3672 }
françois romieu065c27c2011-01-03 15:08:12 +00003673}
3674
3675static void r8168_pll_power_down(struct rtl8169_private *tp)
3676{
3677 void __iomem *ioaddr = tp->mmio_addr;
3678
Francois Romieucecb5fd2011-04-01 10:21:07 +02003679 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3680 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3681 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003682 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003683 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003684 }
françois romieu065c27c2011-01-03 15:08:12 +00003685
Francois Romieucecb5fd2011-04-01 10:21:07 +02003686 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3687 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003688 (RTL_R16(CPlusCmd) & ASF)) {
3689 return;
3690 }
3691
hayeswang01dc7fe2011-03-21 01:50:28 +00003692 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3693 tp->mac_version == RTL_GIGA_MAC_VER_33)
3694 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3695
David S. Miller1805b2f2011-10-24 18:18:09 -04003696 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003697 return;
françois romieu065c27c2011-01-03 15:08:12 +00003698
3699 r8168_phy_power_down(tp);
3700
3701 switch (tp->mac_version) {
3702 case RTL_GIGA_MAC_VER_25:
3703 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003704 case RTL_GIGA_MAC_VER_27:
3705 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003706 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003707 case RTL_GIGA_MAC_VER_32:
3708 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003709 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3710 break;
3711 }
3712}
3713
3714static void r8168_pll_power_up(struct rtl8169_private *tp)
3715{
3716 void __iomem *ioaddr = tp->mmio_addr;
3717
Francois Romieucecb5fd2011-04-01 10:21:07 +02003718 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3719 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3720 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003721 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003722 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003723 }
françois romieu065c27c2011-01-03 15:08:12 +00003724
3725 switch (tp->mac_version) {
3726 case RTL_GIGA_MAC_VER_25:
3727 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003728 case RTL_GIGA_MAC_VER_27:
3729 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003730 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003731 case RTL_GIGA_MAC_VER_32:
3732 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003733 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3734 break;
3735 }
3736
3737 r8168_phy_power_up(tp);
3738}
3739
Francois Romieud58d46b2011-05-03 16:38:29 +02003740static void rtl_generic_op(struct rtl8169_private *tp,
3741 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00003742{
3743 if (op)
3744 op(tp);
3745}
3746
3747static void rtl_pll_power_down(struct rtl8169_private *tp)
3748{
Francois Romieud58d46b2011-05-03 16:38:29 +02003749 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00003750}
3751
3752static void rtl_pll_power_up(struct rtl8169_private *tp)
3753{
Francois Romieud58d46b2011-05-03 16:38:29 +02003754 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00003755}
3756
3757static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3758{
3759 struct pll_power_ops *ops = &tp->pll_power_ops;
3760
3761 switch (tp->mac_version) {
3762 case RTL_GIGA_MAC_VER_07:
3763 case RTL_GIGA_MAC_VER_08:
3764 case RTL_GIGA_MAC_VER_09:
3765 case RTL_GIGA_MAC_VER_10:
3766 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003767 case RTL_GIGA_MAC_VER_29:
3768 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003769 ops->down = r810x_pll_power_down;
3770 ops->up = r810x_pll_power_up;
3771 break;
3772
3773 case RTL_GIGA_MAC_VER_11:
3774 case RTL_GIGA_MAC_VER_12:
3775 case RTL_GIGA_MAC_VER_17:
3776 case RTL_GIGA_MAC_VER_18:
3777 case RTL_GIGA_MAC_VER_19:
3778 case RTL_GIGA_MAC_VER_20:
3779 case RTL_GIGA_MAC_VER_21:
3780 case RTL_GIGA_MAC_VER_22:
3781 case RTL_GIGA_MAC_VER_23:
3782 case RTL_GIGA_MAC_VER_24:
3783 case RTL_GIGA_MAC_VER_25:
3784 case RTL_GIGA_MAC_VER_26:
3785 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003786 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003787 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003788 case RTL_GIGA_MAC_VER_32:
3789 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003790 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08003791 case RTL_GIGA_MAC_VER_35:
3792 case RTL_GIGA_MAC_VER_36:
françois romieu065c27c2011-01-03 15:08:12 +00003793 ops->down = r8168_pll_power_down;
3794 ops->up = r8168_pll_power_up;
3795 break;
3796
3797 default:
3798 ops->down = NULL;
3799 ops->up = NULL;
3800 break;
3801 }
3802}
3803
Hayes Wange542a222011-07-06 15:58:04 +08003804static void rtl_init_rxcfg(struct rtl8169_private *tp)
3805{
3806 void __iomem *ioaddr = tp->mmio_addr;
3807
3808 switch (tp->mac_version) {
3809 case RTL_GIGA_MAC_VER_01:
3810 case RTL_GIGA_MAC_VER_02:
3811 case RTL_GIGA_MAC_VER_03:
3812 case RTL_GIGA_MAC_VER_04:
3813 case RTL_GIGA_MAC_VER_05:
3814 case RTL_GIGA_MAC_VER_06:
3815 case RTL_GIGA_MAC_VER_10:
3816 case RTL_GIGA_MAC_VER_11:
3817 case RTL_GIGA_MAC_VER_12:
3818 case RTL_GIGA_MAC_VER_13:
3819 case RTL_GIGA_MAC_VER_14:
3820 case RTL_GIGA_MAC_VER_15:
3821 case RTL_GIGA_MAC_VER_16:
3822 case RTL_GIGA_MAC_VER_17:
3823 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3824 break;
3825 case RTL_GIGA_MAC_VER_18:
3826 case RTL_GIGA_MAC_VER_19:
3827 case RTL_GIGA_MAC_VER_20:
3828 case RTL_GIGA_MAC_VER_21:
3829 case RTL_GIGA_MAC_VER_22:
3830 case RTL_GIGA_MAC_VER_23:
3831 case RTL_GIGA_MAC_VER_24:
3832 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3833 break;
3834 default:
3835 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3836 break;
3837 }
3838}
3839
Hayes Wang92fc43b2011-07-06 15:58:03 +08003840static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3841{
3842 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3843}
3844
Francois Romieud58d46b2011-05-03 16:38:29 +02003845static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3846{
3847 rtl_generic_op(tp, tp->jumbo_ops.enable);
3848}
3849
3850static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3851{
3852 rtl_generic_op(tp, tp->jumbo_ops.disable);
3853}
3854
3855static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3856{
3857 void __iomem *ioaddr = tp->mmio_addr;
3858
3859 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3860 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3861 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3862}
3863
3864static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3865{
3866 void __iomem *ioaddr = tp->mmio_addr;
3867
3868 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3869 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3870 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3871}
3872
3873static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3874{
3875 void __iomem *ioaddr = tp->mmio_addr;
3876
3877 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3878}
3879
3880static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3881{
3882 void __iomem *ioaddr = tp->mmio_addr;
3883
3884 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3885}
3886
3887static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3888{
3889 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003890
3891 RTL_W8(MaxTxPacketSize, 0x3f);
3892 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3893 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003894 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003895}
3896
3897static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3898{
3899 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003900
3901 RTL_W8(MaxTxPacketSize, 0x0c);
3902 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3903 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003904 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003905}
3906
3907static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3908{
3909 rtl_tx_performance_tweak(tp->pci_dev,
3910 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3911}
3912
3913static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3914{
3915 rtl_tx_performance_tweak(tp->pci_dev,
3916 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3917}
3918
3919static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3920{
3921 void __iomem *ioaddr = tp->mmio_addr;
3922
3923 r8168b_0_hw_jumbo_enable(tp);
3924
3925 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
3926}
3927
3928static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3929{
3930 void __iomem *ioaddr = tp->mmio_addr;
3931
3932 r8168b_0_hw_jumbo_disable(tp);
3933
3934 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3935}
3936
3937static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
3938{
3939 struct jumbo_ops *ops = &tp->jumbo_ops;
3940
3941 switch (tp->mac_version) {
3942 case RTL_GIGA_MAC_VER_11:
3943 ops->disable = r8168b_0_hw_jumbo_disable;
3944 ops->enable = r8168b_0_hw_jumbo_enable;
3945 break;
3946 case RTL_GIGA_MAC_VER_12:
3947 case RTL_GIGA_MAC_VER_17:
3948 ops->disable = r8168b_1_hw_jumbo_disable;
3949 ops->enable = r8168b_1_hw_jumbo_enable;
3950 break;
3951 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
3952 case RTL_GIGA_MAC_VER_19:
3953 case RTL_GIGA_MAC_VER_20:
3954 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
3955 case RTL_GIGA_MAC_VER_22:
3956 case RTL_GIGA_MAC_VER_23:
3957 case RTL_GIGA_MAC_VER_24:
3958 case RTL_GIGA_MAC_VER_25:
3959 case RTL_GIGA_MAC_VER_26:
3960 ops->disable = r8168c_hw_jumbo_disable;
3961 ops->enable = r8168c_hw_jumbo_enable;
3962 break;
3963 case RTL_GIGA_MAC_VER_27:
3964 case RTL_GIGA_MAC_VER_28:
3965 ops->disable = r8168dp_hw_jumbo_disable;
3966 ops->enable = r8168dp_hw_jumbo_enable;
3967 break;
3968 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
3969 case RTL_GIGA_MAC_VER_32:
3970 case RTL_GIGA_MAC_VER_33:
3971 case RTL_GIGA_MAC_VER_34:
3972 ops->disable = r8168e_hw_jumbo_disable;
3973 ops->enable = r8168e_hw_jumbo_enable;
3974 break;
3975
3976 /*
3977 * No action needed for jumbo frames with 8169.
3978 * No jumbo for 810x at all.
3979 */
3980 default:
3981 ops->disable = NULL;
3982 ops->enable = NULL;
3983 break;
3984 }
3985}
3986
Francois Romieu6f43adc2011-04-29 15:05:51 +02003987static void rtl_hw_reset(struct rtl8169_private *tp)
3988{
3989 void __iomem *ioaddr = tp->mmio_addr;
3990 int i;
3991
3992 /* Soft reset the chip. */
3993 RTL_W8(ChipCmd, CmdReset);
3994
3995 /* Check that the chip has finished the reset. */
3996 for (i = 0; i < 100; i++) {
3997 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3998 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003999 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004000 }
4001}
4002
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004003static int __devinit
4004rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
4005{
Francois Romieu0e485152007-02-20 00:00:26 +01004006 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
4007 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02004009 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004010 struct net_device *dev;
4011 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004012 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01004013 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004015 if (netif_msg_drv(&debug)) {
4016 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
4017 MODULENAME, RTL8169_VERSION);
4018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004021 if (!dev) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004022 rc = -ENOMEM;
4023 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 }
4025
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08004027 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00004029 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02004030 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02004031 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032
Francois Romieuccdffb92008-07-26 14:26:06 +02004033 mii = &tp->mii;
4034 mii->dev = dev;
4035 mii->mdio_read = rtl_mdio_read;
4036 mii->mdio_write = rtl_mdio_write;
4037 mii->phy_id_mask = 0x1f;
4038 mii->reg_num_mask = 0x1f;
4039 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
4040
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00004041 /* disable ASPM completely as that cause random device stop working
4042 * problems as well as full system hangs for some PCIe devices users */
4043 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
4044 PCIE_LINK_STATE_CLKPM);
4045
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 /* enable device (incl. PCI PM wakeup and hotplug setup) */
4047 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02004048 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004049 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004050 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 }
4052
françois romieu87aeec72010-04-26 11:42:06 +00004053 if (pci_set_mwi(pdev) < 0)
4054 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02004057 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004058 netif_err(tp, probe, dev,
4059 "region #%d not an MMIO resource, aborting\n",
4060 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00004062 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004064
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02004066 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004067 netif_err(tp, probe, dev,
4068 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00004070 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 }
4072
4073 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02004074 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004075 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00004076 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 }
4078
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004079 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080
4081 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07004082 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 tp->cp_cmd |= PCIDAC;
4084 dev->features |= NETIF_F_HIGHDMA;
4085 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07004086 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004088 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00004089 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 }
4091 }
4092
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02004094 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004095 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004096 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00004098 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 }
Francois Romieu6f43adc2011-04-29 15:05:51 +02004100 tp->mmio_addr = ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101
Jon Masone44daad2011-06-27 07:46:31 +00004102 if (!pci_is_pcie(pdev))
4103 netif_info(tp, probe, dev, "not PCI Express\n");
David S. Miller4300e8c2010-03-26 10:23:30 -07004104
Hayes Wange542a222011-07-06 15:58:04 +08004105 /* Identify chip attached to board */
4106 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
4107
4108 rtl_init_rxcfg(tp);
4109
Francois Romieu9085cdfa2012-01-26 12:59:08 +01004110 rtl_irq_disable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111
Francois Romieu6f43adc2011-04-29 15:05:51 +02004112 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113
Francois Romieu9085cdfa2012-01-26 12:59:08 +01004114 rtl_ack_events(tp, 0xffff);
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07004115
françois romieuca52efd2009-07-24 12:34:19 +00004116 pci_set_master(pdev);
4117
Francois Romieu7a8fc772011-03-01 17:18:33 +01004118 /*
4119 * Pretend we are using VLANs; This bypasses a nasty bug where
4120 * Interrupts stop flowing on high load on 8110SCd controllers.
4121 */
4122 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4123 tp->cp_cmd |= RxVlan;
4124
françois romieuc0e45c12011-01-03 15:08:04 +00004125 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004126 rtl_init_pll_power_ops(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02004127 rtl_init_jumbo_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00004128
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130
Francois Romieu85bffe62011-04-27 08:22:39 +02004131 chipset = tp->mac_version;
4132 tp->txd_version = rtl_chip_infos[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133
Francois Romieu5d06a992006-02-23 00:47:58 +01004134 RTL_W8(Cfg9346, Cfg9346_Unlock);
4135 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
4136 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07004137 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
4138 tp->features |= RTL_FEATURE_WOL;
4139 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
4140 tp->features |= RTL_FEATURE_WOL;
françois romieu2ca6cf02011-12-15 08:37:43 +00004141 tp->features |= rtl_try_msi(tp, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01004142 RTL_W8(Cfg9346, Cfg9346_Lock);
4143
David S. Miller8decf862011-09-22 03:23:13 -04004144 if (rtl_tbi_enabled(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 tp->set_speed = rtl8169_set_speed_tbi;
4146 tp->get_settings = rtl8169_gset_tbi;
4147 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
4148 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
4149 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08004150 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 } else {
4152 tp->set_speed = rtl8169_set_speed_xmii;
4153 tp->get_settings = rtl8169_gset_xmii;
4154 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
4155 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
4156 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08004157 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 }
4159
Francois Romieuda78dbf2012-01-26 14:18:23 +01004160 mutex_init(&tp->wk.mutex);
Francois Romieudf58ef512008-10-09 14:35:58 -07004161
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00004162 /* Get MAC address */
Joe Perches6a3c910c2011-11-16 09:38:02 +00004163 for (i = 0; i < ETH_ALEN; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04004165 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
4169 dev->irq = pdev->irq;
4170 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004172 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173
Michał Mirosław350fb322011-04-08 06:35:56 +00004174 /* don't enable SG, IP_CSUM and TSO by default - it might not work
4175 * properly for all devices */
4176 dev->features |= NETIF_F_RXCSUM |
4177 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
4178
4179 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
4180 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
4181 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
4182 NETIF_F_HIGHDMA;
4183
4184 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4185 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
4186 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187
Ben Greear6bbe0212012-02-10 15:04:33 +00004188 dev->hw_features |= NETIF_F_RXALL;
Ben Greear79d0c1d2012-02-10 15:04:34 +00004189 dev->hw_features |= NETIF_F_RXFCS;
Ben Greear6bbe0212012-02-10 15:04:33 +00004190
Francois Romieu0e485152007-02-20 00:00:26 +01004191 tp->hw_start = cfg->hw_start;
Francois Romieuda78dbf2012-01-26 14:18:23 +01004192 tp->event_slow = cfg->event_slow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193
David S. Miller8decf862011-09-22 03:23:13 -04004194 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
4195 ~(RxBOVF | RxFOVF) : ~0;
4196
Francois Romieu2efa53f2007-03-09 00:00:05 +01004197 init_timer(&tp->timer);
4198 tp->timer.data = (unsigned long) dev;
4199 tp->timer.function = rtl8169_phy_timer;
4200
Francois Romieub6ffd972011-06-17 17:00:05 +02004201 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
François Romieu953a12c2011-04-24 17:38:48 +02004202
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004204 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00004205 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
4207 pci_set_drvdata(pdev, dev);
4208
Joe Perchesbf82c182010-02-09 11:49:50 +00004209 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu85bffe62011-04-27 08:22:39 +02004210 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00004211 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Francois Romieud58d46b2011-05-03 16:38:29 +02004212 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
4213 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
4214 "tx checksumming: %s]\n",
4215 rtl_chip_infos[chipset].jumbo_max,
4216 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
4217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Francois Romieucecb5fd2011-04-01 10:21:07 +02004219 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4220 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4221 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00004222 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004223 }
françois romieub646d902011-01-03 15:08:21 +00004224
Bruno Prémont8b76ab32008-10-08 17:06:25 -07004225 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226
Alan Sternf3ec4f82010-06-08 15:23:51 -04004227 if (pci_dev_run_wake(pdev))
4228 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004229
Ivan Vecera0d672e92011-02-15 02:08:39 +00004230 netif_carrier_off(dev);
4231
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004232out:
4233 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234
françois romieu87aeec72010-04-26 11:42:06 +00004235err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02004236 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004237 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00004238err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004239 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00004240err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004241 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004242 pci_disable_device(pdev);
4243err_out_free_dev_1:
4244 free_netdev(dev);
4245 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246}
4247
Francois Romieu07d3f512007-02-21 22:40:46 +01004248static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249{
4250 struct net_device *dev = pci_get_drvdata(pdev);
4251 struct rtl8169_private *tp = netdev_priv(dev);
4252
Francois Romieucecb5fd2011-04-01 10:21:07 +02004253 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4254 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4255 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00004256 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004257 }
françois romieub646d902011-01-03 15:08:21 +00004258
Francois Romieu4422bcd2012-01-26 11:23:32 +01004259 cancel_work_sync(&tp->wk.work);
Francois Romieueb2a0212007-02-15 23:37:21 +01004260
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08004262
François Romieu953a12c2011-04-24 17:38:48 +02004263 rtl_release_firmware(tp);
4264
Alan Sternf3ec4f82010-06-08 15:23:51 -04004265 if (pci_dev_run_wake(pdev))
4266 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004267
Ivan Veceracc098dc2009-11-29 23:12:52 -08004268 /* restore original MAC address */
4269 rtl_rar_set(tp, dev->perm_addr);
4270
Francois Romieufbac58f2007-10-04 22:51:38 +02004271 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 rtl8169_release_board(pdev, dev, tp->mmio_addr);
4273 pci_set_drvdata(pdev, NULL);
4274}
4275
Francois Romieub6ffd972011-06-17 17:00:05 +02004276static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4277{
4278 struct rtl_fw *rtl_fw;
4279 const char *name;
4280 int rc = -ENOMEM;
4281
4282 name = rtl_lookup_firmware_name(tp);
4283 if (!name)
4284 goto out_no_firmware;
4285
4286 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4287 if (!rtl_fw)
4288 goto err_warn;
4289
4290 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4291 if (rc < 0)
4292 goto err_free;
4293
Francois Romieufd112f22011-06-18 00:10:29 +02004294 rc = rtl_check_firmware(tp, rtl_fw);
4295 if (rc < 0)
4296 goto err_release_firmware;
4297
Francois Romieub6ffd972011-06-17 17:00:05 +02004298 tp->rtl_fw = rtl_fw;
4299out:
4300 return;
4301
Francois Romieufd112f22011-06-18 00:10:29 +02004302err_release_firmware:
4303 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004304err_free:
4305 kfree(rtl_fw);
4306err_warn:
4307 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4308 name, rc);
4309out_no_firmware:
4310 tp->rtl_fw = NULL;
4311 goto out;
4312}
4313
François Romieu953a12c2011-04-24 17:38:48 +02004314static void rtl_request_firmware(struct rtl8169_private *tp)
4315{
Francois Romieub6ffd972011-06-17 17:00:05 +02004316 if (IS_ERR(tp->rtl_fw))
4317 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004318}
4319
Francois Romieu4422bcd2012-01-26 11:23:32 +01004320static void rtl_task(struct work_struct *);
4321
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322static int rtl8169_open(struct net_device *dev)
4323{
4324 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00004325 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02004327 int retval = -ENOMEM;
4328
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004329 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
Neil Hormanc0cd8842010-03-29 13:16:02 -07004331 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004333 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004335 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
4336 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004338 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004340 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
4341 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02004343 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
4345 retval = rtl8169_init_ring(dev);
4346 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02004347 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348
Francois Romieu4422bcd2012-01-26 11:23:32 +01004349 INIT_WORK(&tp->wk.work, rtl_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350
Francois Romieu99f252b2007-04-02 22:59:59 +02004351 smp_mb();
4352
François Romieu953a12c2011-04-24 17:38:48 +02004353 rtl_request_firmware(tp);
4354
Francois Romieufbac58f2007-10-04 22:51:38 +02004355 retval = request_irq(dev->irq, rtl8169_interrupt,
4356 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02004357 dev->name, dev);
4358 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02004359 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02004360
Francois Romieuda78dbf2012-01-26 14:18:23 +01004361 rtl_lock_work(tp);
4362
Francois Romieu6c4a70c2012-01-31 10:56:44 +01004363 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004364
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004365 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004366
françois romieueee3a962011-01-08 02:17:26 +00004367 rtl8169_init_phy(dev, tp);
4368
Francois Romieuda78dbf2012-01-26 14:18:23 +01004369 __rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00004370
françois romieu065c27c2011-01-03 15:08:12 +00004371 rtl_pll_power_up(tp);
4372
Francois Romieu07ce4062007-02-23 23:36:39 +01004373 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374
Francois Romieuda78dbf2012-01-26 14:18:23 +01004375 netif_start_queue(dev);
4376
4377 rtl_unlock_work(tp);
4378
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004379 tp->saved_wolopts = 0;
4380 pm_runtime_put_noidle(&pdev->dev);
4381
françois romieueee3a962011-01-08 02:17:26 +00004382 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383out:
4384 return retval;
4385
François Romieu953a12c2011-04-24 17:38:48 +02004386err_release_fw_2:
4387 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02004388 rtl8169_rx_clear(tp);
4389err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004390 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
4391 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004392 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02004393err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004394 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
4395 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004396 tp->TxDescArray = NULL;
4397err_pm_runtime_put:
4398 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 goto out;
4400}
4401
Hayes Wang92fc43b2011-07-06 15:58:03 +08004402static void rtl_rx_close(struct rtl8169_private *tp)
4403{
4404 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004405
Francois Romieu1687b562011-07-19 17:21:29 +02004406 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004407}
4408
françois romieue6de30d2011-01-03 15:08:37 +00004409static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410{
françois romieue6de30d2011-01-03 15:08:37 +00004411 void __iomem *ioaddr = tp->mmio_addr;
4412
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004414 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415
Hayes Wang92fc43b2011-07-06 15:58:03 +08004416 rtl_rx_close(tp);
4417
Hayes Wang5d2e1952011-02-22 17:26:22 +08004418 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004419 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4420 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00004421 while (RTL_R8(TxPoll) & NPQ)
4422 udelay(20);
Hayes Wangc2218922011-09-06 16:55:18 +08004423 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4424 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
4425 tp->mac_version == RTL_GIGA_MAC_VER_36) {
David S. Miller8decf862011-09-22 03:23:13 -04004426 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08004427 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4428 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004429 } else {
4430 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4431 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004432 }
4433
Hayes Wang92fc43b2011-07-06 15:58:03 +08004434 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435}
4436
Francois Romieu7f796d832007-06-11 23:04:41 +02004437static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004438{
4439 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004440
4441 /* Set DMA burst size and Interframe Gap Time */
4442 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4443 (InterFrameGap << TxInterFrameGapShift));
4444}
4445
Francois Romieu07ce4062007-02-23 23:36:39 +01004446static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447{
4448 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449
Francois Romieu07ce4062007-02-23 23:36:39 +01004450 tp->hw_start(dev);
4451
Francois Romieuda78dbf2012-01-26 14:18:23 +01004452 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004453}
4454
Francois Romieu7f796d832007-06-11 23:04:41 +02004455static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4456 void __iomem *ioaddr)
4457{
4458 /*
4459 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4460 * register to be written before TxDescAddrLow to work.
4461 * Switching from MMIO to I/O access fixes the issue as well.
4462 */
4463 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004464 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004465 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004466 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004467}
4468
4469static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4470{
4471 u16 cmd;
4472
4473 cmd = RTL_R16(CPlusCmd);
4474 RTL_W16(CPlusCmd, cmd);
4475 return cmd;
4476}
4477
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004478static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004479{
4480 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004481 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004482}
4483
Francois Romieu6dccd162007-02-13 23:38:05 +01004484static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4485{
Francois Romieu37441002011-06-17 22:58:54 +02004486 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004487 u32 mac_version;
4488 u32 clk;
4489 u32 val;
4490 } cfg2_info [] = {
4491 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4492 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4493 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4494 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004495 };
4496 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004497 unsigned int i;
4498 u32 clk;
4499
4500 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004501 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004502 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4503 RTL_W32(0x7c, p->val);
4504 break;
4505 }
4506 }
4507}
4508
Francois Romieu07ce4062007-02-23 23:36:39 +01004509static void rtl_hw_start_8169(struct net_device *dev)
4510{
4511 struct rtl8169_private *tp = netdev_priv(dev);
4512 void __iomem *ioaddr = tp->mmio_addr;
4513 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004514
Francois Romieu9cb427b2006-11-02 00:10:16 +01004515 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4516 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4517 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4518 }
4519
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004521 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4522 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4523 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4524 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004525 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4526
Hayes Wange542a222011-07-06 15:58:04 +08004527 rtl_init_rxcfg(tp);
4528
françois romieuf0298f82011-01-03 15:07:42 +00004529 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004531 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
Francois Romieucecb5fd2011-04-01 10:21:07 +02004533 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4534 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4535 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4536 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004537 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538
Francois Romieu7f796d832007-06-11 23:04:41 +02004539 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004540
Francois Romieucecb5fd2011-04-01 10:21:07 +02004541 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4542 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004543 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004545 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 }
4547
Francois Romieubcf0bf92006-07-26 23:14:13 +02004548 RTL_W16(CPlusCmd, tp->cp_cmd);
4549
Francois Romieu6dccd162007-02-13 23:38:05 +01004550 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4551
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 /*
4553 * Undocumented corner. Supposedly:
4554 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4555 */
4556 RTL_W16(IntrMitigate, 0x0000);
4557
Francois Romieu7f796d832007-06-11 23:04:41 +02004558 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004559
Francois Romieucecb5fd2011-04-01 10:21:07 +02004560 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4561 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4562 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4563 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004564 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4565 rtl_set_rx_tx_config_registers(tp);
4566 }
4567
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004569
4570 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4571 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572
4573 RTL_W32(RxMissed, 0);
4574
Francois Romieu07ce4062007-02-23 23:36:39 +01004575 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576
4577 /* no early-rx interrupts */
4578 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004579}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580
françois romieu650e8d52011-01-03 15:08:29 +00004581static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004582{
4583 u32 csi;
4584
4585 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00004586 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4587}
4588
françois romieue6de30d2011-01-03 15:08:37 +00004589static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4590{
4591 rtl_csi_access_enable(ioaddr, 0x17000000);
4592}
4593
françois romieu650e8d52011-01-03 15:08:29 +00004594static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4595{
4596 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02004597}
4598
4599struct ephy_info {
4600 unsigned int offset;
4601 u16 mask;
4602 u16 bits;
4603};
4604
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004605static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004606{
4607 u16 w;
4608
4609 while (len-- > 0) {
4610 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4611 rtl_ephy_write(ioaddr, e->offset, w);
4612 e++;
4613 }
4614}
4615
Francois Romieub726e492008-06-28 12:22:59 +02004616static void rtl_disable_clock_request(struct pci_dev *pdev)
4617{
Jon Masone44daad2011-06-27 07:46:31 +00004618 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004619
4620 if (cap) {
4621 u16 ctl;
4622
4623 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4624 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4625 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4626 }
4627}
4628
françois romieue6de30d2011-01-03 15:08:37 +00004629static void rtl_enable_clock_request(struct pci_dev *pdev)
4630{
Jon Masone44daad2011-06-27 07:46:31 +00004631 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004632
4633 if (cap) {
4634 u16 ctl;
4635
4636 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4637 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4638 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4639 }
4640}
4641
Francois Romieub726e492008-06-28 12:22:59 +02004642#define R8168_CPCMD_QUIRK_MASK (\
4643 EnableBist | \
4644 Mac_dbgo_oe | \
4645 Force_half_dup | \
4646 Force_rxflow_en | \
4647 Force_txflow_en | \
4648 Cxpl_dbg_sel | \
4649 ASF | \
4650 PktCntrDisable | \
4651 Mac_dbgo_sel)
4652
Francois Romieu219a1e92008-06-28 11:58:39 +02004653static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4654{
Francois Romieub726e492008-06-28 12:22:59 +02004655 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4656
4657 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4658
Francois Romieu2e68ae42008-06-28 12:00:55 +02004659 rtl_tx_performance_tweak(pdev,
4660 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004661}
4662
4663static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4664{
4665 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004666
françois romieuf0298f82011-01-03 15:07:42 +00004667 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004668
4669 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004670}
4671
4672static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4673{
Francois Romieub726e492008-06-28 12:22:59 +02004674 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4675
4676 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4677
Francois Romieu219a1e92008-06-28 11:58:39 +02004678 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004679
4680 rtl_disable_clock_request(pdev);
4681
4682 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004683}
4684
Francois Romieuef3386f2008-06-29 12:24:30 +02004685static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004686{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004687 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004688 { 0x01, 0, 0x0001 },
4689 { 0x02, 0x0800, 0x1000 },
4690 { 0x03, 0, 0x0042 },
4691 { 0x06, 0x0080, 0x0000 },
4692 { 0x07, 0, 0x2000 }
4693 };
4694
françois romieu650e8d52011-01-03 15:08:29 +00004695 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004696
4697 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4698
Francois Romieu219a1e92008-06-28 11:58:39 +02004699 __rtl_hw_start_8168cp(ioaddr, pdev);
4700}
4701
Francois Romieuef3386f2008-06-29 12:24:30 +02004702static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4703{
françois romieu650e8d52011-01-03 15:08:29 +00004704 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004705
4706 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4707
4708 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4709
4710 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4711}
4712
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004713static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4714{
françois romieu650e8d52011-01-03 15:08:29 +00004715 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004716
4717 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4718
4719 /* Magic. */
4720 RTL_W8(DBG_REG, 0x20);
4721
françois romieuf0298f82011-01-03 15:07:42 +00004722 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004723
4724 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4725
4726 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4727}
4728
Francois Romieu219a1e92008-06-28 11:58:39 +02004729static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4730{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004731 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004732 { 0x02, 0x0800, 0x1000 },
4733 { 0x03, 0, 0x0002 },
4734 { 0x06, 0x0080, 0x0000 }
4735 };
4736
françois romieu650e8d52011-01-03 15:08:29 +00004737 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004738
4739 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4740
4741 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4742
Francois Romieu219a1e92008-06-28 11:58:39 +02004743 __rtl_hw_start_8168cp(ioaddr, pdev);
4744}
4745
4746static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4747{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004748 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004749 { 0x01, 0, 0x0001 },
4750 { 0x03, 0x0400, 0x0220 }
4751 };
4752
françois romieu650e8d52011-01-03 15:08:29 +00004753 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004754
4755 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4756
Francois Romieu219a1e92008-06-28 11:58:39 +02004757 __rtl_hw_start_8168cp(ioaddr, pdev);
4758}
4759
Francois Romieu197ff762008-06-28 13:16:02 +02004760static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4761{
4762 rtl_hw_start_8168c_2(ioaddr, pdev);
4763}
4764
Francois Romieu6fb07052008-06-29 11:54:28 +02004765static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4766{
françois romieu650e8d52011-01-03 15:08:29 +00004767 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004768
4769 __rtl_hw_start_8168cp(ioaddr, pdev);
4770}
4771
Francois Romieu5b538df2008-07-20 16:22:45 +02004772static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4773{
françois romieu650e8d52011-01-03 15:08:29 +00004774 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004775
4776 rtl_disable_clock_request(pdev);
4777
françois romieuf0298f82011-01-03 15:07:42 +00004778 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004779
4780 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4781
4782 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4783}
4784
hayeswang4804b3b2011-03-21 01:50:29 +00004785static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4786{
4787 rtl_csi_access_enable_1(ioaddr);
4788
4789 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4790
4791 RTL_W8(MaxTxPacketSize, TxPacketMax);
4792
4793 rtl_disable_clock_request(pdev);
4794}
4795
françois romieue6de30d2011-01-03 15:08:37 +00004796static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4797{
4798 static const struct ephy_info e_info_8168d_4[] = {
4799 { 0x0b, ~0, 0x48 },
4800 { 0x19, 0x20, 0x50 },
4801 { 0x0c, ~0, 0x20 }
4802 };
4803 int i;
4804
4805 rtl_csi_access_enable_1(ioaddr);
4806
4807 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4808
4809 RTL_W8(MaxTxPacketSize, TxPacketMax);
4810
4811 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4812 const struct ephy_info *e = e_info_8168d_4 + i;
4813 u16 w;
4814
4815 w = rtl_ephy_read(ioaddr, e->offset);
4816 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4817 }
4818
4819 rtl_enable_clock_request(pdev);
4820}
4821
Hayes Wang70090422011-07-06 15:58:06 +08004822static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
hayeswang01dc7fe2011-03-21 01:50:28 +00004823{
Hayes Wang70090422011-07-06 15:58:06 +08004824 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004825 { 0x00, 0x0200, 0x0100 },
4826 { 0x00, 0x0000, 0x0004 },
4827 { 0x06, 0x0002, 0x0001 },
4828 { 0x06, 0x0000, 0x0030 },
4829 { 0x07, 0x0000, 0x2000 },
4830 { 0x00, 0x0000, 0x0020 },
4831 { 0x03, 0x5800, 0x2000 },
4832 { 0x03, 0x0000, 0x0001 },
4833 { 0x01, 0x0800, 0x1000 },
4834 { 0x07, 0x0000, 0x4000 },
4835 { 0x1e, 0x0000, 0x2000 },
4836 { 0x19, 0xffff, 0xfe6c },
4837 { 0x0a, 0x0000, 0x0040 }
4838 };
4839
4840 rtl_csi_access_enable_2(ioaddr);
4841
Hayes Wang70090422011-07-06 15:58:06 +08004842 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004843
4844 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4845
4846 RTL_W8(MaxTxPacketSize, TxPacketMax);
4847
4848 rtl_disable_clock_request(pdev);
4849
4850 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004851 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4852 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004853
Francois Romieucecb5fd2011-04-01 10:21:07 +02004854 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004855}
4856
Hayes Wang70090422011-07-06 15:58:06 +08004857static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4858{
4859 static const struct ephy_info e_info_8168e_2[] = {
4860 { 0x09, 0x0000, 0x0080 },
4861 { 0x19, 0x0000, 0x0224 }
4862 };
4863
4864 rtl_csi_access_enable_1(ioaddr);
4865
4866 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4867
4868 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4869
4870 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4871 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4872 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4873 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4874 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4875 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4876 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4877 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4878 ERIAR_EXGMAC);
4879
Hayes Wang3090bd92011-09-06 16:55:15 +08004880 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004881
4882 rtl_disable_clock_request(pdev);
4883
4884 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4885 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4886
4887 /* Adjust EEE LED frequency */
4888 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4889
4890 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4891 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4892 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4893}
4894
Hayes Wangc2218922011-09-06 16:55:18 +08004895static void rtl_hw_start_8168f_1(void __iomem *ioaddr, struct pci_dev *pdev)
4896{
4897 static const struct ephy_info e_info_8168f_1[] = {
4898 { 0x06, 0x00c0, 0x0020 },
4899 { 0x08, 0x0001, 0x0002 },
4900 { 0x09, 0x0000, 0x0080 },
4901 { 0x19, 0x0000, 0x0224 }
4902 };
4903
4904 rtl_csi_access_enable_1(ioaddr);
4905
4906 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4907
4908 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4909
4910 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4911 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4912 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4913 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4914 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4915 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4916 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4917 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4918 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4919 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
4920 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4921 ERIAR_EXGMAC);
4922
4923 RTL_W8(MaxTxPacketSize, EarlySize);
4924
4925 rtl_disable_clock_request(pdev);
4926
4927 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4928 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4929
4930 /* Adjust EEE LED frequency */
4931 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4932
4933 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4934 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4935 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4936}
4937
Francois Romieu07ce4062007-02-23 23:36:39 +01004938static void rtl_hw_start_8168(struct net_device *dev)
4939{
Francois Romieu2dd99532007-06-11 23:22:52 +02004940 struct rtl8169_private *tp = netdev_priv(dev);
4941 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004942 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004943
4944 RTL_W8(Cfg9346, Cfg9346_Unlock);
4945
françois romieuf0298f82011-01-03 15:07:42 +00004946 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004947
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004948 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004949
Francois Romieu0e485152007-02-20 00:00:26 +01004950 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004951
4952 RTL_W16(CPlusCmd, tp->cp_cmd);
4953
Francois Romieu0e485152007-02-20 00:00:26 +01004954 RTL_W16(IntrMitigate, 0x5151);
4955
4956 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00004957 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01004958 tp->event_slow |= RxFIFOOver | PCSTimeout;
4959 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01004960 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004961
4962 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4963
Francois Romieub8363902008-06-01 12:31:57 +02004964 rtl_set_rx_mode(dev);
4965
4966 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4967 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004968
4969 RTL_R8(IntrMask);
4970
Francois Romieu219a1e92008-06-28 11:58:39 +02004971 switch (tp->mac_version) {
4972 case RTL_GIGA_MAC_VER_11:
4973 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004974 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004975
4976 case RTL_GIGA_MAC_VER_12:
4977 case RTL_GIGA_MAC_VER_17:
4978 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004979 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004980
4981 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004982 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004983 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004984
4985 case RTL_GIGA_MAC_VER_19:
4986 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004987 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004988
4989 case RTL_GIGA_MAC_VER_20:
4990 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004991 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004992
Francois Romieu197ff762008-06-28 13:16:02 +02004993 case RTL_GIGA_MAC_VER_21:
4994 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004995 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004996
Francois Romieu6fb07052008-06-29 11:54:28 +02004997 case RTL_GIGA_MAC_VER_22:
4998 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004999 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02005000
Francois Romieuef3386f2008-06-29 12:24:30 +02005001 case RTL_GIGA_MAC_VER_23:
5002 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00005003 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02005004
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005005 case RTL_GIGA_MAC_VER_24:
5006 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00005007 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005008
Francois Romieu5b538df2008-07-20 16:22:45 +02005009 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00005010 case RTL_GIGA_MAC_VER_26:
5011 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02005012 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00005013 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02005014
françois romieue6de30d2011-01-03 15:08:37 +00005015 case RTL_GIGA_MAC_VER_28:
5016 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00005017 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02005018
hayeswang4804b3b2011-03-21 01:50:29 +00005019 case RTL_GIGA_MAC_VER_31:
5020 rtl_hw_start_8168dp(ioaddr, pdev);
5021 break;
5022
hayeswang01dc7fe2011-03-21 01:50:28 +00005023 case RTL_GIGA_MAC_VER_32:
5024 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08005025 rtl_hw_start_8168e_1(ioaddr, pdev);
5026 break;
5027 case RTL_GIGA_MAC_VER_34:
5028 rtl_hw_start_8168e_2(ioaddr, pdev);
hayeswang01dc7fe2011-03-21 01:50:28 +00005029 break;
françois romieue6de30d2011-01-03 15:08:37 +00005030
Hayes Wangc2218922011-09-06 16:55:18 +08005031 case RTL_GIGA_MAC_VER_35:
5032 case RTL_GIGA_MAC_VER_36:
5033 rtl_hw_start_8168f_1(ioaddr, pdev);
5034 break;
5035
Francois Romieu219a1e92008-06-28 11:58:39 +02005036 default:
5037 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
5038 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00005039 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005040 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005041
Francois Romieu0e485152007-02-20 00:00:26 +01005042 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5043
Francois Romieub8363902008-06-01 12:31:57 +02005044 RTL_W8(Cfg9346, Cfg9346_Lock);
5045
Francois Romieu2dd99532007-06-11 23:22:52 +02005046 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005047}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048
Francois Romieu2857ffb2008-08-02 21:08:49 +02005049#define R810X_CPCMD_QUIRK_MASK (\
5050 EnableBist | \
5051 Mac_dbgo_oe | \
5052 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00005053 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02005054 Force_txflow_en | \
5055 Cxpl_dbg_sel | \
5056 ASF | \
5057 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005058 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005059
5060static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
5061{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005062 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02005063 { 0x01, 0, 0x6e65 },
5064 { 0x02, 0, 0x091f },
5065 { 0x03, 0, 0xc2f9 },
5066 { 0x06, 0, 0xafb5 },
5067 { 0x07, 0, 0x0e00 },
5068 { 0x19, 0, 0xec80 },
5069 { 0x01, 0, 0x2e65 },
5070 { 0x01, 0, 0x6e65 }
5071 };
5072 u8 cfg1;
5073
françois romieu650e8d52011-01-03 15:08:29 +00005074 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005075
5076 RTL_W8(DBG_REG, FIX_NAK_1);
5077
5078 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5079
5080 RTL_W8(Config1,
5081 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5082 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5083
5084 cfg1 = RTL_R8(Config1);
5085 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5086 RTL_W8(Config1, cfg1 & ~LEDS0);
5087
Francois Romieu2857ffb2008-08-02 21:08:49 +02005088 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
5089}
5090
5091static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
5092{
françois romieu650e8d52011-01-03 15:08:29 +00005093 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005094
5095 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5096
5097 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5098 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005099}
5100
5101static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
5102{
5103 rtl_hw_start_8102e_2(ioaddr, pdev);
5104
5105 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
5106}
5107
Hayes Wang5a5e4442011-02-22 17:26:21 +08005108static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
5109{
5110 static const struct ephy_info e_info_8105e_1[] = {
5111 { 0x07, 0, 0x4000 },
5112 { 0x19, 0, 0x0200 },
5113 { 0x19, 0, 0x0020 },
5114 { 0x1e, 0, 0x2000 },
5115 { 0x03, 0, 0x0001 },
5116 { 0x19, 0, 0x0100 },
5117 { 0x19, 0, 0x0004 },
5118 { 0x0a, 0, 0x0020 }
5119 };
5120
Francois Romieucecb5fd2011-04-01 10:21:07 +02005121 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005122 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5123
Francois Romieucecb5fd2011-04-01 10:21:07 +02005124 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005125 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5126
5127 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08005128 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005129
5130 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
5131}
5132
5133static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
5134{
5135 rtl_hw_start_8105e_1(ioaddr, pdev);
5136 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
5137}
5138
Francois Romieu07ce4062007-02-23 23:36:39 +01005139static void rtl_hw_start_8101(struct net_device *dev)
5140{
Francois Romieucdf1a602007-06-11 23:29:50 +02005141 struct rtl8169_private *tp = netdev_priv(dev);
5142 void __iomem *ioaddr = tp->mmio_addr;
5143 struct pci_dev *pdev = tp->pci_dev;
5144
Francois Romieuda78dbf2012-01-26 14:18:23 +01005145 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5146 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00005147
Francois Romieucecb5fd2011-04-01 10:21:07 +02005148 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5149 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00005150 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02005151
5152 if (cap) {
5153 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
5154 PCI_EXP_DEVCTL_NOSNOOP_EN);
5155 }
Francois Romieucdf1a602007-06-11 23:29:50 +02005156 }
5157
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005158 RTL_W8(Cfg9346, Cfg9346_Unlock);
5159
Francois Romieu2857ffb2008-08-02 21:08:49 +02005160 switch (tp->mac_version) {
5161 case RTL_GIGA_MAC_VER_07:
5162 rtl_hw_start_8102e_1(ioaddr, pdev);
5163 break;
5164
5165 case RTL_GIGA_MAC_VER_08:
5166 rtl_hw_start_8102e_3(ioaddr, pdev);
5167 break;
5168
5169 case RTL_GIGA_MAC_VER_09:
5170 rtl_hw_start_8102e_2(ioaddr, pdev);
5171 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005172
5173 case RTL_GIGA_MAC_VER_29:
5174 rtl_hw_start_8105e_1(ioaddr, pdev);
5175 break;
5176 case RTL_GIGA_MAC_VER_30:
5177 rtl_hw_start_8105e_2(ioaddr, pdev);
5178 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02005179 }
5180
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005181 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02005182
françois romieuf0298f82011-01-03 15:07:42 +00005183 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02005184
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005185 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02005186
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005187 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02005188 RTL_W16(CPlusCmd, tp->cp_cmd);
5189
5190 RTL_W16(IntrMitigate, 0x0000);
5191
5192 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5193
5194 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5195 rtl_set_rx_tx_config_registers(tp);
5196
Francois Romieucdf1a602007-06-11 23:29:50 +02005197 RTL_R8(IntrMask);
5198
Francois Romieucdf1a602007-06-11 23:29:50 +02005199 rtl_set_rx_mode(dev);
5200
5201 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202}
5203
5204static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5205{
Francois Romieud58d46b2011-05-03 16:38:29 +02005206 struct rtl8169_private *tp = netdev_priv(dev);
5207
5208 if (new_mtu < ETH_ZLEN ||
5209 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 return -EINVAL;
5211
Francois Romieud58d46b2011-05-03 16:38:29 +02005212 if (new_mtu > ETH_DATA_LEN)
5213 rtl_hw_jumbo_enable(tp);
5214 else
5215 rtl_hw_jumbo_disable(tp);
5216
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005218 netdev_update_features(dev);
5219
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005220 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221}
5222
5223static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5224{
Al Viro95e09182007-12-22 18:55:39 +00005225 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5227}
5228
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005229static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5230 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005232 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005233 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005234
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005235 kfree(*data_buff);
5236 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 rtl8169_make_unusable_by_asic(desc);
5238}
5239
5240static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5241{
5242 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5243
5244 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5245}
5246
5247static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5248 u32 rx_buf_sz)
5249{
5250 desc->addr = cpu_to_le64(mapping);
5251 wmb();
5252 rtl8169_mark_to_asic(desc, rx_buf_sz);
5253}
5254
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005255static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005257 return (void *)ALIGN((long)data, 16);
5258}
5259
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005260static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5261 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005262{
5263 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005265 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005266 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005267 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005269 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5270 if (!data)
5271 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005272
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005273 if (rtl8169_align(data) != data) {
5274 kfree(data);
5275 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5276 if (!data)
5277 return NULL;
5278 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005279
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005280 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005281 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005282 if (unlikely(dma_mapping_error(d, mapping))) {
5283 if (net_ratelimit())
5284 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005285 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287
5288 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005289 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005290
5291err_out:
5292 kfree(data);
5293 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294}
5295
5296static void rtl8169_rx_clear(struct rtl8169_private *tp)
5297{
Francois Romieu07d3f512007-02-21 22:40:46 +01005298 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299
5300 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005301 if (tp->Rx_databuff[i]) {
5302 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 tp->RxDescArray + i);
5304 }
5305 }
5306}
5307
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005308static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005310 desc->opts1 |= cpu_to_le32(RingEnd);
5311}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005312
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005313static int rtl8169_rx_fill(struct rtl8169_private *tp)
5314{
5315 unsigned int i;
5316
5317 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005318 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005319
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005320 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005322
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005323 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005324 if (!data) {
5325 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005326 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005327 }
5328 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005331 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5332 return 0;
5333
5334err_out:
5335 rtl8169_rx_clear(tp);
5336 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337}
5338
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339static int rtl8169_init_ring(struct net_device *dev)
5340{
5341 struct rtl8169_private *tp = netdev_priv(dev);
5342
5343 rtl8169_init_ring_indexes(tp);
5344
5345 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005346 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005348 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349}
5350
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005351static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 struct TxDesc *desc)
5353{
5354 unsigned int len = tx_skb->len;
5355
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005356 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5357
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 desc->opts1 = 0x00;
5359 desc->opts2 = 0x00;
5360 desc->addr = 0x00;
5361 tx_skb->len = 0;
5362}
5363
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005364static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5365 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366{
5367 unsigned int i;
5368
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005369 for (i = 0; i < n; i++) {
5370 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 struct ring_info *tx_skb = tp->tx_skb + entry;
5372 unsigned int len = tx_skb->len;
5373
5374 if (len) {
5375 struct sk_buff *skb = tx_skb->skb;
5376
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005377 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 tp->TxDescArray + entry);
5379 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005380 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 dev_kfree_skb(skb);
5382 tx_skb->skb = NULL;
5383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 }
5385 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005386}
5387
5388static void rtl8169_tx_clear(struct rtl8169_private *tp)
5389{
5390 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 tp->cur_tx = tp->dirty_tx = 0;
5392}
5393
Francois Romieu4422bcd2012-01-26 11:23:32 +01005394static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395{
David Howellsc4028952006-11-22 14:57:56 +00005396 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005397 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398
Francois Romieuda78dbf2012-01-26 14:18:23 +01005399 napi_disable(&tp->napi);
5400 netif_stop_queue(dev);
5401 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402
françois romieuc7c2c392011-12-04 20:30:52 +00005403 rtl8169_hw_reset(tp);
5404
Francois Romieu56de4142011-03-15 17:29:31 +01005405 for (i = 0; i < NUM_RX_DESC; i++)
5406 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5407
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005409 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410
Francois Romieuda78dbf2012-01-26 14:18:23 +01005411 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01005412 rtl_hw_start(dev);
5413 netif_wake_queue(dev);
5414 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415}
5416
5417static void rtl8169_tx_timeout(struct net_device *dev)
5418{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005419 struct rtl8169_private *tp = netdev_priv(dev);
5420
5421 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422}
5423
5424static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005425 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426{
5427 struct skb_shared_info *info = skb_shinfo(skb);
5428 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005429 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005430 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431
5432 entry = tp->cur_tx;
5433 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005434 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 dma_addr_t mapping;
5436 u32 status, len;
5437 void *addr;
5438
5439 entry = (entry + 1) % NUM_TX_DESC;
5440
5441 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005442 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005443 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005444 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005445 if (unlikely(dma_mapping_error(d, mapping))) {
5446 if (net_ratelimit())
5447 netif_err(tp, drv, tp->dev,
5448 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005449 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451
Francois Romieucecb5fd2011-04-01 10:21:07 +02005452 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005453 status = opts[0] | len |
5454 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455
5456 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005457 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 txd->addr = cpu_to_le64(mapping);
5459
5460 tp->tx_skb[entry].len = len;
5461 }
5462
5463 if (cur_frag) {
5464 tp->tx_skb[entry].skb = skb;
5465 txd->opts1 |= cpu_to_le32(LastFrag);
5466 }
5467
5468 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005469
5470err_out:
5471 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5472 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473}
5474
Francois Romieu2b7b4312011-04-18 22:53:24 -07005475static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5476 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005478 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005479 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005480 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481
Francois Romieu2b7b4312011-04-18 22:53:24 -07005482 if (mss) {
5483 opts[0] |= TD_LSO;
5484 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5485 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005486 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487
5488 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005489 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005491 opts[offset] |= info->checksum.udp;
5492 else
5493 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495}
5496
Stephen Hemminger613573252009-08-31 19:50:58 +00005497static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5498 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499{
5500 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005501 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 struct TxDesc *txd = tp->TxDescArray + entry;
5503 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005504 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 dma_addr_t mapping;
5506 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005507 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005508 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005509
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005511 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005512 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 }
5514
5515 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005516 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005518 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005519 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005520 if (unlikely(dma_mapping_error(d, mapping))) {
5521 if (net_ratelimit())
5522 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005523 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525
5526 tp->tx_skb[entry].len = len;
5527 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528
Francois Romieu2b7b4312011-04-18 22:53:24 -07005529 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5530 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005531
Francois Romieu2b7b4312011-04-18 22:53:24 -07005532 rtl8169_tso_csum(tp, skb, opts);
5533
5534 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005535 if (frags < 0)
5536 goto err_dma_1;
5537 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005538 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005539 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005540 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005541 tp->tx_skb[entry].skb = skb;
5542 }
5543
Francois Romieu2b7b4312011-04-18 22:53:24 -07005544 txd->opts2 = cpu_to_le32(opts[1]);
5545
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 wmb();
5547
Francois Romieucecb5fd2011-04-01 10:21:07 +02005548 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005549 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 txd->opts1 = cpu_to_le32(status);
5551
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 tp->cur_tx += frags + 1;
5553
David Dillow4c020a92010-03-03 16:33:10 +00005554 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555
Francois Romieucecb5fd2011-04-01 10:21:07 +02005556 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557
Francois Romieuda78dbf2012-01-26 14:18:23 +01005558 mmiowb();
5559
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01005561 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5562 * not miss a ring update when it notices a stopped queue.
5563 */
5564 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01005566 /* Sync with rtl_tx:
5567 * - publish queue status and cur_tx ring index (write barrier)
5568 * - refresh dirty_tx ring index (read barrier).
5569 * May the current thread have a pessimistic view of the ring
5570 * status and forget to wake up queue, a racing rtl_tx thread
5571 * can't.
5572 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005573 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
5575 netif_wake_queue(dev);
5576 }
5577
Stephen Hemminger613573252009-08-31 19:50:58 +00005578 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005580err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005581 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005582err_dma_0:
5583 dev_kfree_skb(skb);
5584 dev->stats.tx_dropped++;
5585 return NETDEV_TX_OK;
5586
5587err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005589 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005590 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591}
5592
5593static void rtl8169_pcierr_interrupt(struct net_device *dev)
5594{
5595 struct rtl8169_private *tp = netdev_priv(dev);
5596 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 u16 pci_status, pci_cmd;
5598
5599 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5600 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5601
Joe Perchesbf82c182010-02-09 11:49:50 +00005602 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5603 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604
5605 /*
5606 * The recovery sequence below admits a very elaborated explanation:
5607 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005608 * - I did not see what else could be done;
5609 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 *
5611 * Feel free to adjust to your needs.
5612 */
Francois Romieua27993f2006-12-18 00:04:19 +01005613 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005614 pci_cmd &= ~PCI_COMMAND_PARITY;
5615 else
5616 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5617
5618 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619
5620 pci_write_config_word(pdev, PCI_STATUS,
5621 pci_status & (PCI_STATUS_DETECTED_PARITY |
5622 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5623 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5624
5625 /* The infamous DAC f*ckup only happens at boot time */
5626 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005627 void __iomem *ioaddr = tp->mmio_addr;
5628
Joe Perchesbf82c182010-02-09 11:49:50 +00005629 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 tp->cp_cmd &= ~PCIDAC;
5631 RTL_W16(CPlusCmd, tp->cp_cmd);
5632 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 }
5634
françois romieue6de30d2011-01-03 15:08:37 +00005635 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005636
Francois Romieu98ddf982012-01-31 10:47:34 +01005637 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638}
5639
Francois Romieuda78dbf2012-01-26 14:18:23 +01005640static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641{
5642 unsigned int dirty_tx, tx_left;
5643
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 dirty_tx = tp->dirty_tx;
5645 smp_rmb();
5646 tx_left = tp->cur_tx - dirty_tx;
5647
5648 while (tx_left > 0) {
5649 unsigned int entry = dirty_tx % NUM_TX_DESC;
5650 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 u32 status;
5652
5653 rmb();
5654 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5655 if (status & DescOwn)
5656 break;
5657
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005658 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5659 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005661 dev->stats.tx_packets++;
5662 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00005663 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 tx_skb->skb = NULL;
5665 }
5666 dirty_tx++;
5667 tx_left--;
5668 }
5669
5670 if (tp->dirty_tx != dirty_tx) {
5671 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005672 /* Sync with rtl8169_start_xmit:
5673 * - publish dirty_tx ring index (write barrier)
5674 * - refresh cur_tx ring index and queue status (read barrier)
5675 * May the current thread miss the stopped queue condition,
5676 * a racing xmit thread can only have a right view of the
5677 * ring status.
5678 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005679 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 if (netif_queue_stopped(dev) &&
5681 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5682 netif_wake_queue(dev);
5683 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005684 /*
5685 * 8168 hack: TxPoll requests are lost when the Tx packets are
5686 * too close. Let's kick an extra TxPoll request when a burst
5687 * of start_xmit activity is detected (if it is not detected,
5688 * it is slow enough). -- FR
5689 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005690 if (tp->cur_tx != dirty_tx) {
5691 void __iomem *ioaddr = tp->mmio_addr;
5692
Francois Romieud78ae2d2007-08-26 20:08:19 +02005693 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 }
5696}
5697
Francois Romieu126fa4b2005-05-12 20:09:17 -04005698static inline int rtl8169_fragmented_frame(u32 status)
5699{
5700 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5701}
5702
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005703static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 u32 status = opts1 & RxProtoMask;
5706
5707 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005708 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 skb->ip_summed = CHECKSUM_UNNECESSARY;
5710 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005711 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712}
5713
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005714static struct sk_buff *rtl8169_try_rx_copy(void *data,
5715 struct rtl8169_private *tp,
5716 int pkt_size,
5717 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005719 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005720 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005722 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005723 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005724 prefetch(data);
5725 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5726 if (skb)
5727 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005728 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5729
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005730 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731}
5732
Francois Romieuda78dbf2012-01-26 14:18:23 +01005733static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734{
5735 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005736 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 cur_rx = tp->cur_rx;
5739 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005740 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005742 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005744 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 u32 status;
5746
5747 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005748 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749
5750 if (status & DescOwn)
5751 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005752 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005753 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5754 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005755 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005757 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005759 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005760 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005761 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005762 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005763 }
Ben Greear6bbe0212012-02-10 15:04:33 +00005764 if ((status & (RxRUNT | RxCRC)) &&
5765 !(status & (RxRWT | RxFOVF)) &&
5766 (dev->features & NETIF_F_RXALL))
5767 goto process_pkt;
5768
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005769 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005771 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00005772 dma_addr_t addr;
5773 int pkt_size;
5774
5775process_pkt:
5776 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00005777 if (likely(!(dev->features & NETIF_F_RXFCS)))
5778 pkt_size = (status & 0x00003fff) - 4;
5779 else
5780 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781
Francois Romieu126fa4b2005-05-12 20:09:17 -04005782 /*
5783 * The driver does not support incoming fragmented
5784 * frames. They are seen as a symptom of over-mtu
5785 * sized frames.
5786 */
5787 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005788 dev->stats.rx_dropped++;
5789 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005790 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005791 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005792 }
5793
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005794 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5795 tp, pkt_size, addr);
5796 rtl8169_mark_to_asic(desc, rx_buf_sz);
5797 if (!skb) {
5798 dev->stats.rx_dropped++;
5799 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 }
5801
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005802 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 skb_put(skb, pkt_size);
5804 skb->protocol = eth_type_trans(skb, dev);
5805
Francois Romieu7a8fc772011-03-01 17:18:33 +01005806 rtl8169_rx_vlan_tag(desc, skb);
5807
Francois Romieu56de4142011-03-15 17:29:31 +01005808 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809
Francois Romieucebf8cc2007-10-18 12:06:54 +02005810 dev->stats.rx_bytes += pkt_size;
5811 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005813
5814 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005815 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005816 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5817 desc->opts2 = 0;
5818 cur_rx++;
5819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 }
5821
5822 count = cur_rx - tp->cur_rx;
5823 tp->cur_rx = cur_rx;
5824
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005825 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826
5827 return count;
5828}
5829
Francois Romieu07d3f512007-02-21 22:40:46 +01005830static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831{
Francois Romieu07d3f512007-02-21 22:40:46 +01005832 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005835 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005837 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005838 if (status && status != 0xffff) {
5839 status &= RTL_EVENT_NAPI | tp->event_slow;
5840 if (status) {
5841 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00005842
Francois Romieuda78dbf2012-01-26 14:18:23 +01005843 rtl_irq_disable(tp);
5844 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 return IRQ_RETVAL(handled);
5848}
5849
Francois Romieuda78dbf2012-01-26 14:18:23 +01005850/*
5851 * Workqueue context.
5852 */
5853static void rtl_slow_event_work(struct rtl8169_private *tp)
5854{
5855 struct net_device *dev = tp->dev;
5856 u16 status;
5857
5858 status = rtl_get_events(tp) & tp->event_slow;
5859 rtl_ack_events(tp, status);
5860
5861 if (unlikely(status & RxFIFOOver)) {
5862 switch (tp->mac_version) {
5863 /* Work around for rx fifo overflow */
5864 case RTL_GIGA_MAC_VER_11:
5865 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01005866 /* XXX - Hack alert. See rtl_task(). */
5867 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005868 default:
5869 break;
5870 }
5871 }
5872
5873 if (unlikely(status & SYSErr))
5874 rtl8169_pcierr_interrupt(dev);
5875
5876 if (status & LinkChg)
5877 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
5878
5879 napi_disable(&tp->napi);
5880 rtl_irq_disable(tp);
5881
5882 napi_enable(&tp->napi);
5883 napi_schedule(&tp->napi);
5884}
5885
Francois Romieu4422bcd2012-01-26 11:23:32 +01005886static void rtl_task(struct work_struct *work)
5887{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005888 static const struct {
5889 int bitnr;
5890 void (*action)(struct rtl8169_private *);
5891 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01005892 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005893 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
5894 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
5895 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
5896 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01005897 struct rtl8169_private *tp =
5898 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005899 struct net_device *dev = tp->dev;
5900 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01005901
Francois Romieuda78dbf2012-01-26 14:18:23 +01005902 rtl_lock_work(tp);
5903
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005904 if (!netif_running(dev) ||
5905 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01005906 goto out_unlock;
5907
5908 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
5909 bool pending;
5910
Francois Romieuda78dbf2012-01-26 14:18:23 +01005911 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005912 if (pending)
5913 rtl_work[i].action(tp);
5914 }
5915
5916out_unlock:
5917 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01005918}
5919
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005920static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005922 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5923 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005924 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
5925 int work_done= 0;
5926 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927
Francois Romieuda78dbf2012-01-26 14:18:23 +01005928 status = rtl_get_events(tp);
5929 rtl_ack_events(tp, status & ~tp->event_slow);
5930
5931 if (status & RTL_EVENT_NAPI_RX)
5932 work_done = rtl_rx(dev, tp, (u32) budget);
5933
5934 if (status & RTL_EVENT_NAPI_TX)
5935 rtl_tx(dev, tp);
5936
5937 if (status & tp->event_slow) {
5938 enable_mask &= ~tp->event_slow;
5939
5940 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
5941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005943 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005944 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005945
Francois Romieuda78dbf2012-01-26 14:18:23 +01005946 rtl_irq_enable(tp, enable_mask);
5947 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 }
5949
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005950 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952
Francois Romieu523a6092008-09-10 22:28:56 +02005953static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5954{
5955 struct rtl8169_private *tp = netdev_priv(dev);
5956
5957 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5958 return;
5959
5960 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5961 RTL_W32(RxMissed, 0);
5962}
5963
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964static void rtl8169_down(struct net_device *dev)
5965{
5966 struct rtl8169_private *tp = netdev_priv(dev);
5967 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968
Francois Romieu4876cc12011-03-11 21:07:11 +01005969 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005971 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005972 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973
Hayes Wang92fc43b2011-07-06 15:58:03 +08005974 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005975 /*
5976 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01005977 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
5978 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005979 */
Francois Romieu523a6092008-09-10 22:28:56 +02005980 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005983 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985 rtl8169_tx_clear(tp);
5986
5987 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005988
5989 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990}
5991
5992static int rtl8169_close(struct net_device *dev)
5993{
5994 struct rtl8169_private *tp = netdev_priv(dev);
5995 struct pci_dev *pdev = tp->pci_dev;
5996
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005997 pm_runtime_get_sync(&pdev->dev);
5998
Francois Romieucecb5fd2011-04-01 10:21:07 +02005999 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08006000 rtl8169_update_counters(dev);
6001
Francois Romieuda78dbf2012-01-26 14:18:23 +01006002 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006003 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006004
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006006 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007
6008 free_irq(dev->irq, dev);
6009
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00006010 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6011 tp->RxPhyAddr);
6012 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6013 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 tp->TxDescArray = NULL;
6015 tp->RxDescArray = NULL;
6016
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006017 pm_runtime_put_sync(&pdev->dev);
6018
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 return 0;
6020}
6021
Francois Romieu07ce4062007-02-23 23:36:39 +01006022static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023{
6024 struct rtl8169_private *tp = netdev_priv(dev);
6025 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01006027 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 u32 tmp = 0;
6029
6030 if (dev->flags & IFF_PROMISC) {
6031 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00006032 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 rx_mode =
6034 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
6035 AcceptAllPhys;
6036 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00006037 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00006038 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 /* Too many to filter perfectly -- accept all multicasts. */
6040 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
6041 mc_filter[1] = mc_filter[0] = 0xffffffff;
6042 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +00006043 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01006044
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 rx_mode = AcceptBroadcast | AcceptMyPhys;
6046 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00006047 netdev_for_each_mc_addr(ha, dev) {
6048 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
6050 rx_mode |= AcceptMulticast;
6051 }
6052 }
6053
Ben Greear6bbe0212012-02-10 15:04:33 +00006054 if (dev->features & NETIF_F_RXALL)
6055 rx_mode |= (AcceptErr | AcceptRunt);
6056
Francois Romieu1687b562011-07-19 17:21:29 +02006057 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058
Francois Romieuf887cce2008-07-17 22:24:18 +02006059 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01006060 u32 data = mc_filter[0];
6061
6062 mc_filter[0] = swab32(mc_filter[1]);
6063 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02006064 }
6065
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07006067 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068
Francois Romieu57a9f232007-06-04 22:10:15 +02006069 RTL_W32(RxConfig, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070}
6071
6072/**
6073 * rtl8169_get_stats - Get rtl8169 read/write statistics
6074 * @dev: The Ethernet Device to get statistics for
6075 *
6076 * Get TX/RX statistics for rtl8169
6077 */
6078static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
6079{
6080 struct rtl8169_private *tp = netdev_priv(dev);
6081 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082
Francois Romieuda78dbf2012-01-26 14:18:23 +01006083 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02006084 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02006085
Francois Romieucebf8cc2007-10-18 12:06:54 +02006086 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087}
6088
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006089static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006090{
françois romieu065c27c2011-01-03 15:08:12 +00006091 struct rtl8169_private *tp = netdev_priv(dev);
6092
Francois Romieu5d06a992006-02-23 00:47:58 +01006093 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006094 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006095
6096 netif_device_detach(dev);
6097 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006098
6099 rtl_lock_work(tp);
6100 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006101 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006102 rtl_unlock_work(tp);
6103
6104 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006105}
Francois Romieu5d06a992006-02-23 00:47:58 +01006106
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006107#ifdef CONFIG_PM
6108
6109static int rtl8169_suspend(struct device *device)
6110{
6111 struct pci_dev *pdev = to_pci_dev(device);
6112 struct net_device *dev = pci_get_drvdata(pdev);
6113
6114 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02006115
Francois Romieu5d06a992006-02-23 00:47:58 +01006116 return 0;
6117}
6118
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006119static void __rtl8169_resume(struct net_device *dev)
6120{
françois romieu065c27c2011-01-03 15:08:12 +00006121 struct rtl8169_private *tp = netdev_priv(dev);
6122
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006123 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006124
6125 rtl_pll_power_up(tp);
6126
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006127 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006128
Francois Romieu98ddf982012-01-31 10:47:34 +01006129 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006130}
6131
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006132static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006133{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006134 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01006135 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006136 struct rtl8169_private *tp = netdev_priv(dev);
6137
6138 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01006139
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006140 if (netif_running(dev))
6141 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006142
Francois Romieu5d06a992006-02-23 00:47:58 +01006143 return 0;
6144}
6145
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006146static int rtl8169_runtime_suspend(struct device *device)
6147{
6148 struct pci_dev *pdev = to_pci_dev(device);
6149 struct net_device *dev = pci_get_drvdata(pdev);
6150 struct rtl8169_private *tp = netdev_priv(dev);
6151
6152 if (!tp->TxDescArray)
6153 return 0;
6154
Francois Romieuda78dbf2012-01-26 14:18:23 +01006155 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006156 tp->saved_wolopts = __rtl8169_get_wol(tp);
6157 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006158 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006159
6160 rtl8169_net_suspend(dev);
6161
6162 return 0;
6163}
6164
6165static int rtl8169_runtime_resume(struct device *device)
6166{
6167 struct pci_dev *pdev = to_pci_dev(device);
6168 struct net_device *dev = pci_get_drvdata(pdev);
6169 struct rtl8169_private *tp = netdev_priv(dev);
6170
6171 if (!tp->TxDescArray)
6172 return 0;
6173
Francois Romieuda78dbf2012-01-26 14:18:23 +01006174 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006175 __rtl8169_set_wol(tp, tp->saved_wolopts);
6176 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006177 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006178
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006179 rtl8169_init_phy(dev, tp);
6180
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006181 __rtl8169_resume(dev);
6182
6183 return 0;
6184}
6185
6186static int rtl8169_runtime_idle(struct device *device)
6187{
6188 struct pci_dev *pdev = to_pci_dev(device);
6189 struct net_device *dev = pci_get_drvdata(pdev);
6190 struct rtl8169_private *tp = netdev_priv(dev);
6191
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00006192 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006193}
6194
Alexey Dobriyan47145212009-12-14 18:00:08 -08006195static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006196 .suspend = rtl8169_suspend,
6197 .resume = rtl8169_resume,
6198 .freeze = rtl8169_suspend,
6199 .thaw = rtl8169_resume,
6200 .poweroff = rtl8169_suspend,
6201 .restore = rtl8169_resume,
6202 .runtime_suspend = rtl8169_runtime_suspend,
6203 .runtime_resume = rtl8169_runtime_resume,
6204 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006205};
6206
6207#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6208
6209#else /* !CONFIG_PM */
6210
6211#define RTL8169_PM_OPS NULL
6212
6213#endif /* !CONFIG_PM */
6214
David S. Miller1805b2f2011-10-24 18:18:09 -04006215static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6216{
6217 void __iomem *ioaddr = tp->mmio_addr;
6218
6219 /* WoL fails with 8168b when the receiver is disabled. */
6220 switch (tp->mac_version) {
6221 case RTL_GIGA_MAC_VER_11:
6222 case RTL_GIGA_MAC_VER_12:
6223 case RTL_GIGA_MAC_VER_17:
6224 pci_clear_master(tp->pci_dev);
6225
6226 RTL_W8(ChipCmd, CmdRxEnb);
6227 /* PCI commit */
6228 RTL_R8(ChipCmd);
6229 break;
6230 default:
6231 break;
6232 }
6233}
6234
Francois Romieu1765f952008-09-13 17:21:40 +02006235static void rtl_shutdown(struct pci_dev *pdev)
6236{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006237 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006238 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02006239
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006240 rtl8169_net_suspend(dev);
6241
Francois Romieucecb5fd2011-04-01 10:21:07 +02006242 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006243 rtl_rar_set(tp, dev->perm_addr);
6244
Hayes Wang92fc43b2011-07-06 15:58:03 +08006245 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006246
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006247 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006248 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6249 rtl_wol_suspend_quirk(tp);
6250 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006251 }
6252
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006253 pci_wake_from_d3(pdev, true);
6254 pci_set_power_state(pdev, PCI_D3hot);
6255 }
6256}
Francois Romieu5d06a992006-02-23 00:47:58 +01006257
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258static struct pci_driver rtl8169_pci_driver = {
6259 .name = MODULENAME,
6260 .id_table = rtl8169_pci_tbl,
6261 .probe = rtl8169_init_one,
6262 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02006263 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006264 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265};
6266
Francois Romieu07d3f512007-02-21 22:40:46 +01006267static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268{
Jeff Garzik29917622006-08-19 17:48:59 -04006269 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270}
6271
Francois Romieu07d3f512007-02-21 22:40:46 +01006272static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273{
6274 pci_unregister_driver(&rtl8169_pci_driver);
6275}
6276
6277module_init(rtl8169_init_module);
6278module_exit(rtl8169_cleanup_module);