blob: 207aadcd37f2656d718baf8703431df0fa96f789 [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
32#include <asm/io.h>
33#include <asm/irq.h>
34
Francois Romieu865c6522008-05-11 14:51:00 +020035#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define MODULENAME "r8169"
37#define PFX MODULENAME ": "
38
françois romieubca03d52011-01-03 15:07:31 +000039#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
40#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000041#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
42#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080043#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080044#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
45#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080046#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#ifdef RTL8169_DEBUG
49#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020050 if (!(expr)) { \
51 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070052 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020053 }
Joe Perches06fa7352007-10-18 21:15:00 +020054#define dprintk(fmt, args...) \
55 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#else
57#define assert(expr) do {} while (0)
58#define dprintk(fmt, args...) do {} while (0)
59#endif /* RTL8169_DEBUG */
60
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020061#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070062 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#define TX_BUFFS_AVAIL(tp) \
65 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
68 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050069static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Francois Romieu9c14cea2008-07-05 00:21:15 +020071#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
74#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
75
76#define R8169_REGS_SIZE 256
77#define R8169_NAPI_WEIGHT 64
78#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
79#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
80#define RX_BUF_SIZE 1536 /* Rx Buffer size */
81#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
82#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
83
84#define RTL8169_TX_TIMEOUT (6*HZ)
85#define RTL8169_PHY_TIMEOUT (10*HZ)
86
françois romieuea8dbdd2009-03-15 01:10:50 +000087#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
88#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020089#define RTL_EEPROM_SIG_ADDR 0x0000
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/* write/read MMIO register */
92#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
93#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
94#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
95#define RTL_R8(reg) readb (ioaddr + (reg))
96#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +000097#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200100 RTL_GIGA_MAC_VER_01 = 0,
101 RTL_GIGA_MAC_VER_02,
102 RTL_GIGA_MAC_VER_03,
103 RTL_GIGA_MAC_VER_04,
104 RTL_GIGA_MAC_VER_05,
105 RTL_GIGA_MAC_VER_06,
106 RTL_GIGA_MAC_VER_07,
107 RTL_GIGA_MAC_VER_08,
108 RTL_GIGA_MAC_VER_09,
109 RTL_GIGA_MAC_VER_10,
110 RTL_GIGA_MAC_VER_11,
111 RTL_GIGA_MAC_VER_12,
112 RTL_GIGA_MAC_VER_13,
113 RTL_GIGA_MAC_VER_14,
114 RTL_GIGA_MAC_VER_15,
115 RTL_GIGA_MAC_VER_16,
116 RTL_GIGA_MAC_VER_17,
117 RTL_GIGA_MAC_VER_18,
118 RTL_GIGA_MAC_VER_19,
119 RTL_GIGA_MAC_VER_20,
120 RTL_GIGA_MAC_VER_21,
121 RTL_GIGA_MAC_VER_22,
122 RTL_GIGA_MAC_VER_23,
123 RTL_GIGA_MAC_VER_24,
124 RTL_GIGA_MAC_VER_25,
125 RTL_GIGA_MAC_VER_26,
126 RTL_GIGA_MAC_VER_27,
127 RTL_GIGA_MAC_VER_28,
128 RTL_GIGA_MAC_VER_29,
129 RTL_GIGA_MAC_VER_30,
130 RTL_GIGA_MAC_VER_31,
131 RTL_GIGA_MAC_VER_32,
132 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800133 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800134 RTL_GIGA_MAC_VER_35,
135 RTL_GIGA_MAC_VER_36,
Francois Romieu85bffe62011-04-27 08:22:39 +0200136 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
Francois Romieu2b7b4312011-04-18 22:53:24 -0700139enum rtl_tx_desc_version {
140 RTL_TD_0 = 0,
141 RTL_TD_1 = 1,
142};
143
Francois Romieud58d46b2011-05-03 16:38:29 +0200144#define JUMBO_1K ETH_DATA_LEN
145#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
146#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
147#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
148#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
149
150#define _R(NAME,TD,FW,SZ,B) { \
151 .name = NAME, \
152 .txd_version = TD, \
153 .fw_name = FW, \
154 .jumbo_max = SZ, \
155 .jumbo_tx_csum = B \
156}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800158static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700160 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200161 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200162 u16 jumbo_max;
163 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200164} rtl_chip_infos[] = {
165 /* PCI devices. */
166 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200167 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200168 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200169 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200170 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200171 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200172 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200173 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200174 [RTL_GIGA_MAC_VER_05] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200175 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200176 [RTL_GIGA_MAC_VER_06] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200177 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200178 /* PCI-E devices. */
179 [RTL_GIGA_MAC_VER_07] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200180 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200181 [RTL_GIGA_MAC_VER_08] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200182 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200183 [RTL_GIGA_MAC_VER_09] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200184 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200185 [RTL_GIGA_MAC_VER_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200186 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200187 [RTL_GIGA_MAC_VER_11] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200188 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200189 [RTL_GIGA_MAC_VER_12] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200190 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200191 [RTL_GIGA_MAC_VER_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200192 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200193 [RTL_GIGA_MAC_VER_14] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200194 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200195 [RTL_GIGA_MAC_VER_15] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200196 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200197 [RTL_GIGA_MAC_VER_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200198 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200199 [RTL_GIGA_MAC_VER_17] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200200 _R("RTL8168b/8111b", RTL_TD_1, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200201 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200202 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200203 [RTL_GIGA_MAC_VER_19] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200204 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200205 [RTL_GIGA_MAC_VER_20] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200206 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200207 [RTL_GIGA_MAC_VER_21] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200208 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200209 [RTL_GIGA_MAC_VER_22] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200210 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200211 [RTL_GIGA_MAC_VER_23] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200212 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200213 [RTL_GIGA_MAC_VER_24] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200214 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200215 [RTL_GIGA_MAC_VER_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200216 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
217 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200218 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200219 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
220 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200221 [RTL_GIGA_MAC_VER_27] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200222 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200223 [RTL_GIGA_MAC_VER_28] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200224 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200225 [RTL_GIGA_MAC_VER_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200226 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
227 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200228 [RTL_GIGA_MAC_VER_30] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200229 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
230 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200231 [RTL_GIGA_MAC_VER_31] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200232 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200233 [RTL_GIGA_MAC_VER_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200234 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
235 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200236 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200237 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
238 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800239 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200240 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
241 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800242 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200243 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
244 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800245 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200246 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
247 JUMBO_9K, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248};
249#undef _R
250
Francois Romieubcf0bf92006-07-26 23:14:13 +0200251enum cfg_version {
252 RTL_CFG_0 = 0x00,
253 RTL_CFG_1,
254 RTL_CFG_2
255};
256
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000257static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200258 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200259 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200260 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100261 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200262 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
263 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000264 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200265 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200266 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
267 { PCI_VENDOR_ID_LINKSYS, 0x1032,
268 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100269 { 0x0001, 0x8168,
270 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 {0,},
272};
273
274MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
275
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000276static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700277static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200278static struct {
279 u32 msg_enable;
280} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Francois Romieu07d3f512007-02-21 22:40:46 +0100282enum rtl_registers {
283 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100284 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100285 MAR0 = 8, /* Multicast filter. */
286 CounterAddrLow = 0x10,
287 CounterAddrHigh = 0x14,
288 TxDescStartAddrLow = 0x20,
289 TxDescStartAddrHigh = 0x24,
290 TxHDescStartAddrLow = 0x28,
291 TxHDescStartAddrHigh = 0x2c,
292 FLASH = 0x30,
293 ERSR = 0x36,
294 ChipCmd = 0x37,
295 TxPoll = 0x38,
296 IntrMask = 0x3c,
297 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700298
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800299 TxConfig = 0x40,
300#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
301#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
302
303 RxConfig = 0x44,
304#define RX128_INT_EN (1 << 15) /* 8111c and later */
305#define RX_MULTI_EN (1 << 14) /* 8111c only */
306#define RXCFG_FIFO_SHIFT 13
307 /* No threshold before first PCI xfer */
308#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
309#define RXCFG_DMA_SHIFT 8
310 /* Unlimited maximum PCI burst. */
311#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700312
Francois Romieu07d3f512007-02-21 22:40:46 +0100313 RxMissed = 0x4c,
314 Cfg9346 = 0x50,
315 Config0 = 0x51,
316 Config1 = 0x52,
317 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200318#define PME_SIGNAL (1 << 5) /* 8168c and later */
319
Francois Romieu07d3f512007-02-21 22:40:46 +0100320 Config3 = 0x54,
321 Config4 = 0x55,
322 Config5 = 0x56,
323 MultiIntr = 0x5c,
324 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100325 PHYstatus = 0x6c,
326 RxMaxSize = 0xda,
327 CPlusCmd = 0xe0,
328 IntrMitigate = 0xe2,
329 RxDescAddrLow = 0xe4,
330 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000331 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
332
333#define NoEarlyTx 0x3f /* Max value : no early transmit. */
334
335 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
336
337#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800338#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000339
Francois Romieu07d3f512007-02-21 22:40:46 +0100340 FuncEvent = 0xf0,
341 FuncEventMask = 0xf4,
342 FuncPresetState = 0xf8,
343 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344};
345
Francois Romieuf162a5d2008-06-01 22:37:49 +0200346enum rtl8110_registers {
347 TBICSR = 0x64,
348 TBI_ANAR = 0x68,
349 TBI_LPAR = 0x6a,
350};
351
352enum rtl8168_8101_registers {
353 CSIDR = 0x64,
354 CSIAR = 0x68,
355#define CSIAR_FLAG 0x80000000
356#define CSIAR_WRITE_CMD 0x80000000
357#define CSIAR_BYTE_ENABLE 0x0f
358#define CSIAR_BYTE_ENABLE_SHIFT 12
359#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000360 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200361 EPHYAR = 0x80,
362#define EPHYAR_FLAG 0x80000000
363#define EPHYAR_WRITE_CMD 0x80000000
364#define EPHYAR_REG_MASK 0x1f
365#define EPHYAR_REG_SHIFT 16
366#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800367 DLLPR = 0xd0,
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800368#define PFM_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200369 DBG_REG = 0xd1,
370#define FIX_NAK_1 (1 << 4)
371#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800372 TWSI = 0xd2,
373 MCU = 0xd3,
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800374#define NOW_IS_OOB (1 << 7)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800375#define EN_NDP (1 << 3)
376#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000377 EFUSEAR = 0xdc,
378#define EFUSEAR_FLAG 0x80000000
379#define EFUSEAR_WRITE_CMD 0x80000000
380#define EFUSEAR_READ_CMD 0x00000000
381#define EFUSEAR_REG_MASK 0x03ff
382#define EFUSEAR_REG_SHIFT 8
383#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200384};
385
françois romieuc0e45c12011-01-03 15:08:04 +0000386enum rtl8168_registers {
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800387 LED_FREQ = 0x1a,
388 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000389 ERIDR = 0x70,
390 ERIAR = 0x74,
391#define ERIAR_FLAG 0x80000000
392#define ERIAR_WRITE_CMD 0x80000000
393#define ERIAR_READ_CMD 0x00000000
394#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000395#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800396#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
397#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
398#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
399#define ERIAR_MASK_SHIFT 12
400#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
401#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
402#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000403 EPHY_RXER_NUM = 0x7c,
404 OCPDR = 0xb0, /* OCP GPHY access */
405#define OCPDR_WRITE_CMD 0x80000000
406#define OCPDR_READ_CMD 0x00000000
407#define OCPDR_REG_MASK 0x7f
408#define OCPDR_GPHY_REG_SHIFT 16
409#define OCPDR_DATA_MASK 0xffff
410 OCPAR = 0xb4,
411#define OCPAR_FLAG 0x80000000
412#define OCPAR_GPHY_WRITE_CMD 0x8000f060
413#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000414 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
415 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200416#define TXPLA_RST (1 << 29)
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800417#define PWM_EN (1 << 22)
françois romieuc0e45c12011-01-03 15:08:04 +0000418};
419
Francois Romieu07d3f512007-02-21 22:40:46 +0100420enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100422 SYSErr = 0x8000,
423 PCSTimeout = 0x4000,
424 SWInt = 0x0100,
425 TxDescUnavail = 0x0080,
426 RxFIFOOver = 0x0040,
427 LinkChg = 0x0020,
428 RxOverflow = 0x0010,
429 TxErr = 0x0008,
430 TxOK = 0x0004,
431 RxErr = 0x0002,
432 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400435 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200436 RxFOVF = (1 << 23),
437 RxRWT = (1 << 22),
438 RxRES = (1 << 21),
439 RxRUNT = (1 << 20),
440 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 /* ChipCmdBits */
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800443 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100444 CmdReset = 0x10,
445 CmdRxEnb = 0x08,
446 CmdTxEnb = 0x04,
447 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Francois Romieu275391a2007-02-23 23:50:28 +0100449 /* TXPoll register p.5 */
450 HPQ = 0x80, /* Poll cmd on the high prio queue */
451 NPQ = 0x40, /* Poll cmd on the low prio queue */
452 FSWInt = 0x01, /* Forced software interrupt */
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100455 Cfg9346_Lock = 0x00,
456 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100459 AcceptErr = 0x20,
460 AcceptRunt = 0x10,
461 AcceptBroadcast = 0x08,
462 AcceptMulticast = 0x04,
463 AcceptMyPhys = 0x02,
464 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200465#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /* TxConfigBits */
468 TxInterFrameGapShift = 24,
469 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
470
Francois Romieu5d06a992006-02-23 00:47:58 +0100471 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200472 LEDS1 = (1 << 7),
473 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200474 Speed_down = (1 << 4),
475 MEMMAP = (1 << 3),
476 IOMAP = (1 << 2),
477 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100478 PMEnable = (1 << 0), /* Power Management Enable */
479
Francois Romieu6dccd162007-02-13 23:38:05 +0100480 /* Config2 register p. 25 */
françois romieu2ca6cf02011-12-15 08:37:43 +0000481 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100482 PCI_Clock_66MHz = 0x01,
483 PCI_Clock_33MHz = 0x00,
484
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100485 /* Config3 register p.25 */
486 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
487 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200488 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200489 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100490
Francois Romieud58d46b2011-05-03 16:38:29 +0200491 /* Config4 register */
492 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
493
Francois Romieu5d06a992006-02-23 00:47:58 +0100494 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100495 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
496 MWF = (1 << 5), /* Accept Multicast wakeup frame */
497 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200498 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100499 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100500 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /* TBICSR p.28 */
503 TBIReset = 0x80000000,
504 TBILoopback = 0x40000000,
505 TBINwEnable = 0x20000000,
506 TBINwRestart = 0x10000000,
507 TBILinkOk = 0x02000000,
508 TBINwComplete = 0x01000000,
509
510 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200511 EnableBist = (1 << 15), // 8168 8101
512 Mac_dbgo_oe = (1 << 14), // 8168 8101
513 Normal_mode = (1 << 13), // unused
514 Force_half_dup = (1 << 12), // 8168 8101
515 Force_rxflow_en = (1 << 11), // 8168 8101
516 Force_txflow_en = (1 << 10), // 8168 8101
517 Cxpl_dbg_sel = (1 << 9), // 8168 8101
518 ASF = (1 << 8), // 8168 8101
519 PktCntrDisable = (1 << 7), // 8168 8101
520 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 RxVlan = (1 << 6),
522 RxChkSum = (1 << 5),
523 PCIDAC = (1 << 4),
524 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100525 INTT_0 = 0x0000, // 8168
526 INTT_1 = 0x0001, // 8168
527 INTT_2 = 0x0002, // 8168
528 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100531 TBI_Enable = 0x80,
532 TxFlowCtrl = 0x40,
533 RxFlowCtrl = 0x20,
534 _1000bpsF = 0x10,
535 _100bps = 0x08,
536 _10bps = 0x04,
537 LinkStatus = 0x02,
538 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100541 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200542
543 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100544 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545};
546
Francois Romieu2b7b4312011-04-18 22:53:24 -0700547enum rtl_desc_bit {
548 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
550 RingEnd = (1 << 30), /* End of descriptor ring */
551 FirstFrag = (1 << 29), /* First segment of a packet */
552 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700553};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Francois Romieu2b7b4312011-04-18 22:53:24 -0700555/* Generic case. */
556enum rtl_tx_desc_bit {
557 /* First doubleword. */
558 TD_LSO = (1 << 27), /* Large Send Offload */
559#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Francois Romieu2b7b4312011-04-18 22:53:24 -0700561 /* Second doubleword. */
562 TxVlanTag = (1 << 17), /* Add VLAN tag */
563};
564
565/* 8169, 8168b and 810x except 8102e. */
566enum rtl_tx_desc_bit_0 {
567 /* First doubleword. */
568#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
569 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
570 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
571 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
572};
573
574/* 8102e, 8168c and beyond. */
575enum rtl_tx_desc_bit_1 {
576 /* Second doubleword. */
577#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
578 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
579 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
580 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
581};
582
583static const struct rtl_tx_desc_info {
584 struct {
585 u32 udp;
586 u32 tcp;
587 } checksum;
588 u16 mss_shift;
589 u16 opts_offset;
590} tx_desc_info [] = {
591 [RTL_TD_0] = {
592 .checksum = {
593 .udp = TD0_IP_CS | TD0_UDP_CS,
594 .tcp = TD0_IP_CS | TD0_TCP_CS
595 },
596 .mss_shift = TD0_MSS_SHIFT,
597 .opts_offset = 0
598 },
599 [RTL_TD_1] = {
600 .checksum = {
601 .udp = TD1_IP_CS | TD1_UDP_CS,
602 .tcp = TD1_IP_CS | TD1_TCP_CS
603 },
604 .mss_shift = TD1_MSS_SHIFT,
605 .opts_offset = 1
606 }
607};
608
609enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /* Rx private */
611 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
612 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
613
614#define RxProtoUDP (PID1)
615#define RxProtoTCP (PID0)
616#define RxProtoIP (PID1 | PID0)
617#define RxProtoMask RxProtoIP
618
619 IPFail = (1 << 16), /* IP checksum failed */
620 UDPFail = (1 << 15), /* UDP/IP checksum failed */
621 TCPFail = (1 << 14), /* TCP/IP checksum failed */
622 RxVlanTag = (1 << 16), /* VLAN tag available */
623};
624
625#define RsvdMask 0x3fffc000
626
627struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200628 __le32 opts1;
629 __le32 opts2;
630 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631};
632
633struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200634 __le32 opts1;
635 __le32 opts2;
636 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637};
638
639struct ring_info {
640 struct sk_buff *skb;
641 u32 len;
642 u8 __pad[sizeof(void *) - sizeof(u32)];
643};
644
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200645enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200646 RTL_FEATURE_WOL = (1 << 0),
647 RTL_FEATURE_MSI = (1 << 1),
648 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200649};
650
Ivan Vecera355423d2009-02-06 21:49:57 -0800651struct rtl8169_counters {
652 __le64 tx_packets;
653 __le64 rx_packets;
654 __le64 tx_errors;
655 __le32 rx_errors;
656 __le16 rx_missed;
657 __le16 align_errors;
658 __le32 tx_one_collision;
659 __le32 tx_multi_collision;
660 __le64 rx_unicast;
661 __le64 rx_broadcast;
662 __le32 rx_multicast;
663 __le16 tx_aborted;
664 __le16 tx_underun;
665};
666
Francois Romieuda78dbf2012-01-26 14:18:23 +0100667enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100668 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100669 RTL_FLAG_TASK_SLOW_PENDING,
670 RTL_FLAG_TASK_RESET_PENDING,
671 RTL_FLAG_TASK_PHY_PENDING,
672 RTL_FLAG_MAX
673};
674
Junchang Wang8027aa22012-03-04 23:30:32 +0100675struct rtl8169_stats {
676 u64 packets;
677 u64 bytes;
678 struct u64_stats_sync syncp;
679};
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681struct rtl8169_private {
682 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200683 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000684 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700685 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200686 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700687 u16 txd_version;
688 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
690 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
691 u32 dirty_rx;
692 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100693 struct rtl8169_stats rx_stats;
694 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
696 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
697 dma_addr_t TxPhyAddr;
698 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000699 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 struct timer_list timer;
702 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100703
704 u16 event_slow;
françois romieuc0e45c12011-01-03 15:08:04 +0000705
706 struct mdio_ops {
707 void (*write)(void __iomem *, int, int);
708 int (*read)(void __iomem *, int);
709 } mdio_ops;
710
françois romieu065c27c2011-01-03 15:08:12 +0000711 struct pll_power_ops {
712 void (*down)(struct rtl8169_private *);
713 void (*up)(struct rtl8169_private *);
714 } pll_power_ops;
715
Francois Romieud58d46b2011-05-03 16:38:29 +0200716 struct jumbo_ops {
717 void (*enable)(struct rtl8169_private *);
718 void (*disable)(struct rtl8169_private *);
719 } jumbo_ops;
720
Oliver Neukum54405cd2011-01-06 21:55:13 +0100721 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200722 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000723 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100724 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000725 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800727 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100728
729 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100730 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
731 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100732 struct work_struct work;
733 } wk;
734
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200735 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200736
737 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800738 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000739 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400740 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000741
Francois Romieub6ffd972011-06-17 17:00:05 +0200742 struct rtl_fw {
743 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200744
745#define RTL_VER_SIZE 32
746
747 char version[RTL_VER_SIZE];
748
749 struct rtl_fw_phy_action {
750 __le32 *code;
751 size_t size;
752 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200753 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300754#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755};
756
Ralf Baechle979b6c12005-06-13 14:30:40 -0700757MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700760MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200761module_param_named(debug, debug.msg_enable, int, 0);
762MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763MODULE_LICENSE("GPL");
764MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000765MODULE_FIRMWARE(FIRMWARE_8168D_1);
766MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000767MODULE_FIRMWARE(FIRMWARE_8168E_1);
768MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400769MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800770MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800771MODULE_FIRMWARE(FIRMWARE_8168F_1);
772MODULE_FIRMWARE(FIRMWARE_8168F_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Francois Romieuda78dbf2012-01-26 14:18:23 +0100774static void rtl_lock_work(struct rtl8169_private *tp)
775{
776 mutex_lock(&tp->wk.mutex);
777}
778
779static void rtl_unlock_work(struct rtl8169_private *tp)
780{
781 mutex_unlock(&tp->wk.mutex);
782}
783
Francois Romieud58d46b2011-05-03 16:38:29 +0200784static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
785{
786 int cap = pci_pcie_cap(pdev);
787
788 if (cap) {
789 u16 ctl;
790
791 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
792 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
793 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
794 }
795}
796
françois romieub646d902011-01-03 15:08:21 +0000797static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
798{
799 void __iomem *ioaddr = tp->mmio_addr;
800 int i;
801
802 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
803 for (i = 0; i < 20; i++) {
804 udelay(100);
805 if (RTL_R32(OCPAR) & OCPAR_FLAG)
806 break;
807 }
808 return RTL_R32(OCPDR);
809}
810
811static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
812{
813 void __iomem *ioaddr = tp->mmio_addr;
814 int i;
815
816 RTL_W32(OCPDR, data);
817 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
818 for (i = 0; i < 20; i++) {
819 udelay(100);
820 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
821 break;
822 }
823}
824
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800825static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000826{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800827 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000828 int i;
829
830 RTL_W8(ERIDR, cmd);
831 RTL_W32(ERIAR, 0x800010e8);
832 msleep(2);
833 for (i = 0; i < 5; i++) {
834 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200835 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000836 break;
837 }
838
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800839 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000840}
841
842#define OOB_CMD_RESET 0x00
843#define OOB_CMD_DRIVER_START 0x05
844#define OOB_CMD_DRIVER_STOP 0x06
845
Francois Romieucecb5fd2011-04-01 10:21:07 +0200846static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
847{
848 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
849}
850
françois romieub646d902011-01-03 15:08:21 +0000851static void rtl8168_driver_start(struct rtl8169_private *tp)
852{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200853 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000854 int i;
855
856 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
857
Francois Romieucecb5fd2011-04-01 10:21:07 +0200858 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000859
françois romieub646d902011-01-03 15:08:21 +0000860 for (i = 0; i < 10; i++) {
861 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000862 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000863 break;
864 }
865}
866
867static void rtl8168_driver_stop(struct rtl8169_private *tp)
868{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200869 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000870 int i;
871
872 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
873
Francois Romieucecb5fd2011-04-01 10:21:07 +0200874 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000875
françois romieub646d902011-01-03 15:08:21 +0000876 for (i = 0; i < 10; i++) {
877 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000878 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000879 break;
880 }
881}
882
hayeswang4804b3b2011-03-21 01:50:29 +0000883static int r8168dp_check_dash(struct rtl8169_private *tp)
884{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200885 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000886
Francois Romieucecb5fd2011-04-01 10:21:07 +0200887 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000888}
françois romieub646d902011-01-03 15:08:21 +0000889
françois romieu4da19632011-01-03 15:07:55 +0000890static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
892 int i;
893
Francois Romieua6baf3a2007-11-08 23:23:21 +0100894 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Francois Romieu23714082006-01-29 00:49:09 +0100896 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100897 /*
898 * Check if the RTL8169 has completed writing to the specified
899 * MII register.
900 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200901 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 break;
Francois Romieu23714082006-01-29 00:49:09 +0100903 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700905 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700906 * According to hardware specs a 20us delay is required after write
907 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700908 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700909 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
françois romieu4da19632011-01-03 15:07:55 +0000912static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
914 int i, value = -1;
915
Francois Romieua6baf3a2007-11-08 23:23:21 +0100916 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Francois Romieu23714082006-01-29 00:49:09 +0100918 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100919 /*
920 * Check if the RTL8169 has completed retrieving data from
921 * the specified MII register.
922 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100924 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 break;
926 }
Francois Romieu23714082006-01-29 00:49:09 +0100927 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700929 /*
930 * According to hardware specs a 20us delay is required after read
931 * complete indication, but before sending next command.
932 */
933 udelay(20);
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return value;
936}
937
françois romieuc0e45c12011-01-03 15:08:04 +0000938static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
939{
940 int i;
941
942 RTL_W32(OCPDR, data |
943 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
944 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
945 RTL_W32(EPHY_RXER_NUM, 0);
946
947 for (i = 0; i < 100; i++) {
948 mdelay(1);
949 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
950 break;
951 }
952}
953
954static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
955{
956 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
957 (value & OCPDR_DATA_MASK));
958}
959
960static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
961{
962 int i;
963
964 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
965
966 mdelay(1);
967 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
968 RTL_W32(EPHY_RXER_NUM, 0);
969
970 for (i = 0; i < 100; i++) {
971 mdelay(1);
972 if (RTL_R32(OCPAR) & OCPAR_FLAG)
973 break;
974 }
975
976 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
977}
978
françois romieue6de30d2011-01-03 15:08:37 +0000979#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
980
981static void r8168dp_2_mdio_start(void __iomem *ioaddr)
982{
983 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
984}
985
986static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
987{
988 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
989}
990
991static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
992{
993 r8168dp_2_mdio_start(ioaddr);
994
995 r8169_mdio_write(ioaddr, reg_addr, value);
996
997 r8168dp_2_mdio_stop(ioaddr);
998}
999
1000static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
1001{
1002 int value;
1003
1004 r8168dp_2_mdio_start(ioaddr);
1005
1006 value = r8169_mdio_read(ioaddr, reg_addr);
1007
1008 r8168dp_2_mdio_stop(ioaddr);
1009
1010 return value;
1011}
1012
françois romieu4da19632011-01-03 15:07:55 +00001013static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001014{
françois romieuc0e45c12011-01-03 15:08:04 +00001015 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001016}
1017
françois romieu4da19632011-01-03 15:07:55 +00001018static int rtl_readphy(struct rtl8169_private *tp, int location)
1019{
françois romieuc0e45c12011-01-03 15:08:04 +00001020 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +00001021}
1022
1023static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1024{
1025 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1026}
1027
1028static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001029{
1030 int val;
1031
françois romieu4da19632011-01-03 15:07:55 +00001032 val = rtl_readphy(tp, reg_addr);
1033 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001034}
1035
Francois Romieuccdffb92008-07-26 14:26:06 +02001036static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1037 int val)
1038{
1039 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001040
françois romieu4da19632011-01-03 15:07:55 +00001041 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001042}
1043
1044static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1045{
1046 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001047
françois romieu4da19632011-01-03 15:07:55 +00001048 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001049}
1050
Francois Romieudacf8152008-08-02 20:44:13 +02001051static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
1052{
1053 unsigned int i;
1054
1055 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1056 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1057
1058 for (i = 0; i < 100; i++) {
1059 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1060 break;
1061 udelay(10);
1062 }
1063}
1064
1065static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1066{
1067 u16 value = 0xffff;
1068 unsigned int i;
1069
1070 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1071
1072 for (i = 0; i < 100; i++) {
1073 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1074 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1075 break;
1076 }
1077 udelay(10);
1078 }
1079
1080 return value;
1081}
1082
1083static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1084{
1085 unsigned int i;
1086
1087 RTL_W32(CSIDR, value);
1088 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1089 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1090
1091 for (i = 0; i < 100; i++) {
1092 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1093 break;
1094 udelay(10);
1095 }
1096}
1097
1098static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1099{
1100 u32 value = ~0x00;
1101 unsigned int i;
1102
1103 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1104 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1105
1106 for (i = 0; i < 100; i++) {
1107 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1108 value = RTL_R32(CSIDR);
1109 break;
1110 }
1111 udelay(10);
1112 }
1113
1114 return value;
1115}
1116
Hayes Wang133ac402011-07-06 15:58:05 +08001117static
1118void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1119{
1120 unsigned int i;
1121
1122 BUG_ON((addr & 3) || (mask == 0));
1123 RTL_W32(ERIDR, val);
1124 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1125
1126 for (i = 0; i < 100; i++) {
1127 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1128 break;
1129 udelay(100);
1130 }
1131}
1132
1133static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1134{
1135 u32 value = ~0x00;
1136 unsigned int i;
1137
1138 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1139
1140 for (i = 0; i < 100; i++) {
1141 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1142 value = RTL_R32(ERIDR);
1143 break;
1144 }
1145 udelay(100);
1146 }
1147
1148 return value;
1149}
1150
1151static void
1152rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1153{
1154 u32 val;
1155
1156 val = rtl_eri_read(ioaddr, addr, type);
1157 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1158}
1159
françois romieuc28aa382011-08-02 03:53:43 +00001160struct exgmac_reg {
1161 u16 addr;
1162 u16 mask;
1163 u32 val;
1164};
1165
1166static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1167 const struct exgmac_reg *r, int len)
1168{
1169 while (len-- > 0) {
1170 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1171 r++;
1172 }
1173}
1174
françois romieudaf9df62009-10-07 12:44:20 +00001175static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1176{
1177 u8 value = 0xff;
1178 unsigned int i;
1179
1180 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1181
1182 for (i = 0; i < 300; i++) {
1183 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1184 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1185 break;
1186 }
1187 udelay(100);
1188 }
1189
1190 return value;
1191}
1192
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001193static u16 rtl_get_events(struct rtl8169_private *tp)
1194{
1195 void __iomem *ioaddr = tp->mmio_addr;
1196
1197 return RTL_R16(IntrStatus);
1198}
1199
1200static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1201{
1202 void __iomem *ioaddr = tp->mmio_addr;
1203
1204 RTL_W16(IntrStatus, bits);
1205 mmiowb();
1206}
1207
1208static void rtl_irq_disable(struct rtl8169_private *tp)
1209{
1210 void __iomem *ioaddr = tp->mmio_addr;
1211
1212 RTL_W16(IntrMask, 0);
1213 mmiowb();
1214}
1215
Francois Romieu3e990ff2012-01-26 12:50:01 +01001216static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1217{
1218 void __iomem *ioaddr = tp->mmio_addr;
1219
1220 RTL_W16(IntrMask, bits);
1221}
1222
Francois Romieuda78dbf2012-01-26 14:18:23 +01001223#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1224#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1225#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1226
1227static void rtl_irq_enable_all(struct rtl8169_private *tp)
1228{
1229 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1230}
1231
françois romieu811fd302011-12-04 20:30:45 +00001232static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
françois romieu811fd302011-12-04 20:30:45 +00001234 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001236 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001237 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001238 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
françois romieu4da19632011-01-03 15:07:55 +00001241static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
françois romieu4da19632011-01-03 15:07:55 +00001243 void __iomem *ioaddr = tp->mmio_addr;
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return RTL_R32(TBICSR) & TBIReset;
1246}
1247
françois romieu4da19632011-01-03 15:07:55 +00001248static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
françois romieu4da19632011-01-03 15:07:55 +00001250 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
1252
1253static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1254{
1255 return RTL_R32(TBICSR) & TBILinkOk;
1256}
1257
1258static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1259{
1260 return RTL_R8(PHYstatus) & LinkStatus;
1261}
1262
françois romieu4da19632011-01-03 15:07:55 +00001263static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
françois romieu4da19632011-01-03 15:07:55 +00001265 void __iomem *ioaddr = tp->mmio_addr;
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1268}
1269
françois romieu4da19632011-01-03 15:07:55 +00001270static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
1272 unsigned int val;
1273
françois romieu4da19632011-01-03 15:07:55 +00001274 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1275 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
Hayes Wang70090422011-07-06 15:58:06 +08001278static void rtl_link_chg_patch(struct rtl8169_private *tp)
1279{
1280 void __iomem *ioaddr = tp->mmio_addr;
1281 struct net_device *dev = tp->dev;
1282
1283 if (!netif_running(dev))
1284 return;
1285
1286 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1287 if (RTL_R8(PHYstatus) & _1000bpsF) {
1288 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1289 0x00000011, ERIAR_EXGMAC);
1290 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1291 0x00000005, ERIAR_EXGMAC);
1292 } else if (RTL_R8(PHYstatus) & _100bps) {
1293 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1294 0x0000001f, ERIAR_EXGMAC);
1295 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1296 0x00000005, ERIAR_EXGMAC);
1297 } else {
1298 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1299 0x0000001f, ERIAR_EXGMAC);
1300 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1301 0x0000003f, ERIAR_EXGMAC);
1302 }
1303 /* Reset packet filter */
1304 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1305 ERIAR_EXGMAC);
1306 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1307 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001308 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1309 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1310 if (RTL_R8(PHYstatus) & _1000bpsF) {
1311 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1312 0x00000011, ERIAR_EXGMAC);
1313 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1314 0x00000005, ERIAR_EXGMAC);
1315 } else {
1316 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1317 0x0000001f, ERIAR_EXGMAC);
1318 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1319 0x0000003f, ERIAR_EXGMAC);
1320 }
Hayes Wang70090422011-07-06 15:58:06 +08001321 }
1322}
1323
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001324static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001325 struct rtl8169_private *tp,
1326 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001329 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001330 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001331 if (pm)
1332 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001334 if (net_ratelimit())
1335 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001336 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001338 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001339 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001340 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
1343
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001344static void rtl8169_check_link_status(struct net_device *dev,
1345 struct rtl8169_private *tp,
1346 void __iomem *ioaddr)
1347{
1348 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1349}
1350
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001351#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1352
1353static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1354{
1355 void __iomem *ioaddr = tp->mmio_addr;
1356 u8 options;
1357 u32 wolopts = 0;
1358
1359 options = RTL_R8(Config1);
1360 if (!(options & PMEnable))
1361 return 0;
1362
1363 options = RTL_R8(Config3);
1364 if (options & LinkUp)
1365 wolopts |= WAKE_PHY;
1366 if (options & MagicPacket)
1367 wolopts |= WAKE_MAGIC;
1368
1369 options = RTL_R8(Config5);
1370 if (options & UWF)
1371 wolopts |= WAKE_UCAST;
1372 if (options & BWF)
1373 wolopts |= WAKE_BCAST;
1374 if (options & MWF)
1375 wolopts |= WAKE_MCAST;
1376
1377 return wolopts;
1378}
1379
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001380static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1381{
1382 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001383
Francois Romieuda78dbf2012-01-26 14:18:23 +01001384 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001385
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001386 wol->supported = WAKE_ANY;
1387 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001388
Francois Romieuda78dbf2012-01-26 14:18:23 +01001389 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001390}
1391
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001392static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001393{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001394 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001395 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001396 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001397 u32 opt;
1398 u16 reg;
1399 u8 mask;
1400 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001401 { WAKE_PHY, Config3, LinkUp },
1402 { WAKE_MAGIC, Config3, MagicPacket },
1403 { WAKE_UCAST, Config5, UWF },
1404 { WAKE_BCAST, Config5, BWF },
1405 { WAKE_MCAST, Config5, MWF },
1406 { WAKE_ANY, Config5, LanWake }
1407 };
Francois Romieu851e6022012-04-17 11:10:11 +02001408 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001409
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001410 RTL_W8(Cfg9346, Cfg9346_Unlock);
1411
1412 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
Francois Romieu851e6022012-04-17 11:10:11 +02001413 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001414 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001415 options |= cfg[i].mask;
1416 RTL_W8(cfg[i].reg, options);
1417 }
1418
Francois Romieu851e6022012-04-17 11:10:11 +02001419 switch (tp->mac_version) {
1420 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1421 options = RTL_R8(Config1) & ~PMEnable;
1422 if (wolopts)
1423 options |= PMEnable;
1424 RTL_W8(Config1, options);
1425 break;
1426 default:
Francois Romieud387b422012-04-17 11:12:01 +02001427 options = RTL_R8(Config2) & ~PME_SIGNAL;
1428 if (wolopts)
1429 options |= PME_SIGNAL;
1430 RTL_W8(Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001431 break;
1432 }
1433
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001434 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001435}
1436
1437static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1438{
1439 struct rtl8169_private *tp = netdev_priv(dev);
1440
Francois Romieuda78dbf2012-01-26 14:18:23 +01001441 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001442
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001443 if (wol->wolopts)
1444 tp->features |= RTL_FEATURE_WOL;
1445 else
1446 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001447 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001448
1449 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001450
françois romieuea809072010-11-08 13:23:58 +00001451 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1452
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001453 return 0;
1454}
1455
Francois Romieu31bd2042011-04-26 18:58:59 +02001456static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1457{
Francois Romieu85bffe62011-04-27 08:22:39 +02001458 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001459}
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461static void rtl8169_get_drvinfo(struct net_device *dev,
1462 struct ethtool_drvinfo *info)
1463{
1464 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001465 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Rick Jones68aad782011-11-07 13:29:27 +00001467 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1468 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1469 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001470 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001471 if (!IS_ERR_OR_NULL(rtl_fw))
1472 strlcpy(info->fw_version, rtl_fw->version,
1473 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
1476static int rtl8169_get_regs_len(struct net_device *dev)
1477{
1478 return R8169_REGS_SIZE;
1479}
1480
1481static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001482 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
1484 struct rtl8169_private *tp = netdev_priv(dev);
1485 void __iomem *ioaddr = tp->mmio_addr;
1486 int ret = 0;
1487 u32 reg;
1488
1489 reg = RTL_R32(TBICSR);
1490 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1491 (duplex == DUPLEX_FULL)) {
1492 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1493 } else if (autoneg == AUTONEG_ENABLE)
1494 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1495 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001496 netif_warn(tp, link, dev,
1497 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 ret = -EOPNOTSUPP;
1499 }
1500
1501 return ret;
1502}
1503
1504static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001505 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001508 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001509 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Hayes Wang716b50a2011-02-22 17:26:18 +08001511 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001514 int auto_nego;
1515
françois romieu4da19632011-01-03 15:07:55 +00001516 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001517 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1518 ADVERTISE_100HALF | ADVERTISE_100FULL);
1519
1520 if (adv & ADVERTISED_10baseT_Half)
1521 auto_nego |= ADVERTISE_10HALF;
1522 if (adv & ADVERTISED_10baseT_Full)
1523 auto_nego |= ADVERTISE_10FULL;
1524 if (adv & ADVERTISED_100baseT_Half)
1525 auto_nego |= ADVERTISE_100HALF;
1526 if (adv & ADVERTISED_100baseT_Full)
1527 auto_nego |= ADVERTISE_100FULL;
1528
françois romieu3577aa12009-05-19 10:46:48 +00001529 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1530
françois romieu4da19632011-01-03 15:07:55 +00001531 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001532 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1533
1534 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001535 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001536 if (adv & ADVERTISED_1000baseT_Half)
1537 giga_ctrl |= ADVERTISE_1000HALF;
1538 if (adv & ADVERTISED_1000baseT_Full)
1539 giga_ctrl |= ADVERTISE_1000FULL;
1540 } else if (adv & (ADVERTISED_1000baseT_Half |
1541 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001542 netif_info(tp, link, dev,
1543 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001544 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
françois romieu3577aa12009-05-19 10:46:48 +00001547 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001548
françois romieu4da19632011-01-03 15:07:55 +00001549 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1550 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001551 } else {
1552 giga_ctrl = 0;
1553
1554 if (speed == SPEED_10)
1555 bmcr = 0;
1556 else if (speed == SPEED_100)
1557 bmcr = BMCR_SPEED100;
1558 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001559 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001560
1561 if (duplex == DUPLEX_FULL)
1562 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001563 }
1564
françois romieu4da19632011-01-03 15:07:55 +00001565 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001566
Francois Romieucecb5fd2011-04-01 10:21:07 +02001567 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1568 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001569 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001570 rtl_writephy(tp, 0x17, 0x2138);
1571 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001572 } else {
françois romieu4da19632011-01-03 15:07:55 +00001573 rtl_writephy(tp, 0x17, 0x2108);
1574 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001575 }
1576 }
1577
Oliver Neukum54405cd2011-01-06 21:55:13 +01001578 rc = 0;
1579out:
1580 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
1583static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001584 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
1586 struct rtl8169_private *tp = netdev_priv(dev);
1587 int ret;
1588
Oliver Neukum54405cd2011-01-06 21:55:13 +01001589 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001590 if (ret < 0)
1591 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Francois Romieu4876cc12011-03-11 21:07:11 +01001593 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1594 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001596 }
1597out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 return ret;
1599}
1600
1601static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1602{
1603 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 int ret;
1605
Francois Romieu4876cc12011-03-11 21:07:11 +01001606 del_timer_sync(&tp->timer);
1607
Francois Romieuda78dbf2012-01-26 14:18:23 +01001608 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001609 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001610 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001611 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 return ret;
1614}
1615
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001616static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1617 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
Francois Romieud58d46b2011-05-03 16:38:29 +02001619 struct rtl8169_private *tp = netdev_priv(dev);
1620
Francois Romieu2b7b4312011-04-18 22:53:24 -07001621 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001622 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Francois Romieud58d46b2011-05-03 16:38:29 +02001624 if (dev->mtu > JUMBO_1K &&
1625 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1626 features &= ~NETIF_F_IP_CSUM;
1627
Michał Mirosław350fb322011-04-08 06:35:56 +00001628 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
Francois Romieuda78dbf2012-01-26 14:18:23 +01001631static void __rtl8169_set_features(struct net_device *dev,
1632 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
1634 struct rtl8169_private *tp = netdev_priv(dev);
Ben Greear6bbe0212012-02-10 15:04:33 +00001635 netdev_features_t changed = features ^ dev->features;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001636 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Ben Greear6bbe0212012-02-10 15:04:33 +00001638 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)))
1639 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Ben Greear6bbe0212012-02-10 15:04:33 +00001641 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)) {
1642 if (features & NETIF_F_RXCSUM)
1643 tp->cp_cmd |= RxChkSum;
1644 else
1645 tp->cp_cmd &= ~RxChkSum;
Michał Mirosław350fb322011-04-08 06:35:56 +00001646
Ben Greear6bbe0212012-02-10 15:04:33 +00001647 if (dev->features & NETIF_F_HW_VLAN_RX)
1648 tp->cp_cmd |= RxVlan;
1649 else
1650 tp->cp_cmd &= ~RxVlan;
1651
1652 RTL_W16(CPlusCmd, tp->cp_cmd);
1653 RTL_R16(CPlusCmd);
1654 }
1655 if (changed & NETIF_F_RXALL) {
1656 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1657 if (features & NETIF_F_RXALL)
1658 tmp |= (AcceptErr | AcceptRunt);
1659 RTL_W32(RxConfig, tmp);
1660 }
Francois Romieuda78dbf2012-01-26 14:18:23 +01001661}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Francois Romieuda78dbf2012-01-26 14:18:23 +01001663static int rtl8169_set_features(struct net_device *dev,
1664 netdev_features_t features)
1665{
1666 struct rtl8169_private *tp = netdev_priv(dev);
1667
1668 rtl_lock_work(tp);
1669 __rtl8169_set_features(dev, features);
1670 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672 return 0;
1673}
1674
Francois Romieuda78dbf2012-01-26 14:18:23 +01001675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1677 struct sk_buff *skb)
1678{
Jesse Grosseab6d182010-10-20 13:56:03 +00001679 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1681}
1682
Francois Romieu7a8fc772011-03-01 17:18:33 +01001683static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
1685 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Francois Romieu7a8fc772011-03-01 17:18:33 +01001687 if (opts2 & RxVlanTag)
1688 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}
1692
Francois Romieuccdffb92008-07-26 14:26:06 +02001693static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
1695 struct rtl8169_private *tp = netdev_priv(dev);
1696 void __iomem *ioaddr = tp->mmio_addr;
1697 u32 status;
1698
1699 cmd->supported =
1700 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1701 cmd->port = PORT_FIBRE;
1702 cmd->transceiver = XCVR_INTERNAL;
1703
1704 status = RTL_R32(TBICSR);
1705 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1706 cmd->autoneg = !!(status & TBINwEnable);
1707
David Decotigny70739492011-04-27 18:32:40 +00001708 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001710
1711 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
1713
Francois Romieuccdffb92008-07-26 14:26:06 +02001714static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
1716 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Francois Romieuccdffb92008-07-26 14:26:06 +02001718 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719}
1720
1721static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1722{
1723 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001724 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Francois Romieuda78dbf2012-01-26 14:18:23 +01001726 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02001727 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001728 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Francois Romieuccdffb92008-07-26 14:26:06 +02001730 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
1733static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1734 void *p)
1735{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001736 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Francois Romieu5b0384f2006-08-16 16:00:01 +02001738 if (regs->len > R8169_REGS_SIZE)
1739 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Francois Romieuda78dbf2012-01-26 14:18:23 +01001741 rtl_lock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001742 memcpy_fromio(p, tp->mmio_addr, regs->len);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001743 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744}
1745
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001746static u32 rtl8169_get_msglevel(struct net_device *dev)
1747{
1748 struct rtl8169_private *tp = netdev_priv(dev);
1749
1750 return tp->msg_enable;
1751}
1752
1753static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1754{
1755 struct rtl8169_private *tp = netdev_priv(dev);
1756
1757 tp->msg_enable = value;
1758}
1759
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001760static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1761 "tx_packets",
1762 "rx_packets",
1763 "tx_errors",
1764 "rx_errors",
1765 "rx_missed",
1766 "align_errors",
1767 "tx_single_collisions",
1768 "tx_multi_collisions",
1769 "unicast",
1770 "broadcast",
1771 "multicast",
1772 "tx_aborted",
1773 "tx_underrun",
1774};
1775
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001776static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001777{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001778 switch (sset) {
1779 case ETH_SS_STATS:
1780 return ARRAY_SIZE(rtl8169_gstrings);
1781 default:
1782 return -EOPNOTSUPP;
1783 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001784}
1785
Ivan Vecera355423d2009-02-06 21:49:57 -08001786static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001787{
1788 struct rtl8169_private *tp = netdev_priv(dev);
1789 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001790 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001791 struct rtl8169_counters *counters;
1792 dma_addr_t paddr;
1793 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001794 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001795
Ivan Vecera355423d2009-02-06 21:49:57 -08001796 /*
1797 * Some chips are unable to dump tally counters when the receiver
1798 * is disabled.
1799 */
1800 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1801 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001802
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001803 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001804 if (!counters)
1805 return;
1806
1807 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001808 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001809 RTL_W32(CounterAddrLow, cmd);
1810 RTL_W32(CounterAddrLow, cmd | CounterDump);
1811
Ivan Vecera355423d2009-02-06 21:49:57 -08001812 while (wait--) {
1813 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001814 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001815 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001816 }
1817 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001818 }
1819
1820 RTL_W32(CounterAddrLow, 0);
1821 RTL_W32(CounterAddrHigh, 0);
1822
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001823 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001824}
1825
Ivan Vecera355423d2009-02-06 21:49:57 -08001826static void rtl8169_get_ethtool_stats(struct net_device *dev,
1827 struct ethtool_stats *stats, u64 *data)
1828{
1829 struct rtl8169_private *tp = netdev_priv(dev);
1830
1831 ASSERT_RTNL();
1832
1833 rtl8169_update_counters(dev);
1834
1835 data[0] = le64_to_cpu(tp->counters.tx_packets);
1836 data[1] = le64_to_cpu(tp->counters.rx_packets);
1837 data[2] = le64_to_cpu(tp->counters.tx_errors);
1838 data[3] = le32_to_cpu(tp->counters.rx_errors);
1839 data[4] = le16_to_cpu(tp->counters.rx_missed);
1840 data[5] = le16_to_cpu(tp->counters.align_errors);
1841 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1842 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1843 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1844 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1845 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1846 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1847 data[12] = le16_to_cpu(tp->counters.tx_underun);
1848}
1849
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001850static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1851{
1852 switch(stringset) {
1853 case ETH_SS_STATS:
1854 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1855 break;
1856 }
1857}
1858
Jeff Garzik7282d492006-09-13 14:30:00 -04001859static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 .get_drvinfo = rtl8169_get_drvinfo,
1861 .get_regs_len = rtl8169_get_regs_len,
1862 .get_link = ethtool_op_get_link,
1863 .get_settings = rtl8169_get_settings,
1864 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001865 .get_msglevel = rtl8169_get_msglevel,
1866 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001868 .get_wol = rtl8169_get_wol,
1869 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001870 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001871 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001872 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00001873 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874};
1875
Francois Romieu07d3f512007-02-21 22:40:46 +01001876static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001877 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
Francois Romieu5d320a22011-05-08 17:47:36 +02001879 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001880 /*
1881 * The driver currently handles the 8168Bf and the 8168Be identically
1882 * but they can be identified more specifically through the test below
1883 * if needed:
1884 *
1885 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001886 *
1887 * Same thing for the 8101Eb and the 8101Ec:
1888 *
1889 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001890 */
Francois Romieu37441002011-06-17 22:58:54 +02001891 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001893 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 int mac_version;
1895 } mac_info[] = {
Hayes Wangc2218922011-09-06 16:55:18 +08001896 /* 8168F family. */
1897 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1898 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1899
hayeswang01dc7fe2011-03-21 01:50:28 +00001900 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001901 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001902 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1903 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1904 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1905
Francois Romieu5b538df2008-07-20 16:22:45 +02001906 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001907 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1908 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001909 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001910
françois romieue6de30d2011-01-03 15:08:37 +00001911 /* 8168DP family. */
1912 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1913 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001914 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001915
Francois Romieuef808d52008-06-29 13:10:54 +02001916 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001917 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001918 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001919 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001920 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001921 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1922 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001923 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001924 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001925 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001926
1927 /* 8168B family. */
1928 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1929 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1930 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1931 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1932
1933 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001934 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001935 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1936 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1937 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001938 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1939 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1940 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1941 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1942 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1943 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001944 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001945 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001946 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001947 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1948 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001949 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1950 /* FIXME: where did these entries come from ? -- FR */
1951 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1952 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1953
1954 /* 8110 family. */
1955 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1956 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1957 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1958 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1959 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1960 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1961
Jean Delvaref21b75e2009-05-26 20:54:48 -07001962 /* Catch-all */
1963 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001964 };
1965 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 u32 reg;
1967
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001968 reg = RTL_R32(TxConfig);
1969 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 p++;
1971 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001972
1973 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1974 netif_notice(tp, probe, dev,
1975 "unknown MAC, using family default\n");
1976 tp->mac_version = default_version;
1977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978}
1979
1980static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1981{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001982 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
Francois Romieu867763c2007-08-17 18:21:58 +02001985struct phy_reg {
1986 u16 reg;
1987 u16 val;
1988};
1989
françois romieu4da19632011-01-03 15:07:55 +00001990static void rtl_writephy_batch(struct rtl8169_private *tp,
1991 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001992{
1993 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001994 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001995 regs++;
1996 }
1997}
1998
françois romieubca03d52011-01-03 15:07:31 +00001999#define PHY_READ 0x00000000
2000#define PHY_DATA_OR 0x10000000
2001#define PHY_DATA_AND 0x20000000
2002#define PHY_BJMPN 0x30000000
2003#define PHY_READ_EFUSE 0x40000000
2004#define PHY_READ_MAC_BYTE 0x50000000
2005#define PHY_WRITE_MAC_BYTE 0x60000000
2006#define PHY_CLEAR_READCOUNT 0x70000000
2007#define PHY_WRITE 0x80000000
2008#define PHY_READCOUNT_EQ_SKIP 0x90000000
2009#define PHY_COMP_EQ_SKIPN 0xa0000000
2010#define PHY_COMP_NEQ_SKIPN 0xb0000000
2011#define PHY_WRITE_PREVIOUS 0xc0000000
2012#define PHY_SKIPN 0xd0000000
2013#define PHY_DELAY_MS 0xe0000000
2014#define PHY_WRITE_ERI_WORD 0xf0000000
2015
Hayes Wang960aee62011-06-18 11:37:48 +02002016struct fw_info {
2017 u32 magic;
2018 char version[RTL_VER_SIZE];
2019 __le32 fw_start;
2020 __le32 fw_len;
2021 u8 chksum;
2022} __packed;
2023
Francois Romieu1c361ef2011-06-17 17:16:24 +02002024#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2025
2026static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002027{
Francois Romieub6ffd972011-06-17 17:00:05 +02002028 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002029 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002030 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2031 char *version = rtl_fw->version;
2032 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002033
Francois Romieu1c361ef2011-06-17 17:16:24 +02002034 if (fw->size < FW_OPCODE_SIZE)
2035 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002036
2037 if (!fw_info->magic) {
2038 size_t i, size, start;
2039 u8 checksum = 0;
2040
2041 if (fw->size < sizeof(*fw_info))
2042 goto out;
2043
2044 for (i = 0; i < fw->size; i++)
2045 checksum += fw->data[i];
2046 if (checksum != 0)
2047 goto out;
2048
2049 start = le32_to_cpu(fw_info->fw_start);
2050 if (start > fw->size)
2051 goto out;
2052
2053 size = le32_to_cpu(fw_info->fw_len);
2054 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2055 goto out;
2056
2057 memcpy(version, fw_info->version, RTL_VER_SIZE);
2058
2059 pa->code = (__le32 *)(fw->data + start);
2060 pa->size = size;
2061 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002062 if (fw->size % FW_OPCODE_SIZE)
2063 goto out;
2064
2065 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2066
2067 pa->code = (__le32 *)fw->data;
2068 pa->size = fw->size / FW_OPCODE_SIZE;
2069 }
2070 version[RTL_VER_SIZE - 1] = 0;
2071
2072 rc = true;
2073out:
2074 return rc;
2075}
2076
Francois Romieufd112f22011-06-18 00:10:29 +02002077static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2078 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002079{
Francois Romieufd112f22011-06-18 00:10:29 +02002080 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002081 size_t index;
2082
Francois Romieu1c361ef2011-06-17 17:16:24 +02002083 for (index = 0; index < pa->size; index++) {
2084 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002085 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002086
hayeswang42b82dc2011-01-10 02:07:25 +00002087 switch(action & 0xf0000000) {
2088 case PHY_READ:
2089 case PHY_DATA_OR:
2090 case PHY_DATA_AND:
2091 case PHY_READ_EFUSE:
2092 case PHY_CLEAR_READCOUNT:
2093 case PHY_WRITE:
2094 case PHY_WRITE_PREVIOUS:
2095 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002096 break;
2097
hayeswang42b82dc2011-01-10 02:07:25 +00002098 case PHY_BJMPN:
2099 if (regno > index) {
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_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002106 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002107 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002108 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002109 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002110 }
2111 break;
2112 case PHY_COMP_EQ_SKIPN:
2113 case PHY_COMP_NEQ_SKIPN:
2114 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002115 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002116 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002117 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002118 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002119 }
2120 break;
2121
2122 case PHY_READ_MAC_BYTE:
2123 case PHY_WRITE_MAC_BYTE:
2124 case PHY_WRITE_ERI_WORD:
2125 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002126 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002127 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002128 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002129 }
2130 }
Francois Romieufd112f22011-06-18 00:10:29 +02002131 rc = true;
2132out:
2133 return rc;
2134}
françois romieubca03d52011-01-03 15:07:31 +00002135
Francois Romieufd112f22011-06-18 00:10:29 +02002136static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2137{
2138 struct net_device *dev = tp->dev;
2139 int rc = -EINVAL;
2140
2141 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2142 netif_err(tp, ifup, dev, "invalid firwmare\n");
2143 goto out;
2144 }
2145
2146 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2147 rc = 0;
2148out:
2149 return rc;
2150}
2151
2152static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2153{
2154 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2155 u32 predata, count;
2156 size_t index;
2157
2158 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002159
Francois Romieu1c361ef2011-06-17 17:16:24 +02002160 for (index = 0; index < pa->size; ) {
2161 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002162 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002163 u32 regno = (action & 0x0fff0000) >> 16;
2164
2165 if (!action)
2166 break;
françois romieubca03d52011-01-03 15:07:31 +00002167
2168 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002169 case PHY_READ:
2170 predata = rtl_readphy(tp, regno);
2171 count++;
2172 index++;
françois romieubca03d52011-01-03 15:07:31 +00002173 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002174 case PHY_DATA_OR:
2175 predata |= data;
2176 index++;
2177 break;
2178 case PHY_DATA_AND:
2179 predata &= data;
2180 index++;
2181 break;
2182 case PHY_BJMPN:
2183 index -= regno;
2184 break;
2185 case PHY_READ_EFUSE:
2186 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2187 index++;
2188 break;
2189 case PHY_CLEAR_READCOUNT:
2190 count = 0;
2191 index++;
2192 break;
2193 case PHY_WRITE:
2194 rtl_writephy(tp, regno, data);
2195 index++;
2196 break;
2197 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002198 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002199 break;
2200 case PHY_COMP_EQ_SKIPN:
2201 if (predata == data)
2202 index += regno;
2203 index++;
2204 break;
2205 case PHY_COMP_NEQ_SKIPN:
2206 if (predata != data)
2207 index += regno;
2208 index++;
2209 break;
2210 case PHY_WRITE_PREVIOUS:
2211 rtl_writephy(tp, regno, predata);
2212 index++;
2213 break;
2214 case PHY_SKIPN:
2215 index += regno + 1;
2216 break;
2217 case PHY_DELAY_MS:
2218 mdelay(data);
2219 index++;
2220 break;
2221
2222 case PHY_READ_MAC_BYTE:
2223 case PHY_WRITE_MAC_BYTE:
2224 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002225 default:
2226 BUG();
2227 }
2228 }
2229}
2230
françois romieuf1e02ed2011-01-13 13:07:53 +00002231static void rtl_release_firmware(struct rtl8169_private *tp)
2232{
Francois Romieub6ffd972011-06-17 17:00:05 +02002233 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2234 release_firmware(tp->rtl_fw->fw);
2235 kfree(tp->rtl_fw);
2236 }
2237 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002238}
2239
François Romieu953a12c2011-04-24 17:38:48 +02002240static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002241{
Francois Romieub6ffd972011-06-17 17:00:05 +02002242 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002243
2244 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002245 if (!IS_ERR_OR_NULL(rtl_fw))
2246 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002247}
2248
2249static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2250{
2251 if (rtl_readphy(tp, reg) != val)
2252 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2253 else
2254 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002255}
2256
françois romieu4da19632011-01-03 15:07:55 +00002257static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002259 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002260 { 0x1f, 0x0001 },
2261 { 0x06, 0x006e },
2262 { 0x08, 0x0708 },
2263 { 0x15, 0x4000 },
2264 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
françois romieu0b9b5712009-08-10 19:44:56 +00002266 { 0x1f, 0x0001 },
2267 { 0x03, 0x00a1 },
2268 { 0x02, 0x0008 },
2269 { 0x01, 0x0120 },
2270 { 0x00, 0x1000 },
2271 { 0x04, 0x0800 },
2272 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
françois romieu0b9b5712009-08-10 19:44:56 +00002274 { 0x03, 0xff41 },
2275 { 0x02, 0xdf60 },
2276 { 0x01, 0x0140 },
2277 { 0x00, 0x0077 },
2278 { 0x04, 0x7800 },
2279 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
françois romieu0b9b5712009-08-10 19:44:56 +00002281 { 0x03, 0x802f },
2282 { 0x02, 0x4f02 },
2283 { 0x01, 0x0409 },
2284 { 0x00, 0xf0f9 },
2285 { 0x04, 0x9800 },
2286 { 0x04, 0x9000 },
2287
2288 { 0x03, 0xdf01 },
2289 { 0x02, 0xdf20 },
2290 { 0x01, 0xff95 },
2291 { 0x00, 0xba00 },
2292 { 0x04, 0xa800 },
2293 { 0x04, 0xa000 },
2294
2295 { 0x03, 0xff41 },
2296 { 0x02, 0xdf20 },
2297 { 0x01, 0x0140 },
2298 { 0x00, 0x00bb },
2299 { 0x04, 0xb800 },
2300 { 0x04, 0xb000 },
2301
2302 { 0x03, 0xdf41 },
2303 { 0x02, 0xdc60 },
2304 { 0x01, 0x6340 },
2305 { 0x00, 0x007d },
2306 { 0x04, 0xd800 },
2307 { 0x04, 0xd000 },
2308
2309 { 0x03, 0xdf01 },
2310 { 0x02, 0xdf20 },
2311 { 0x01, 0x100a },
2312 { 0x00, 0xa0ff },
2313 { 0x04, 0xf800 },
2314 { 0x04, 0xf000 },
2315
2316 { 0x1f, 0x0000 },
2317 { 0x0b, 0x0000 },
2318 { 0x00, 0x9200 }
2319 };
2320
françois romieu4da19632011-01-03 15:07:55 +00002321 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322}
2323
françois romieu4da19632011-01-03 15:07:55 +00002324static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002325{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002326 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002327 { 0x1f, 0x0002 },
2328 { 0x01, 0x90d0 },
2329 { 0x1f, 0x0000 }
2330 };
2331
françois romieu4da19632011-01-03 15:07:55 +00002332 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002333}
2334
françois romieu4da19632011-01-03 15:07:55 +00002335static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002336{
2337 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002338
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002339 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2340 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002341 return;
2342
françois romieu4da19632011-01-03 15:07:55 +00002343 rtl_writephy(tp, 0x1f, 0x0001);
2344 rtl_writephy(tp, 0x10, 0xf01b);
2345 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002346}
2347
françois romieu4da19632011-01-03 15:07:55 +00002348static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002349{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002350 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002351 { 0x1f, 0x0001 },
2352 { 0x04, 0x0000 },
2353 { 0x03, 0x00a1 },
2354 { 0x02, 0x0008 },
2355 { 0x01, 0x0120 },
2356 { 0x00, 0x1000 },
2357 { 0x04, 0x0800 },
2358 { 0x04, 0x9000 },
2359 { 0x03, 0x802f },
2360 { 0x02, 0x4f02 },
2361 { 0x01, 0x0409 },
2362 { 0x00, 0xf099 },
2363 { 0x04, 0x9800 },
2364 { 0x04, 0xa000 },
2365 { 0x03, 0xdf01 },
2366 { 0x02, 0xdf20 },
2367 { 0x01, 0xff95 },
2368 { 0x00, 0xba00 },
2369 { 0x04, 0xa800 },
2370 { 0x04, 0xf000 },
2371 { 0x03, 0xdf01 },
2372 { 0x02, 0xdf20 },
2373 { 0x01, 0x101a },
2374 { 0x00, 0xa0ff },
2375 { 0x04, 0xf800 },
2376 { 0x04, 0x0000 },
2377 { 0x1f, 0x0000 },
2378
2379 { 0x1f, 0x0001 },
2380 { 0x10, 0xf41b },
2381 { 0x14, 0xfb54 },
2382 { 0x18, 0xf5c7 },
2383 { 0x1f, 0x0000 },
2384
2385 { 0x1f, 0x0001 },
2386 { 0x17, 0x0cc0 },
2387 { 0x1f, 0x0000 }
2388 };
2389
françois romieu4da19632011-01-03 15:07:55 +00002390 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002391
françois romieu4da19632011-01-03 15:07:55 +00002392 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002393}
2394
françois romieu4da19632011-01-03 15:07:55 +00002395static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002396{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002397 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002398 { 0x1f, 0x0001 },
2399 { 0x04, 0x0000 },
2400 { 0x03, 0x00a1 },
2401 { 0x02, 0x0008 },
2402 { 0x01, 0x0120 },
2403 { 0x00, 0x1000 },
2404 { 0x04, 0x0800 },
2405 { 0x04, 0x9000 },
2406 { 0x03, 0x802f },
2407 { 0x02, 0x4f02 },
2408 { 0x01, 0x0409 },
2409 { 0x00, 0xf099 },
2410 { 0x04, 0x9800 },
2411 { 0x04, 0xa000 },
2412 { 0x03, 0xdf01 },
2413 { 0x02, 0xdf20 },
2414 { 0x01, 0xff95 },
2415 { 0x00, 0xba00 },
2416 { 0x04, 0xa800 },
2417 { 0x04, 0xf000 },
2418 { 0x03, 0xdf01 },
2419 { 0x02, 0xdf20 },
2420 { 0x01, 0x101a },
2421 { 0x00, 0xa0ff },
2422 { 0x04, 0xf800 },
2423 { 0x04, 0x0000 },
2424 { 0x1f, 0x0000 },
2425
2426 { 0x1f, 0x0001 },
2427 { 0x0b, 0x8480 },
2428 { 0x1f, 0x0000 },
2429
2430 { 0x1f, 0x0001 },
2431 { 0x18, 0x67c7 },
2432 { 0x04, 0x2000 },
2433 { 0x03, 0x002f },
2434 { 0x02, 0x4360 },
2435 { 0x01, 0x0109 },
2436 { 0x00, 0x3022 },
2437 { 0x04, 0x2800 },
2438 { 0x1f, 0x0000 },
2439
2440 { 0x1f, 0x0001 },
2441 { 0x17, 0x0cc0 },
2442 { 0x1f, 0x0000 }
2443 };
2444
françois romieu4da19632011-01-03 15:07:55 +00002445 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002446}
2447
françois romieu4da19632011-01-03 15:07:55 +00002448static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002449{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002450 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002451 { 0x10, 0xf41b },
2452 { 0x1f, 0x0000 }
2453 };
2454
françois romieu4da19632011-01-03 15:07:55 +00002455 rtl_writephy(tp, 0x1f, 0x0001);
2456 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002457
françois romieu4da19632011-01-03 15:07:55 +00002458 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002459}
2460
françois romieu4da19632011-01-03 15:07:55 +00002461static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002462{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002463 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002464 { 0x1f, 0x0001 },
2465 { 0x10, 0xf41b },
2466 { 0x1f, 0x0000 }
2467 };
2468
françois romieu4da19632011-01-03 15:07:55 +00002469 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002470}
2471
françois romieu4da19632011-01-03 15:07:55 +00002472static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002473{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002474 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002475 { 0x1f, 0x0000 },
2476 { 0x1d, 0x0f00 },
2477 { 0x1f, 0x0002 },
2478 { 0x0c, 0x1ec8 },
2479 { 0x1f, 0x0000 }
2480 };
2481
françois romieu4da19632011-01-03 15:07:55 +00002482 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002483}
2484
françois romieu4da19632011-01-03 15:07:55 +00002485static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002486{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002487 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002488 { 0x1f, 0x0001 },
2489 { 0x1d, 0x3d98 },
2490 { 0x1f, 0x0000 }
2491 };
2492
françois romieu4da19632011-01-03 15:07:55 +00002493 rtl_writephy(tp, 0x1f, 0x0000);
2494 rtl_patchphy(tp, 0x14, 1 << 5);
2495 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002496
françois romieu4da19632011-01-03 15:07:55 +00002497 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002498}
2499
françois romieu4da19632011-01-03 15:07:55 +00002500static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002501{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002502 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002503 { 0x1f, 0x0001 },
2504 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002505 { 0x1f, 0x0002 },
2506 { 0x00, 0x88d4 },
2507 { 0x01, 0x82b1 },
2508 { 0x03, 0x7002 },
2509 { 0x08, 0x9e30 },
2510 { 0x09, 0x01f0 },
2511 { 0x0a, 0x5500 },
2512 { 0x0c, 0x00c8 },
2513 { 0x1f, 0x0003 },
2514 { 0x12, 0xc096 },
2515 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002516 { 0x1f, 0x0000 },
2517 { 0x1f, 0x0000 },
2518 { 0x09, 0x2000 },
2519 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002520 };
2521
françois romieu4da19632011-01-03 15:07:55 +00002522 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002523
françois romieu4da19632011-01-03 15:07:55 +00002524 rtl_patchphy(tp, 0x14, 1 << 5);
2525 rtl_patchphy(tp, 0x0d, 1 << 5);
2526 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002527}
2528
françois romieu4da19632011-01-03 15:07:55 +00002529static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002530{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002531 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002532 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002533 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002534 { 0x03, 0x802f },
2535 { 0x02, 0x4f02 },
2536 { 0x01, 0x0409 },
2537 { 0x00, 0xf099 },
2538 { 0x04, 0x9800 },
2539 { 0x04, 0x9000 },
2540 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002541 { 0x1f, 0x0002 },
2542 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002543 { 0x06, 0x0761 },
2544 { 0x1f, 0x0003 },
2545 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002546 { 0x1f, 0x0000 }
2547 };
2548
françois romieu4da19632011-01-03 15:07:55 +00002549 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002550
françois romieu4da19632011-01-03 15:07:55 +00002551 rtl_patchphy(tp, 0x16, 1 << 0);
2552 rtl_patchphy(tp, 0x14, 1 << 5);
2553 rtl_patchphy(tp, 0x0d, 1 << 5);
2554 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002555}
2556
françois romieu4da19632011-01-03 15:07:55 +00002557static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002558{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002559 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002560 { 0x1f, 0x0001 },
2561 { 0x12, 0x2300 },
2562 { 0x1d, 0x3d98 },
2563 { 0x1f, 0x0002 },
2564 { 0x0c, 0x7eb8 },
2565 { 0x06, 0x5461 },
2566 { 0x1f, 0x0003 },
2567 { 0x16, 0x0f0a },
2568 { 0x1f, 0x0000 }
2569 };
2570
françois romieu4da19632011-01-03 15:07:55 +00002571 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002572
françois romieu4da19632011-01-03 15:07:55 +00002573 rtl_patchphy(tp, 0x16, 1 << 0);
2574 rtl_patchphy(tp, 0x14, 1 << 5);
2575 rtl_patchphy(tp, 0x0d, 1 << 5);
2576 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002577}
2578
françois romieu4da19632011-01-03 15:07:55 +00002579static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002580{
françois romieu4da19632011-01-03 15:07:55 +00002581 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002582}
2583
françois romieubca03d52011-01-03 15:07:31 +00002584static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002585{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002586 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002587 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002588 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002589 { 0x06, 0x4064 },
2590 { 0x07, 0x2863 },
2591 { 0x08, 0x059c },
2592 { 0x09, 0x26b4 },
2593 { 0x0a, 0x6a19 },
2594 { 0x0b, 0xdcc8 },
2595 { 0x10, 0xf06d },
2596 { 0x14, 0x7f68 },
2597 { 0x18, 0x7fd9 },
2598 { 0x1c, 0xf0ff },
2599 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002600 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002601 { 0x12, 0xf49f },
2602 { 0x13, 0x070b },
2603 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002604 { 0x14, 0x94c0 },
2605
2606 /*
2607 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002608 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002609 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002610 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002611 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002612 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002613 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002614 { 0x06, 0x5561 },
2615
2616 /*
2617 * Can not link to 1Gbps with bad cable
2618 * Decrease SNR threshold form 21.07dB to 19.04dB
2619 */
2620 { 0x1f, 0x0001 },
2621 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002622
2623 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002624 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002625 };
françois romieubca03d52011-01-03 15:07:31 +00002626 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002627
françois romieu4da19632011-01-03 15:07:55 +00002628 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002629
françois romieubca03d52011-01-03 15:07:31 +00002630 /*
2631 * Rx Error Issue
2632 * Fine Tune Switching regulator parameter
2633 */
françois romieu4da19632011-01-03 15:07:55 +00002634 rtl_writephy(tp, 0x1f, 0x0002);
2635 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2636 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002637
françois romieudaf9df62009-10-07 12:44:20 +00002638 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002639 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002640 { 0x1f, 0x0002 },
2641 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002642 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002643 { 0x05, 0x8330 },
2644 { 0x06, 0x669a },
2645 { 0x1f, 0x0002 }
2646 };
2647 int val;
2648
françois romieu4da19632011-01-03 15:07:55 +00002649 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002650
françois romieu4da19632011-01-03 15:07:55 +00002651 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002652
2653 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002654 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002655 0x0065, 0x0066, 0x0067, 0x0068,
2656 0x0069, 0x006a, 0x006b, 0x006c
2657 };
2658 int i;
2659
françois romieu4da19632011-01-03 15:07:55 +00002660 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002661
2662 val &= 0xff00;
2663 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002664 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002665 }
2666 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002667 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002668 { 0x1f, 0x0002 },
2669 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002670 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002671 { 0x05, 0x8330 },
2672 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002673 };
2674
françois romieu4da19632011-01-03 15:07:55 +00002675 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002676 }
2677
françois romieubca03d52011-01-03 15:07:31 +00002678 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002679 rtl_writephy(tp, 0x1f, 0x0002);
2680 rtl_patchphy(tp, 0x0d, 0x0300);
2681 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002682
françois romieubca03d52011-01-03 15:07:31 +00002683 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002684 rtl_writephy(tp, 0x1f, 0x0002);
2685 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2686 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002687
françois romieu4da19632011-01-03 15:07:55 +00002688 rtl_writephy(tp, 0x1f, 0x0005);
2689 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002690
2691 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002692
françois romieu4da19632011-01-03 15:07:55 +00002693 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002694}
2695
françois romieubca03d52011-01-03 15:07:31 +00002696static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002697{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002698 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002699 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002700 { 0x1f, 0x0001 },
2701 { 0x06, 0x4064 },
2702 { 0x07, 0x2863 },
2703 { 0x08, 0x059c },
2704 { 0x09, 0x26b4 },
2705 { 0x0a, 0x6a19 },
2706 { 0x0b, 0xdcc8 },
2707 { 0x10, 0xf06d },
2708 { 0x14, 0x7f68 },
2709 { 0x18, 0x7fd9 },
2710 { 0x1c, 0xf0ff },
2711 { 0x1d, 0x3d9c },
2712 { 0x1f, 0x0003 },
2713 { 0x12, 0xf49f },
2714 { 0x13, 0x070b },
2715 { 0x1a, 0x05ad },
2716 { 0x14, 0x94c0 },
2717
françois romieubca03d52011-01-03 15:07:31 +00002718 /*
2719 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002720 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002721 */
françois romieudaf9df62009-10-07 12:44:20 +00002722 { 0x1f, 0x0002 },
2723 { 0x06, 0x5561 },
2724 { 0x1f, 0x0005 },
2725 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002726 { 0x06, 0x5561 },
2727
2728 /*
2729 * Can not link to 1Gbps with bad cable
2730 * Decrease SNR threshold form 21.07dB to 19.04dB
2731 */
2732 { 0x1f, 0x0001 },
2733 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002734
2735 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002736 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002737 };
françois romieubca03d52011-01-03 15:07:31 +00002738 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002739
françois romieu4da19632011-01-03 15:07:55 +00002740 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002741
2742 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002743 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002744 { 0x1f, 0x0002 },
2745 { 0x05, 0x669a },
2746 { 0x1f, 0x0005 },
2747 { 0x05, 0x8330 },
2748 { 0x06, 0x669a },
2749
2750 { 0x1f, 0x0002 }
2751 };
2752 int val;
2753
françois romieu4da19632011-01-03 15:07:55 +00002754 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002755
françois romieu4da19632011-01-03 15:07:55 +00002756 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002757 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002758 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002759 0x0065, 0x0066, 0x0067, 0x0068,
2760 0x0069, 0x006a, 0x006b, 0x006c
2761 };
2762 int i;
2763
françois romieu4da19632011-01-03 15:07:55 +00002764 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002765
2766 val &= 0xff00;
2767 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002768 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002769 }
2770 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002771 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002772 { 0x1f, 0x0002 },
2773 { 0x05, 0x2642 },
2774 { 0x1f, 0x0005 },
2775 { 0x05, 0x8330 },
2776 { 0x06, 0x2642 }
2777 };
2778
françois romieu4da19632011-01-03 15:07:55 +00002779 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002780 }
2781
françois romieubca03d52011-01-03 15:07:31 +00002782 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002783 rtl_writephy(tp, 0x1f, 0x0002);
2784 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2785 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002786
françois romieubca03d52011-01-03 15:07:31 +00002787 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002788 rtl_writephy(tp, 0x1f, 0x0002);
2789 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002790
françois romieu4da19632011-01-03 15:07:55 +00002791 rtl_writephy(tp, 0x1f, 0x0005);
2792 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002793
2794 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002795
françois romieu4da19632011-01-03 15:07:55 +00002796 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002797}
2798
françois romieu4da19632011-01-03 15:07:55 +00002799static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002800{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002801 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002802 { 0x1f, 0x0002 },
2803 { 0x10, 0x0008 },
2804 { 0x0d, 0x006c },
2805
2806 { 0x1f, 0x0000 },
2807 { 0x0d, 0xf880 },
2808
2809 { 0x1f, 0x0001 },
2810 { 0x17, 0x0cc0 },
2811
2812 { 0x1f, 0x0001 },
2813 { 0x0b, 0xa4d8 },
2814 { 0x09, 0x281c },
2815 { 0x07, 0x2883 },
2816 { 0x0a, 0x6b35 },
2817 { 0x1d, 0x3da4 },
2818 { 0x1c, 0xeffd },
2819 { 0x14, 0x7f52 },
2820 { 0x18, 0x7fc6 },
2821 { 0x08, 0x0601 },
2822 { 0x06, 0x4063 },
2823 { 0x10, 0xf074 },
2824 { 0x1f, 0x0003 },
2825 { 0x13, 0x0789 },
2826 { 0x12, 0xf4bd },
2827 { 0x1a, 0x04fd },
2828 { 0x14, 0x84b0 },
2829 { 0x1f, 0x0000 },
2830 { 0x00, 0x9200 },
2831
2832 { 0x1f, 0x0005 },
2833 { 0x01, 0x0340 },
2834 { 0x1f, 0x0001 },
2835 { 0x04, 0x4000 },
2836 { 0x03, 0x1d21 },
2837 { 0x02, 0x0c32 },
2838 { 0x01, 0x0200 },
2839 { 0x00, 0x5554 },
2840 { 0x04, 0x4800 },
2841 { 0x04, 0x4000 },
2842 { 0x04, 0xf000 },
2843 { 0x03, 0xdf01 },
2844 { 0x02, 0xdf20 },
2845 { 0x01, 0x101a },
2846 { 0x00, 0xa0ff },
2847 { 0x04, 0xf800 },
2848 { 0x04, 0xf000 },
2849 { 0x1f, 0x0000 },
2850
2851 { 0x1f, 0x0007 },
2852 { 0x1e, 0x0023 },
2853 { 0x16, 0x0000 },
2854 { 0x1f, 0x0000 }
2855 };
2856
françois romieu4da19632011-01-03 15:07:55 +00002857 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002858}
2859
françois romieue6de30d2011-01-03 15:08:37 +00002860static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2861{
2862 static const struct phy_reg phy_reg_init[] = {
2863 { 0x1f, 0x0001 },
2864 { 0x17, 0x0cc0 },
2865
2866 { 0x1f, 0x0007 },
2867 { 0x1e, 0x002d },
2868 { 0x18, 0x0040 },
2869 { 0x1f, 0x0000 }
2870 };
2871
2872 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2873 rtl_patchphy(tp, 0x0d, 1 << 5);
2874}
2875
Hayes Wang70090422011-07-06 15:58:06 +08002876static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002877{
2878 static const struct phy_reg phy_reg_init[] = {
2879 /* Enable Delay cap */
2880 { 0x1f, 0x0005 },
2881 { 0x05, 0x8b80 },
2882 { 0x06, 0xc896 },
2883 { 0x1f, 0x0000 },
2884
2885 /* Channel estimation fine tune */
2886 { 0x1f, 0x0001 },
2887 { 0x0b, 0x6c20 },
2888 { 0x07, 0x2872 },
2889 { 0x1c, 0xefff },
2890 { 0x1f, 0x0003 },
2891 { 0x14, 0x6420 },
2892 { 0x1f, 0x0000 },
2893
2894 /* Update PFM & 10M TX idle timer */
2895 { 0x1f, 0x0007 },
2896 { 0x1e, 0x002f },
2897 { 0x15, 0x1919 },
2898 { 0x1f, 0x0000 },
2899
2900 { 0x1f, 0x0007 },
2901 { 0x1e, 0x00ac },
2902 { 0x18, 0x0006 },
2903 { 0x1f, 0x0000 }
2904 };
2905
Francois Romieu15ecd032011-04-27 13:52:22 -07002906 rtl_apply_firmware(tp);
2907
hayeswang01dc7fe2011-03-21 01:50:28 +00002908 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2909
2910 /* DCO enable for 10M IDLE Power */
2911 rtl_writephy(tp, 0x1f, 0x0007);
2912 rtl_writephy(tp, 0x1e, 0x0023);
2913 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2914 rtl_writephy(tp, 0x1f, 0x0000);
2915
2916 /* For impedance matching */
2917 rtl_writephy(tp, 0x1f, 0x0002);
2918 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002919 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002920
2921 /* PHY auto speed down */
2922 rtl_writephy(tp, 0x1f, 0x0007);
2923 rtl_writephy(tp, 0x1e, 0x002d);
2924 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2925 rtl_writephy(tp, 0x1f, 0x0000);
2926 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2927
2928 rtl_writephy(tp, 0x1f, 0x0005);
2929 rtl_writephy(tp, 0x05, 0x8b86);
2930 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2931 rtl_writephy(tp, 0x1f, 0x0000);
2932
2933 rtl_writephy(tp, 0x1f, 0x0005);
2934 rtl_writephy(tp, 0x05, 0x8b85);
2935 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2936 rtl_writephy(tp, 0x1f, 0x0007);
2937 rtl_writephy(tp, 0x1e, 0x0020);
2938 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2939 rtl_writephy(tp, 0x1f, 0x0006);
2940 rtl_writephy(tp, 0x00, 0x5a00);
2941 rtl_writephy(tp, 0x1f, 0x0000);
2942 rtl_writephy(tp, 0x0d, 0x0007);
2943 rtl_writephy(tp, 0x0e, 0x003c);
2944 rtl_writephy(tp, 0x0d, 0x4007);
2945 rtl_writephy(tp, 0x0e, 0x0000);
2946 rtl_writephy(tp, 0x0d, 0x0000);
2947}
2948
Hayes Wang70090422011-07-06 15:58:06 +08002949static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2950{
2951 static const struct phy_reg phy_reg_init[] = {
2952 /* Enable Delay cap */
2953 { 0x1f, 0x0004 },
2954 { 0x1f, 0x0007 },
2955 { 0x1e, 0x00ac },
2956 { 0x18, 0x0006 },
2957 { 0x1f, 0x0002 },
2958 { 0x1f, 0x0000 },
2959 { 0x1f, 0x0000 },
2960
2961 /* Channel estimation fine tune */
2962 { 0x1f, 0x0003 },
2963 { 0x09, 0xa20f },
2964 { 0x1f, 0x0000 },
2965 { 0x1f, 0x0000 },
2966
2967 /* Green Setting */
2968 { 0x1f, 0x0005 },
2969 { 0x05, 0x8b5b },
2970 { 0x06, 0x9222 },
2971 { 0x05, 0x8b6d },
2972 { 0x06, 0x8000 },
2973 { 0x05, 0x8b76 },
2974 { 0x06, 0x8000 },
2975 { 0x1f, 0x0000 }
2976 };
2977
2978 rtl_apply_firmware(tp);
2979
2980 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2981
2982 /* For 4-corner performance improve */
2983 rtl_writephy(tp, 0x1f, 0x0005);
2984 rtl_writephy(tp, 0x05, 0x8b80);
2985 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2986 rtl_writephy(tp, 0x1f, 0x0000);
2987
2988 /* PHY auto speed down */
2989 rtl_writephy(tp, 0x1f, 0x0004);
2990 rtl_writephy(tp, 0x1f, 0x0007);
2991 rtl_writephy(tp, 0x1e, 0x002d);
2992 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2993 rtl_writephy(tp, 0x1f, 0x0002);
2994 rtl_writephy(tp, 0x1f, 0x0000);
2995 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2996
2997 /* improve 10M EEE waveform */
2998 rtl_writephy(tp, 0x1f, 0x0005);
2999 rtl_writephy(tp, 0x05, 0x8b86);
3000 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3001 rtl_writephy(tp, 0x1f, 0x0000);
3002
3003 /* Improve 2-pair detection performance */
3004 rtl_writephy(tp, 0x1f, 0x0005);
3005 rtl_writephy(tp, 0x05, 0x8b85);
3006 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3007 rtl_writephy(tp, 0x1f, 0x0000);
3008
3009 /* EEE setting */
3010 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
3011 ERIAR_EXGMAC);
3012 rtl_writephy(tp, 0x1f, 0x0005);
3013 rtl_writephy(tp, 0x05, 0x8b85);
3014 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3015 rtl_writephy(tp, 0x1f, 0x0004);
3016 rtl_writephy(tp, 0x1f, 0x0007);
3017 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04003018 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003019 rtl_writephy(tp, 0x1f, 0x0002);
3020 rtl_writephy(tp, 0x1f, 0x0000);
3021 rtl_writephy(tp, 0x0d, 0x0007);
3022 rtl_writephy(tp, 0x0e, 0x003c);
3023 rtl_writephy(tp, 0x0d, 0x4007);
3024 rtl_writephy(tp, 0x0e, 0x0000);
3025 rtl_writephy(tp, 0x0d, 0x0000);
3026
3027 /* Green feature */
3028 rtl_writephy(tp, 0x1f, 0x0003);
3029 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3030 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3031 rtl_writephy(tp, 0x1f, 0x0000);
3032}
3033
Hayes Wangc2218922011-09-06 16:55:18 +08003034static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3035{
3036 static const struct phy_reg phy_reg_init[] = {
3037 /* Channel estimation fine tune */
3038 { 0x1f, 0x0003 },
3039 { 0x09, 0xa20f },
3040 { 0x1f, 0x0000 },
3041
3042 /* Modify green table for giga & fnet */
3043 { 0x1f, 0x0005 },
3044 { 0x05, 0x8b55 },
3045 { 0x06, 0x0000 },
3046 { 0x05, 0x8b5e },
3047 { 0x06, 0x0000 },
3048 { 0x05, 0x8b67 },
3049 { 0x06, 0x0000 },
3050 { 0x05, 0x8b70 },
3051 { 0x06, 0x0000 },
3052 { 0x1f, 0x0000 },
3053 { 0x1f, 0x0007 },
3054 { 0x1e, 0x0078 },
3055 { 0x17, 0x0000 },
3056 { 0x19, 0x00fb },
3057 { 0x1f, 0x0000 },
3058
3059 /* Modify green table for 10M */
3060 { 0x1f, 0x0005 },
3061 { 0x05, 0x8b79 },
3062 { 0x06, 0xaa00 },
3063 { 0x1f, 0x0000 },
3064
3065 /* Disable hiimpedance detection (RTCT) */
3066 { 0x1f, 0x0003 },
3067 { 0x01, 0x328a },
3068 { 0x1f, 0x0000 }
3069 };
3070
3071 rtl_apply_firmware(tp);
3072
3073 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3074
3075 /* For 4-corner performance improve */
3076 rtl_writephy(tp, 0x1f, 0x0005);
3077 rtl_writephy(tp, 0x05, 0x8b80);
3078 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3079 rtl_writephy(tp, 0x1f, 0x0000);
3080
3081 /* PHY auto speed down */
3082 rtl_writephy(tp, 0x1f, 0x0007);
3083 rtl_writephy(tp, 0x1e, 0x002d);
3084 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3085 rtl_writephy(tp, 0x1f, 0x0000);
3086 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3087
3088 /* Improve 10M EEE waveform */
3089 rtl_writephy(tp, 0x1f, 0x0005);
3090 rtl_writephy(tp, 0x05, 0x8b86);
3091 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3092 rtl_writephy(tp, 0x1f, 0x0000);
3093
3094 /* Improve 2-pair detection performance */
3095 rtl_writephy(tp, 0x1f, 0x0005);
3096 rtl_writephy(tp, 0x05, 0x8b85);
3097 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3098 rtl_writephy(tp, 0x1f, 0x0000);
3099}
3100
3101static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3102{
3103 rtl_apply_firmware(tp);
3104
3105 /* For 4-corner performance improve */
3106 rtl_writephy(tp, 0x1f, 0x0005);
3107 rtl_writephy(tp, 0x05, 0x8b80);
3108 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3109 rtl_writephy(tp, 0x1f, 0x0000);
3110
3111 /* PHY auto speed down */
3112 rtl_writephy(tp, 0x1f, 0x0007);
3113 rtl_writephy(tp, 0x1e, 0x002d);
3114 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3115 rtl_writephy(tp, 0x1f, 0x0000);
3116 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3117
3118 /* Improve 10M EEE waveform */
3119 rtl_writephy(tp, 0x1f, 0x0005);
3120 rtl_writephy(tp, 0x05, 0x8b86);
3121 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3122 rtl_writephy(tp, 0x1f, 0x0000);
3123}
3124
françois romieu4da19632011-01-03 15:07:55 +00003125static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003126{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003127 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003128 { 0x1f, 0x0003 },
3129 { 0x08, 0x441d },
3130 { 0x01, 0x9100 },
3131 { 0x1f, 0x0000 }
3132 };
3133
françois romieu4da19632011-01-03 15:07:55 +00003134 rtl_writephy(tp, 0x1f, 0x0000);
3135 rtl_patchphy(tp, 0x11, 1 << 12);
3136 rtl_patchphy(tp, 0x19, 1 << 13);
3137 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003138
françois romieu4da19632011-01-03 15:07:55 +00003139 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003140}
3141
Hayes Wang5a5e4442011-02-22 17:26:21 +08003142static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3143{
3144 static const struct phy_reg phy_reg_init[] = {
3145 { 0x1f, 0x0005 },
3146 { 0x1a, 0x0000 },
3147 { 0x1f, 0x0000 },
3148
3149 { 0x1f, 0x0004 },
3150 { 0x1c, 0x0000 },
3151 { 0x1f, 0x0000 },
3152
3153 { 0x1f, 0x0001 },
3154 { 0x15, 0x7701 },
3155 { 0x1f, 0x0000 }
3156 };
3157
3158 /* Disable ALDPS before ram code */
3159 rtl_writephy(tp, 0x1f, 0x0000);
3160 rtl_writephy(tp, 0x18, 0x0310);
3161 msleep(100);
3162
François Romieu953a12c2011-04-24 17:38:48 +02003163 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003164
3165 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3166}
3167
Francois Romieu5615d9f2007-08-17 17:50:46 +02003168static void rtl_hw_phy_config(struct net_device *dev)
3169{
3170 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003171
3172 rtl8169_print_mac_version(tp);
3173
3174 switch (tp->mac_version) {
3175 case RTL_GIGA_MAC_VER_01:
3176 break;
3177 case RTL_GIGA_MAC_VER_02:
3178 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003179 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003180 break;
3181 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003182 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003183 break;
françois romieu2e9558562009-08-10 19:44:19 +00003184 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003185 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003186 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003187 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003188 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003189 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003190 case RTL_GIGA_MAC_VER_07:
3191 case RTL_GIGA_MAC_VER_08:
3192 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003193 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003194 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003195 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003196 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003197 break;
3198 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003199 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003200 break;
3201 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003202 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003203 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003204 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003205 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003206 break;
3207 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003208 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003209 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003210 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003211 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003212 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003213 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003214 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003215 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003216 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003217 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003218 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003219 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003220 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003221 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003222 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003223 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003224 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003225 break;
3226 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003227 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003228 break;
3229 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003230 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003231 break;
françois romieue6de30d2011-01-03 15:08:37 +00003232 case RTL_GIGA_MAC_VER_28:
3233 rtl8168d_4_hw_phy_config(tp);
3234 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003235 case RTL_GIGA_MAC_VER_29:
3236 case RTL_GIGA_MAC_VER_30:
3237 rtl8105e_hw_phy_config(tp);
3238 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003239 case RTL_GIGA_MAC_VER_31:
3240 /* None. */
3241 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003242 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003243 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003244 rtl8168e_1_hw_phy_config(tp);
3245 break;
3246 case RTL_GIGA_MAC_VER_34:
3247 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003248 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003249 case RTL_GIGA_MAC_VER_35:
3250 rtl8168f_1_hw_phy_config(tp);
3251 break;
3252 case RTL_GIGA_MAC_VER_36:
3253 rtl8168f_2_hw_phy_config(tp);
3254 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003255
Francois Romieu5615d9f2007-08-17 17:50:46 +02003256 default:
3257 break;
3258 }
3259}
3260
Francois Romieuda78dbf2012-01-26 14:18:23 +01003261static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 struct timer_list *timer = &tp->timer;
3264 void __iomem *ioaddr = tp->mmio_addr;
3265 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3266
Francois Romieubcf0bf92006-07-26 23:14:13 +02003267 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
françois romieu4da19632011-01-03 15:07:55 +00003269 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003270 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 * A busy loop could burn quite a few cycles on nowadays CPU.
3272 * Let's delay the execution of the timer for a few ticks.
3273 */
3274 timeout = HZ/10;
3275 goto out_mod_timer;
3276 }
3277
3278 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01003279 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
Francois Romieuda78dbf2012-01-26 14:18:23 +01003281 netif_warn(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
françois romieu4da19632011-01-03 15:07:55 +00003283 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
3285out_mod_timer:
3286 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003287}
3288
3289static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3290{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003291 if (!test_and_set_bit(flag, tp->wk.flags))
3292 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003293}
3294
3295static void rtl8169_phy_timer(unsigned long __opaque)
3296{
3297 struct net_device *dev = (struct net_device *)__opaque;
3298 struct rtl8169_private *tp = netdev_priv(dev);
3299
Francois Romieu98ddf982012-01-31 10:47:34 +01003300 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301}
3302
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3304 void __iomem *ioaddr)
3305{
3306 iounmap(ioaddr);
3307 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003308 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 pci_disable_device(pdev);
3310 free_netdev(dev);
3311}
3312
Francois Romieubf793292006-11-01 00:53:05 +01003313static void rtl8169_phy_reset(struct net_device *dev,
3314 struct rtl8169_private *tp)
3315{
Francois Romieu07d3f512007-02-21 22:40:46 +01003316 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003317
françois romieu4da19632011-01-03 15:07:55 +00003318 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003319 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003320 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003321 return;
3322 msleep(1);
3323 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003324 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003325}
3326
David S. Miller8decf862011-09-22 03:23:13 -04003327static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3328{
3329 void __iomem *ioaddr = tp->mmio_addr;
3330
3331 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3332 (RTL_R8(PHYstatus) & TBI_Enable);
3333}
3334
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003335static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003337 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003338
Francois Romieu5615d9f2007-08-17 17:50:46 +02003339 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003340
Marcus Sundberg773328942008-07-10 21:28:08 +02003341 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3342 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3343 RTL_W8(0x82, 0x01);
3344 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003345
Francois Romieu6dccd162007-02-13 23:38:05 +01003346 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3347
3348 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3349 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003350
Francois Romieubcf0bf92006-07-26 23:14:13 +02003351 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003352 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3353 RTL_W8(0x82, 0x01);
3354 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003355 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003356 }
3357
Francois Romieubf793292006-11-01 00:53:05 +01003358 rtl8169_phy_reset(dev, tp);
3359
Oliver Neukum54405cd2011-01-06 21:55:13 +01003360 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003361 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3362 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3363 (tp->mii.supports_gmii ?
3364 ADVERTISED_1000baseT_Half |
3365 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003366
David S. Miller8decf862011-09-22 03:23:13 -04003367 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003368 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003369}
3370
Francois Romieu773d2022007-01-31 23:47:43 +01003371static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3372{
3373 void __iomem *ioaddr = tp->mmio_addr;
3374 u32 high;
3375 u32 low;
3376
3377 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3378 high = addr[4] | (addr[5] << 8);
3379
Francois Romieuda78dbf2012-01-26 14:18:23 +01003380 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003381
3382 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003383
Francois Romieu773d2022007-01-31 23:47:43 +01003384 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00003385 RTL_R32(MAC4);
3386
Francois Romieu78f1cd02010-03-27 19:35:46 -07003387 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00003388 RTL_R32(MAC0);
3389
françois romieuc28aa382011-08-02 03:53:43 +00003390 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3391 const struct exgmac_reg e[] = {
3392 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3393 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3394 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3395 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3396 low >> 16 },
3397 };
3398
3399 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3400 }
3401
Francois Romieu773d2022007-01-31 23:47:43 +01003402 RTL_W8(Cfg9346, Cfg9346_Lock);
3403
Francois Romieuda78dbf2012-01-26 14:18:23 +01003404 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003405}
3406
3407static int rtl_set_mac_address(struct net_device *dev, void *p)
3408{
3409 struct rtl8169_private *tp = netdev_priv(dev);
3410 struct sockaddr *addr = p;
3411
3412 if (!is_valid_ether_addr(addr->sa_data))
3413 return -EADDRNOTAVAIL;
3414
3415 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3416
3417 rtl_rar_set(tp, dev->dev_addr);
3418
3419 return 0;
3420}
3421
Francois Romieu5f787a12006-08-17 13:02:36 +02003422static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3423{
3424 struct rtl8169_private *tp = netdev_priv(dev);
3425 struct mii_ioctl_data *data = if_mii(ifr);
3426
Francois Romieu8b4ab282008-11-19 22:05:25 -08003427 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3428}
Francois Romieu5f787a12006-08-17 13:02:36 +02003429
Francois Romieucecb5fd2011-04-01 10:21:07 +02003430static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3431 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003432{
Francois Romieu5f787a12006-08-17 13:02:36 +02003433 switch (cmd) {
3434 case SIOCGMIIPHY:
3435 data->phy_id = 32; /* Internal PHY */
3436 return 0;
3437
3438 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003439 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003440 return 0;
3441
3442 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003443 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003444 return 0;
3445 }
3446 return -EOPNOTSUPP;
3447}
3448
Francois Romieu8b4ab282008-11-19 22:05:25 -08003449static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3450{
3451 return -EOPNOTSUPP;
3452}
3453
Francois Romieufbac58f2007-10-04 22:51:38 +02003454static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3455{
3456 if (tp->features & RTL_FEATURE_MSI) {
3457 pci_disable_msi(pdev);
3458 tp->features &= ~RTL_FEATURE_MSI;
3459 }
3460}
3461
françois romieuc0e45c12011-01-03 15:08:04 +00003462static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3463{
3464 struct mdio_ops *ops = &tp->mdio_ops;
3465
3466 switch (tp->mac_version) {
3467 case RTL_GIGA_MAC_VER_27:
3468 ops->write = r8168dp_1_mdio_write;
3469 ops->read = r8168dp_1_mdio_read;
3470 break;
françois romieue6de30d2011-01-03 15:08:37 +00003471 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003472 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003473 ops->write = r8168dp_2_mdio_write;
3474 ops->read = r8168dp_2_mdio_read;
3475 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003476 default:
3477 ops->write = r8169_mdio_write;
3478 ops->read = r8169_mdio_read;
3479 break;
3480 }
3481}
3482
David S. Miller1805b2f2011-10-24 18:18:09 -04003483static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3484{
3485 void __iomem *ioaddr = tp->mmio_addr;
3486
3487 switch (tp->mac_version) {
3488 case RTL_GIGA_MAC_VER_29:
3489 case RTL_GIGA_MAC_VER_30:
3490 case RTL_GIGA_MAC_VER_32:
3491 case RTL_GIGA_MAC_VER_33:
3492 case RTL_GIGA_MAC_VER_34:
3493 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3494 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3495 break;
3496 default:
3497 break;
3498 }
3499}
3500
3501static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3502{
3503 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3504 return false;
3505
3506 rtl_writephy(tp, 0x1f, 0x0000);
3507 rtl_writephy(tp, MII_BMCR, 0x0000);
3508
3509 rtl_wol_suspend_quirk(tp);
3510
3511 return true;
3512}
3513
françois romieu065c27c2011-01-03 15:08:12 +00003514static void r810x_phy_power_down(struct rtl8169_private *tp)
3515{
3516 rtl_writephy(tp, 0x1f, 0x0000);
3517 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3518}
3519
3520static void r810x_phy_power_up(struct rtl8169_private *tp)
3521{
3522 rtl_writephy(tp, 0x1f, 0x0000);
3523 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3524}
3525
3526static void r810x_pll_power_down(struct rtl8169_private *tp)
3527{
David S. Miller1805b2f2011-10-24 18:18:09 -04003528 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003529 return;
françois romieu065c27c2011-01-03 15:08:12 +00003530
3531 r810x_phy_power_down(tp);
3532}
3533
3534static void r810x_pll_power_up(struct rtl8169_private *tp)
3535{
3536 r810x_phy_power_up(tp);
3537}
3538
3539static void r8168_phy_power_up(struct rtl8169_private *tp)
3540{
3541 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003542 switch (tp->mac_version) {
3543 case RTL_GIGA_MAC_VER_11:
3544 case RTL_GIGA_MAC_VER_12:
3545 case RTL_GIGA_MAC_VER_17:
3546 case RTL_GIGA_MAC_VER_18:
3547 case RTL_GIGA_MAC_VER_19:
3548 case RTL_GIGA_MAC_VER_20:
3549 case RTL_GIGA_MAC_VER_21:
3550 case RTL_GIGA_MAC_VER_22:
3551 case RTL_GIGA_MAC_VER_23:
3552 case RTL_GIGA_MAC_VER_24:
3553 case RTL_GIGA_MAC_VER_25:
3554 case RTL_GIGA_MAC_VER_26:
3555 case RTL_GIGA_MAC_VER_27:
3556 case RTL_GIGA_MAC_VER_28:
3557 case RTL_GIGA_MAC_VER_31:
3558 rtl_writephy(tp, 0x0e, 0x0000);
3559 break;
3560 default:
3561 break;
3562 }
françois romieu065c27c2011-01-03 15:08:12 +00003563 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3564}
3565
3566static void r8168_phy_power_down(struct rtl8169_private *tp)
3567{
3568 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003569 switch (tp->mac_version) {
3570 case RTL_GIGA_MAC_VER_32:
3571 case RTL_GIGA_MAC_VER_33:
3572 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3573 break;
3574
3575 case RTL_GIGA_MAC_VER_11:
3576 case RTL_GIGA_MAC_VER_12:
3577 case RTL_GIGA_MAC_VER_17:
3578 case RTL_GIGA_MAC_VER_18:
3579 case RTL_GIGA_MAC_VER_19:
3580 case RTL_GIGA_MAC_VER_20:
3581 case RTL_GIGA_MAC_VER_21:
3582 case RTL_GIGA_MAC_VER_22:
3583 case RTL_GIGA_MAC_VER_23:
3584 case RTL_GIGA_MAC_VER_24:
3585 case RTL_GIGA_MAC_VER_25:
3586 case RTL_GIGA_MAC_VER_26:
3587 case RTL_GIGA_MAC_VER_27:
3588 case RTL_GIGA_MAC_VER_28:
3589 case RTL_GIGA_MAC_VER_31:
3590 rtl_writephy(tp, 0x0e, 0x0200);
3591 default:
3592 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3593 break;
3594 }
françois romieu065c27c2011-01-03 15:08:12 +00003595}
3596
3597static void r8168_pll_power_down(struct rtl8169_private *tp)
3598{
3599 void __iomem *ioaddr = tp->mmio_addr;
3600
Francois Romieucecb5fd2011-04-01 10:21:07 +02003601 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3602 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3603 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003604 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003605 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003606 }
françois romieu065c27c2011-01-03 15:08:12 +00003607
Francois Romieucecb5fd2011-04-01 10:21:07 +02003608 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3609 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003610 (RTL_R16(CPlusCmd) & ASF)) {
3611 return;
3612 }
3613
hayeswang01dc7fe2011-03-21 01:50:28 +00003614 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3615 tp->mac_version == RTL_GIGA_MAC_VER_33)
3616 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3617
David S. Miller1805b2f2011-10-24 18:18:09 -04003618 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003619 return;
françois romieu065c27c2011-01-03 15:08:12 +00003620
3621 r8168_phy_power_down(tp);
3622
3623 switch (tp->mac_version) {
3624 case RTL_GIGA_MAC_VER_25:
3625 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003626 case RTL_GIGA_MAC_VER_27:
3627 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003628 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003629 case RTL_GIGA_MAC_VER_32:
3630 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003631 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3632 break;
3633 }
3634}
3635
3636static void r8168_pll_power_up(struct rtl8169_private *tp)
3637{
3638 void __iomem *ioaddr = tp->mmio_addr;
3639
Francois Romieucecb5fd2011-04-01 10:21:07 +02003640 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3641 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3642 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003643 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003644 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003645 }
françois romieu065c27c2011-01-03 15:08:12 +00003646
3647 switch (tp->mac_version) {
3648 case RTL_GIGA_MAC_VER_25:
3649 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003650 case RTL_GIGA_MAC_VER_27:
3651 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003652 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003653 case RTL_GIGA_MAC_VER_32:
3654 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003655 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3656 break;
3657 }
3658
3659 r8168_phy_power_up(tp);
3660}
3661
Francois Romieud58d46b2011-05-03 16:38:29 +02003662static void rtl_generic_op(struct rtl8169_private *tp,
3663 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00003664{
3665 if (op)
3666 op(tp);
3667}
3668
3669static void rtl_pll_power_down(struct rtl8169_private *tp)
3670{
Francois Romieud58d46b2011-05-03 16:38:29 +02003671 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00003672}
3673
3674static void rtl_pll_power_up(struct rtl8169_private *tp)
3675{
Francois Romieud58d46b2011-05-03 16:38:29 +02003676 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00003677}
3678
3679static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3680{
3681 struct pll_power_ops *ops = &tp->pll_power_ops;
3682
3683 switch (tp->mac_version) {
3684 case RTL_GIGA_MAC_VER_07:
3685 case RTL_GIGA_MAC_VER_08:
3686 case RTL_GIGA_MAC_VER_09:
3687 case RTL_GIGA_MAC_VER_10:
3688 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003689 case RTL_GIGA_MAC_VER_29:
3690 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003691 ops->down = r810x_pll_power_down;
3692 ops->up = r810x_pll_power_up;
3693 break;
3694
3695 case RTL_GIGA_MAC_VER_11:
3696 case RTL_GIGA_MAC_VER_12:
3697 case RTL_GIGA_MAC_VER_17:
3698 case RTL_GIGA_MAC_VER_18:
3699 case RTL_GIGA_MAC_VER_19:
3700 case RTL_GIGA_MAC_VER_20:
3701 case RTL_GIGA_MAC_VER_21:
3702 case RTL_GIGA_MAC_VER_22:
3703 case RTL_GIGA_MAC_VER_23:
3704 case RTL_GIGA_MAC_VER_24:
3705 case RTL_GIGA_MAC_VER_25:
3706 case RTL_GIGA_MAC_VER_26:
3707 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003708 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003709 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003710 case RTL_GIGA_MAC_VER_32:
3711 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003712 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08003713 case RTL_GIGA_MAC_VER_35:
3714 case RTL_GIGA_MAC_VER_36:
françois romieu065c27c2011-01-03 15:08:12 +00003715 ops->down = r8168_pll_power_down;
3716 ops->up = r8168_pll_power_up;
3717 break;
3718
3719 default:
3720 ops->down = NULL;
3721 ops->up = NULL;
3722 break;
3723 }
3724}
3725
Hayes Wange542a222011-07-06 15:58:04 +08003726static void rtl_init_rxcfg(struct rtl8169_private *tp)
3727{
3728 void __iomem *ioaddr = tp->mmio_addr;
3729
3730 switch (tp->mac_version) {
3731 case RTL_GIGA_MAC_VER_01:
3732 case RTL_GIGA_MAC_VER_02:
3733 case RTL_GIGA_MAC_VER_03:
3734 case RTL_GIGA_MAC_VER_04:
3735 case RTL_GIGA_MAC_VER_05:
3736 case RTL_GIGA_MAC_VER_06:
3737 case RTL_GIGA_MAC_VER_10:
3738 case RTL_GIGA_MAC_VER_11:
3739 case RTL_GIGA_MAC_VER_12:
3740 case RTL_GIGA_MAC_VER_13:
3741 case RTL_GIGA_MAC_VER_14:
3742 case RTL_GIGA_MAC_VER_15:
3743 case RTL_GIGA_MAC_VER_16:
3744 case RTL_GIGA_MAC_VER_17:
3745 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3746 break;
3747 case RTL_GIGA_MAC_VER_18:
3748 case RTL_GIGA_MAC_VER_19:
3749 case RTL_GIGA_MAC_VER_20:
3750 case RTL_GIGA_MAC_VER_21:
3751 case RTL_GIGA_MAC_VER_22:
3752 case RTL_GIGA_MAC_VER_23:
3753 case RTL_GIGA_MAC_VER_24:
3754 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3755 break;
3756 default:
3757 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3758 break;
3759 }
3760}
3761
Hayes Wang92fc43b2011-07-06 15:58:03 +08003762static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3763{
3764 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3765}
3766
Francois Romieud58d46b2011-05-03 16:38:29 +02003767static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3768{
françois romieu9c5028e2012-03-02 04:43:14 +00003769 void __iomem *ioaddr = tp->mmio_addr;
3770
3771 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003772 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00003773 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003774}
3775
3776static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3777{
françois romieu9c5028e2012-03-02 04:43:14 +00003778 void __iomem *ioaddr = tp->mmio_addr;
3779
3780 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003781 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00003782 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003783}
3784
3785static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3786{
3787 void __iomem *ioaddr = tp->mmio_addr;
3788
3789 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3790 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3791 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3792}
3793
3794static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3795{
3796 void __iomem *ioaddr = tp->mmio_addr;
3797
3798 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3799 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3800 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3801}
3802
3803static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3804{
3805 void __iomem *ioaddr = tp->mmio_addr;
3806
3807 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3808}
3809
3810static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3811{
3812 void __iomem *ioaddr = tp->mmio_addr;
3813
3814 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3815}
3816
3817static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3818{
3819 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003820
3821 RTL_W8(MaxTxPacketSize, 0x3f);
3822 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3823 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003824 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003825}
3826
3827static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3828{
3829 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003830
3831 RTL_W8(MaxTxPacketSize, 0x0c);
3832 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3833 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003834 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003835}
3836
3837static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3838{
3839 rtl_tx_performance_tweak(tp->pci_dev,
3840 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3841}
3842
3843static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3844{
3845 rtl_tx_performance_tweak(tp->pci_dev,
3846 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3847}
3848
3849static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3850{
3851 void __iomem *ioaddr = tp->mmio_addr;
3852
3853 r8168b_0_hw_jumbo_enable(tp);
3854
3855 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
3856}
3857
3858static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3859{
3860 void __iomem *ioaddr = tp->mmio_addr;
3861
3862 r8168b_0_hw_jumbo_disable(tp);
3863
3864 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3865}
3866
3867static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
3868{
3869 struct jumbo_ops *ops = &tp->jumbo_ops;
3870
3871 switch (tp->mac_version) {
3872 case RTL_GIGA_MAC_VER_11:
3873 ops->disable = r8168b_0_hw_jumbo_disable;
3874 ops->enable = r8168b_0_hw_jumbo_enable;
3875 break;
3876 case RTL_GIGA_MAC_VER_12:
3877 case RTL_GIGA_MAC_VER_17:
3878 ops->disable = r8168b_1_hw_jumbo_disable;
3879 ops->enable = r8168b_1_hw_jumbo_enable;
3880 break;
3881 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
3882 case RTL_GIGA_MAC_VER_19:
3883 case RTL_GIGA_MAC_VER_20:
3884 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
3885 case RTL_GIGA_MAC_VER_22:
3886 case RTL_GIGA_MAC_VER_23:
3887 case RTL_GIGA_MAC_VER_24:
3888 case RTL_GIGA_MAC_VER_25:
3889 case RTL_GIGA_MAC_VER_26:
3890 ops->disable = r8168c_hw_jumbo_disable;
3891 ops->enable = r8168c_hw_jumbo_enable;
3892 break;
3893 case RTL_GIGA_MAC_VER_27:
3894 case RTL_GIGA_MAC_VER_28:
3895 ops->disable = r8168dp_hw_jumbo_disable;
3896 ops->enable = r8168dp_hw_jumbo_enable;
3897 break;
3898 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
3899 case RTL_GIGA_MAC_VER_32:
3900 case RTL_GIGA_MAC_VER_33:
3901 case RTL_GIGA_MAC_VER_34:
3902 ops->disable = r8168e_hw_jumbo_disable;
3903 ops->enable = r8168e_hw_jumbo_enable;
3904 break;
3905
3906 /*
3907 * No action needed for jumbo frames with 8169.
3908 * No jumbo for 810x at all.
3909 */
3910 default:
3911 ops->disable = NULL;
3912 ops->enable = NULL;
3913 break;
3914 }
3915}
3916
Francois Romieu6f43adc2011-04-29 15:05:51 +02003917static void rtl_hw_reset(struct rtl8169_private *tp)
3918{
3919 void __iomem *ioaddr = tp->mmio_addr;
3920 int i;
3921
3922 /* Soft reset the chip. */
3923 RTL_W8(ChipCmd, CmdReset);
3924
3925 /* Check that the chip has finished the reset. */
3926 for (i = 0; i < 100; i++) {
3927 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3928 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003929 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003930 }
3931}
3932
Francois Romieub6ffd972011-06-17 17:00:05 +02003933static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
3934{
3935 struct rtl_fw *rtl_fw;
3936 const char *name;
3937 int rc = -ENOMEM;
3938
3939 name = rtl_lookup_firmware_name(tp);
3940 if (!name)
3941 goto out_no_firmware;
3942
3943 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
3944 if (!rtl_fw)
3945 goto err_warn;
3946
3947 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
3948 if (rc < 0)
3949 goto err_free;
3950
Francois Romieufd112f22011-06-18 00:10:29 +02003951 rc = rtl_check_firmware(tp, rtl_fw);
3952 if (rc < 0)
3953 goto err_release_firmware;
3954
Francois Romieub6ffd972011-06-17 17:00:05 +02003955 tp->rtl_fw = rtl_fw;
3956out:
3957 return;
3958
Francois Romieufd112f22011-06-18 00:10:29 +02003959err_release_firmware:
3960 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02003961err_free:
3962 kfree(rtl_fw);
3963err_warn:
3964 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
3965 name, rc);
3966out_no_firmware:
3967 tp->rtl_fw = NULL;
3968 goto out;
3969}
3970
François Romieu953a12c2011-04-24 17:38:48 +02003971static void rtl_request_firmware(struct rtl8169_private *tp)
3972{
Francois Romieub6ffd972011-06-17 17:00:05 +02003973 if (IS_ERR(tp->rtl_fw))
3974 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02003975}
3976
Hayes Wang92fc43b2011-07-06 15:58:03 +08003977static void rtl_rx_close(struct rtl8169_private *tp)
3978{
3979 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003980
Francois Romieu1687b562011-07-19 17:21:29 +02003981 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08003982}
3983
françois romieue6de30d2011-01-03 15:08:37 +00003984static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985{
françois romieue6de30d2011-01-03 15:08:37 +00003986 void __iomem *ioaddr = tp->mmio_addr;
3987
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00003989 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990
Hayes Wang92fc43b2011-07-06 15:58:03 +08003991 rtl_rx_close(tp);
3992
Hayes Wang5d2e1952011-02-22 17:26:22 +08003993 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00003994 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3995 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00003996 while (RTL_R8(TxPoll) & NPQ)
3997 udelay(20);
Hayes Wangc2218922011-09-06 16:55:18 +08003998 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
3999 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
4000 tp->mac_version == RTL_GIGA_MAC_VER_36) {
David S. Miller8decf862011-09-22 03:23:13 -04004001 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08004002 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4003 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004004 } else {
4005 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4006 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004007 }
4008
Hayes Wang92fc43b2011-07-06 15:58:03 +08004009 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010}
4011
Francois Romieu7f796d832007-06-11 23:04:41 +02004012static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004013{
4014 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004015
4016 /* Set DMA burst size and Interframe Gap Time */
4017 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4018 (InterFrameGap << TxInterFrameGapShift));
4019}
4020
Francois Romieu07ce4062007-02-23 23:36:39 +01004021static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022{
4023 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024
Francois Romieu07ce4062007-02-23 23:36:39 +01004025 tp->hw_start(dev);
4026
Francois Romieuda78dbf2012-01-26 14:18:23 +01004027 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004028}
4029
Francois Romieu7f796d832007-06-11 23:04:41 +02004030static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4031 void __iomem *ioaddr)
4032{
4033 /*
4034 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4035 * register to be written before TxDescAddrLow to work.
4036 * Switching from MMIO to I/O access fixes the issue as well.
4037 */
4038 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004039 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004040 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004041 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004042}
4043
4044static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4045{
4046 u16 cmd;
4047
4048 cmd = RTL_R16(CPlusCmd);
4049 RTL_W16(CPlusCmd, cmd);
4050 return cmd;
4051}
4052
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004053static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004054{
4055 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00004056 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004057}
4058
Francois Romieu6dccd162007-02-13 23:38:05 +01004059static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4060{
Francois Romieu37441002011-06-17 22:58:54 +02004061 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004062 u32 mac_version;
4063 u32 clk;
4064 u32 val;
4065 } cfg2_info [] = {
4066 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4067 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4068 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4069 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004070 };
4071 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004072 unsigned int i;
4073 u32 clk;
4074
4075 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004076 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004077 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4078 RTL_W32(0x7c, p->val);
4079 break;
4080 }
4081 }
4082}
4083
Francois Romieue6b763e2012-03-08 09:35:39 +01004084static void rtl_set_rx_mode(struct net_device *dev)
4085{
4086 struct rtl8169_private *tp = netdev_priv(dev);
4087 void __iomem *ioaddr = tp->mmio_addr;
4088 u32 mc_filter[2]; /* Multicast hash filter */
4089 int rx_mode;
4090 u32 tmp = 0;
4091
4092 if (dev->flags & IFF_PROMISC) {
4093 /* Unconditionally log net taps. */
4094 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4095 rx_mode =
4096 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4097 AcceptAllPhys;
4098 mc_filter[1] = mc_filter[0] = 0xffffffff;
4099 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4100 (dev->flags & IFF_ALLMULTI)) {
4101 /* Too many to filter perfectly -- accept all multicasts. */
4102 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4103 mc_filter[1] = mc_filter[0] = 0xffffffff;
4104 } else {
4105 struct netdev_hw_addr *ha;
4106
4107 rx_mode = AcceptBroadcast | AcceptMyPhys;
4108 mc_filter[1] = mc_filter[0] = 0;
4109 netdev_for_each_mc_addr(ha, dev) {
4110 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4111 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4112 rx_mode |= AcceptMulticast;
4113 }
4114 }
4115
4116 if (dev->features & NETIF_F_RXALL)
4117 rx_mode |= (AcceptErr | AcceptRunt);
4118
4119 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4120
4121 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4122 u32 data = mc_filter[0];
4123
4124 mc_filter[0] = swab32(mc_filter[1]);
4125 mc_filter[1] = swab32(data);
4126 }
4127
4128 RTL_W32(MAR0 + 4, mc_filter[1]);
4129 RTL_W32(MAR0 + 0, mc_filter[0]);
4130
4131 RTL_W32(RxConfig, tmp);
4132}
4133
Francois Romieu07ce4062007-02-23 23:36:39 +01004134static void rtl_hw_start_8169(struct net_device *dev)
4135{
4136 struct rtl8169_private *tp = netdev_priv(dev);
4137 void __iomem *ioaddr = tp->mmio_addr;
4138 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004139
Francois Romieu9cb427b2006-11-02 00:10:16 +01004140 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4141 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4142 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4143 }
4144
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004146 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4147 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4148 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4149 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004150 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4151
Hayes Wange542a222011-07-06 15:58:04 +08004152 rtl_init_rxcfg(tp);
4153
françois romieuf0298f82011-01-03 15:07:42 +00004154 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004156 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157
Francois Romieucecb5fd2011-04-01 10:21:07 +02004158 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4159 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4160 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4161 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004162 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
Francois Romieu7f796d832007-06-11 23:04:41 +02004164 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004165
Francois Romieucecb5fd2011-04-01 10:21:07 +02004166 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4167 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004168 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004170 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 }
4172
Francois Romieubcf0bf92006-07-26 23:14:13 +02004173 RTL_W16(CPlusCmd, tp->cp_cmd);
4174
Francois Romieu6dccd162007-02-13 23:38:05 +01004175 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4176
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 /*
4178 * Undocumented corner. Supposedly:
4179 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4180 */
4181 RTL_W16(IntrMitigate, 0x0000);
4182
Francois Romieu7f796d832007-06-11 23:04:41 +02004183 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004184
Francois Romieucecb5fd2011-04-01 10:21:07 +02004185 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4186 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4187 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4188 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004189 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4190 rtl_set_rx_tx_config_registers(tp);
4191 }
4192
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004194
4195 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4196 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197
4198 RTL_W32(RxMissed, 0);
4199
Francois Romieu07ce4062007-02-23 23:36:39 +01004200 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201
4202 /* no early-rx interrupts */
4203 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004204}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205
françois romieu650e8d52011-01-03 15:08:29 +00004206static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004207{
4208 u32 csi;
4209
4210 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00004211 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4212}
4213
françois romieue6de30d2011-01-03 15:08:37 +00004214static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4215{
4216 rtl_csi_access_enable(ioaddr, 0x17000000);
4217}
4218
françois romieu650e8d52011-01-03 15:08:29 +00004219static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4220{
4221 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02004222}
4223
4224struct ephy_info {
4225 unsigned int offset;
4226 u16 mask;
4227 u16 bits;
4228};
4229
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004230static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004231{
4232 u16 w;
4233
4234 while (len-- > 0) {
4235 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4236 rtl_ephy_write(ioaddr, e->offset, w);
4237 e++;
4238 }
4239}
4240
Francois Romieub726e492008-06-28 12:22:59 +02004241static void rtl_disable_clock_request(struct pci_dev *pdev)
4242{
Jon Masone44daad2011-06-27 07:46:31 +00004243 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004244
4245 if (cap) {
4246 u16 ctl;
4247
4248 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4249 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4250 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4251 }
4252}
4253
françois romieue6de30d2011-01-03 15:08:37 +00004254static void rtl_enable_clock_request(struct pci_dev *pdev)
4255{
Jon Masone44daad2011-06-27 07:46:31 +00004256 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004257
4258 if (cap) {
4259 u16 ctl;
4260
4261 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4262 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4263 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4264 }
4265}
4266
Francois Romieub726e492008-06-28 12:22:59 +02004267#define R8168_CPCMD_QUIRK_MASK (\
4268 EnableBist | \
4269 Mac_dbgo_oe | \
4270 Force_half_dup | \
4271 Force_rxflow_en | \
4272 Force_txflow_en | \
4273 Cxpl_dbg_sel | \
4274 ASF | \
4275 PktCntrDisable | \
4276 Mac_dbgo_sel)
4277
Francois Romieu219a1e92008-06-28 11:58:39 +02004278static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4279{
Francois Romieub726e492008-06-28 12:22:59 +02004280 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4281
4282 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4283
Francois Romieu2e68ae42008-06-28 12:00:55 +02004284 rtl_tx_performance_tweak(pdev,
4285 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004286}
4287
4288static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4289{
4290 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004291
françois romieuf0298f82011-01-03 15:07:42 +00004292 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004293
4294 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004295}
4296
4297static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4298{
Francois Romieub726e492008-06-28 12:22:59 +02004299 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4300
4301 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4302
Francois Romieu219a1e92008-06-28 11:58:39 +02004303 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004304
4305 rtl_disable_clock_request(pdev);
4306
4307 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004308}
4309
Francois Romieuef3386f2008-06-29 12:24:30 +02004310static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004311{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004312 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004313 { 0x01, 0, 0x0001 },
4314 { 0x02, 0x0800, 0x1000 },
4315 { 0x03, 0, 0x0042 },
4316 { 0x06, 0x0080, 0x0000 },
4317 { 0x07, 0, 0x2000 }
4318 };
4319
françois romieu650e8d52011-01-03 15:08:29 +00004320 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004321
4322 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4323
Francois Romieu219a1e92008-06-28 11:58:39 +02004324 __rtl_hw_start_8168cp(ioaddr, pdev);
4325}
4326
Francois Romieuef3386f2008-06-29 12:24:30 +02004327static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4328{
françois romieu650e8d52011-01-03 15:08:29 +00004329 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004330
4331 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4332
4333 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4334
4335 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4336}
4337
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004338static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4339{
françois romieu650e8d52011-01-03 15:08:29 +00004340 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004341
4342 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4343
4344 /* Magic. */
4345 RTL_W8(DBG_REG, 0x20);
4346
françois romieuf0298f82011-01-03 15:07:42 +00004347 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004348
4349 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4350
4351 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4352}
4353
Francois Romieu219a1e92008-06-28 11:58:39 +02004354static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4355{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004356 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004357 { 0x02, 0x0800, 0x1000 },
4358 { 0x03, 0, 0x0002 },
4359 { 0x06, 0x0080, 0x0000 }
4360 };
4361
françois romieu650e8d52011-01-03 15:08:29 +00004362 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004363
4364 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4365
4366 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4367
Francois Romieu219a1e92008-06-28 11:58:39 +02004368 __rtl_hw_start_8168cp(ioaddr, pdev);
4369}
4370
4371static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4372{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004373 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004374 { 0x01, 0, 0x0001 },
4375 { 0x03, 0x0400, 0x0220 }
4376 };
4377
françois romieu650e8d52011-01-03 15:08:29 +00004378 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004379
4380 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4381
Francois Romieu219a1e92008-06-28 11:58:39 +02004382 __rtl_hw_start_8168cp(ioaddr, pdev);
4383}
4384
Francois Romieu197ff762008-06-28 13:16:02 +02004385static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4386{
4387 rtl_hw_start_8168c_2(ioaddr, pdev);
4388}
4389
Francois Romieu6fb07052008-06-29 11:54:28 +02004390static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4391{
françois romieu650e8d52011-01-03 15:08:29 +00004392 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004393
4394 __rtl_hw_start_8168cp(ioaddr, pdev);
4395}
4396
Francois Romieu5b538df2008-07-20 16:22:45 +02004397static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4398{
françois romieu650e8d52011-01-03 15:08:29 +00004399 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004400
4401 rtl_disable_clock_request(pdev);
4402
françois romieuf0298f82011-01-03 15:07:42 +00004403 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004404
4405 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4406
4407 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4408}
4409
hayeswang4804b3b2011-03-21 01:50:29 +00004410static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4411{
4412 rtl_csi_access_enable_1(ioaddr);
4413
4414 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4415
4416 RTL_W8(MaxTxPacketSize, TxPacketMax);
4417
4418 rtl_disable_clock_request(pdev);
4419}
4420
françois romieue6de30d2011-01-03 15:08:37 +00004421static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4422{
4423 static const struct ephy_info e_info_8168d_4[] = {
4424 { 0x0b, ~0, 0x48 },
4425 { 0x19, 0x20, 0x50 },
4426 { 0x0c, ~0, 0x20 }
4427 };
4428 int i;
4429
4430 rtl_csi_access_enable_1(ioaddr);
4431
4432 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4433
4434 RTL_W8(MaxTxPacketSize, TxPacketMax);
4435
4436 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4437 const struct ephy_info *e = e_info_8168d_4 + i;
4438 u16 w;
4439
4440 w = rtl_ephy_read(ioaddr, e->offset);
4441 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4442 }
4443
4444 rtl_enable_clock_request(pdev);
4445}
4446
Hayes Wang70090422011-07-06 15:58:06 +08004447static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
hayeswang01dc7fe2011-03-21 01:50:28 +00004448{
Hayes Wang70090422011-07-06 15:58:06 +08004449 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004450 { 0x00, 0x0200, 0x0100 },
4451 { 0x00, 0x0000, 0x0004 },
4452 { 0x06, 0x0002, 0x0001 },
4453 { 0x06, 0x0000, 0x0030 },
4454 { 0x07, 0x0000, 0x2000 },
4455 { 0x00, 0x0000, 0x0020 },
4456 { 0x03, 0x5800, 0x2000 },
4457 { 0x03, 0x0000, 0x0001 },
4458 { 0x01, 0x0800, 0x1000 },
4459 { 0x07, 0x0000, 0x4000 },
4460 { 0x1e, 0x0000, 0x2000 },
4461 { 0x19, 0xffff, 0xfe6c },
4462 { 0x0a, 0x0000, 0x0040 }
4463 };
4464
4465 rtl_csi_access_enable_2(ioaddr);
4466
Hayes Wang70090422011-07-06 15:58:06 +08004467 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004468
4469 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4470
4471 RTL_W8(MaxTxPacketSize, TxPacketMax);
4472
4473 rtl_disable_clock_request(pdev);
4474
4475 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004476 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4477 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004478
Francois Romieucecb5fd2011-04-01 10:21:07 +02004479 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004480}
4481
Hayes Wang70090422011-07-06 15:58:06 +08004482static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4483{
4484 static const struct ephy_info e_info_8168e_2[] = {
4485 { 0x09, 0x0000, 0x0080 },
4486 { 0x19, 0x0000, 0x0224 }
4487 };
4488
4489 rtl_csi_access_enable_1(ioaddr);
4490
4491 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4492
4493 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4494
4495 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4496 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4497 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4498 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4499 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4500 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4501 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4502 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4503 ERIAR_EXGMAC);
4504
Hayes Wang3090bd92011-09-06 16:55:15 +08004505 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004506
4507 rtl_disable_clock_request(pdev);
4508
4509 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4510 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4511
4512 /* Adjust EEE LED frequency */
4513 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4514
4515 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4516 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4517 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4518}
4519
Hayes Wangc2218922011-09-06 16:55:18 +08004520static void rtl_hw_start_8168f_1(void __iomem *ioaddr, struct pci_dev *pdev)
4521{
4522 static const struct ephy_info e_info_8168f_1[] = {
4523 { 0x06, 0x00c0, 0x0020 },
4524 { 0x08, 0x0001, 0x0002 },
4525 { 0x09, 0x0000, 0x0080 },
4526 { 0x19, 0x0000, 0x0224 }
4527 };
4528
4529 rtl_csi_access_enable_1(ioaddr);
4530
4531 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4532
4533 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4534
4535 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4536 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4537 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4538 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4539 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4540 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4541 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4542 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4543 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4544 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
4545 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4546 ERIAR_EXGMAC);
4547
4548 RTL_W8(MaxTxPacketSize, EarlySize);
4549
4550 rtl_disable_clock_request(pdev);
4551
4552 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4553 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4554
4555 /* Adjust EEE LED frequency */
4556 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4557
4558 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4559 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4560 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4561}
4562
Francois Romieu07ce4062007-02-23 23:36:39 +01004563static void rtl_hw_start_8168(struct net_device *dev)
4564{
Francois Romieu2dd99532007-06-11 23:22:52 +02004565 struct rtl8169_private *tp = netdev_priv(dev);
4566 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004567 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004568
4569 RTL_W8(Cfg9346, Cfg9346_Unlock);
4570
françois romieuf0298f82011-01-03 15:07:42 +00004571 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004572
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004573 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004574
Francois Romieu0e485152007-02-20 00:00:26 +01004575 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004576
4577 RTL_W16(CPlusCmd, tp->cp_cmd);
4578
Francois Romieu0e485152007-02-20 00:00:26 +01004579 RTL_W16(IntrMitigate, 0x5151);
4580
4581 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00004582 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01004583 tp->event_slow |= RxFIFOOver | PCSTimeout;
4584 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01004585 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004586
4587 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4588
Francois Romieub8363902008-06-01 12:31:57 +02004589 rtl_set_rx_mode(dev);
4590
4591 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4592 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004593
4594 RTL_R8(IntrMask);
4595
Francois Romieu219a1e92008-06-28 11:58:39 +02004596 switch (tp->mac_version) {
4597 case RTL_GIGA_MAC_VER_11:
4598 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004599 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004600
4601 case RTL_GIGA_MAC_VER_12:
4602 case RTL_GIGA_MAC_VER_17:
4603 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004604 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004605
4606 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004607 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004608 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004609
4610 case RTL_GIGA_MAC_VER_19:
4611 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004612 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004613
4614 case RTL_GIGA_MAC_VER_20:
4615 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004616 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004617
Francois Romieu197ff762008-06-28 13:16:02 +02004618 case RTL_GIGA_MAC_VER_21:
4619 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004620 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004621
Francois Romieu6fb07052008-06-29 11:54:28 +02004622 case RTL_GIGA_MAC_VER_22:
4623 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004624 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004625
Francois Romieuef3386f2008-06-29 12:24:30 +02004626 case RTL_GIGA_MAC_VER_23:
4627 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004628 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004629
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004630 case RTL_GIGA_MAC_VER_24:
4631 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004632 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004633
Francois Romieu5b538df2008-07-20 16:22:45 +02004634 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004635 case RTL_GIGA_MAC_VER_26:
4636 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004637 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004638 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004639
françois romieue6de30d2011-01-03 15:08:37 +00004640 case RTL_GIGA_MAC_VER_28:
4641 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004642 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004643
hayeswang4804b3b2011-03-21 01:50:29 +00004644 case RTL_GIGA_MAC_VER_31:
4645 rtl_hw_start_8168dp(ioaddr, pdev);
4646 break;
4647
hayeswang01dc7fe2011-03-21 01:50:28 +00004648 case RTL_GIGA_MAC_VER_32:
4649 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004650 rtl_hw_start_8168e_1(ioaddr, pdev);
4651 break;
4652 case RTL_GIGA_MAC_VER_34:
4653 rtl_hw_start_8168e_2(ioaddr, pdev);
hayeswang01dc7fe2011-03-21 01:50:28 +00004654 break;
françois romieue6de30d2011-01-03 15:08:37 +00004655
Hayes Wangc2218922011-09-06 16:55:18 +08004656 case RTL_GIGA_MAC_VER_35:
4657 case RTL_GIGA_MAC_VER_36:
4658 rtl_hw_start_8168f_1(ioaddr, pdev);
4659 break;
4660
Francois Romieu219a1e92008-06-28 11:58:39 +02004661 default:
4662 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4663 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004664 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004665 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004666
Francois Romieu0e485152007-02-20 00:00:26 +01004667 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4668
Francois Romieub8363902008-06-01 12:31:57 +02004669 RTL_W8(Cfg9346, Cfg9346_Lock);
4670
Francois Romieu2dd99532007-06-11 23:22:52 +02004671 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004672}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
Francois Romieu2857ffb2008-08-02 21:08:49 +02004674#define R810X_CPCMD_QUIRK_MASK (\
4675 EnableBist | \
4676 Mac_dbgo_oe | \
4677 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004678 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004679 Force_txflow_en | \
4680 Cxpl_dbg_sel | \
4681 ASF | \
4682 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004683 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004684
4685static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4686{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004687 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004688 { 0x01, 0, 0x6e65 },
4689 { 0x02, 0, 0x091f },
4690 { 0x03, 0, 0xc2f9 },
4691 { 0x06, 0, 0xafb5 },
4692 { 0x07, 0, 0x0e00 },
4693 { 0x19, 0, 0xec80 },
4694 { 0x01, 0, 0x2e65 },
4695 { 0x01, 0, 0x6e65 }
4696 };
4697 u8 cfg1;
4698
françois romieu650e8d52011-01-03 15:08:29 +00004699 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004700
4701 RTL_W8(DBG_REG, FIX_NAK_1);
4702
4703 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4704
4705 RTL_W8(Config1,
4706 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4707 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4708
4709 cfg1 = RTL_R8(Config1);
4710 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4711 RTL_W8(Config1, cfg1 & ~LEDS0);
4712
Francois Romieu2857ffb2008-08-02 21:08:49 +02004713 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4714}
4715
4716static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4717{
françois romieu650e8d52011-01-03 15:08:29 +00004718 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004719
4720 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4721
4722 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4723 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004724}
4725
4726static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4727{
4728 rtl_hw_start_8102e_2(ioaddr, pdev);
4729
4730 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4731}
4732
Hayes Wang5a5e4442011-02-22 17:26:21 +08004733static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4734{
4735 static const struct ephy_info e_info_8105e_1[] = {
4736 { 0x07, 0, 0x4000 },
4737 { 0x19, 0, 0x0200 },
4738 { 0x19, 0, 0x0020 },
4739 { 0x1e, 0, 0x2000 },
4740 { 0x03, 0, 0x0001 },
4741 { 0x19, 0, 0x0100 },
4742 { 0x19, 0, 0x0004 },
4743 { 0x0a, 0, 0x0020 }
4744 };
4745
Francois Romieucecb5fd2011-04-01 10:21:07 +02004746 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004747 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4748
Francois Romieucecb5fd2011-04-01 10:21:07 +02004749 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004750 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4751
4752 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e2011-07-06 15:58:02 +08004753 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004754
4755 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4756}
4757
4758static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4759{
4760 rtl_hw_start_8105e_1(ioaddr, pdev);
4761 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4762}
4763
Francois Romieu07ce4062007-02-23 23:36:39 +01004764static void rtl_hw_start_8101(struct net_device *dev)
4765{
Francois Romieucdf1a602007-06-11 23:29:50 +02004766 struct rtl8169_private *tp = netdev_priv(dev);
4767 void __iomem *ioaddr = tp->mmio_addr;
4768 struct pci_dev *pdev = tp->pci_dev;
4769
Francois Romieuda78dbf2012-01-26 14:18:23 +01004770 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
4771 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00004772
Francois Romieucecb5fd2011-04-01 10:21:07 +02004773 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4774 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00004775 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02004776
4777 if (cap) {
4778 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4779 PCI_EXP_DEVCTL_NOSNOOP_EN);
4780 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004781 }
4782
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004783 RTL_W8(Cfg9346, Cfg9346_Unlock);
4784
Francois Romieu2857ffb2008-08-02 21:08:49 +02004785 switch (tp->mac_version) {
4786 case RTL_GIGA_MAC_VER_07:
4787 rtl_hw_start_8102e_1(ioaddr, pdev);
4788 break;
4789
4790 case RTL_GIGA_MAC_VER_08:
4791 rtl_hw_start_8102e_3(ioaddr, pdev);
4792 break;
4793
4794 case RTL_GIGA_MAC_VER_09:
4795 rtl_hw_start_8102e_2(ioaddr, pdev);
4796 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004797
4798 case RTL_GIGA_MAC_VER_29:
4799 rtl_hw_start_8105e_1(ioaddr, pdev);
4800 break;
4801 case RTL_GIGA_MAC_VER_30:
4802 rtl_hw_start_8105e_2(ioaddr, pdev);
4803 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004804 }
4805
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004806 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004807
françois romieuf0298f82011-01-03 15:07:42 +00004808 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004809
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004810 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004811
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004812 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004813 RTL_W16(CPlusCmd, tp->cp_cmd);
4814
4815 RTL_W16(IntrMitigate, 0x0000);
4816
4817 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4818
4819 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4820 rtl_set_rx_tx_config_registers(tp);
4821
Francois Romieucdf1a602007-06-11 23:29:50 +02004822 RTL_R8(IntrMask);
4823
Francois Romieucdf1a602007-06-11 23:29:50 +02004824 rtl_set_rx_mode(dev);
4825
4826 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827}
4828
4829static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4830{
Francois Romieud58d46b2011-05-03 16:38:29 +02004831 struct rtl8169_private *tp = netdev_priv(dev);
4832
4833 if (new_mtu < ETH_ZLEN ||
4834 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 return -EINVAL;
4836
Francois Romieud58d46b2011-05-03 16:38:29 +02004837 if (new_mtu > ETH_DATA_LEN)
4838 rtl_hw_jumbo_enable(tp);
4839 else
4840 rtl_hw_jumbo_disable(tp);
4841
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004843 netdev_update_features(dev);
4844
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004845 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846}
4847
4848static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4849{
Al Viro95e09182007-12-22 18:55:39 +00004850 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4852}
4853
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004854static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4855 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004857 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004858 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004859
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004860 kfree(*data_buff);
4861 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 rtl8169_make_unusable_by_asic(desc);
4863}
4864
4865static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4866{
4867 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4868
4869 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4870}
4871
4872static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4873 u32 rx_buf_sz)
4874{
4875 desc->addr = cpu_to_le64(mapping);
4876 wmb();
4877 rtl8169_mark_to_asic(desc, rx_buf_sz);
4878}
4879
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004880static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004882 return (void *)ALIGN((long)data, 16);
4883}
4884
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004885static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4886 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004887{
4888 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004890 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004891 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004892 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004894 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4895 if (!data)
4896 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004897
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004898 if (rtl8169_align(data) != data) {
4899 kfree(data);
4900 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4901 if (!data)
4902 return NULL;
4903 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004904
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004905 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004906 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004907 if (unlikely(dma_mapping_error(d, mapping))) {
4908 if (net_ratelimit())
4909 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004910 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912
4913 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004914 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004915
4916err_out:
4917 kfree(data);
4918 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919}
4920
4921static void rtl8169_rx_clear(struct rtl8169_private *tp)
4922{
Francois Romieu07d3f512007-02-21 22:40:46 +01004923 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
4925 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004926 if (tp->Rx_databuff[i]) {
4927 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 tp->RxDescArray + i);
4929 }
4930 }
4931}
4932
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004933static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004935 desc->opts1 |= cpu_to_le32(RingEnd);
4936}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004937
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004938static int rtl8169_rx_fill(struct rtl8169_private *tp)
4939{
4940 unsigned int i;
4941
4942 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004943 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004944
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004945 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004947
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004948 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004949 if (!data) {
4950 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004951 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004952 }
4953 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004956 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4957 return 0;
4958
4959err_out:
4960 rtl8169_rx_clear(tp);
4961 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962}
4963
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964static int rtl8169_init_ring(struct net_device *dev)
4965{
4966 struct rtl8169_private *tp = netdev_priv(dev);
4967
4968 rtl8169_init_ring_indexes(tp);
4969
4970 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004971 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004973 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974}
4975
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004976static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 struct TxDesc *desc)
4978{
4979 unsigned int len = tx_skb->len;
4980
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004981 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4982
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 desc->opts1 = 0x00;
4984 desc->opts2 = 0x00;
4985 desc->addr = 0x00;
4986 tx_skb->len = 0;
4987}
4988
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004989static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4990 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991{
4992 unsigned int i;
4993
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004994 for (i = 0; i < n; i++) {
4995 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 struct ring_info *tx_skb = tp->tx_skb + entry;
4997 unsigned int len = tx_skb->len;
4998
4999 if (len) {
5000 struct sk_buff *skb = tx_skb->skb;
5001
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005002 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 tp->TxDescArray + entry);
5004 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005005 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 dev_kfree_skb(skb);
5007 tx_skb->skb = NULL;
5008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 }
5010 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005011}
5012
5013static void rtl8169_tx_clear(struct rtl8169_private *tp)
5014{
5015 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 tp->cur_tx = tp->dirty_tx = 0;
Igor Maravic036dafa2012-03-05 00:01:25 +01005017 netdev_reset_queue(tp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018}
5019
Francois Romieu4422bcd2012-01-26 11:23:32 +01005020static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021{
David Howellsc4028952006-11-22 14:57:56 +00005022 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005023 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024
Francois Romieuda78dbf2012-01-26 14:18:23 +01005025 napi_disable(&tp->napi);
5026 netif_stop_queue(dev);
5027 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028
françois romieuc7c2c392011-12-04 20:30:52 +00005029 rtl8169_hw_reset(tp);
5030
Francois Romieu56de4142011-03-15 17:29:31 +01005031 for (i = 0; i < NUM_RX_DESC; i++)
5032 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5033
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005035 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036
Francois Romieuda78dbf2012-01-26 14:18:23 +01005037 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01005038 rtl_hw_start(dev);
5039 netif_wake_queue(dev);
5040 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041}
5042
5043static void rtl8169_tx_timeout(struct net_device *dev)
5044{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005045 struct rtl8169_private *tp = netdev_priv(dev);
5046
5047 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048}
5049
5050static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005051 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052{
5053 struct skb_shared_info *info = skb_shinfo(skb);
5054 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005055 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005056 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057
5058 entry = tp->cur_tx;
5059 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005060 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 dma_addr_t mapping;
5062 u32 status, len;
5063 void *addr;
5064
5065 entry = (entry + 1) % NUM_TX_DESC;
5066
5067 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005068 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005069 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005070 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005071 if (unlikely(dma_mapping_error(d, mapping))) {
5072 if (net_ratelimit())
5073 netif_err(tp, drv, tp->dev,
5074 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005075 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077
Francois Romieucecb5fd2011-04-01 10:21:07 +02005078 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005079 status = opts[0] | len |
5080 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081
5082 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005083 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 txd->addr = cpu_to_le64(mapping);
5085
5086 tp->tx_skb[entry].len = len;
5087 }
5088
5089 if (cur_frag) {
5090 tp->tx_skb[entry].skb = skb;
5091 txd->opts1 |= cpu_to_le32(LastFrag);
5092 }
5093
5094 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005095
5096err_out:
5097 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5098 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099}
5100
Francois Romieu2b7b4312011-04-18 22:53:24 -07005101static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5102 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005104 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005105 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005106 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
Francois Romieu2b7b4312011-04-18 22:53:24 -07005108 if (mss) {
5109 opts[0] |= TD_LSO;
5110 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5111 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005112 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113
5114 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005115 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005117 opts[offset] |= info->checksum.udp;
5118 else
5119 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121}
5122
Stephen Hemminger613573252009-08-31 19:50:58 +00005123static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5124 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125{
5126 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005127 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 struct TxDesc *txd = tp->TxDescArray + entry;
5129 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005130 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 dma_addr_t mapping;
5132 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005133 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005134 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005135
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005137 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005138 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 }
5140
5141 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005142 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005144 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005145 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005146 if (unlikely(dma_mapping_error(d, mapping))) {
5147 if (net_ratelimit())
5148 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005149 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151
5152 tp->tx_skb[entry].len = len;
5153 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154
Francois Romieu2b7b4312011-04-18 22:53:24 -07005155 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5156 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005157
Francois Romieu2b7b4312011-04-18 22:53:24 -07005158 rtl8169_tso_csum(tp, skb, opts);
5159
5160 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005161 if (frags < 0)
5162 goto err_dma_1;
5163 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005164 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005165 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005166 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005167 tp->tx_skb[entry].skb = skb;
5168 }
5169
Francois Romieu2b7b4312011-04-18 22:53:24 -07005170 txd->opts2 = cpu_to_le32(opts[1]);
5171
Igor Maravic036dafa2012-03-05 00:01:25 +01005172 netdev_sent_queue(dev, skb->len);
5173
Richard Cochran5047fb52012-03-10 07:29:42 +00005174 skb_tx_timestamp(skb);
5175
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 wmb();
5177
Francois Romieucecb5fd2011-04-01 10:21:07 +02005178 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005179 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 txd->opts1 = cpu_to_le32(status);
5181
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 tp->cur_tx += frags + 1;
5183
David Dillow4c020a92010-03-03 16:33:10 +00005184 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185
Francois Romieucecb5fd2011-04-01 10:21:07 +02005186 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187
Francois Romieuda78dbf2012-01-26 14:18:23 +01005188 mmiowb();
5189
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01005191 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5192 * not miss a ring update when it notices a stopped queue.
5193 */
5194 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01005196 /* Sync with rtl_tx:
5197 * - publish queue status and cur_tx ring index (write barrier)
5198 * - refresh dirty_tx ring index (read barrier).
5199 * May the current thread have a pessimistic view of the ring
5200 * status and forget to wake up queue, a racing rtl_tx thread
5201 * can't.
5202 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005203 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
5205 netif_wake_queue(dev);
5206 }
5207
Stephen Hemminger613573252009-08-31 19:50:58 +00005208 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005210err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005211 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005212err_dma_0:
5213 dev_kfree_skb(skb);
5214 dev->stats.tx_dropped++;
5215 return NETDEV_TX_OK;
5216
5217err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005219 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005220 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221}
5222
5223static void rtl8169_pcierr_interrupt(struct net_device *dev)
5224{
5225 struct rtl8169_private *tp = netdev_priv(dev);
5226 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 u16 pci_status, pci_cmd;
5228
5229 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5230 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5231
Joe Perchesbf82c182010-02-09 11:49:50 +00005232 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5233 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234
5235 /*
5236 * The recovery sequence below admits a very elaborated explanation:
5237 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005238 * - I did not see what else could be done;
5239 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 *
5241 * Feel free to adjust to your needs.
5242 */
Francois Romieua27993f2006-12-18 00:04:19 +01005243 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005244 pci_cmd &= ~PCI_COMMAND_PARITY;
5245 else
5246 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5247
5248 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249
5250 pci_write_config_word(pdev, PCI_STATUS,
5251 pci_status & (PCI_STATUS_DETECTED_PARITY |
5252 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5253 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5254
5255 /* The infamous DAC f*ckup only happens at boot time */
5256 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005257 void __iomem *ioaddr = tp->mmio_addr;
5258
Joe Perchesbf82c182010-02-09 11:49:50 +00005259 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 tp->cp_cmd &= ~PCIDAC;
5261 RTL_W16(CPlusCmd, tp->cp_cmd);
5262 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 }
5264
françois romieue6de30d2011-01-03 15:08:37 +00005265 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005266
Francois Romieu98ddf982012-01-31 10:47:34 +01005267 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268}
5269
Igor Maravic036dafa2012-03-05 00:01:25 +01005270struct rtl_txc {
5271 int packets;
5272 int bytes;
5273};
5274
Francois Romieuda78dbf2012-01-26 14:18:23 +01005275static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276{
Igor Maravic036dafa2012-03-05 00:01:25 +01005277 struct rtl8169_stats *tx_stats = &tp->tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 unsigned int dirty_tx, tx_left;
Igor Maravic036dafa2012-03-05 00:01:25 +01005279 struct rtl_txc txc = { 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 dirty_tx = tp->dirty_tx;
5282 smp_rmb();
5283 tx_left = tp->cur_tx - dirty_tx;
5284
5285 while (tx_left > 0) {
5286 unsigned int entry = dirty_tx % NUM_TX_DESC;
5287 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 u32 status;
5289
5290 rmb();
5291 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5292 if (status & DescOwn)
5293 break;
5294
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005295 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5296 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 if (status & LastFrag) {
Igor Maravic036dafa2012-03-05 00:01:25 +01005298 struct sk_buff *skb = tx_skb->skb;
5299
5300 txc.packets++;
5301 txc.bytes += skb->len;
5302 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 tx_skb->skb = NULL;
5304 }
5305 dirty_tx++;
5306 tx_left--;
5307 }
5308
Igor Maravic036dafa2012-03-05 00:01:25 +01005309 u64_stats_update_begin(&tx_stats->syncp);
5310 tx_stats->packets += txc.packets;
5311 tx_stats->bytes += txc.bytes;
5312 u64_stats_update_end(&tx_stats->syncp);
5313
5314 netdev_completed_queue(dev, txc.packets, txc.bytes);
5315
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 if (tp->dirty_tx != dirty_tx) {
5317 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005318 /* Sync with rtl8169_start_xmit:
5319 * - publish dirty_tx ring index (write barrier)
5320 * - refresh cur_tx ring index and queue status (read barrier)
5321 * May the current thread miss the stopped queue condition,
5322 * a racing xmit thread can only have a right view of the
5323 * ring status.
5324 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005325 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 if (netif_queue_stopped(dev) &&
5327 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5328 netif_wake_queue(dev);
5329 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005330 /*
5331 * 8168 hack: TxPoll requests are lost when the Tx packets are
5332 * too close. Let's kick an extra TxPoll request when a burst
5333 * of start_xmit activity is detected (if it is not detected,
5334 * it is slow enough). -- FR
5335 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005336 if (tp->cur_tx != dirty_tx) {
5337 void __iomem *ioaddr = tp->mmio_addr;
5338
Francois Romieud78ae2d2007-08-26 20:08:19 +02005339 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 }
5342}
5343
Francois Romieu126fa4b2005-05-12 20:09:17 -04005344static inline int rtl8169_fragmented_frame(u32 status)
5345{
5346 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5347}
5348
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005349static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 u32 status = opts1 & RxProtoMask;
5352
5353 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005354 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 skb->ip_summed = CHECKSUM_UNNECESSARY;
5356 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005357 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358}
5359
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005360static struct sk_buff *rtl8169_try_rx_copy(void *data,
5361 struct rtl8169_private *tp,
5362 int pkt_size,
5363 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005365 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005366 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005368 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005369 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005370 prefetch(data);
5371 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5372 if (skb)
5373 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005374 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5375
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005376 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377}
5378
Francois Romieuda78dbf2012-01-26 14:18:23 +01005379static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380{
5381 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005382 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 cur_rx = tp->cur_rx;
5385 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005386 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005388 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005390 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 u32 status;
5392
5393 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005394 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395
5396 if (status & DescOwn)
5397 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005398 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005399 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5400 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005401 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005403 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005405 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005406 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005407 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005408 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005409 }
Ben Greear6bbe0212012-02-10 15:04:33 +00005410 if ((status & (RxRUNT | RxCRC)) &&
5411 !(status & (RxRWT | RxFOVF)) &&
5412 (dev->features & NETIF_F_RXALL))
5413 goto process_pkt;
5414
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005415 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005417 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00005418 dma_addr_t addr;
5419 int pkt_size;
5420
5421process_pkt:
5422 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00005423 if (likely(!(dev->features & NETIF_F_RXFCS)))
5424 pkt_size = (status & 0x00003fff) - 4;
5425 else
5426 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427
Francois Romieu126fa4b2005-05-12 20:09:17 -04005428 /*
5429 * The driver does not support incoming fragmented
5430 * frames. They are seen as a symptom of over-mtu
5431 * sized frames.
5432 */
5433 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005434 dev->stats.rx_dropped++;
5435 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005436 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005437 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005438 }
5439
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005440 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5441 tp, pkt_size, addr);
5442 rtl8169_mark_to_asic(desc, rx_buf_sz);
5443 if (!skb) {
5444 dev->stats.rx_dropped++;
5445 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 }
5447
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005448 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 skb_put(skb, pkt_size);
5450 skb->protocol = eth_type_trans(skb, dev);
5451
Francois Romieu7a8fc772011-03-01 17:18:33 +01005452 rtl8169_rx_vlan_tag(desc, skb);
5453
Francois Romieu56de4142011-03-15 17:29:31 +01005454 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455
Junchang Wang8027aa22012-03-04 23:30:32 +01005456 u64_stats_update_begin(&tp->rx_stats.syncp);
5457 tp->rx_stats.packets++;
5458 tp->rx_stats.bytes += pkt_size;
5459 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005461
5462 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005463 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005464 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5465 desc->opts2 = 0;
5466 cur_rx++;
5467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 }
5469
5470 count = cur_rx - tp->cur_rx;
5471 tp->cur_rx = cur_rx;
5472
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005473 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474
5475 return count;
5476}
5477
Francois Romieu07d3f512007-02-21 22:40:46 +01005478static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479{
Francois Romieu07d3f512007-02-21 22:40:46 +01005480 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005483 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005485 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005486 if (status && status != 0xffff) {
5487 status &= RTL_EVENT_NAPI | tp->event_slow;
5488 if (status) {
5489 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00005490
Francois Romieuda78dbf2012-01-26 14:18:23 +01005491 rtl_irq_disable(tp);
5492 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 return IRQ_RETVAL(handled);
5496}
5497
Francois Romieuda78dbf2012-01-26 14:18:23 +01005498/*
5499 * Workqueue context.
5500 */
5501static void rtl_slow_event_work(struct rtl8169_private *tp)
5502{
5503 struct net_device *dev = tp->dev;
5504 u16 status;
5505
5506 status = rtl_get_events(tp) & tp->event_slow;
5507 rtl_ack_events(tp, status);
5508
5509 if (unlikely(status & RxFIFOOver)) {
5510 switch (tp->mac_version) {
5511 /* Work around for rx fifo overflow */
5512 case RTL_GIGA_MAC_VER_11:
5513 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01005514 /* XXX - Hack alert. See rtl_task(). */
5515 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005516 default:
5517 break;
5518 }
5519 }
5520
5521 if (unlikely(status & SYSErr))
5522 rtl8169_pcierr_interrupt(dev);
5523
5524 if (status & LinkChg)
5525 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
5526
5527 napi_disable(&tp->napi);
5528 rtl_irq_disable(tp);
5529
5530 napi_enable(&tp->napi);
5531 napi_schedule(&tp->napi);
5532}
5533
Francois Romieu4422bcd2012-01-26 11:23:32 +01005534static void rtl_task(struct work_struct *work)
5535{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005536 static const struct {
5537 int bitnr;
5538 void (*action)(struct rtl8169_private *);
5539 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01005540 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005541 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
5542 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
5543 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
5544 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01005545 struct rtl8169_private *tp =
5546 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005547 struct net_device *dev = tp->dev;
5548 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01005549
Francois Romieuda78dbf2012-01-26 14:18:23 +01005550 rtl_lock_work(tp);
5551
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005552 if (!netif_running(dev) ||
5553 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01005554 goto out_unlock;
5555
5556 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
5557 bool pending;
5558
Francois Romieuda78dbf2012-01-26 14:18:23 +01005559 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005560 if (pending)
5561 rtl_work[i].action(tp);
5562 }
5563
5564out_unlock:
5565 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01005566}
5567
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005568static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005570 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5571 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005572 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
5573 int work_done= 0;
5574 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575
Francois Romieuda78dbf2012-01-26 14:18:23 +01005576 status = rtl_get_events(tp);
5577 rtl_ack_events(tp, status & ~tp->event_slow);
5578
5579 if (status & RTL_EVENT_NAPI_RX)
5580 work_done = rtl_rx(dev, tp, (u32) budget);
5581
5582 if (status & RTL_EVENT_NAPI_TX)
5583 rtl_tx(dev, tp);
5584
5585 if (status & tp->event_slow) {
5586 enable_mask &= ~tp->event_slow;
5587
5588 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
5589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005591 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005592 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005593
Francois Romieuda78dbf2012-01-26 14:18:23 +01005594 rtl_irq_enable(tp, enable_mask);
5595 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 }
5597
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005598 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600
Francois Romieu523a6092008-09-10 22:28:56 +02005601static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5602{
5603 struct rtl8169_private *tp = netdev_priv(dev);
5604
5605 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5606 return;
5607
5608 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5609 RTL_W32(RxMissed, 0);
5610}
5611
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612static void rtl8169_down(struct net_device *dev)
5613{
5614 struct rtl8169_private *tp = netdev_priv(dev);
5615 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616
Francois Romieu4876cc12011-03-11 21:07:11 +01005617 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005619 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005620 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621
Hayes Wang92fc43b2011-07-06 15:58:03 +08005622 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005623 /*
5624 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01005625 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
5626 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005627 */
Francois Romieu523a6092008-09-10 22:28:56 +02005628 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005631 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 rtl8169_tx_clear(tp);
5634
5635 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005636
5637 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638}
5639
5640static int rtl8169_close(struct net_device *dev)
5641{
5642 struct rtl8169_private *tp = netdev_priv(dev);
5643 struct pci_dev *pdev = tp->pci_dev;
5644
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005645 pm_runtime_get_sync(&pdev->dev);
5646
Francois Romieucecb5fd2011-04-01 10:21:07 +02005647 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005648 rtl8169_update_counters(dev);
5649
Francois Romieuda78dbf2012-01-26 14:18:23 +01005650 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005651 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005652
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005654 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655
Francois Romieu92a7c4e2012-03-10 10:42:12 +01005656 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005658 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5659 tp->RxPhyAddr);
5660 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5661 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 tp->TxDescArray = NULL;
5663 tp->RxDescArray = NULL;
5664
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005665 pm_runtime_put_sync(&pdev->dev);
5666
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 return 0;
5668}
5669
Francois Romieudc1c00c2012-03-08 10:06:18 +01005670#ifdef CONFIG_NET_POLL_CONTROLLER
5671static void rtl8169_netpoll(struct net_device *dev)
5672{
5673 struct rtl8169_private *tp = netdev_priv(dev);
5674
5675 rtl8169_interrupt(tp->pci_dev->irq, dev);
5676}
5677#endif
5678
Francois Romieudf43ac72012-03-08 09:48:40 +01005679static int rtl_open(struct net_device *dev)
5680{
5681 struct rtl8169_private *tp = netdev_priv(dev);
5682 void __iomem *ioaddr = tp->mmio_addr;
5683 struct pci_dev *pdev = tp->pci_dev;
5684 int retval = -ENOMEM;
5685
5686 pm_runtime_get_sync(&pdev->dev);
5687
5688 /*
5689 * Rx and Tx desscriptors needs 256 bytes alignment.
5690 * dma_alloc_coherent provides more.
5691 */
5692 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
5693 &tp->TxPhyAddr, GFP_KERNEL);
5694 if (!tp->TxDescArray)
5695 goto err_pm_runtime_put;
5696
5697 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
5698 &tp->RxPhyAddr, GFP_KERNEL);
5699 if (!tp->RxDescArray)
5700 goto err_free_tx_0;
5701
5702 retval = rtl8169_init_ring(dev);
5703 if (retval < 0)
5704 goto err_free_rx_1;
5705
5706 INIT_WORK(&tp->wk.work, rtl_task);
5707
5708 smp_mb();
5709
5710 rtl_request_firmware(tp);
5711
Francois Romieu92a7c4e2012-03-10 10:42:12 +01005712 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01005713 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
5714 dev->name, dev);
5715 if (retval < 0)
5716 goto err_release_fw_2;
5717
5718 rtl_lock_work(tp);
5719
5720 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
5721
5722 napi_enable(&tp->napi);
5723
5724 rtl8169_init_phy(dev, tp);
5725
5726 __rtl8169_set_features(dev, dev->features);
5727
5728 rtl_pll_power_up(tp);
5729
5730 rtl_hw_start(dev);
5731
5732 netif_start_queue(dev);
5733
5734 rtl_unlock_work(tp);
5735
5736 tp->saved_wolopts = 0;
5737 pm_runtime_put_noidle(&pdev->dev);
5738
5739 rtl8169_check_link_status(dev, tp, ioaddr);
5740out:
5741 return retval;
5742
5743err_release_fw_2:
5744 rtl_release_firmware(tp);
5745 rtl8169_rx_clear(tp);
5746err_free_rx_1:
5747 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5748 tp->RxPhyAddr);
5749 tp->RxDescArray = NULL;
5750err_free_tx_0:
5751 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5752 tp->TxPhyAddr);
5753 tp->TxDescArray = NULL;
5754err_pm_runtime_put:
5755 pm_runtime_put_noidle(&pdev->dev);
5756 goto out;
5757}
5758
Junchang Wang8027aa22012-03-04 23:30:32 +01005759static struct rtnl_link_stats64 *
5760rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761{
5762 struct rtl8169_private *tp = netdev_priv(dev);
5763 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01005764 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765
Francois Romieuda78dbf2012-01-26 14:18:23 +01005766 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02005767 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02005768
Junchang Wang8027aa22012-03-04 23:30:32 +01005769 do {
5770 start = u64_stats_fetch_begin_bh(&tp->rx_stats.syncp);
5771 stats->rx_packets = tp->rx_stats.packets;
5772 stats->rx_bytes = tp->rx_stats.bytes;
5773 } while (u64_stats_fetch_retry_bh(&tp->rx_stats.syncp, start));
5774
5775
5776 do {
5777 start = u64_stats_fetch_begin_bh(&tp->tx_stats.syncp);
5778 stats->tx_packets = tp->tx_stats.packets;
5779 stats->tx_bytes = tp->tx_stats.bytes;
5780 } while (u64_stats_fetch_retry_bh(&tp->tx_stats.syncp, start));
5781
5782 stats->rx_dropped = dev->stats.rx_dropped;
5783 stats->tx_dropped = dev->stats.tx_dropped;
5784 stats->rx_length_errors = dev->stats.rx_length_errors;
5785 stats->rx_errors = dev->stats.rx_errors;
5786 stats->rx_crc_errors = dev->stats.rx_crc_errors;
5787 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
5788 stats->rx_missed_errors = dev->stats.rx_missed_errors;
5789
5790 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791}
5792
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005793static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005794{
françois romieu065c27c2011-01-03 15:08:12 +00005795 struct rtl8169_private *tp = netdev_priv(dev);
5796
Francois Romieu5d06a992006-02-23 00:47:58 +01005797 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005798 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005799
5800 netif_device_detach(dev);
5801 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005802
5803 rtl_lock_work(tp);
5804 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005805 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005806 rtl_unlock_work(tp);
5807
5808 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005809}
Francois Romieu5d06a992006-02-23 00:47:58 +01005810
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005811#ifdef CONFIG_PM
5812
5813static int rtl8169_suspend(struct device *device)
5814{
5815 struct pci_dev *pdev = to_pci_dev(device);
5816 struct net_device *dev = pci_get_drvdata(pdev);
5817
5818 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005819
Francois Romieu5d06a992006-02-23 00:47:58 +01005820 return 0;
5821}
5822
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005823static void __rtl8169_resume(struct net_device *dev)
5824{
françois romieu065c27c2011-01-03 15:08:12 +00005825 struct rtl8169_private *tp = netdev_priv(dev);
5826
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005827 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005828
5829 rtl_pll_power_up(tp);
5830
Artem Savkovcff4c162012-04-03 10:29:11 +00005831 rtl_lock_work(tp);
5832 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005833 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00005834 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005835
Francois Romieu98ddf982012-01-31 10:47:34 +01005836 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005837}
5838
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005839static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005840{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005841 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005842 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005843 struct rtl8169_private *tp = netdev_priv(dev);
5844
5845 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005846
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005847 if (netif_running(dev))
5848 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005849
Francois Romieu5d06a992006-02-23 00:47:58 +01005850 return 0;
5851}
5852
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005853static int rtl8169_runtime_suspend(struct device *device)
5854{
5855 struct pci_dev *pdev = to_pci_dev(device);
5856 struct net_device *dev = pci_get_drvdata(pdev);
5857 struct rtl8169_private *tp = netdev_priv(dev);
5858
5859 if (!tp->TxDescArray)
5860 return 0;
5861
Francois Romieuda78dbf2012-01-26 14:18:23 +01005862 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005863 tp->saved_wolopts = __rtl8169_get_wol(tp);
5864 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005865 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005866
5867 rtl8169_net_suspend(dev);
5868
5869 return 0;
5870}
5871
5872static int rtl8169_runtime_resume(struct device *device)
5873{
5874 struct pci_dev *pdev = to_pci_dev(device);
5875 struct net_device *dev = pci_get_drvdata(pdev);
5876 struct rtl8169_private *tp = netdev_priv(dev);
5877
5878 if (!tp->TxDescArray)
5879 return 0;
5880
Francois Romieuda78dbf2012-01-26 14:18:23 +01005881 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005882 __rtl8169_set_wol(tp, tp->saved_wolopts);
5883 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005884 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005885
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005886 rtl8169_init_phy(dev, tp);
5887
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005888 __rtl8169_resume(dev);
5889
5890 return 0;
5891}
5892
5893static int rtl8169_runtime_idle(struct device *device)
5894{
5895 struct pci_dev *pdev = to_pci_dev(device);
5896 struct net_device *dev = pci_get_drvdata(pdev);
5897 struct rtl8169_private *tp = netdev_priv(dev);
5898
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005899 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005900}
5901
Alexey Dobriyan47145212009-12-14 18:00:08 -08005902static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005903 .suspend = rtl8169_suspend,
5904 .resume = rtl8169_resume,
5905 .freeze = rtl8169_suspend,
5906 .thaw = rtl8169_resume,
5907 .poweroff = rtl8169_suspend,
5908 .restore = rtl8169_resume,
5909 .runtime_suspend = rtl8169_runtime_suspend,
5910 .runtime_resume = rtl8169_runtime_resume,
5911 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005912};
5913
5914#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5915
5916#else /* !CONFIG_PM */
5917
5918#define RTL8169_PM_OPS NULL
5919
5920#endif /* !CONFIG_PM */
5921
David S. Miller1805b2f2011-10-24 18:18:09 -04005922static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
5923{
5924 void __iomem *ioaddr = tp->mmio_addr;
5925
5926 /* WoL fails with 8168b when the receiver is disabled. */
5927 switch (tp->mac_version) {
5928 case RTL_GIGA_MAC_VER_11:
5929 case RTL_GIGA_MAC_VER_12:
5930 case RTL_GIGA_MAC_VER_17:
5931 pci_clear_master(tp->pci_dev);
5932
5933 RTL_W8(ChipCmd, CmdRxEnb);
5934 /* PCI commit */
5935 RTL_R8(ChipCmd);
5936 break;
5937 default:
5938 break;
5939 }
5940}
5941
Francois Romieu1765f952008-09-13 17:21:40 +02005942static void rtl_shutdown(struct pci_dev *pdev)
5943{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005944 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005945 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00005946 struct device *d = &pdev->dev;
5947
5948 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02005949
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005950 rtl8169_net_suspend(dev);
5951
Francois Romieucecb5fd2011-04-01 10:21:07 +02005952 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08005953 rtl_rar_set(tp, dev->perm_addr);
5954
Hayes Wang92fc43b2011-07-06 15:58:03 +08005955 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00005956
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005957 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04005958 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
5959 rtl_wol_suspend_quirk(tp);
5960 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00005961 }
5962
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005963 pci_wake_from_d3(pdev, true);
5964 pci_set_power_state(pdev, PCI_D3hot);
5965 }
françois romieu2a15cd22012-03-06 01:14:12 +00005966
5967 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005968}
Francois Romieu5d06a992006-02-23 00:47:58 +01005969
Francois Romieue27566e2012-03-08 09:54:01 +01005970static void __devexit rtl_remove_one(struct pci_dev *pdev)
5971{
5972 struct net_device *dev = pci_get_drvdata(pdev);
5973 struct rtl8169_private *tp = netdev_priv(dev);
5974
5975 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
5976 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5977 tp->mac_version == RTL_GIGA_MAC_VER_31) {
5978 rtl8168_driver_stop(tp);
5979 }
5980
5981 cancel_work_sync(&tp->wk.work);
5982
5983 unregister_netdev(dev);
5984
5985 rtl_release_firmware(tp);
5986
5987 if (pci_dev_run_wake(pdev))
5988 pm_runtime_get_noresume(&pdev->dev);
5989
5990 /* restore original MAC address */
5991 rtl_rar_set(tp, dev->perm_addr);
5992
5993 rtl_disable_msi(pdev, tp);
5994 rtl8169_release_board(pdev, dev, tp->mmio_addr);
5995 pci_set_drvdata(pdev, NULL);
5996}
5997
Francois Romieufa9c3852012-03-08 10:01:50 +01005998static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01005999 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01006000 .ndo_stop = rtl8169_close,
6001 .ndo_get_stats64 = rtl8169_get_stats64,
6002 .ndo_start_xmit = rtl8169_start_xmit,
6003 .ndo_tx_timeout = rtl8169_tx_timeout,
6004 .ndo_validate_addr = eth_validate_addr,
6005 .ndo_change_mtu = rtl8169_change_mtu,
6006 .ndo_fix_features = rtl8169_fix_features,
6007 .ndo_set_features = rtl8169_set_features,
6008 .ndo_set_mac_address = rtl_set_mac_address,
6009 .ndo_do_ioctl = rtl8169_ioctl,
6010 .ndo_set_rx_mode = rtl_set_rx_mode,
6011#ifdef CONFIG_NET_POLL_CONTROLLER
6012 .ndo_poll_controller = rtl8169_netpoll,
6013#endif
6014
6015};
6016
Francois Romieu31fa8b12012-03-08 10:09:40 +01006017static const struct rtl_cfg_info {
6018 void (*hw_start)(struct net_device *);
6019 unsigned int region;
6020 unsigned int align;
6021 u16 event_slow;
6022 unsigned features;
6023 u8 default_ver;
6024} rtl_cfg_infos [] = {
6025 [RTL_CFG_0] = {
6026 .hw_start = rtl_hw_start_8169,
6027 .region = 1,
6028 .align = 0,
6029 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
6030 .features = RTL_FEATURE_GMII,
6031 .default_ver = RTL_GIGA_MAC_VER_01,
6032 },
6033 [RTL_CFG_1] = {
6034 .hw_start = rtl_hw_start_8168,
6035 .region = 2,
6036 .align = 8,
6037 .event_slow = SYSErr | LinkChg | RxOverflow,
6038 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
6039 .default_ver = RTL_GIGA_MAC_VER_11,
6040 },
6041 [RTL_CFG_2] = {
6042 .hw_start = rtl_hw_start_8101,
6043 .region = 2,
6044 .align = 8,
6045 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
6046 PCSTimeout,
6047 .features = RTL_FEATURE_MSI,
6048 .default_ver = RTL_GIGA_MAC_VER_13,
6049 }
6050};
6051
6052/* Cfg9346_Unlock assumed. */
6053static unsigned rtl_try_msi(struct rtl8169_private *tp,
6054 const struct rtl_cfg_info *cfg)
6055{
6056 void __iomem *ioaddr = tp->mmio_addr;
6057 unsigned msi = 0;
6058 u8 cfg2;
6059
6060 cfg2 = RTL_R8(Config2) & ~MSIEnable;
6061 if (cfg->features & RTL_FEATURE_MSI) {
6062 if (pci_enable_msi(tp->pci_dev)) {
6063 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
6064 } else {
6065 cfg2 |= MSIEnable;
6066 msi = RTL_FEATURE_MSI;
6067 }
6068 }
6069 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6070 RTL_W8(Config2, cfg2);
6071 return msi;
6072}
6073
Francois Romieu3b6cf252012-03-08 09:59:04 +01006074static int __devinit
6075rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6076{
6077 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
6078 const unsigned int region = cfg->region;
6079 struct rtl8169_private *tp;
6080 struct mii_if_info *mii;
6081 struct net_device *dev;
6082 void __iomem *ioaddr;
6083 int chipset, i;
6084 int rc;
6085
6086 if (netif_msg_drv(&debug)) {
6087 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
6088 MODULENAME, RTL8169_VERSION);
6089 }
6090
6091 dev = alloc_etherdev(sizeof (*tp));
6092 if (!dev) {
6093 rc = -ENOMEM;
6094 goto out;
6095 }
6096
6097 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01006098 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006099 tp = netdev_priv(dev);
6100 tp->dev = dev;
6101 tp->pci_dev = pdev;
6102 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
6103
6104 mii = &tp->mii;
6105 mii->dev = dev;
6106 mii->mdio_read = rtl_mdio_read;
6107 mii->mdio_write = rtl_mdio_write;
6108 mii->phy_id_mask = 0x1f;
6109 mii->reg_num_mask = 0x1f;
6110 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
6111
6112 /* disable ASPM completely as that cause random device stop working
6113 * problems as well as full system hangs for some PCIe devices users */
6114 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6115 PCIE_LINK_STATE_CLKPM);
6116
6117 /* enable device (incl. PCI PM wakeup and hotplug setup) */
6118 rc = pci_enable_device(pdev);
6119 if (rc < 0) {
6120 netif_err(tp, probe, dev, "enable failure\n");
6121 goto err_out_free_dev_1;
6122 }
6123
6124 if (pci_set_mwi(pdev) < 0)
6125 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
6126
6127 /* make sure PCI base addr 1 is MMIO */
6128 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
6129 netif_err(tp, probe, dev,
6130 "region #%d not an MMIO resource, aborting\n",
6131 region);
6132 rc = -ENODEV;
6133 goto err_out_mwi_2;
6134 }
6135
6136 /* check for weird/broken PCI region reporting */
6137 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
6138 netif_err(tp, probe, dev,
6139 "Invalid PCI region size(s), aborting\n");
6140 rc = -ENODEV;
6141 goto err_out_mwi_2;
6142 }
6143
6144 rc = pci_request_regions(pdev, MODULENAME);
6145 if (rc < 0) {
6146 netif_err(tp, probe, dev, "could not request regions\n");
6147 goto err_out_mwi_2;
6148 }
6149
6150 tp->cp_cmd = RxChkSum;
6151
6152 if ((sizeof(dma_addr_t) > 4) &&
6153 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
6154 tp->cp_cmd |= PCIDAC;
6155 dev->features |= NETIF_F_HIGHDMA;
6156 } else {
6157 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6158 if (rc < 0) {
6159 netif_err(tp, probe, dev, "DMA configuration failed\n");
6160 goto err_out_free_res_3;
6161 }
6162 }
6163
6164 /* ioremap MMIO region */
6165 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
6166 if (!ioaddr) {
6167 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
6168 rc = -EIO;
6169 goto err_out_free_res_3;
6170 }
6171 tp->mmio_addr = ioaddr;
6172
6173 if (!pci_is_pcie(pdev))
6174 netif_info(tp, probe, dev, "not PCI Express\n");
6175
6176 /* Identify chip attached to board */
6177 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
6178
6179 rtl_init_rxcfg(tp);
6180
6181 rtl_irq_disable(tp);
6182
6183 rtl_hw_reset(tp);
6184
6185 rtl_ack_events(tp, 0xffff);
6186
6187 pci_set_master(pdev);
6188
6189 /*
6190 * Pretend we are using VLANs; This bypasses a nasty bug where
6191 * Interrupts stop flowing on high load on 8110SCd controllers.
6192 */
6193 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6194 tp->cp_cmd |= RxVlan;
6195
6196 rtl_init_mdio_ops(tp);
6197 rtl_init_pll_power_ops(tp);
6198 rtl_init_jumbo_ops(tp);
6199
6200 rtl8169_print_mac_version(tp);
6201
6202 chipset = tp->mac_version;
6203 tp->txd_version = rtl_chip_infos[chipset].txd_version;
6204
6205 RTL_W8(Cfg9346, Cfg9346_Unlock);
6206 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
6207 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
6208 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
6209 tp->features |= RTL_FEATURE_WOL;
6210 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
6211 tp->features |= RTL_FEATURE_WOL;
6212 tp->features |= rtl_try_msi(tp, cfg);
6213 RTL_W8(Cfg9346, Cfg9346_Lock);
6214
6215 if (rtl_tbi_enabled(tp)) {
6216 tp->set_speed = rtl8169_set_speed_tbi;
6217 tp->get_settings = rtl8169_gset_tbi;
6218 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
6219 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
6220 tp->link_ok = rtl8169_tbi_link_ok;
6221 tp->do_ioctl = rtl_tbi_ioctl;
6222 } else {
6223 tp->set_speed = rtl8169_set_speed_xmii;
6224 tp->get_settings = rtl8169_gset_xmii;
6225 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
6226 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
6227 tp->link_ok = rtl8169_xmii_link_ok;
6228 tp->do_ioctl = rtl_xmii_ioctl;
6229 }
6230
6231 mutex_init(&tp->wk.mutex);
6232
6233 /* Get MAC address */
6234 for (i = 0; i < ETH_ALEN; i++)
6235 dev->dev_addr[i] = RTL_R8(MAC0 + i);
6236 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
6237
6238 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
6239 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006240
6241 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
6242
6243 /* don't enable SG, IP_CSUM and TSO by default - it might not work
6244 * properly for all devices */
6245 dev->features |= NETIF_F_RXCSUM |
6246 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6247
6248 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6249 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6250 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6251 NETIF_F_HIGHDMA;
6252
6253 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6254 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
6255 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
6256
6257 dev->hw_features |= NETIF_F_RXALL;
6258 dev->hw_features |= NETIF_F_RXFCS;
6259
6260 tp->hw_start = cfg->hw_start;
6261 tp->event_slow = cfg->event_slow;
6262
6263 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
6264 ~(RxBOVF | RxFOVF) : ~0;
6265
6266 init_timer(&tp->timer);
6267 tp->timer.data = (unsigned long) dev;
6268 tp->timer.function = rtl8169_phy_timer;
6269
6270 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
6271
6272 rc = register_netdev(dev);
6273 if (rc < 0)
6274 goto err_out_msi_4;
6275
6276 pci_set_drvdata(pdev, dev);
6277
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006278 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
6279 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
6280 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006281 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
6282 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
6283 "tx checksumming: %s]\n",
6284 rtl_chip_infos[chipset].jumbo_max,
6285 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
6286 }
6287
6288 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6289 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6290 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6291 rtl8168_driver_start(tp);
6292 }
6293
6294 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
6295
6296 if (pci_dev_run_wake(pdev))
6297 pm_runtime_put_noidle(&pdev->dev);
6298
6299 netif_carrier_off(dev);
6300
6301out:
6302 return rc;
6303
6304err_out_msi_4:
6305 rtl_disable_msi(pdev, tp);
6306 iounmap(ioaddr);
6307err_out_free_res_3:
6308 pci_release_regions(pdev);
6309err_out_mwi_2:
6310 pci_clear_mwi(pdev);
6311 pci_disable_device(pdev);
6312err_out_free_dev_1:
6313 free_netdev(dev);
6314 goto out;
6315}
6316
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317static struct pci_driver rtl8169_pci_driver = {
6318 .name = MODULENAME,
6319 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01006320 .probe = rtl_init_one,
Francois Romieue27566e2012-03-08 09:54:01 +01006321 .remove = __devexit_p(rtl_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02006322 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006323 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324};
6325
Francois Romieu07d3f512007-02-21 22:40:46 +01006326static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327{
Jeff Garzik29917622006-08-19 17:48:59 -04006328 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329}
6330
Francois Romieu07d3f512007-02-21 22:40:46 +01006331static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332{
6333 pci_unregister_driver(&rtl8169_pci_driver);
6334}
6335
6336module_init(rtl8169_init_module);
6337module_exit(rtl8169_cleanup_module);