blob: d9bae307c14458e34ca654c9b40b0861789b22ad [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 Wang4f6b00e52011-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 Wang4f6b00e52011-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 Wang4f6b00e52011-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 Wang4f6b00e52011-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 Wang4f6b00e52011-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 Wang4f6b00e52011-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 Wang4f6b00e52011-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{
Hayes Wang00042992012-03-30 14:33:00 +08003528 void __iomem *ioaddr = tp->mmio_addr;
3529
David S. Miller1805b2f2011-10-24 18:18:09 -04003530 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003531 return;
françois romieu065c27c2011-01-03 15:08:12 +00003532
3533 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003534
3535 switch (tp->mac_version) {
3536 case RTL_GIGA_MAC_VER_07:
3537 case RTL_GIGA_MAC_VER_08:
3538 case RTL_GIGA_MAC_VER_09:
3539 case RTL_GIGA_MAC_VER_10:
3540 case RTL_GIGA_MAC_VER_13:
3541 case RTL_GIGA_MAC_VER_16:
3542 break;
3543 default:
3544 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3545 break;
3546 }
françois romieu065c27c2011-01-03 15:08:12 +00003547}
3548
3549static void r810x_pll_power_up(struct rtl8169_private *tp)
3550{
Hayes Wang00042992012-03-30 14:33:00 +08003551 void __iomem *ioaddr = tp->mmio_addr;
3552
françois romieu065c27c2011-01-03 15:08:12 +00003553 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003554
3555 switch (tp->mac_version) {
3556 case RTL_GIGA_MAC_VER_07:
3557 case RTL_GIGA_MAC_VER_08:
3558 case RTL_GIGA_MAC_VER_09:
3559 case RTL_GIGA_MAC_VER_10:
3560 case RTL_GIGA_MAC_VER_13:
3561 case RTL_GIGA_MAC_VER_16:
3562 break;
3563 default:
3564 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3565 break;
3566 }
françois romieu065c27c2011-01-03 15:08:12 +00003567}
3568
3569static void r8168_phy_power_up(struct rtl8169_private *tp)
3570{
3571 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003572 switch (tp->mac_version) {
3573 case RTL_GIGA_MAC_VER_11:
3574 case RTL_GIGA_MAC_VER_12:
3575 case RTL_GIGA_MAC_VER_17:
3576 case RTL_GIGA_MAC_VER_18:
3577 case RTL_GIGA_MAC_VER_19:
3578 case RTL_GIGA_MAC_VER_20:
3579 case RTL_GIGA_MAC_VER_21:
3580 case RTL_GIGA_MAC_VER_22:
3581 case RTL_GIGA_MAC_VER_23:
3582 case RTL_GIGA_MAC_VER_24:
3583 case RTL_GIGA_MAC_VER_25:
3584 case RTL_GIGA_MAC_VER_26:
3585 case RTL_GIGA_MAC_VER_27:
3586 case RTL_GIGA_MAC_VER_28:
3587 case RTL_GIGA_MAC_VER_31:
3588 rtl_writephy(tp, 0x0e, 0x0000);
3589 break;
3590 default:
3591 break;
3592 }
françois romieu065c27c2011-01-03 15:08:12 +00003593 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3594}
3595
3596static void r8168_phy_power_down(struct rtl8169_private *tp)
3597{
3598 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003599 switch (tp->mac_version) {
3600 case RTL_GIGA_MAC_VER_32:
3601 case RTL_GIGA_MAC_VER_33:
3602 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3603 break;
3604
3605 case RTL_GIGA_MAC_VER_11:
3606 case RTL_GIGA_MAC_VER_12:
3607 case RTL_GIGA_MAC_VER_17:
3608 case RTL_GIGA_MAC_VER_18:
3609 case RTL_GIGA_MAC_VER_19:
3610 case RTL_GIGA_MAC_VER_20:
3611 case RTL_GIGA_MAC_VER_21:
3612 case RTL_GIGA_MAC_VER_22:
3613 case RTL_GIGA_MAC_VER_23:
3614 case RTL_GIGA_MAC_VER_24:
3615 case RTL_GIGA_MAC_VER_25:
3616 case RTL_GIGA_MAC_VER_26:
3617 case RTL_GIGA_MAC_VER_27:
3618 case RTL_GIGA_MAC_VER_28:
3619 case RTL_GIGA_MAC_VER_31:
3620 rtl_writephy(tp, 0x0e, 0x0200);
3621 default:
3622 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3623 break;
3624 }
françois romieu065c27c2011-01-03 15:08:12 +00003625}
3626
3627static void r8168_pll_power_down(struct rtl8169_private *tp)
3628{
3629 void __iomem *ioaddr = tp->mmio_addr;
3630
Francois Romieucecb5fd2011-04-01 10:21:07 +02003631 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3632 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3633 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003634 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003635 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003636 }
françois romieu065c27c2011-01-03 15:08:12 +00003637
Francois Romieucecb5fd2011-04-01 10:21:07 +02003638 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3639 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003640 (RTL_R16(CPlusCmd) & ASF)) {
3641 return;
3642 }
3643
hayeswang01dc7fe2011-03-21 01:50:28 +00003644 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3645 tp->mac_version == RTL_GIGA_MAC_VER_33)
3646 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3647
David S. Miller1805b2f2011-10-24 18:18:09 -04003648 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003649 return;
françois romieu065c27c2011-01-03 15:08:12 +00003650
3651 r8168_phy_power_down(tp);
3652
3653 switch (tp->mac_version) {
3654 case RTL_GIGA_MAC_VER_25:
3655 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003656 case RTL_GIGA_MAC_VER_27:
3657 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003658 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003659 case RTL_GIGA_MAC_VER_32:
3660 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003661 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3662 break;
3663 }
3664}
3665
3666static void r8168_pll_power_up(struct rtl8169_private *tp)
3667{
3668 void __iomem *ioaddr = tp->mmio_addr;
3669
françois romieu065c27c2011-01-03 15:08:12 +00003670 switch (tp->mac_version) {
3671 case RTL_GIGA_MAC_VER_25:
3672 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003673 case RTL_GIGA_MAC_VER_27:
3674 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003675 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003676 case RTL_GIGA_MAC_VER_32:
3677 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003678 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3679 break;
3680 }
3681
3682 r8168_phy_power_up(tp);
3683}
3684
Francois Romieud58d46b2011-05-03 16:38:29 +02003685static void rtl_generic_op(struct rtl8169_private *tp,
3686 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00003687{
3688 if (op)
3689 op(tp);
3690}
3691
3692static void rtl_pll_power_down(struct rtl8169_private *tp)
3693{
Francois Romieud58d46b2011-05-03 16:38:29 +02003694 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00003695}
3696
3697static void rtl_pll_power_up(struct rtl8169_private *tp)
3698{
Francois Romieud58d46b2011-05-03 16:38:29 +02003699 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00003700}
3701
3702static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3703{
3704 struct pll_power_ops *ops = &tp->pll_power_ops;
3705
3706 switch (tp->mac_version) {
3707 case RTL_GIGA_MAC_VER_07:
3708 case RTL_GIGA_MAC_VER_08:
3709 case RTL_GIGA_MAC_VER_09:
3710 case RTL_GIGA_MAC_VER_10:
3711 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003712 case RTL_GIGA_MAC_VER_29:
3713 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003714 ops->down = r810x_pll_power_down;
3715 ops->up = r810x_pll_power_up;
3716 break;
3717
3718 case RTL_GIGA_MAC_VER_11:
3719 case RTL_GIGA_MAC_VER_12:
3720 case RTL_GIGA_MAC_VER_17:
3721 case RTL_GIGA_MAC_VER_18:
3722 case RTL_GIGA_MAC_VER_19:
3723 case RTL_GIGA_MAC_VER_20:
3724 case RTL_GIGA_MAC_VER_21:
3725 case RTL_GIGA_MAC_VER_22:
3726 case RTL_GIGA_MAC_VER_23:
3727 case RTL_GIGA_MAC_VER_24:
3728 case RTL_GIGA_MAC_VER_25:
3729 case RTL_GIGA_MAC_VER_26:
3730 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003731 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003732 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003733 case RTL_GIGA_MAC_VER_32:
3734 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003735 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08003736 case RTL_GIGA_MAC_VER_35:
3737 case RTL_GIGA_MAC_VER_36:
françois romieu065c27c2011-01-03 15:08:12 +00003738 ops->down = r8168_pll_power_down;
3739 ops->up = r8168_pll_power_up;
3740 break;
3741
3742 default:
3743 ops->down = NULL;
3744 ops->up = NULL;
3745 break;
3746 }
3747}
3748
Hayes Wange542a222011-07-06 15:58:04 +08003749static void rtl_init_rxcfg(struct rtl8169_private *tp)
3750{
3751 void __iomem *ioaddr = tp->mmio_addr;
3752
3753 switch (tp->mac_version) {
3754 case RTL_GIGA_MAC_VER_01:
3755 case RTL_GIGA_MAC_VER_02:
3756 case RTL_GIGA_MAC_VER_03:
3757 case RTL_GIGA_MAC_VER_04:
3758 case RTL_GIGA_MAC_VER_05:
3759 case RTL_GIGA_MAC_VER_06:
3760 case RTL_GIGA_MAC_VER_10:
3761 case RTL_GIGA_MAC_VER_11:
3762 case RTL_GIGA_MAC_VER_12:
3763 case RTL_GIGA_MAC_VER_13:
3764 case RTL_GIGA_MAC_VER_14:
3765 case RTL_GIGA_MAC_VER_15:
3766 case RTL_GIGA_MAC_VER_16:
3767 case RTL_GIGA_MAC_VER_17:
3768 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3769 break;
3770 case RTL_GIGA_MAC_VER_18:
3771 case RTL_GIGA_MAC_VER_19:
3772 case RTL_GIGA_MAC_VER_20:
3773 case RTL_GIGA_MAC_VER_21:
3774 case RTL_GIGA_MAC_VER_22:
3775 case RTL_GIGA_MAC_VER_23:
3776 case RTL_GIGA_MAC_VER_24:
3777 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3778 break;
3779 default:
3780 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3781 break;
3782 }
3783}
3784
Hayes Wang92fc43b2011-07-06 15:58:03 +08003785static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3786{
3787 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3788}
3789
Francois Romieud58d46b2011-05-03 16:38:29 +02003790static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3791{
françois romieu9c5028e2012-03-02 04:43:14 +00003792 void __iomem *ioaddr = tp->mmio_addr;
3793
3794 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003795 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00003796 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003797}
3798
3799static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3800{
françois romieu9c5028e2012-03-02 04:43:14 +00003801 void __iomem *ioaddr = tp->mmio_addr;
3802
3803 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003804 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00003805 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003806}
3807
3808static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3809{
3810 void __iomem *ioaddr = tp->mmio_addr;
3811
3812 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3813 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3814 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3815}
3816
3817static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3818{
3819 void __iomem *ioaddr = tp->mmio_addr;
3820
3821 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3822 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3823 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3824}
3825
3826static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3827{
3828 void __iomem *ioaddr = tp->mmio_addr;
3829
3830 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3831}
3832
3833static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3834{
3835 void __iomem *ioaddr = tp->mmio_addr;
3836
3837 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3838}
3839
3840static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3841{
3842 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003843
3844 RTL_W8(MaxTxPacketSize, 0x3f);
3845 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3846 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003847 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003848}
3849
3850static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3851{
3852 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003853
3854 RTL_W8(MaxTxPacketSize, 0x0c);
3855 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3856 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003857 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003858}
3859
3860static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3861{
3862 rtl_tx_performance_tweak(tp->pci_dev,
3863 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3864}
3865
3866static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3867{
3868 rtl_tx_performance_tweak(tp->pci_dev,
3869 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3870}
3871
3872static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3873{
3874 void __iomem *ioaddr = tp->mmio_addr;
3875
3876 r8168b_0_hw_jumbo_enable(tp);
3877
3878 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
3879}
3880
3881static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3882{
3883 void __iomem *ioaddr = tp->mmio_addr;
3884
3885 r8168b_0_hw_jumbo_disable(tp);
3886
3887 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3888}
3889
3890static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
3891{
3892 struct jumbo_ops *ops = &tp->jumbo_ops;
3893
3894 switch (tp->mac_version) {
3895 case RTL_GIGA_MAC_VER_11:
3896 ops->disable = r8168b_0_hw_jumbo_disable;
3897 ops->enable = r8168b_0_hw_jumbo_enable;
3898 break;
3899 case RTL_GIGA_MAC_VER_12:
3900 case RTL_GIGA_MAC_VER_17:
3901 ops->disable = r8168b_1_hw_jumbo_disable;
3902 ops->enable = r8168b_1_hw_jumbo_enable;
3903 break;
3904 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
3905 case RTL_GIGA_MAC_VER_19:
3906 case RTL_GIGA_MAC_VER_20:
3907 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
3908 case RTL_GIGA_MAC_VER_22:
3909 case RTL_GIGA_MAC_VER_23:
3910 case RTL_GIGA_MAC_VER_24:
3911 case RTL_GIGA_MAC_VER_25:
3912 case RTL_GIGA_MAC_VER_26:
3913 ops->disable = r8168c_hw_jumbo_disable;
3914 ops->enable = r8168c_hw_jumbo_enable;
3915 break;
3916 case RTL_GIGA_MAC_VER_27:
3917 case RTL_GIGA_MAC_VER_28:
3918 ops->disable = r8168dp_hw_jumbo_disable;
3919 ops->enable = r8168dp_hw_jumbo_enable;
3920 break;
3921 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
3922 case RTL_GIGA_MAC_VER_32:
3923 case RTL_GIGA_MAC_VER_33:
3924 case RTL_GIGA_MAC_VER_34:
3925 ops->disable = r8168e_hw_jumbo_disable;
3926 ops->enable = r8168e_hw_jumbo_enable;
3927 break;
3928
3929 /*
3930 * No action needed for jumbo frames with 8169.
3931 * No jumbo for 810x at all.
3932 */
3933 default:
3934 ops->disable = NULL;
3935 ops->enable = NULL;
3936 break;
3937 }
3938}
3939
Francois Romieu6f43adc2011-04-29 15:05:51 +02003940static void rtl_hw_reset(struct rtl8169_private *tp)
3941{
3942 void __iomem *ioaddr = tp->mmio_addr;
3943 int i;
3944
3945 /* Soft reset the chip. */
3946 RTL_W8(ChipCmd, CmdReset);
3947
3948 /* Check that the chip has finished the reset. */
3949 for (i = 0; i < 100; i++) {
3950 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3951 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003952 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003953 }
3954}
3955
Francois Romieub6ffd972011-06-17 17:00:05 +02003956static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
3957{
3958 struct rtl_fw *rtl_fw;
3959 const char *name;
3960 int rc = -ENOMEM;
3961
3962 name = rtl_lookup_firmware_name(tp);
3963 if (!name)
3964 goto out_no_firmware;
3965
3966 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
3967 if (!rtl_fw)
3968 goto err_warn;
3969
3970 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
3971 if (rc < 0)
3972 goto err_free;
3973
Francois Romieufd112f22011-06-18 00:10:29 +02003974 rc = rtl_check_firmware(tp, rtl_fw);
3975 if (rc < 0)
3976 goto err_release_firmware;
3977
Francois Romieub6ffd972011-06-17 17:00:05 +02003978 tp->rtl_fw = rtl_fw;
3979out:
3980 return;
3981
Francois Romieufd112f22011-06-18 00:10:29 +02003982err_release_firmware:
3983 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02003984err_free:
3985 kfree(rtl_fw);
3986err_warn:
3987 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
3988 name, rc);
3989out_no_firmware:
3990 tp->rtl_fw = NULL;
3991 goto out;
3992}
3993
François Romieu953a12c2011-04-24 17:38:48 +02003994static void rtl_request_firmware(struct rtl8169_private *tp)
3995{
Francois Romieub6ffd972011-06-17 17:00:05 +02003996 if (IS_ERR(tp->rtl_fw))
3997 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02003998}
3999
Hayes Wang92fc43b2011-07-06 15:58:03 +08004000static void rtl_rx_close(struct rtl8169_private *tp)
4001{
4002 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004003
Francois Romieu1687b562011-07-19 17:21:29 +02004004 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004005}
4006
françois romieue6de30d2011-01-03 15:08:37 +00004007static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008{
françois romieue6de30d2011-01-03 15:08:37 +00004009 void __iomem *ioaddr = tp->mmio_addr;
4010
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004012 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013
Hayes Wang92fc43b2011-07-06 15:58:03 +08004014 rtl_rx_close(tp);
4015
Hayes Wang5d2e1952011-02-22 17:26:22 +08004016 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004017 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4018 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00004019 while (RTL_R8(TxPoll) & NPQ)
4020 udelay(20);
Hayes Wangc2218922011-09-06 16:55:18 +08004021 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4022 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
4023 tp->mac_version == RTL_GIGA_MAC_VER_36) {
David S. Miller8decf862011-09-22 03:23:13 -04004024 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08004025 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4026 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004027 } else {
4028 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4029 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004030 }
4031
Hayes Wang92fc43b2011-07-06 15:58:03 +08004032 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033}
4034
Francois Romieu7f796d832007-06-11 23:04:41 +02004035static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004036{
4037 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004038
4039 /* Set DMA burst size and Interframe Gap Time */
4040 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4041 (InterFrameGap << TxInterFrameGapShift));
4042}
4043
Francois Romieu07ce4062007-02-23 23:36:39 +01004044static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045{
4046 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
Francois Romieu07ce4062007-02-23 23:36:39 +01004048 tp->hw_start(dev);
4049
Francois Romieuda78dbf2012-01-26 14:18:23 +01004050 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004051}
4052
Francois Romieu7f796d832007-06-11 23:04:41 +02004053static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4054 void __iomem *ioaddr)
4055{
4056 /*
4057 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4058 * register to be written before TxDescAddrLow to work.
4059 * Switching from MMIO to I/O access fixes the issue as well.
4060 */
4061 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004062 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004063 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004064 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004065}
4066
4067static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4068{
4069 u16 cmd;
4070
4071 cmd = RTL_R16(CPlusCmd);
4072 RTL_W16(CPlusCmd, cmd);
4073 return cmd;
4074}
4075
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004076static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004077{
4078 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004079 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004080}
4081
Francois Romieu6dccd162007-02-13 23:38:05 +01004082static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4083{
Francois Romieu37441002011-06-17 22:58:54 +02004084 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004085 u32 mac_version;
4086 u32 clk;
4087 u32 val;
4088 } cfg2_info [] = {
4089 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4090 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4091 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4092 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004093 };
4094 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004095 unsigned int i;
4096 u32 clk;
4097
4098 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004099 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004100 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4101 RTL_W32(0x7c, p->val);
4102 break;
4103 }
4104 }
4105}
4106
Francois Romieue6b763e2012-03-08 09:35:39 +01004107static void rtl_set_rx_mode(struct net_device *dev)
4108{
4109 struct rtl8169_private *tp = netdev_priv(dev);
4110 void __iomem *ioaddr = tp->mmio_addr;
4111 u32 mc_filter[2]; /* Multicast hash filter */
4112 int rx_mode;
4113 u32 tmp = 0;
4114
4115 if (dev->flags & IFF_PROMISC) {
4116 /* Unconditionally log net taps. */
4117 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4118 rx_mode =
4119 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4120 AcceptAllPhys;
4121 mc_filter[1] = mc_filter[0] = 0xffffffff;
4122 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4123 (dev->flags & IFF_ALLMULTI)) {
4124 /* Too many to filter perfectly -- accept all multicasts. */
4125 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4126 mc_filter[1] = mc_filter[0] = 0xffffffff;
4127 } else {
4128 struct netdev_hw_addr *ha;
4129
4130 rx_mode = AcceptBroadcast | AcceptMyPhys;
4131 mc_filter[1] = mc_filter[0] = 0;
4132 netdev_for_each_mc_addr(ha, dev) {
4133 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4134 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4135 rx_mode |= AcceptMulticast;
4136 }
4137 }
4138
4139 if (dev->features & NETIF_F_RXALL)
4140 rx_mode |= (AcceptErr | AcceptRunt);
4141
4142 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4143
4144 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4145 u32 data = mc_filter[0];
4146
4147 mc_filter[0] = swab32(mc_filter[1]);
4148 mc_filter[1] = swab32(data);
4149 }
4150
4151 RTL_W32(MAR0 + 4, mc_filter[1]);
4152 RTL_W32(MAR0 + 0, mc_filter[0]);
4153
4154 RTL_W32(RxConfig, tmp);
4155}
4156
Francois Romieu07ce4062007-02-23 23:36:39 +01004157static void rtl_hw_start_8169(struct net_device *dev)
4158{
4159 struct rtl8169_private *tp = netdev_priv(dev);
4160 void __iomem *ioaddr = tp->mmio_addr;
4161 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004162
Francois Romieu9cb427b2006-11-02 00:10:16 +01004163 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4164 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4165 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4166 }
4167
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004169 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4170 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4171 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4172 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004173 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4174
Hayes Wange542a222011-07-06 15:58:04 +08004175 rtl_init_rxcfg(tp);
4176
françois romieuf0298f82011-01-03 15:07:42 +00004177 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004179 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
Francois Romieucecb5fd2011-04-01 10:21:07 +02004181 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4182 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4183 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4184 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004185 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
Francois Romieu7f796d832007-06-11 23:04:41 +02004187 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004188
Francois Romieucecb5fd2011-04-01 10:21:07 +02004189 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4190 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004191 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004193 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 }
4195
Francois Romieubcf0bf92006-07-26 23:14:13 +02004196 RTL_W16(CPlusCmd, tp->cp_cmd);
4197
Francois Romieu6dccd162007-02-13 23:38:05 +01004198 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4199
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 /*
4201 * Undocumented corner. Supposedly:
4202 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4203 */
4204 RTL_W16(IntrMitigate, 0x0000);
4205
Francois Romieu7f796d832007-06-11 23:04:41 +02004206 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004207
Francois Romieucecb5fd2011-04-01 10:21:07 +02004208 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4209 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4210 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4211 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004212 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4213 rtl_set_rx_tx_config_registers(tp);
4214 }
4215
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004217
4218 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4219 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220
4221 RTL_W32(RxMissed, 0);
4222
Francois Romieu07ce4062007-02-23 23:36:39 +01004223 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
4225 /* no early-rx interrupts */
4226 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004227}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228
françois romieu650e8d52011-01-03 15:08:29 +00004229static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004230{
4231 u32 csi;
4232
4233 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00004234 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4235}
4236
françois romieue6de30d2011-01-03 15:08:37 +00004237static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4238{
4239 rtl_csi_access_enable(ioaddr, 0x17000000);
4240}
4241
françois romieu650e8d52011-01-03 15:08:29 +00004242static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4243{
4244 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02004245}
4246
4247struct ephy_info {
4248 unsigned int offset;
4249 u16 mask;
4250 u16 bits;
4251};
4252
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004253static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004254{
4255 u16 w;
4256
4257 while (len-- > 0) {
4258 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4259 rtl_ephy_write(ioaddr, e->offset, w);
4260 e++;
4261 }
4262}
4263
Francois Romieub726e492008-06-28 12:22:59 +02004264static void rtl_disable_clock_request(struct pci_dev *pdev)
4265{
Jon Masone44daad2011-06-27 07:46:31 +00004266 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004267
4268 if (cap) {
4269 u16 ctl;
4270
4271 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4272 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4273 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4274 }
4275}
4276
françois romieue6de30d2011-01-03 15:08:37 +00004277static void rtl_enable_clock_request(struct pci_dev *pdev)
4278{
Jon Masone44daad2011-06-27 07:46:31 +00004279 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004280
4281 if (cap) {
4282 u16 ctl;
4283
4284 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4285 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4286 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4287 }
4288}
4289
Francois Romieub726e492008-06-28 12:22:59 +02004290#define R8168_CPCMD_QUIRK_MASK (\
4291 EnableBist | \
4292 Mac_dbgo_oe | \
4293 Force_half_dup | \
4294 Force_rxflow_en | \
4295 Force_txflow_en | \
4296 Cxpl_dbg_sel | \
4297 ASF | \
4298 PktCntrDisable | \
4299 Mac_dbgo_sel)
4300
Francois Romieu219a1e92008-06-28 11:58:39 +02004301static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4302{
Francois Romieub726e492008-06-28 12:22:59 +02004303 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4304
4305 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4306
Francois Romieu2e68ae42008-06-28 12:00:55 +02004307 rtl_tx_performance_tweak(pdev,
4308 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004309}
4310
4311static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4312{
4313 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004314
françois romieuf0298f82011-01-03 15:07:42 +00004315 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004316
4317 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004318}
4319
4320static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4321{
Francois Romieub726e492008-06-28 12:22:59 +02004322 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4323
4324 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4325
Francois Romieu219a1e92008-06-28 11:58:39 +02004326 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004327
4328 rtl_disable_clock_request(pdev);
4329
4330 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004331}
4332
Francois Romieuef3386f2008-06-29 12:24:30 +02004333static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004334{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004335 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004336 { 0x01, 0, 0x0001 },
4337 { 0x02, 0x0800, 0x1000 },
4338 { 0x03, 0, 0x0042 },
4339 { 0x06, 0x0080, 0x0000 },
4340 { 0x07, 0, 0x2000 }
4341 };
4342
françois romieu650e8d52011-01-03 15:08:29 +00004343 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004344
4345 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4346
Francois Romieu219a1e92008-06-28 11:58:39 +02004347 __rtl_hw_start_8168cp(ioaddr, pdev);
4348}
4349
Francois Romieuef3386f2008-06-29 12:24:30 +02004350static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4351{
françois romieu650e8d52011-01-03 15:08:29 +00004352 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004353
4354 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4355
4356 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4357
4358 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4359}
4360
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004361static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4362{
françois romieu650e8d52011-01-03 15:08:29 +00004363 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004364
4365 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4366
4367 /* Magic. */
4368 RTL_W8(DBG_REG, 0x20);
4369
françois romieuf0298f82011-01-03 15:07:42 +00004370 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004371
4372 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4373
4374 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4375}
4376
Francois Romieu219a1e92008-06-28 11:58:39 +02004377static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4378{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004379 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004380 { 0x02, 0x0800, 0x1000 },
4381 { 0x03, 0, 0x0002 },
4382 { 0x06, 0x0080, 0x0000 }
4383 };
4384
françois romieu650e8d52011-01-03 15:08:29 +00004385 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004386
4387 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4388
4389 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4390
Francois Romieu219a1e92008-06-28 11:58:39 +02004391 __rtl_hw_start_8168cp(ioaddr, pdev);
4392}
4393
4394static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4395{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004396 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004397 { 0x01, 0, 0x0001 },
4398 { 0x03, 0x0400, 0x0220 }
4399 };
4400
françois romieu650e8d52011-01-03 15:08:29 +00004401 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004402
4403 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4404
Francois Romieu219a1e92008-06-28 11:58:39 +02004405 __rtl_hw_start_8168cp(ioaddr, pdev);
4406}
4407
Francois Romieu197ff762008-06-28 13:16:02 +02004408static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4409{
4410 rtl_hw_start_8168c_2(ioaddr, pdev);
4411}
4412
Francois Romieu6fb07052008-06-29 11:54:28 +02004413static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4414{
françois romieu650e8d52011-01-03 15:08:29 +00004415 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004416
4417 __rtl_hw_start_8168cp(ioaddr, pdev);
4418}
4419
Francois Romieu5b538df2008-07-20 16:22:45 +02004420static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4421{
françois romieu650e8d52011-01-03 15:08:29 +00004422 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004423
4424 rtl_disable_clock_request(pdev);
4425
françois romieuf0298f82011-01-03 15:07:42 +00004426 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004427
4428 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4429
4430 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4431}
4432
hayeswang4804b3b2011-03-21 01:50:29 +00004433static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4434{
4435 rtl_csi_access_enable_1(ioaddr);
4436
4437 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4438
4439 RTL_W8(MaxTxPacketSize, TxPacketMax);
4440
4441 rtl_disable_clock_request(pdev);
4442}
4443
françois romieue6de30d2011-01-03 15:08:37 +00004444static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4445{
4446 static const struct ephy_info e_info_8168d_4[] = {
4447 { 0x0b, ~0, 0x48 },
4448 { 0x19, 0x20, 0x50 },
4449 { 0x0c, ~0, 0x20 }
4450 };
4451 int i;
4452
4453 rtl_csi_access_enable_1(ioaddr);
4454
4455 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4456
4457 RTL_W8(MaxTxPacketSize, TxPacketMax);
4458
4459 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4460 const struct ephy_info *e = e_info_8168d_4 + i;
4461 u16 w;
4462
4463 w = rtl_ephy_read(ioaddr, e->offset);
4464 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4465 }
4466
4467 rtl_enable_clock_request(pdev);
4468}
4469
Hayes Wang70090422011-07-06 15:58:06 +08004470static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
hayeswang01dc7fe2011-03-21 01:50:28 +00004471{
Hayes Wang70090422011-07-06 15:58:06 +08004472 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004473 { 0x00, 0x0200, 0x0100 },
4474 { 0x00, 0x0000, 0x0004 },
4475 { 0x06, 0x0002, 0x0001 },
4476 { 0x06, 0x0000, 0x0030 },
4477 { 0x07, 0x0000, 0x2000 },
4478 { 0x00, 0x0000, 0x0020 },
4479 { 0x03, 0x5800, 0x2000 },
4480 { 0x03, 0x0000, 0x0001 },
4481 { 0x01, 0x0800, 0x1000 },
4482 { 0x07, 0x0000, 0x4000 },
4483 { 0x1e, 0x0000, 0x2000 },
4484 { 0x19, 0xffff, 0xfe6c },
4485 { 0x0a, 0x0000, 0x0040 }
4486 };
4487
4488 rtl_csi_access_enable_2(ioaddr);
4489
Hayes Wang70090422011-07-06 15:58:06 +08004490 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004491
4492 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4493
4494 RTL_W8(MaxTxPacketSize, TxPacketMax);
4495
4496 rtl_disable_clock_request(pdev);
4497
4498 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004499 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4500 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004501
Francois Romieucecb5fd2011-04-01 10:21:07 +02004502 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004503}
4504
Hayes Wang70090422011-07-06 15:58:06 +08004505static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4506{
4507 static const struct ephy_info e_info_8168e_2[] = {
4508 { 0x09, 0x0000, 0x0080 },
4509 { 0x19, 0x0000, 0x0224 }
4510 };
4511
4512 rtl_csi_access_enable_1(ioaddr);
4513
4514 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4515
4516 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4517
4518 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4519 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4520 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4521 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4522 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4523 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4524 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4525 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4526 ERIAR_EXGMAC);
4527
Hayes Wang3090bd92011-09-06 16:55:15 +08004528 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004529
4530 rtl_disable_clock_request(pdev);
4531
4532 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4533 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4534
4535 /* Adjust EEE LED frequency */
4536 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4537
4538 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4539 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4540 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4541}
4542
Hayes Wangc2218922011-09-06 16:55:18 +08004543static void rtl_hw_start_8168f_1(void __iomem *ioaddr, struct pci_dev *pdev)
4544{
4545 static const struct ephy_info e_info_8168f_1[] = {
4546 { 0x06, 0x00c0, 0x0020 },
4547 { 0x08, 0x0001, 0x0002 },
4548 { 0x09, 0x0000, 0x0080 },
4549 { 0x19, 0x0000, 0x0224 }
4550 };
4551
4552 rtl_csi_access_enable_1(ioaddr);
4553
4554 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4555
4556 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4557
4558 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4559 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4560 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4561 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4562 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4563 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4564 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4565 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4566 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4567 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
4568 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4569 ERIAR_EXGMAC);
4570
4571 RTL_W8(MaxTxPacketSize, EarlySize);
4572
4573 rtl_disable_clock_request(pdev);
4574
4575 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4576 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4577
4578 /* Adjust EEE LED frequency */
4579 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4580
4581 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4582 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4583 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4584}
4585
Francois Romieu07ce4062007-02-23 23:36:39 +01004586static void rtl_hw_start_8168(struct net_device *dev)
4587{
Francois Romieu2dd99532007-06-11 23:22:52 +02004588 struct rtl8169_private *tp = netdev_priv(dev);
4589 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004590 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004591
4592 RTL_W8(Cfg9346, Cfg9346_Unlock);
4593
françois romieuf0298f82011-01-03 15:07:42 +00004594 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004595
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004596 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004597
Francois Romieu0e485152007-02-20 00:00:26 +01004598 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004599
4600 RTL_W16(CPlusCmd, tp->cp_cmd);
4601
Francois Romieu0e485152007-02-20 00:00:26 +01004602 RTL_W16(IntrMitigate, 0x5151);
4603
4604 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00004605 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01004606 tp->event_slow |= RxFIFOOver | PCSTimeout;
4607 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01004608 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004609
4610 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4611
Francois Romieub8363902008-06-01 12:31:57 +02004612 rtl_set_rx_mode(dev);
4613
4614 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4615 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004616
4617 RTL_R8(IntrMask);
4618
Francois Romieu219a1e92008-06-28 11:58:39 +02004619 switch (tp->mac_version) {
4620 case RTL_GIGA_MAC_VER_11:
4621 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004622 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004623
4624 case RTL_GIGA_MAC_VER_12:
4625 case RTL_GIGA_MAC_VER_17:
4626 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004627 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004628
4629 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004630 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004631 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004632
4633 case RTL_GIGA_MAC_VER_19:
4634 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004635 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004636
4637 case RTL_GIGA_MAC_VER_20:
4638 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004639 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004640
Francois Romieu197ff762008-06-28 13:16:02 +02004641 case RTL_GIGA_MAC_VER_21:
4642 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004643 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004644
Francois Romieu6fb07052008-06-29 11:54:28 +02004645 case RTL_GIGA_MAC_VER_22:
4646 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004647 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004648
Francois Romieuef3386f2008-06-29 12:24:30 +02004649 case RTL_GIGA_MAC_VER_23:
4650 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004651 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004652
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004653 case RTL_GIGA_MAC_VER_24:
4654 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004655 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004656
Francois Romieu5b538df2008-07-20 16:22:45 +02004657 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004658 case RTL_GIGA_MAC_VER_26:
4659 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004660 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004661 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004662
françois romieue6de30d2011-01-03 15:08:37 +00004663 case RTL_GIGA_MAC_VER_28:
4664 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004665 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004666
hayeswang4804b3b2011-03-21 01:50:29 +00004667 case RTL_GIGA_MAC_VER_31:
4668 rtl_hw_start_8168dp(ioaddr, pdev);
4669 break;
4670
hayeswang01dc7fe2011-03-21 01:50:28 +00004671 case RTL_GIGA_MAC_VER_32:
4672 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004673 rtl_hw_start_8168e_1(ioaddr, pdev);
4674 break;
4675 case RTL_GIGA_MAC_VER_34:
4676 rtl_hw_start_8168e_2(ioaddr, pdev);
hayeswang01dc7fe2011-03-21 01:50:28 +00004677 break;
françois romieue6de30d2011-01-03 15:08:37 +00004678
Hayes Wangc2218922011-09-06 16:55:18 +08004679 case RTL_GIGA_MAC_VER_35:
4680 case RTL_GIGA_MAC_VER_36:
4681 rtl_hw_start_8168f_1(ioaddr, pdev);
4682 break;
4683
Francois Romieu219a1e92008-06-28 11:58:39 +02004684 default:
4685 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4686 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004687 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004688 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004689
Francois Romieu0e485152007-02-20 00:00:26 +01004690 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4691
Francois Romieub8363902008-06-01 12:31:57 +02004692 RTL_W8(Cfg9346, Cfg9346_Lock);
4693
Francois Romieu2dd99532007-06-11 23:22:52 +02004694 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004695}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696
Francois Romieu2857ffb2008-08-02 21:08:49 +02004697#define R810X_CPCMD_QUIRK_MASK (\
4698 EnableBist | \
4699 Mac_dbgo_oe | \
4700 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004701 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004702 Force_txflow_en | \
4703 Cxpl_dbg_sel | \
4704 ASF | \
4705 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004706 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004707
4708static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4709{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004710 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004711 { 0x01, 0, 0x6e65 },
4712 { 0x02, 0, 0x091f },
4713 { 0x03, 0, 0xc2f9 },
4714 { 0x06, 0, 0xafb5 },
4715 { 0x07, 0, 0x0e00 },
4716 { 0x19, 0, 0xec80 },
4717 { 0x01, 0, 0x2e65 },
4718 { 0x01, 0, 0x6e65 }
4719 };
4720 u8 cfg1;
4721
françois romieu650e8d52011-01-03 15:08:29 +00004722 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004723
4724 RTL_W8(DBG_REG, FIX_NAK_1);
4725
4726 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4727
4728 RTL_W8(Config1,
4729 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4730 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4731
4732 cfg1 = RTL_R8(Config1);
4733 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4734 RTL_W8(Config1, cfg1 & ~LEDS0);
4735
Francois Romieu2857ffb2008-08-02 21:08:49 +02004736 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4737}
4738
4739static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4740{
françois romieu650e8d52011-01-03 15:08:29 +00004741 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004742
4743 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4744
4745 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4746 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004747}
4748
4749static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4750{
4751 rtl_hw_start_8102e_2(ioaddr, pdev);
4752
4753 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4754}
4755
Hayes Wang5a5e4442011-02-22 17:26:21 +08004756static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4757{
4758 static const struct ephy_info e_info_8105e_1[] = {
4759 { 0x07, 0, 0x4000 },
4760 { 0x19, 0, 0x0200 },
4761 { 0x19, 0, 0x0020 },
4762 { 0x1e, 0, 0x2000 },
4763 { 0x03, 0, 0x0001 },
4764 { 0x19, 0, 0x0100 },
4765 { 0x19, 0, 0x0004 },
4766 { 0x0a, 0, 0x0020 }
4767 };
4768
Francois Romieucecb5fd2011-04-01 10:21:07 +02004769 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004770 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4771
Francois Romieucecb5fd2011-04-01 10:21:07 +02004772 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004773 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4774
4775 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08004776 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004777
4778 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4779}
4780
4781static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4782{
4783 rtl_hw_start_8105e_1(ioaddr, pdev);
4784 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4785}
4786
Francois Romieu07ce4062007-02-23 23:36:39 +01004787static void rtl_hw_start_8101(struct net_device *dev)
4788{
Francois Romieucdf1a602007-06-11 23:29:50 +02004789 struct rtl8169_private *tp = netdev_priv(dev);
4790 void __iomem *ioaddr = tp->mmio_addr;
4791 struct pci_dev *pdev = tp->pci_dev;
4792
Francois Romieuda78dbf2012-01-26 14:18:23 +01004793 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
4794 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00004795
Francois Romieucecb5fd2011-04-01 10:21:07 +02004796 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4797 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00004798 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02004799
4800 if (cap) {
4801 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4802 PCI_EXP_DEVCTL_NOSNOOP_EN);
4803 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004804 }
4805
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004806 RTL_W8(Cfg9346, Cfg9346_Unlock);
4807
Francois Romieu2857ffb2008-08-02 21:08:49 +02004808 switch (tp->mac_version) {
4809 case RTL_GIGA_MAC_VER_07:
4810 rtl_hw_start_8102e_1(ioaddr, pdev);
4811 break;
4812
4813 case RTL_GIGA_MAC_VER_08:
4814 rtl_hw_start_8102e_3(ioaddr, pdev);
4815 break;
4816
4817 case RTL_GIGA_MAC_VER_09:
4818 rtl_hw_start_8102e_2(ioaddr, pdev);
4819 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004820
4821 case RTL_GIGA_MAC_VER_29:
4822 rtl_hw_start_8105e_1(ioaddr, pdev);
4823 break;
4824 case RTL_GIGA_MAC_VER_30:
4825 rtl_hw_start_8105e_2(ioaddr, pdev);
4826 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004827 }
4828
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004829 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004830
françois romieuf0298f82011-01-03 15:07:42 +00004831 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004832
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004833 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004834
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004835 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004836 RTL_W16(CPlusCmd, tp->cp_cmd);
4837
4838 RTL_W16(IntrMitigate, 0x0000);
4839
4840 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4841
4842 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4843 rtl_set_rx_tx_config_registers(tp);
4844
Francois Romieucdf1a602007-06-11 23:29:50 +02004845 RTL_R8(IntrMask);
4846
Francois Romieucdf1a602007-06-11 23:29:50 +02004847 rtl_set_rx_mode(dev);
4848
4849 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850}
4851
4852static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4853{
Francois Romieud58d46b2011-05-03 16:38:29 +02004854 struct rtl8169_private *tp = netdev_priv(dev);
4855
4856 if (new_mtu < ETH_ZLEN ||
4857 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 return -EINVAL;
4859
Francois Romieud58d46b2011-05-03 16:38:29 +02004860 if (new_mtu > ETH_DATA_LEN)
4861 rtl_hw_jumbo_enable(tp);
4862 else
4863 rtl_hw_jumbo_disable(tp);
4864
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004866 netdev_update_features(dev);
4867
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004868 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869}
4870
4871static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4872{
Al Viro95e09182007-12-22 18:55:39 +00004873 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4875}
4876
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004877static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4878 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004880 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004881 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004882
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004883 kfree(*data_buff);
4884 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 rtl8169_make_unusable_by_asic(desc);
4886}
4887
4888static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4889{
4890 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4891
4892 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4893}
4894
4895static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4896 u32 rx_buf_sz)
4897{
4898 desc->addr = cpu_to_le64(mapping);
4899 wmb();
4900 rtl8169_mark_to_asic(desc, rx_buf_sz);
4901}
4902
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004903static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004905 return (void *)ALIGN((long)data, 16);
4906}
4907
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004908static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4909 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004910{
4911 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004913 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004914 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004915 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004917 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4918 if (!data)
4919 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004920
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004921 if (rtl8169_align(data) != data) {
4922 kfree(data);
4923 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4924 if (!data)
4925 return NULL;
4926 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004927
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004928 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004929 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004930 if (unlikely(dma_mapping_error(d, mapping))) {
4931 if (net_ratelimit())
4932 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004933 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935
4936 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004937 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004938
4939err_out:
4940 kfree(data);
4941 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942}
4943
4944static void rtl8169_rx_clear(struct rtl8169_private *tp)
4945{
Francois Romieu07d3f512007-02-21 22:40:46 +01004946 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947
4948 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004949 if (tp->Rx_databuff[i]) {
4950 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 tp->RxDescArray + i);
4952 }
4953 }
4954}
4955
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004956static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004958 desc->opts1 |= cpu_to_le32(RingEnd);
4959}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004960
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004961static int rtl8169_rx_fill(struct rtl8169_private *tp)
4962{
4963 unsigned int i;
4964
4965 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004966 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004967
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004968 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004970
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004971 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004972 if (!data) {
4973 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004974 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004975 }
4976 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004979 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4980 return 0;
4981
4982err_out:
4983 rtl8169_rx_clear(tp);
4984 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985}
4986
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987static int rtl8169_init_ring(struct net_device *dev)
4988{
4989 struct rtl8169_private *tp = netdev_priv(dev);
4990
4991 rtl8169_init_ring_indexes(tp);
4992
4993 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004994 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004996 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997}
4998
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004999static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 struct TxDesc *desc)
5001{
5002 unsigned int len = tx_skb->len;
5003
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005004 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5005
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 desc->opts1 = 0x00;
5007 desc->opts2 = 0x00;
5008 desc->addr = 0x00;
5009 tx_skb->len = 0;
5010}
5011
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005012static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5013 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014{
5015 unsigned int i;
5016
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005017 for (i = 0; i < n; i++) {
5018 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 struct ring_info *tx_skb = tp->tx_skb + entry;
5020 unsigned int len = tx_skb->len;
5021
5022 if (len) {
5023 struct sk_buff *skb = tx_skb->skb;
5024
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005025 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 tp->TxDescArray + entry);
5027 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005028 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 dev_kfree_skb(skb);
5030 tx_skb->skb = NULL;
5031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 }
5033 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005034}
5035
5036static void rtl8169_tx_clear(struct rtl8169_private *tp)
5037{
5038 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 tp->cur_tx = tp->dirty_tx = 0;
Igor Maravic036dafa2012-03-05 00:01:25 +01005040 netdev_reset_queue(tp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041}
5042
Francois Romieu4422bcd2012-01-26 11:23:32 +01005043static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044{
David Howellsc4028952006-11-22 14:57:56 +00005045 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005046 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047
Francois Romieuda78dbf2012-01-26 14:18:23 +01005048 napi_disable(&tp->napi);
5049 netif_stop_queue(dev);
5050 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051
françois romieuc7c2c392011-12-04 20:30:52 +00005052 rtl8169_hw_reset(tp);
5053
Francois Romieu56de4142011-03-15 17:29:31 +01005054 for (i = 0; i < NUM_RX_DESC; i++)
5055 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5056
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005058 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059
Francois Romieuda78dbf2012-01-26 14:18:23 +01005060 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01005061 rtl_hw_start(dev);
5062 netif_wake_queue(dev);
5063 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064}
5065
5066static void rtl8169_tx_timeout(struct net_device *dev)
5067{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005068 struct rtl8169_private *tp = netdev_priv(dev);
5069
5070 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071}
5072
5073static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005074 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075{
5076 struct skb_shared_info *info = skb_shinfo(skb);
5077 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005078 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005079 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080
5081 entry = tp->cur_tx;
5082 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005083 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 dma_addr_t mapping;
5085 u32 status, len;
5086 void *addr;
5087
5088 entry = (entry + 1) % NUM_TX_DESC;
5089
5090 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005091 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005092 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005093 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005094 if (unlikely(dma_mapping_error(d, mapping))) {
5095 if (net_ratelimit())
5096 netif_err(tp, drv, tp->dev,
5097 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005098 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100
Francois Romieucecb5fd2011-04-01 10:21:07 +02005101 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005102 status = opts[0] | len |
5103 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104
5105 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005106 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 txd->addr = cpu_to_le64(mapping);
5108
5109 tp->tx_skb[entry].len = len;
5110 }
5111
5112 if (cur_frag) {
5113 tp->tx_skb[entry].skb = skb;
5114 txd->opts1 |= cpu_to_le32(LastFrag);
5115 }
5116
5117 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005118
5119err_out:
5120 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5121 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122}
5123
Francois Romieu2b7b4312011-04-18 22:53:24 -07005124static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5125 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005127 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005128 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005129 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130
Francois Romieu2b7b4312011-04-18 22:53:24 -07005131 if (mss) {
5132 opts[0] |= TD_LSO;
5133 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5134 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005135 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
5137 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005138 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005140 opts[offset] |= info->checksum.udp;
5141 else
5142 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144}
5145
Stephen Hemminger613573252009-08-31 19:50:58 +00005146static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5147 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148{
5149 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005150 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 struct TxDesc *txd = tp->TxDescArray + entry;
5152 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005153 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 dma_addr_t mapping;
5155 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005156 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005157 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005158
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005160 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005161 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 }
5163
5164 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005165 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005167 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005168 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005169 if (unlikely(dma_mapping_error(d, mapping))) {
5170 if (net_ratelimit())
5171 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005172 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174
5175 tp->tx_skb[entry].len = len;
5176 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177
Francois Romieu2b7b4312011-04-18 22:53:24 -07005178 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5179 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005180
Francois Romieu2b7b4312011-04-18 22:53:24 -07005181 rtl8169_tso_csum(tp, skb, opts);
5182
5183 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005184 if (frags < 0)
5185 goto err_dma_1;
5186 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005187 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005188 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005189 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005190 tp->tx_skb[entry].skb = skb;
5191 }
5192
Francois Romieu2b7b4312011-04-18 22:53:24 -07005193 txd->opts2 = cpu_to_le32(opts[1]);
5194
Igor Maravic036dafa2012-03-05 00:01:25 +01005195 netdev_sent_queue(dev, skb->len);
5196
Richard Cochran5047fb52012-03-10 07:29:42 +00005197 skb_tx_timestamp(skb);
5198
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 wmb();
5200
Francois Romieucecb5fd2011-04-01 10:21:07 +02005201 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005202 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 txd->opts1 = cpu_to_le32(status);
5204
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 tp->cur_tx += frags + 1;
5206
David Dillow4c020a92010-03-03 16:33:10 +00005207 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208
Francois Romieucecb5fd2011-04-01 10:21:07 +02005209 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210
Francois Romieuda78dbf2012-01-26 14:18:23 +01005211 mmiowb();
5212
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01005214 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5215 * not miss a ring update when it notices a stopped queue.
5216 */
5217 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01005219 /* Sync with rtl_tx:
5220 * - publish queue status and cur_tx ring index (write barrier)
5221 * - refresh dirty_tx ring index (read barrier).
5222 * May the current thread have a pessimistic view of the ring
5223 * status and forget to wake up queue, a racing rtl_tx thread
5224 * can't.
5225 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005226 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
5228 netif_wake_queue(dev);
5229 }
5230
Stephen Hemminger613573252009-08-31 19:50:58 +00005231 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005233err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005234 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005235err_dma_0:
5236 dev_kfree_skb(skb);
5237 dev->stats.tx_dropped++;
5238 return NETDEV_TX_OK;
5239
5240err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005242 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005243 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244}
5245
5246static void rtl8169_pcierr_interrupt(struct net_device *dev)
5247{
5248 struct rtl8169_private *tp = netdev_priv(dev);
5249 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 u16 pci_status, pci_cmd;
5251
5252 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5253 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5254
Joe Perchesbf82c182010-02-09 11:49:50 +00005255 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5256 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257
5258 /*
5259 * The recovery sequence below admits a very elaborated explanation:
5260 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005261 * - I did not see what else could be done;
5262 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 *
5264 * Feel free to adjust to your needs.
5265 */
Francois Romieua27993f2006-12-18 00:04:19 +01005266 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005267 pci_cmd &= ~PCI_COMMAND_PARITY;
5268 else
5269 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5270
5271 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272
5273 pci_write_config_word(pdev, PCI_STATUS,
5274 pci_status & (PCI_STATUS_DETECTED_PARITY |
5275 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5276 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5277
5278 /* The infamous DAC f*ckup only happens at boot time */
5279 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005280 void __iomem *ioaddr = tp->mmio_addr;
5281
Joe Perchesbf82c182010-02-09 11:49:50 +00005282 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 tp->cp_cmd &= ~PCIDAC;
5284 RTL_W16(CPlusCmd, tp->cp_cmd);
5285 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 }
5287
françois romieue6de30d2011-01-03 15:08:37 +00005288 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005289
Francois Romieu98ddf982012-01-31 10:47:34 +01005290 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291}
5292
Igor Maravic036dafa2012-03-05 00:01:25 +01005293struct rtl_txc {
5294 int packets;
5295 int bytes;
5296};
5297
Francois Romieuda78dbf2012-01-26 14:18:23 +01005298static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299{
Igor Maravic036dafa2012-03-05 00:01:25 +01005300 struct rtl8169_stats *tx_stats = &tp->tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 unsigned int dirty_tx, tx_left;
Igor Maravic036dafa2012-03-05 00:01:25 +01005302 struct rtl_txc txc = { 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 dirty_tx = tp->dirty_tx;
5305 smp_rmb();
5306 tx_left = tp->cur_tx - dirty_tx;
5307
5308 while (tx_left > 0) {
5309 unsigned int entry = dirty_tx % NUM_TX_DESC;
5310 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 u32 status;
5312
5313 rmb();
5314 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5315 if (status & DescOwn)
5316 break;
5317
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005318 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5319 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 if (status & LastFrag) {
Igor Maravic036dafa2012-03-05 00:01:25 +01005321 struct sk_buff *skb = tx_skb->skb;
5322
5323 txc.packets++;
5324 txc.bytes += skb->len;
5325 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 tx_skb->skb = NULL;
5327 }
5328 dirty_tx++;
5329 tx_left--;
5330 }
5331
Igor Maravic036dafa2012-03-05 00:01:25 +01005332 u64_stats_update_begin(&tx_stats->syncp);
5333 tx_stats->packets += txc.packets;
5334 tx_stats->bytes += txc.bytes;
5335 u64_stats_update_end(&tx_stats->syncp);
5336
5337 netdev_completed_queue(dev, txc.packets, txc.bytes);
5338
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 if (tp->dirty_tx != dirty_tx) {
5340 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005341 /* Sync with rtl8169_start_xmit:
5342 * - publish dirty_tx ring index (write barrier)
5343 * - refresh cur_tx ring index and queue status (read barrier)
5344 * May the current thread miss the stopped queue condition,
5345 * a racing xmit thread can only have a right view of the
5346 * ring status.
5347 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005348 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 if (netif_queue_stopped(dev) &&
5350 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5351 netif_wake_queue(dev);
5352 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005353 /*
5354 * 8168 hack: TxPoll requests are lost when the Tx packets are
5355 * too close. Let's kick an extra TxPoll request when a burst
5356 * of start_xmit activity is detected (if it is not detected,
5357 * it is slow enough). -- FR
5358 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005359 if (tp->cur_tx != dirty_tx) {
5360 void __iomem *ioaddr = tp->mmio_addr;
5361
Francois Romieud78ae2d2007-08-26 20:08:19 +02005362 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 }
5365}
5366
Francois Romieu126fa4b2005-05-12 20:09:17 -04005367static inline int rtl8169_fragmented_frame(u32 status)
5368{
5369 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5370}
5371
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005372static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 u32 status = opts1 & RxProtoMask;
5375
5376 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005377 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 skb->ip_summed = CHECKSUM_UNNECESSARY;
5379 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005380 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381}
5382
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005383static struct sk_buff *rtl8169_try_rx_copy(void *data,
5384 struct rtl8169_private *tp,
5385 int pkt_size,
5386 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005388 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005389 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005391 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005392 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005393 prefetch(data);
5394 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5395 if (skb)
5396 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005397 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5398
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005399 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400}
5401
Francois Romieuda78dbf2012-01-26 14:18:23 +01005402static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403{
5404 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005405 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 cur_rx = tp->cur_rx;
5408 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005409 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005411 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005413 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 u32 status;
5415
5416 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005417 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418
5419 if (status & DescOwn)
5420 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005421 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005422 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5423 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005424 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005426 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005428 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005429 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005430 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005431 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005432 }
Ben Greear6bbe0212012-02-10 15:04:33 +00005433 if ((status & (RxRUNT | RxCRC)) &&
5434 !(status & (RxRWT | RxFOVF)) &&
5435 (dev->features & NETIF_F_RXALL))
5436 goto process_pkt;
5437
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005438 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005440 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00005441 dma_addr_t addr;
5442 int pkt_size;
5443
5444process_pkt:
5445 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00005446 if (likely(!(dev->features & NETIF_F_RXFCS)))
5447 pkt_size = (status & 0x00003fff) - 4;
5448 else
5449 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450
Francois Romieu126fa4b2005-05-12 20:09:17 -04005451 /*
5452 * The driver does not support incoming fragmented
5453 * frames. They are seen as a symptom of over-mtu
5454 * sized frames.
5455 */
5456 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005457 dev->stats.rx_dropped++;
5458 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005459 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005460 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005461 }
5462
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005463 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5464 tp, pkt_size, addr);
5465 rtl8169_mark_to_asic(desc, rx_buf_sz);
5466 if (!skb) {
5467 dev->stats.rx_dropped++;
5468 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 }
5470
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005471 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 skb_put(skb, pkt_size);
5473 skb->protocol = eth_type_trans(skb, dev);
5474
Francois Romieu7a8fc772011-03-01 17:18:33 +01005475 rtl8169_rx_vlan_tag(desc, skb);
5476
Francois Romieu56de4142011-03-15 17:29:31 +01005477 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478
Junchang Wang8027aa22012-03-04 23:30:32 +01005479 u64_stats_update_begin(&tp->rx_stats.syncp);
5480 tp->rx_stats.packets++;
5481 tp->rx_stats.bytes += pkt_size;
5482 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005484
5485 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005486 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005487 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5488 desc->opts2 = 0;
5489 cur_rx++;
5490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 }
5492
5493 count = cur_rx - tp->cur_rx;
5494 tp->cur_rx = cur_rx;
5495
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005496 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497
5498 return count;
5499}
5500
Francois Romieu07d3f512007-02-21 22:40:46 +01005501static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502{
Francois Romieu07d3f512007-02-21 22:40:46 +01005503 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005506 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005508 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005509 if (status && status != 0xffff) {
5510 status &= RTL_EVENT_NAPI | tp->event_slow;
5511 if (status) {
5512 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00005513
Francois Romieuda78dbf2012-01-26 14:18:23 +01005514 rtl_irq_disable(tp);
5515 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 return IRQ_RETVAL(handled);
5519}
5520
Francois Romieuda78dbf2012-01-26 14:18:23 +01005521/*
5522 * Workqueue context.
5523 */
5524static void rtl_slow_event_work(struct rtl8169_private *tp)
5525{
5526 struct net_device *dev = tp->dev;
5527 u16 status;
5528
5529 status = rtl_get_events(tp) & tp->event_slow;
5530 rtl_ack_events(tp, status);
5531
5532 if (unlikely(status & RxFIFOOver)) {
5533 switch (tp->mac_version) {
5534 /* Work around for rx fifo overflow */
5535 case RTL_GIGA_MAC_VER_11:
5536 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01005537 /* XXX - Hack alert. See rtl_task(). */
5538 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005539 default:
5540 break;
5541 }
5542 }
5543
5544 if (unlikely(status & SYSErr))
5545 rtl8169_pcierr_interrupt(dev);
5546
5547 if (status & LinkChg)
5548 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
5549
5550 napi_disable(&tp->napi);
5551 rtl_irq_disable(tp);
5552
5553 napi_enable(&tp->napi);
5554 napi_schedule(&tp->napi);
5555}
5556
Francois Romieu4422bcd2012-01-26 11:23:32 +01005557static void rtl_task(struct work_struct *work)
5558{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005559 static const struct {
5560 int bitnr;
5561 void (*action)(struct rtl8169_private *);
5562 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01005563 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005564 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
5565 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
5566 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
5567 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01005568 struct rtl8169_private *tp =
5569 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005570 struct net_device *dev = tp->dev;
5571 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01005572
Francois Romieuda78dbf2012-01-26 14:18:23 +01005573 rtl_lock_work(tp);
5574
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005575 if (!netif_running(dev) ||
5576 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01005577 goto out_unlock;
5578
5579 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
5580 bool pending;
5581
Francois Romieuda78dbf2012-01-26 14:18:23 +01005582 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005583 if (pending)
5584 rtl_work[i].action(tp);
5585 }
5586
5587out_unlock:
5588 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01005589}
5590
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005591static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005593 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5594 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005595 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
5596 int work_done= 0;
5597 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598
Francois Romieuda78dbf2012-01-26 14:18:23 +01005599 status = rtl_get_events(tp);
5600 rtl_ack_events(tp, status & ~tp->event_slow);
5601
5602 if (status & RTL_EVENT_NAPI_RX)
5603 work_done = rtl_rx(dev, tp, (u32) budget);
5604
5605 if (status & RTL_EVENT_NAPI_TX)
5606 rtl_tx(dev, tp);
5607
5608 if (status & tp->event_slow) {
5609 enable_mask &= ~tp->event_slow;
5610
5611 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
5612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005614 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005615 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005616
Francois Romieuda78dbf2012-01-26 14:18:23 +01005617 rtl_irq_enable(tp, enable_mask);
5618 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 }
5620
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005621 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623
Francois Romieu523a6092008-09-10 22:28:56 +02005624static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5625{
5626 struct rtl8169_private *tp = netdev_priv(dev);
5627
5628 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5629 return;
5630
5631 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5632 RTL_W32(RxMissed, 0);
5633}
5634
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635static void rtl8169_down(struct net_device *dev)
5636{
5637 struct rtl8169_private *tp = netdev_priv(dev);
5638 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639
Francois Romieu4876cc12011-03-11 21:07:11 +01005640 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005642 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005643 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644
Hayes Wang92fc43b2011-07-06 15:58:03 +08005645 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005646 /*
5647 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01005648 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
5649 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005650 */
Francois Romieu523a6092008-09-10 22:28:56 +02005651 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005654 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 rtl8169_tx_clear(tp);
5657
5658 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005659
5660 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661}
5662
5663static int rtl8169_close(struct net_device *dev)
5664{
5665 struct rtl8169_private *tp = netdev_priv(dev);
5666 struct pci_dev *pdev = tp->pci_dev;
5667
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005668 pm_runtime_get_sync(&pdev->dev);
5669
Francois Romieucecb5fd2011-04-01 10:21:07 +02005670 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005671 rtl8169_update_counters(dev);
5672
Francois Romieuda78dbf2012-01-26 14:18:23 +01005673 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005674 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005675
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005677 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678
Francois Romieu92a7c4e2012-03-10 10:42:12 +01005679 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005681 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5682 tp->RxPhyAddr);
5683 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5684 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 tp->TxDescArray = NULL;
5686 tp->RxDescArray = NULL;
5687
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005688 pm_runtime_put_sync(&pdev->dev);
5689
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 return 0;
5691}
5692
Francois Romieudc1c00c2012-03-08 10:06:18 +01005693#ifdef CONFIG_NET_POLL_CONTROLLER
5694static void rtl8169_netpoll(struct net_device *dev)
5695{
5696 struct rtl8169_private *tp = netdev_priv(dev);
5697
5698 rtl8169_interrupt(tp->pci_dev->irq, dev);
5699}
5700#endif
5701
Francois Romieudf43ac72012-03-08 09:48:40 +01005702static int rtl_open(struct net_device *dev)
5703{
5704 struct rtl8169_private *tp = netdev_priv(dev);
5705 void __iomem *ioaddr = tp->mmio_addr;
5706 struct pci_dev *pdev = tp->pci_dev;
5707 int retval = -ENOMEM;
5708
5709 pm_runtime_get_sync(&pdev->dev);
5710
5711 /*
5712 * Rx and Tx desscriptors needs 256 bytes alignment.
5713 * dma_alloc_coherent provides more.
5714 */
5715 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
5716 &tp->TxPhyAddr, GFP_KERNEL);
5717 if (!tp->TxDescArray)
5718 goto err_pm_runtime_put;
5719
5720 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
5721 &tp->RxPhyAddr, GFP_KERNEL);
5722 if (!tp->RxDescArray)
5723 goto err_free_tx_0;
5724
5725 retval = rtl8169_init_ring(dev);
5726 if (retval < 0)
5727 goto err_free_rx_1;
5728
5729 INIT_WORK(&tp->wk.work, rtl_task);
5730
5731 smp_mb();
5732
5733 rtl_request_firmware(tp);
5734
Francois Romieu92a7c4e2012-03-10 10:42:12 +01005735 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01005736 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
5737 dev->name, dev);
5738 if (retval < 0)
5739 goto err_release_fw_2;
5740
5741 rtl_lock_work(tp);
5742
5743 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
5744
5745 napi_enable(&tp->napi);
5746
5747 rtl8169_init_phy(dev, tp);
5748
5749 __rtl8169_set_features(dev, dev->features);
5750
5751 rtl_pll_power_up(tp);
5752
5753 rtl_hw_start(dev);
5754
5755 netif_start_queue(dev);
5756
5757 rtl_unlock_work(tp);
5758
5759 tp->saved_wolopts = 0;
5760 pm_runtime_put_noidle(&pdev->dev);
5761
5762 rtl8169_check_link_status(dev, tp, ioaddr);
5763out:
5764 return retval;
5765
5766err_release_fw_2:
5767 rtl_release_firmware(tp);
5768 rtl8169_rx_clear(tp);
5769err_free_rx_1:
5770 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5771 tp->RxPhyAddr);
5772 tp->RxDescArray = NULL;
5773err_free_tx_0:
5774 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5775 tp->TxPhyAddr);
5776 tp->TxDescArray = NULL;
5777err_pm_runtime_put:
5778 pm_runtime_put_noidle(&pdev->dev);
5779 goto out;
5780}
5781
Junchang Wang8027aa22012-03-04 23:30:32 +01005782static struct rtnl_link_stats64 *
5783rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784{
5785 struct rtl8169_private *tp = netdev_priv(dev);
5786 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01005787 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788
Francois Romieuda78dbf2012-01-26 14:18:23 +01005789 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02005790 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02005791
Junchang Wang8027aa22012-03-04 23:30:32 +01005792 do {
5793 start = u64_stats_fetch_begin_bh(&tp->rx_stats.syncp);
5794 stats->rx_packets = tp->rx_stats.packets;
5795 stats->rx_bytes = tp->rx_stats.bytes;
5796 } while (u64_stats_fetch_retry_bh(&tp->rx_stats.syncp, start));
5797
5798
5799 do {
5800 start = u64_stats_fetch_begin_bh(&tp->tx_stats.syncp);
5801 stats->tx_packets = tp->tx_stats.packets;
5802 stats->tx_bytes = tp->tx_stats.bytes;
5803 } while (u64_stats_fetch_retry_bh(&tp->tx_stats.syncp, start));
5804
5805 stats->rx_dropped = dev->stats.rx_dropped;
5806 stats->tx_dropped = dev->stats.tx_dropped;
5807 stats->rx_length_errors = dev->stats.rx_length_errors;
5808 stats->rx_errors = dev->stats.rx_errors;
5809 stats->rx_crc_errors = dev->stats.rx_crc_errors;
5810 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
5811 stats->rx_missed_errors = dev->stats.rx_missed_errors;
5812
5813 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814}
5815
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005816static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005817{
françois romieu065c27c2011-01-03 15:08:12 +00005818 struct rtl8169_private *tp = netdev_priv(dev);
5819
Francois Romieu5d06a992006-02-23 00:47:58 +01005820 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005821 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005822
5823 netif_device_detach(dev);
5824 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005825
5826 rtl_lock_work(tp);
5827 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005828 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005829 rtl_unlock_work(tp);
5830
5831 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005832}
Francois Romieu5d06a992006-02-23 00:47:58 +01005833
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005834#ifdef CONFIG_PM
5835
5836static int rtl8169_suspend(struct device *device)
5837{
5838 struct pci_dev *pdev = to_pci_dev(device);
5839 struct net_device *dev = pci_get_drvdata(pdev);
5840
5841 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005842
Francois Romieu5d06a992006-02-23 00:47:58 +01005843 return 0;
5844}
5845
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005846static void __rtl8169_resume(struct net_device *dev)
5847{
françois romieu065c27c2011-01-03 15:08:12 +00005848 struct rtl8169_private *tp = netdev_priv(dev);
5849
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005850 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005851
5852 rtl_pll_power_up(tp);
5853
Artem Savkovcff4c162012-04-03 10:29:11 +00005854 rtl_lock_work(tp);
5855 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005856 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00005857 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005858
Francois Romieu98ddf982012-01-31 10:47:34 +01005859 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005860}
5861
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005862static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005863{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005864 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005865 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005866 struct rtl8169_private *tp = netdev_priv(dev);
5867
5868 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005869
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005870 if (netif_running(dev))
5871 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005872
Francois Romieu5d06a992006-02-23 00:47:58 +01005873 return 0;
5874}
5875
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005876static int rtl8169_runtime_suspend(struct device *device)
5877{
5878 struct pci_dev *pdev = to_pci_dev(device);
5879 struct net_device *dev = pci_get_drvdata(pdev);
5880 struct rtl8169_private *tp = netdev_priv(dev);
5881
5882 if (!tp->TxDescArray)
5883 return 0;
5884
Francois Romieuda78dbf2012-01-26 14:18:23 +01005885 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005886 tp->saved_wolopts = __rtl8169_get_wol(tp);
5887 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005888 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005889
5890 rtl8169_net_suspend(dev);
5891
5892 return 0;
5893}
5894
5895static int rtl8169_runtime_resume(struct device *device)
5896{
5897 struct pci_dev *pdev = to_pci_dev(device);
5898 struct net_device *dev = pci_get_drvdata(pdev);
5899 struct rtl8169_private *tp = netdev_priv(dev);
5900
5901 if (!tp->TxDescArray)
5902 return 0;
5903
Francois Romieuda78dbf2012-01-26 14:18:23 +01005904 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005905 __rtl8169_set_wol(tp, tp->saved_wolopts);
5906 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005907 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005908
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005909 rtl8169_init_phy(dev, tp);
5910
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005911 __rtl8169_resume(dev);
5912
5913 return 0;
5914}
5915
5916static int rtl8169_runtime_idle(struct device *device)
5917{
5918 struct pci_dev *pdev = to_pci_dev(device);
5919 struct net_device *dev = pci_get_drvdata(pdev);
5920 struct rtl8169_private *tp = netdev_priv(dev);
5921
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005922 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005923}
5924
Alexey Dobriyan47145212009-12-14 18:00:08 -08005925static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005926 .suspend = rtl8169_suspend,
5927 .resume = rtl8169_resume,
5928 .freeze = rtl8169_suspend,
5929 .thaw = rtl8169_resume,
5930 .poweroff = rtl8169_suspend,
5931 .restore = rtl8169_resume,
5932 .runtime_suspend = rtl8169_runtime_suspend,
5933 .runtime_resume = rtl8169_runtime_resume,
5934 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005935};
5936
5937#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5938
5939#else /* !CONFIG_PM */
5940
5941#define RTL8169_PM_OPS NULL
5942
5943#endif /* !CONFIG_PM */
5944
David S. Miller1805b2f2011-10-24 18:18:09 -04005945static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
5946{
5947 void __iomem *ioaddr = tp->mmio_addr;
5948
5949 /* WoL fails with 8168b when the receiver is disabled. */
5950 switch (tp->mac_version) {
5951 case RTL_GIGA_MAC_VER_11:
5952 case RTL_GIGA_MAC_VER_12:
5953 case RTL_GIGA_MAC_VER_17:
5954 pci_clear_master(tp->pci_dev);
5955
5956 RTL_W8(ChipCmd, CmdRxEnb);
5957 /* PCI commit */
5958 RTL_R8(ChipCmd);
5959 break;
5960 default:
5961 break;
5962 }
5963}
5964
Francois Romieu1765f952008-09-13 17:21:40 +02005965static void rtl_shutdown(struct pci_dev *pdev)
5966{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005967 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005968 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00005969 struct device *d = &pdev->dev;
5970
5971 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02005972
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005973 rtl8169_net_suspend(dev);
5974
Francois Romieucecb5fd2011-04-01 10:21:07 +02005975 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08005976 rtl_rar_set(tp, dev->perm_addr);
5977
Hayes Wang92fc43b2011-07-06 15:58:03 +08005978 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00005979
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005980 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04005981 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
5982 rtl_wol_suspend_quirk(tp);
5983 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00005984 }
5985
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005986 pci_wake_from_d3(pdev, true);
5987 pci_set_power_state(pdev, PCI_D3hot);
5988 }
françois romieu2a15cd22012-03-06 01:14:12 +00005989
5990 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005991}
Francois Romieu5d06a992006-02-23 00:47:58 +01005992
Francois Romieue27566e2012-03-08 09:54:01 +01005993static void __devexit rtl_remove_one(struct pci_dev *pdev)
5994{
5995 struct net_device *dev = pci_get_drvdata(pdev);
5996 struct rtl8169_private *tp = netdev_priv(dev);
5997
5998 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
5999 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6000 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6001 rtl8168_driver_stop(tp);
6002 }
6003
6004 cancel_work_sync(&tp->wk.work);
6005
6006 unregister_netdev(dev);
6007
6008 rtl_release_firmware(tp);
6009
6010 if (pci_dev_run_wake(pdev))
6011 pm_runtime_get_noresume(&pdev->dev);
6012
6013 /* restore original MAC address */
6014 rtl_rar_set(tp, dev->perm_addr);
6015
6016 rtl_disable_msi(pdev, tp);
6017 rtl8169_release_board(pdev, dev, tp->mmio_addr);
6018 pci_set_drvdata(pdev, NULL);
6019}
6020
Francois Romieufa9c3852012-03-08 10:01:50 +01006021static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01006022 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01006023 .ndo_stop = rtl8169_close,
6024 .ndo_get_stats64 = rtl8169_get_stats64,
6025 .ndo_start_xmit = rtl8169_start_xmit,
6026 .ndo_tx_timeout = rtl8169_tx_timeout,
6027 .ndo_validate_addr = eth_validate_addr,
6028 .ndo_change_mtu = rtl8169_change_mtu,
6029 .ndo_fix_features = rtl8169_fix_features,
6030 .ndo_set_features = rtl8169_set_features,
6031 .ndo_set_mac_address = rtl_set_mac_address,
6032 .ndo_do_ioctl = rtl8169_ioctl,
6033 .ndo_set_rx_mode = rtl_set_rx_mode,
6034#ifdef CONFIG_NET_POLL_CONTROLLER
6035 .ndo_poll_controller = rtl8169_netpoll,
6036#endif
6037
6038};
6039
Francois Romieu31fa8b12012-03-08 10:09:40 +01006040static const struct rtl_cfg_info {
6041 void (*hw_start)(struct net_device *);
6042 unsigned int region;
6043 unsigned int align;
6044 u16 event_slow;
6045 unsigned features;
6046 u8 default_ver;
6047} rtl_cfg_infos [] = {
6048 [RTL_CFG_0] = {
6049 .hw_start = rtl_hw_start_8169,
6050 .region = 1,
6051 .align = 0,
6052 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
6053 .features = RTL_FEATURE_GMII,
6054 .default_ver = RTL_GIGA_MAC_VER_01,
6055 },
6056 [RTL_CFG_1] = {
6057 .hw_start = rtl_hw_start_8168,
6058 .region = 2,
6059 .align = 8,
6060 .event_slow = SYSErr | LinkChg | RxOverflow,
6061 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
6062 .default_ver = RTL_GIGA_MAC_VER_11,
6063 },
6064 [RTL_CFG_2] = {
6065 .hw_start = rtl_hw_start_8101,
6066 .region = 2,
6067 .align = 8,
6068 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
6069 PCSTimeout,
6070 .features = RTL_FEATURE_MSI,
6071 .default_ver = RTL_GIGA_MAC_VER_13,
6072 }
6073};
6074
6075/* Cfg9346_Unlock assumed. */
6076static unsigned rtl_try_msi(struct rtl8169_private *tp,
6077 const struct rtl_cfg_info *cfg)
6078{
6079 void __iomem *ioaddr = tp->mmio_addr;
6080 unsigned msi = 0;
6081 u8 cfg2;
6082
6083 cfg2 = RTL_R8(Config2) & ~MSIEnable;
6084 if (cfg->features & RTL_FEATURE_MSI) {
6085 if (pci_enable_msi(tp->pci_dev)) {
6086 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
6087 } else {
6088 cfg2 |= MSIEnable;
6089 msi = RTL_FEATURE_MSI;
6090 }
6091 }
6092 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6093 RTL_W8(Config2, cfg2);
6094 return msi;
6095}
6096
Francois Romieu3b6cf252012-03-08 09:59:04 +01006097static int __devinit
6098rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6099{
6100 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
6101 const unsigned int region = cfg->region;
6102 struct rtl8169_private *tp;
6103 struct mii_if_info *mii;
6104 struct net_device *dev;
6105 void __iomem *ioaddr;
6106 int chipset, i;
6107 int rc;
6108
6109 if (netif_msg_drv(&debug)) {
6110 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
6111 MODULENAME, RTL8169_VERSION);
6112 }
6113
6114 dev = alloc_etherdev(sizeof (*tp));
6115 if (!dev) {
6116 rc = -ENOMEM;
6117 goto out;
6118 }
6119
6120 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01006121 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006122 tp = netdev_priv(dev);
6123 tp->dev = dev;
6124 tp->pci_dev = pdev;
6125 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
6126
6127 mii = &tp->mii;
6128 mii->dev = dev;
6129 mii->mdio_read = rtl_mdio_read;
6130 mii->mdio_write = rtl_mdio_write;
6131 mii->phy_id_mask = 0x1f;
6132 mii->reg_num_mask = 0x1f;
6133 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
6134
6135 /* disable ASPM completely as that cause random device stop working
6136 * problems as well as full system hangs for some PCIe devices users */
6137 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6138 PCIE_LINK_STATE_CLKPM);
6139
6140 /* enable device (incl. PCI PM wakeup and hotplug setup) */
6141 rc = pci_enable_device(pdev);
6142 if (rc < 0) {
6143 netif_err(tp, probe, dev, "enable failure\n");
6144 goto err_out_free_dev_1;
6145 }
6146
6147 if (pci_set_mwi(pdev) < 0)
6148 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
6149
6150 /* make sure PCI base addr 1 is MMIO */
6151 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
6152 netif_err(tp, probe, dev,
6153 "region #%d not an MMIO resource, aborting\n",
6154 region);
6155 rc = -ENODEV;
6156 goto err_out_mwi_2;
6157 }
6158
6159 /* check for weird/broken PCI region reporting */
6160 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
6161 netif_err(tp, probe, dev,
6162 "Invalid PCI region size(s), aborting\n");
6163 rc = -ENODEV;
6164 goto err_out_mwi_2;
6165 }
6166
6167 rc = pci_request_regions(pdev, MODULENAME);
6168 if (rc < 0) {
6169 netif_err(tp, probe, dev, "could not request regions\n");
6170 goto err_out_mwi_2;
6171 }
6172
6173 tp->cp_cmd = RxChkSum;
6174
6175 if ((sizeof(dma_addr_t) > 4) &&
6176 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
6177 tp->cp_cmd |= PCIDAC;
6178 dev->features |= NETIF_F_HIGHDMA;
6179 } else {
6180 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6181 if (rc < 0) {
6182 netif_err(tp, probe, dev, "DMA configuration failed\n");
6183 goto err_out_free_res_3;
6184 }
6185 }
6186
6187 /* ioremap MMIO region */
6188 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
6189 if (!ioaddr) {
6190 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
6191 rc = -EIO;
6192 goto err_out_free_res_3;
6193 }
6194 tp->mmio_addr = ioaddr;
6195
6196 if (!pci_is_pcie(pdev))
6197 netif_info(tp, probe, dev, "not PCI Express\n");
6198
6199 /* Identify chip attached to board */
6200 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
6201
6202 rtl_init_rxcfg(tp);
6203
6204 rtl_irq_disable(tp);
6205
6206 rtl_hw_reset(tp);
6207
6208 rtl_ack_events(tp, 0xffff);
6209
6210 pci_set_master(pdev);
6211
6212 /*
6213 * Pretend we are using VLANs; This bypasses a nasty bug where
6214 * Interrupts stop flowing on high load on 8110SCd controllers.
6215 */
6216 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6217 tp->cp_cmd |= RxVlan;
6218
6219 rtl_init_mdio_ops(tp);
6220 rtl_init_pll_power_ops(tp);
6221 rtl_init_jumbo_ops(tp);
6222
6223 rtl8169_print_mac_version(tp);
6224
6225 chipset = tp->mac_version;
6226 tp->txd_version = rtl_chip_infos[chipset].txd_version;
6227
6228 RTL_W8(Cfg9346, Cfg9346_Unlock);
6229 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
6230 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
6231 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
6232 tp->features |= RTL_FEATURE_WOL;
6233 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
6234 tp->features |= RTL_FEATURE_WOL;
6235 tp->features |= rtl_try_msi(tp, cfg);
6236 RTL_W8(Cfg9346, Cfg9346_Lock);
6237
6238 if (rtl_tbi_enabled(tp)) {
6239 tp->set_speed = rtl8169_set_speed_tbi;
6240 tp->get_settings = rtl8169_gset_tbi;
6241 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
6242 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
6243 tp->link_ok = rtl8169_tbi_link_ok;
6244 tp->do_ioctl = rtl_tbi_ioctl;
6245 } else {
6246 tp->set_speed = rtl8169_set_speed_xmii;
6247 tp->get_settings = rtl8169_gset_xmii;
6248 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
6249 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
6250 tp->link_ok = rtl8169_xmii_link_ok;
6251 tp->do_ioctl = rtl_xmii_ioctl;
6252 }
6253
6254 mutex_init(&tp->wk.mutex);
6255
6256 /* Get MAC address */
6257 for (i = 0; i < ETH_ALEN; i++)
6258 dev->dev_addr[i] = RTL_R8(MAC0 + i);
6259 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
6260
6261 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
6262 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006263
6264 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
6265
6266 /* don't enable SG, IP_CSUM and TSO by default - it might not work
6267 * properly for all devices */
6268 dev->features |= NETIF_F_RXCSUM |
6269 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6270
6271 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6272 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6273 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6274 NETIF_F_HIGHDMA;
6275
6276 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6277 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
6278 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
6279
6280 dev->hw_features |= NETIF_F_RXALL;
6281 dev->hw_features |= NETIF_F_RXFCS;
6282
6283 tp->hw_start = cfg->hw_start;
6284 tp->event_slow = cfg->event_slow;
6285
6286 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
6287 ~(RxBOVF | RxFOVF) : ~0;
6288
6289 init_timer(&tp->timer);
6290 tp->timer.data = (unsigned long) dev;
6291 tp->timer.function = rtl8169_phy_timer;
6292
6293 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
6294
6295 rc = register_netdev(dev);
6296 if (rc < 0)
6297 goto err_out_msi_4;
6298
6299 pci_set_drvdata(pdev, dev);
6300
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006301 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
6302 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
6303 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006304 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
6305 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
6306 "tx checksumming: %s]\n",
6307 rtl_chip_infos[chipset].jumbo_max,
6308 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
6309 }
6310
6311 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6312 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6313 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6314 rtl8168_driver_start(tp);
6315 }
6316
6317 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
6318
6319 if (pci_dev_run_wake(pdev))
6320 pm_runtime_put_noidle(&pdev->dev);
6321
6322 netif_carrier_off(dev);
6323
6324out:
6325 return rc;
6326
6327err_out_msi_4:
6328 rtl_disable_msi(pdev, tp);
6329 iounmap(ioaddr);
6330err_out_free_res_3:
6331 pci_release_regions(pdev);
6332err_out_mwi_2:
6333 pci_clear_mwi(pdev);
6334 pci_disable_device(pdev);
6335err_out_free_dev_1:
6336 free_netdev(dev);
6337 goto out;
6338}
6339
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340static struct pci_driver rtl8169_pci_driver = {
6341 .name = MODULENAME,
6342 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01006343 .probe = rtl_init_one,
Francois Romieue27566e2012-03-08 09:54:01 +01006344 .remove = __devexit_p(rtl_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02006345 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006346 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347};
6348
Francois Romieu07d3f512007-02-21 22:40:46 +01006349static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350{
Jeff Garzik29917622006-08-19 17:48:59 -04006351 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352}
6353
Francois Romieu07d3f512007-02-21 22:40:46 +01006354static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355{
6356 pci_unregister_driver(&rtl8169_pci_driver);
6357}
6358
6359module_init(rtl8169_init_module);
6360module_exit(rtl8169_cleanup_module);