blob: f54509377efad8354345176fdb9c1951a9fdd4de [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,
318 Config3 = 0x54,
319 Config4 = 0x55,
320 Config5 = 0x56,
321 MultiIntr = 0x5c,
322 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100323 PHYstatus = 0x6c,
324 RxMaxSize = 0xda,
325 CPlusCmd = 0xe0,
326 IntrMitigate = 0xe2,
327 RxDescAddrLow = 0xe4,
328 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000329 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
330
331#define NoEarlyTx 0x3f /* Max value : no early transmit. */
332
333 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
334
335#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800336#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000337
Francois Romieu07d3f512007-02-21 22:40:46 +0100338 FuncEvent = 0xf0,
339 FuncEventMask = 0xf4,
340 FuncPresetState = 0xf8,
341 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342};
343
Francois Romieuf162a5d2008-06-01 22:37:49 +0200344enum rtl8110_registers {
345 TBICSR = 0x64,
346 TBI_ANAR = 0x68,
347 TBI_LPAR = 0x6a,
348};
349
350enum rtl8168_8101_registers {
351 CSIDR = 0x64,
352 CSIAR = 0x68,
353#define CSIAR_FLAG 0x80000000
354#define CSIAR_WRITE_CMD 0x80000000
355#define CSIAR_BYTE_ENABLE 0x0f
356#define CSIAR_BYTE_ENABLE_SHIFT 12
357#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000358 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200359 EPHYAR = 0x80,
360#define EPHYAR_FLAG 0x80000000
361#define EPHYAR_WRITE_CMD 0x80000000
362#define EPHYAR_REG_MASK 0x1f
363#define EPHYAR_REG_SHIFT 16
364#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800365 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800366#define PFM_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200367 DBG_REG = 0xd1,
368#define FIX_NAK_1 (1 << 4)
369#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800370 TWSI = 0xd2,
371 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800372#define NOW_IS_OOB (1 << 7)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800373#define EN_NDP (1 << 3)
374#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000375 EFUSEAR = 0xdc,
376#define EFUSEAR_FLAG 0x80000000
377#define EFUSEAR_WRITE_CMD 0x80000000
378#define EFUSEAR_READ_CMD 0x00000000
379#define EFUSEAR_REG_MASK 0x03ff
380#define EFUSEAR_REG_SHIFT 8
381#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200382};
383
françois romieuc0e45c12011-01-03 15:08:04 +0000384enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800385 LED_FREQ = 0x1a,
386 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000387 ERIDR = 0x70,
388 ERIAR = 0x74,
389#define ERIAR_FLAG 0x80000000
390#define ERIAR_WRITE_CMD 0x80000000
391#define ERIAR_READ_CMD 0x00000000
392#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000393#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800394#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
395#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
396#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
397#define ERIAR_MASK_SHIFT 12
398#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
399#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
400#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000401 EPHY_RXER_NUM = 0x7c,
402 OCPDR = 0xb0, /* OCP GPHY access */
403#define OCPDR_WRITE_CMD 0x80000000
404#define OCPDR_READ_CMD 0x00000000
405#define OCPDR_REG_MASK 0x7f
406#define OCPDR_GPHY_REG_SHIFT 16
407#define OCPDR_DATA_MASK 0xffff
408 OCPAR = 0xb4,
409#define OCPAR_FLAG 0x80000000
410#define OCPAR_GPHY_WRITE_CMD 0x8000f060
411#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000412 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
413 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200414#define TXPLA_RST (1 << 29)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800415#define PWM_EN (1 << 22)
françois romieuc0e45c12011-01-03 15:08:04 +0000416};
417
Francois Romieu07d3f512007-02-21 22:40:46 +0100418enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100420 SYSErr = 0x8000,
421 PCSTimeout = 0x4000,
422 SWInt = 0x0100,
423 TxDescUnavail = 0x0080,
424 RxFIFOOver = 0x0040,
425 LinkChg = 0x0020,
426 RxOverflow = 0x0010,
427 TxErr = 0x0008,
428 TxOK = 0x0004,
429 RxErr = 0x0002,
430 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400433 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200434 RxFOVF = (1 << 23),
435 RxRWT = (1 << 22),
436 RxRES = (1 << 21),
437 RxRUNT = (1 << 20),
438 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800441 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100442 CmdReset = 0x10,
443 CmdRxEnb = 0x08,
444 CmdTxEnb = 0x04,
445 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Francois Romieu275391a2007-02-23 23:50:28 +0100447 /* TXPoll register p.5 */
448 HPQ = 0x80, /* Poll cmd on the high prio queue */
449 NPQ = 0x40, /* Poll cmd on the low prio queue */
450 FSWInt = 0x01, /* Forced software interrupt */
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100453 Cfg9346_Lock = 0x00,
454 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100457 AcceptErr = 0x20,
458 AcceptRunt = 0x10,
459 AcceptBroadcast = 0x08,
460 AcceptMulticast = 0x04,
461 AcceptMyPhys = 0x02,
462 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200463#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /* TxConfigBits */
466 TxInterFrameGapShift = 24,
467 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
468
Francois Romieu5d06a992006-02-23 00:47:58 +0100469 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200470 LEDS1 = (1 << 7),
471 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200472 Speed_down = (1 << 4),
473 MEMMAP = (1 << 3),
474 IOMAP = (1 << 2),
475 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100476 PMEnable = (1 << 0), /* Power Management Enable */
477
Francois Romieu6dccd162007-02-13 23:38:05 +0100478 /* Config2 register p. 25 */
françois romieu2ca6cf02011-12-15 08:37:43 +0000479 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100480 PCI_Clock_66MHz = 0x01,
481 PCI_Clock_33MHz = 0x00,
482
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100483 /* Config3 register p.25 */
484 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
485 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200486 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200487 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100488
Francois Romieud58d46b2011-05-03 16:38:29 +0200489 /* Config4 register */
490 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
491
Francois Romieu5d06a992006-02-23 00:47:58 +0100492 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100493 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
494 MWF = (1 << 5), /* Accept Multicast wakeup frame */
495 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200496 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100497 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100498 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /* TBICSR p.28 */
501 TBIReset = 0x80000000,
502 TBILoopback = 0x40000000,
503 TBINwEnable = 0x20000000,
504 TBINwRestart = 0x10000000,
505 TBILinkOk = 0x02000000,
506 TBINwComplete = 0x01000000,
507
508 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200509 EnableBist = (1 << 15), // 8168 8101
510 Mac_dbgo_oe = (1 << 14), // 8168 8101
511 Normal_mode = (1 << 13), // unused
512 Force_half_dup = (1 << 12), // 8168 8101
513 Force_rxflow_en = (1 << 11), // 8168 8101
514 Force_txflow_en = (1 << 10), // 8168 8101
515 Cxpl_dbg_sel = (1 << 9), // 8168 8101
516 ASF = (1 << 8), // 8168 8101
517 PktCntrDisable = (1 << 7), // 8168 8101
518 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 RxVlan = (1 << 6),
520 RxChkSum = (1 << 5),
521 PCIDAC = (1 << 4),
522 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100523 INTT_0 = 0x0000, // 8168
524 INTT_1 = 0x0001, // 8168
525 INTT_2 = 0x0002, // 8168
526 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100529 TBI_Enable = 0x80,
530 TxFlowCtrl = 0x40,
531 RxFlowCtrl = 0x20,
532 _1000bpsF = 0x10,
533 _100bps = 0x08,
534 _10bps = 0x04,
535 LinkStatus = 0x02,
536 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100539 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200540
541 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100542 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543};
544
Francois Romieu2b7b4312011-04-18 22:53:24 -0700545enum rtl_desc_bit {
546 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
548 RingEnd = (1 << 30), /* End of descriptor ring */
549 FirstFrag = (1 << 29), /* First segment of a packet */
550 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700551};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Francois Romieu2b7b4312011-04-18 22:53:24 -0700553/* Generic case. */
554enum rtl_tx_desc_bit {
555 /* First doubleword. */
556 TD_LSO = (1 << 27), /* Large Send Offload */
557#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Francois Romieu2b7b4312011-04-18 22:53:24 -0700559 /* Second doubleword. */
560 TxVlanTag = (1 << 17), /* Add VLAN tag */
561};
562
563/* 8169, 8168b and 810x except 8102e. */
564enum rtl_tx_desc_bit_0 {
565 /* First doubleword. */
566#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
567 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
568 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
569 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
570};
571
572/* 8102e, 8168c and beyond. */
573enum rtl_tx_desc_bit_1 {
574 /* Second doubleword. */
575#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
576 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
577 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
578 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
579};
580
581static const struct rtl_tx_desc_info {
582 struct {
583 u32 udp;
584 u32 tcp;
585 } checksum;
586 u16 mss_shift;
587 u16 opts_offset;
588} tx_desc_info [] = {
589 [RTL_TD_0] = {
590 .checksum = {
591 .udp = TD0_IP_CS | TD0_UDP_CS,
592 .tcp = TD0_IP_CS | TD0_TCP_CS
593 },
594 .mss_shift = TD0_MSS_SHIFT,
595 .opts_offset = 0
596 },
597 [RTL_TD_1] = {
598 .checksum = {
599 .udp = TD1_IP_CS | TD1_UDP_CS,
600 .tcp = TD1_IP_CS | TD1_TCP_CS
601 },
602 .mss_shift = TD1_MSS_SHIFT,
603 .opts_offset = 1
604 }
605};
606
607enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /* Rx private */
609 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
610 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
611
612#define RxProtoUDP (PID1)
613#define RxProtoTCP (PID0)
614#define RxProtoIP (PID1 | PID0)
615#define RxProtoMask RxProtoIP
616
617 IPFail = (1 << 16), /* IP checksum failed */
618 UDPFail = (1 << 15), /* UDP/IP checksum failed */
619 TCPFail = (1 << 14), /* TCP/IP checksum failed */
620 RxVlanTag = (1 << 16), /* VLAN tag available */
621};
622
623#define RsvdMask 0x3fffc000
624
625struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200626 __le32 opts1;
627 __le32 opts2;
628 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629};
630
631struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200632 __le32 opts1;
633 __le32 opts2;
634 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635};
636
637struct ring_info {
638 struct sk_buff *skb;
639 u32 len;
640 u8 __pad[sizeof(void *) - sizeof(u32)];
641};
642
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200643enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200644 RTL_FEATURE_WOL = (1 << 0),
645 RTL_FEATURE_MSI = (1 << 1),
646 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200647};
648
Ivan Vecera355423d2009-02-06 21:49:57 -0800649struct rtl8169_counters {
650 __le64 tx_packets;
651 __le64 rx_packets;
652 __le64 tx_errors;
653 __le32 rx_errors;
654 __le16 rx_missed;
655 __le16 align_errors;
656 __le32 tx_one_collision;
657 __le32 tx_multi_collision;
658 __le64 rx_unicast;
659 __le64 rx_broadcast;
660 __le32 rx_multicast;
661 __le16 tx_aborted;
662 __le16 tx_underun;
663};
664
Francois Romieuda78dbf2012-01-26 14:18:23 +0100665enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100666 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100667 RTL_FLAG_TASK_SLOW_PENDING,
668 RTL_FLAG_TASK_RESET_PENDING,
669 RTL_FLAG_TASK_PHY_PENDING,
670 RTL_FLAG_MAX
671};
672
Junchang Wang8027aa22012-03-04 23:30:32 +0100673struct rtl8169_stats {
674 u64 packets;
675 u64 bytes;
676 struct u64_stats_sync syncp;
677};
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679struct rtl8169_private {
680 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200681 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000682 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700683 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200684 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700685 u16 txd_version;
686 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
688 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
689 u32 dirty_rx;
690 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100691 struct rtl8169_stats rx_stats;
692 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
694 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
695 dma_addr_t TxPhyAddr;
696 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000697 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 struct timer_list timer;
700 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100701
702 u16 event_slow;
françois romieuc0e45c12011-01-03 15:08:04 +0000703
704 struct mdio_ops {
705 void (*write)(void __iomem *, int, int);
706 int (*read)(void __iomem *, int);
707 } mdio_ops;
708
françois romieu065c27c2011-01-03 15:08:12 +0000709 struct pll_power_ops {
710 void (*down)(struct rtl8169_private *);
711 void (*up)(struct rtl8169_private *);
712 } pll_power_ops;
713
Francois Romieud58d46b2011-05-03 16:38:29 +0200714 struct jumbo_ops {
715 void (*enable)(struct rtl8169_private *);
716 void (*disable)(struct rtl8169_private *);
717 } jumbo_ops;
718
Oliver Neukum54405cd2011-01-06 21:55:13 +0100719 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200720 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000721 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100722 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000723 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800725 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100726
727 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100728 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
729 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100730 struct work_struct work;
731 } wk;
732
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200733 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200734
735 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800736 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000737 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400738 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000739
Francois Romieub6ffd972011-06-17 17:00:05 +0200740 struct rtl_fw {
741 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200742
743#define RTL_VER_SIZE 32
744
745 char version[RTL_VER_SIZE];
746
747 struct rtl_fw_phy_action {
748 __le32 *code;
749 size_t size;
750 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200751 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300752#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753};
754
Ralf Baechle979b6c12005-06-13 14:30:40 -0700755MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700758MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200759module_param_named(debug, debug.msg_enable, int, 0);
760MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761MODULE_LICENSE("GPL");
762MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000763MODULE_FIRMWARE(FIRMWARE_8168D_1);
764MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000765MODULE_FIRMWARE(FIRMWARE_8168E_1);
766MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400767MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800768MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800769MODULE_FIRMWARE(FIRMWARE_8168F_1);
770MODULE_FIRMWARE(FIRMWARE_8168F_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Francois Romieuda78dbf2012-01-26 14:18:23 +0100772static void rtl_lock_work(struct rtl8169_private *tp)
773{
774 mutex_lock(&tp->wk.mutex);
775}
776
777static void rtl_unlock_work(struct rtl8169_private *tp)
778{
779 mutex_unlock(&tp->wk.mutex);
780}
781
Francois Romieud58d46b2011-05-03 16:38:29 +0200782static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
783{
784 int cap = pci_pcie_cap(pdev);
785
786 if (cap) {
787 u16 ctl;
788
789 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
790 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
791 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
792 }
793}
794
françois romieub646d902011-01-03 15:08:21 +0000795static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
796{
797 void __iomem *ioaddr = tp->mmio_addr;
798 int i;
799
800 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
801 for (i = 0; i < 20; i++) {
802 udelay(100);
803 if (RTL_R32(OCPAR) & OCPAR_FLAG)
804 break;
805 }
806 return RTL_R32(OCPDR);
807}
808
809static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
810{
811 void __iomem *ioaddr = tp->mmio_addr;
812 int i;
813
814 RTL_W32(OCPDR, data);
815 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
816 for (i = 0; i < 20; i++) {
817 udelay(100);
818 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
819 break;
820 }
821}
822
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800823static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000824{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800825 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000826 int i;
827
828 RTL_W8(ERIDR, cmd);
829 RTL_W32(ERIAR, 0x800010e8);
830 msleep(2);
831 for (i = 0; i < 5; i++) {
832 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200833 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000834 break;
835 }
836
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800837 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000838}
839
840#define OOB_CMD_RESET 0x00
841#define OOB_CMD_DRIVER_START 0x05
842#define OOB_CMD_DRIVER_STOP 0x06
843
Francois Romieucecb5fd2011-04-01 10:21:07 +0200844static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
845{
846 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
847}
848
françois romieub646d902011-01-03 15:08:21 +0000849static void rtl8168_driver_start(struct rtl8169_private *tp)
850{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200851 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000852 int i;
853
854 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
855
Francois Romieucecb5fd2011-04-01 10:21:07 +0200856 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000857
françois romieub646d902011-01-03 15:08:21 +0000858 for (i = 0; i < 10; i++) {
859 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000860 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000861 break;
862 }
863}
864
865static void rtl8168_driver_stop(struct rtl8169_private *tp)
866{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200867 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000868 int i;
869
870 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
871
Francois Romieucecb5fd2011-04-01 10:21:07 +0200872 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000873
françois romieub646d902011-01-03 15:08:21 +0000874 for (i = 0; i < 10; i++) {
875 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000876 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000877 break;
878 }
879}
880
hayeswang4804b3b2011-03-21 01:50:29 +0000881static int r8168dp_check_dash(struct rtl8169_private *tp)
882{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200883 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000884
Francois Romieucecb5fd2011-04-01 10:21:07 +0200885 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000886}
françois romieub646d902011-01-03 15:08:21 +0000887
françois romieu4da19632011-01-03 15:07:55 +0000888static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 int i;
891
Francois Romieua6baf3a2007-11-08 23:23:21 +0100892 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Francois Romieu23714082006-01-29 00:49:09 +0100894 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100895 /*
896 * Check if the RTL8169 has completed writing to the specified
897 * MII register.
898 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200899 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 break;
Francois Romieu23714082006-01-29 00:49:09 +0100901 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700903 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700904 * According to hardware specs a 20us delay is required after write
905 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700906 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700907 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
françois romieu4da19632011-01-03 15:07:55 +0000910static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 int i, value = -1;
913
Francois Romieua6baf3a2007-11-08 23:23:21 +0100914 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Francois Romieu23714082006-01-29 00:49:09 +0100916 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100917 /*
918 * Check if the RTL8169 has completed retrieving data from
919 * the specified MII register.
920 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100922 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 break;
924 }
Francois Romieu23714082006-01-29 00:49:09 +0100925 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700927 /*
928 * According to hardware specs a 20us delay is required after read
929 * complete indication, but before sending next command.
930 */
931 udelay(20);
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return value;
934}
935
françois romieuc0e45c12011-01-03 15:08:04 +0000936static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
937{
938 int i;
939
940 RTL_W32(OCPDR, data |
941 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
942 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
943 RTL_W32(EPHY_RXER_NUM, 0);
944
945 for (i = 0; i < 100; i++) {
946 mdelay(1);
947 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
948 break;
949 }
950}
951
952static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
953{
954 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
955 (value & OCPDR_DATA_MASK));
956}
957
958static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
959{
960 int i;
961
962 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
963
964 mdelay(1);
965 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
966 RTL_W32(EPHY_RXER_NUM, 0);
967
968 for (i = 0; i < 100; i++) {
969 mdelay(1);
970 if (RTL_R32(OCPAR) & OCPAR_FLAG)
971 break;
972 }
973
974 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
975}
976
françois romieue6de30d2011-01-03 15:08:37 +0000977#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
978
979static void r8168dp_2_mdio_start(void __iomem *ioaddr)
980{
981 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
982}
983
984static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
985{
986 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
987}
988
989static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
990{
991 r8168dp_2_mdio_start(ioaddr);
992
993 r8169_mdio_write(ioaddr, reg_addr, value);
994
995 r8168dp_2_mdio_stop(ioaddr);
996}
997
998static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
999{
1000 int value;
1001
1002 r8168dp_2_mdio_start(ioaddr);
1003
1004 value = r8169_mdio_read(ioaddr, reg_addr);
1005
1006 r8168dp_2_mdio_stop(ioaddr);
1007
1008 return value;
1009}
1010
françois romieu4da19632011-01-03 15:07:55 +00001011static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001012{
françois romieuc0e45c12011-01-03 15:08:04 +00001013 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001014}
1015
françois romieu4da19632011-01-03 15:07:55 +00001016static int rtl_readphy(struct rtl8169_private *tp, int location)
1017{
françois romieuc0e45c12011-01-03 15:08:04 +00001018 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +00001019}
1020
1021static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1022{
1023 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1024}
1025
1026static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001027{
1028 int val;
1029
françois romieu4da19632011-01-03 15:07:55 +00001030 val = rtl_readphy(tp, reg_addr);
1031 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001032}
1033
Francois Romieuccdffb92008-07-26 14:26:06 +02001034static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1035 int val)
1036{
1037 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001038
françois romieu4da19632011-01-03 15:07:55 +00001039 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001040}
1041
1042static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1043{
1044 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001045
françois romieu4da19632011-01-03 15:07:55 +00001046 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001047}
1048
Francois Romieudacf8152008-08-02 20:44:13 +02001049static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
1050{
1051 unsigned int i;
1052
1053 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1054 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1055
1056 for (i = 0; i < 100; i++) {
1057 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1058 break;
1059 udelay(10);
1060 }
1061}
1062
1063static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1064{
1065 u16 value = 0xffff;
1066 unsigned int i;
1067
1068 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1069
1070 for (i = 0; i < 100; i++) {
1071 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1072 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1073 break;
1074 }
1075 udelay(10);
1076 }
1077
1078 return value;
1079}
1080
1081static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1082{
1083 unsigned int i;
1084
1085 RTL_W32(CSIDR, value);
1086 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1087 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1088
1089 for (i = 0; i < 100; i++) {
1090 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1091 break;
1092 udelay(10);
1093 }
1094}
1095
1096static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1097{
1098 u32 value = ~0x00;
1099 unsigned int i;
1100
1101 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1102 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1103
1104 for (i = 0; i < 100; i++) {
1105 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1106 value = RTL_R32(CSIDR);
1107 break;
1108 }
1109 udelay(10);
1110 }
1111
1112 return value;
1113}
1114
Hayes Wang133ac402011-07-06 15:58:05 +08001115static
1116void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1117{
1118 unsigned int i;
1119
1120 BUG_ON((addr & 3) || (mask == 0));
1121 RTL_W32(ERIDR, val);
1122 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1123
1124 for (i = 0; i < 100; i++) {
1125 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1126 break;
1127 udelay(100);
1128 }
1129}
1130
1131static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1132{
1133 u32 value = ~0x00;
1134 unsigned int i;
1135
1136 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1137
1138 for (i = 0; i < 100; i++) {
1139 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1140 value = RTL_R32(ERIDR);
1141 break;
1142 }
1143 udelay(100);
1144 }
1145
1146 return value;
1147}
1148
1149static void
1150rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1151{
1152 u32 val;
1153
1154 val = rtl_eri_read(ioaddr, addr, type);
1155 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1156}
1157
françois romieuc28aa382011-08-02 03:53:43 +00001158struct exgmac_reg {
1159 u16 addr;
1160 u16 mask;
1161 u32 val;
1162};
1163
1164static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1165 const struct exgmac_reg *r, int len)
1166{
1167 while (len-- > 0) {
1168 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1169 r++;
1170 }
1171}
1172
françois romieudaf9df62009-10-07 12:44:20 +00001173static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1174{
1175 u8 value = 0xff;
1176 unsigned int i;
1177
1178 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1179
1180 for (i = 0; i < 300; i++) {
1181 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1182 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1183 break;
1184 }
1185 udelay(100);
1186 }
1187
1188 return value;
1189}
1190
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001191static u16 rtl_get_events(struct rtl8169_private *tp)
1192{
1193 void __iomem *ioaddr = tp->mmio_addr;
1194
1195 return RTL_R16(IntrStatus);
1196}
1197
1198static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1199{
1200 void __iomem *ioaddr = tp->mmio_addr;
1201
1202 RTL_W16(IntrStatus, bits);
1203 mmiowb();
1204}
1205
1206static void rtl_irq_disable(struct rtl8169_private *tp)
1207{
1208 void __iomem *ioaddr = tp->mmio_addr;
1209
1210 RTL_W16(IntrMask, 0);
1211 mmiowb();
1212}
1213
Francois Romieu3e990ff2012-01-26 12:50:01 +01001214static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1215{
1216 void __iomem *ioaddr = tp->mmio_addr;
1217
1218 RTL_W16(IntrMask, bits);
1219}
1220
Francois Romieuda78dbf2012-01-26 14:18:23 +01001221#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1222#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1223#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1224
1225static void rtl_irq_enable_all(struct rtl8169_private *tp)
1226{
1227 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1228}
1229
françois romieu811fd302011-12-04 20:30:45 +00001230static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
françois romieu811fd302011-12-04 20:30:45 +00001232 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001234 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001235 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001236 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
françois romieu4da19632011-01-03 15:07:55 +00001239static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
françois romieu4da19632011-01-03 15:07:55 +00001241 void __iomem *ioaddr = tp->mmio_addr;
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return RTL_R32(TBICSR) & TBIReset;
1244}
1245
françois romieu4da19632011-01-03 15:07:55 +00001246static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
françois romieu4da19632011-01-03 15:07:55 +00001248 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
1251static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1252{
1253 return RTL_R32(TBICSR) & TBILinkOk;
1254}
1255
1256static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1257{
1258 return RTL_R8(PHYstatus) & LinkStatus;
1259}
1260
françois romieu4da19632011-01-03 15:07:55 +00001261static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
françois romieu4da19632011-01-03 15:07:55 +00001263 void __iomem *ioaddr = tp->mmio_addr;
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1266}
1267
françois romieu4da19632011-01-03 15:07:55 +00001268static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 unsigned int val;
1271
françois romieu4da19632011-01-03 15:07:55 +00001272 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1273 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
Hayes Wang70090422011-07-06 15:58:06 +08001276static void rtl_link_chg_patch(struct rtl8169_private *tp)
1277{
1278 void __iomem *ioaddr = tp->mmio_addr;
1279 struct net_device *dev = tp->dev;
1280
1281 if (!netif_running(dev))
1282 return;
1283
1284 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1285 if (RTL_R8(PHYstatus) & _1000bpsF) {
1286 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1287 0x00000011, ERIAR_EXGMAC);
1288 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1289 0x00000005, ERIAR_EXGMAC);
1290 } else if (RTL_R8(PHYstatus) & _100bps) {
1291 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1292 0x0000001f, ERIAR_EXGMAC);
1293 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1294 0x00000005, ERIAR_EXGMAC);
1295 } else {
1296 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1297 0x0000001f, ERIAR_EXGMAC);
1298 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1299 0x0000003f, ERIAR_EXGMAC);
1300 }
1301 /* Reset packet filter */
1302 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1303 ERIAR_EXGMAC);
1304 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1305 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001306 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1307 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1308 if (RTL_R8(PHYstatus) & _1000bpsF) {
1309 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1310 0x00000011, ERIAR_EXGMAC);
1311 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1312 0x00000005, ERIAR_EXGMAC);
1313 } else {
1314 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1315 0x0000001f, ERIAR_EXGMAC);
1316 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1317 0x0000003f, ERIAR_EXGMAC);
1318 }
Hayes Wang70090422011-07-06 15:58:06 +08001319 }
1320}
1321
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001322static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001323 struct rtl8169_private *tp,
1324 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001327 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001328 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001329 if (pm)
1330 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001332 if (net_ratelimit())
1333 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001334 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001336 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001337 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001338 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001342static void rtl8169_check_link_status(struct net_device *dev,
1343 struct rtl8169_private *tp,
1344 void __iomem *ioaddr)
1345{
1346 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1347}
1348
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001349#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1350
1351static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1352{
1353 void __iomem *ioaddr = tp->mmio_addr;
1354 u8 options;
1355 u32 wolopts = 0;
1356
1357 options = RTL_R8(Config1);
1358 if (!(options & PMEnable))
1359 return 0;
1360
1361 options = RTL_R8(Config3);
1362 if (options & LinkUp)
1363 wolopts |= WAKE_PHY;
1364 if (options & MagicPacket)
1365 wolopts |= WAKE_MAGIC;
1366
1367 options = RTL_R8(Config5);
1368 if (options & UWF)
1369 wolopts |= WAKE_UCAST;
1370 if (options & BWF)
1371 wolopts |= WAKE_BCAST;
1372 if (options & MWF)
1373 wolopts |= WAKE_MCAST;
1374
1375 return wolopts;
1376}
1377
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001378static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1379{
1380 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001381
Francois Romieuda78dbf2012-01-26 14:18:23 +01001382 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001383
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001384 wol->supported = WAKE_ANY;
1385 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001386
Francois Romieuda78dbf2012-01-26 14:18:23 +01001387 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001388}
1389
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001390static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001391{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001392 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001393 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001394 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001395 u32 opt;
1396 u16 reg;
1397 u8 mask;
1398 } cfg[] = {
1399 { WAKE_ANY, Config1, PMEnable },
1400 { WAKE_PHY, Config3, LinkUp },
1401 { WAKE_MAGIC, Config3, MagicPacket },
1402 { WAKE_UCAST, Config5, UWF },
1403 { WAKE_BCAST, Config5, BWF },
1404 { WAKE_MCAST, Config5, MWF },
1405 { WAKE_ANY, Config5, LanWake }
1406 };
1407
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001408 RTL_W8(Cfg9346, Cfg9346_Unlock);
1409
1410 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1411 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001412 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001413 options |= cfg[i].mask;
1414 RTL_W8(cfg[i].reg, options);
1415 }
1416
1417 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001418}
1419
1420static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1421{
1422 struct rtl8169_private *tp = netdev_priv(dev);
1423
Francois Romieuda78dbf2012-01-26 14:18:23 +01001424 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001425
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001426 if (wol->wolopts)
1427 tp->features |= RTL_FEATURE_WOL;
1428 else
1429 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001430 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001431
1432 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001433
françois romieuea809072010-11-08 13:23:58 +00001434 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1435
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001436 return 0;
1437}
1438
Francois Romieu31bd2042011-04-26 18:58:59 +02001439static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1440{
Francois Romieu85bffe62011-04-27 08:22:39 +02001441 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001442}
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444static void rtl8169_get_drvinfo(struct net_device *dev,
1445 struct ethtool_drvinfo *info)
1446{
1447 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001448 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Rick Jones68aad782011-11-07 13:29:27 +00001450 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1451 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1452 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001453 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001454 if (!IS_ERR_OR_NULL(rtl_fw))
1455 strlcpy(info->fw_version, rtl_fw->version,
1456 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458
1459static int rtl8169_get_regs_len(struct net_device *dev)
1460{
1461 return R8169_REGS_SIZE;
1462}
1463
1464static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001465 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
1467 struct rtl8169_private *tp = netdev_priv(dev);
1468 void __iomem *ioaddr = tp->mmio_addr;
1469 int ret = 0;
1470 u32 reg;
1471
1472 reg = RTL_R32(TBICSR);
1473 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1474 (duplex == DUPLEX_FULL)) {
1475 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1476 } else if (autoneg == AUTONEG_ENABLE)
1477 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1478 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001479 netif_warn(tp, link, dev,
1480 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 ret = -EOPNOTSUPP;
1482 }
1483
1484 return ret;
1485}
1486
1487static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001488 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
1490 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001491 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001492 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Hayes Wang716b50a2011-02-22 17:26:18 +08001494 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001497 int auto_nego;
1498
françois romieu4da19632011-01-03 15:07:55 +00001499 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001500 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1501 ADVERTISE_100HALF | ADVERTISE_100FULL);
1502
1503 if (adv & ADVERTISED_10baseT_Half)
1504 auto_nego |= ADVERTISE_10HALF;
1505 if (adv & ADVERTISED_10baseT_Full)
1506 auto_nego |= ADVERTISE_10FULL;
1507 if (adv & ADVERTISED_100baseT_Half)
1508 auto_nego |= ADVERTISE_100HALF;
1509 if (adv & ADVERTISED_100baseT_Full)
1510 auto_nego |= ADVERTISE_100FULL;
1511
françois romieu3577aa12009-05-19 10:46:48 +00001512 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1513
françois romieu4da19632011-01-03 15:07:55 +00001514 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001515 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1516
1517 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001518 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001519 if (adv & ADVERTISED_1000baseT_Half)
1520 giga_ctrl |= ADVERTISE_1000HALF;
1521 if (adv & ADVERTISED_1000baseT_Full)
1522 giga_ctrl |= ADVERTISE_1000FULL;
1523 } else if (adv & (ADVERTISED_1000baseT_Half |
1524 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001525 netif_info(tp, link, dev,
1526 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001527 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
françois romieu3577aa12009-05-19 10:46:48 +00001530 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001531
françois romieu4da19632011-01-03 15:07:55 +00001532 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1533 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001534 } else {
1535 giga_ctrl = 0;
1536
1537 if (speed == SPEED_10)
1538 bmcr = 0;
1539 else if (speed == SPEED_100)
1540 bmcr = BMCR_SPEED100;
1541 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001542 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001543
1544 if (duplex == DUPLEX_FULL)
1545 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001546 }
1547
françois romieu4da19632011-01-03 15:07:55 +00001548 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001549
Francois Romieucecb5fd2011-04-01 10:21:07 +02001550 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1551 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001552 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001553 rtl_writephy(tp, 0x17, 0x2138);
1554 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001555 } else {
françois romieu4da19632011-01-03 15:07:55 +00001556 rtl_writephy(tp, 0x17, 0x2108);
1557 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001558 }
1559 }
1560
Oliver Neukum54405cd2011-01-06 21:55:13 +01001561 rc = 0;
1562out:
1563 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
1566static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001567 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
1569 struct rtl8169_private *tp = netdev_priv(dev);
1570 int ret;
1571
Oliver Neukum54405cd2011-01-06 21:55:13 +01001572 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001573 if (ret < 0)
1574 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Francois Romieu4876cc12011-03-11 21:07:11 +01001576 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1577 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001579 }
1580out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return ret;
1582}
1583
1584static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1585{
1586 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 int ret;
1588
Francois Romieu4876cc12011-03-11 21:07:11 +01001589 del_timer_sync(&tp->timer);
1590
Francois Romieuda78dbf2012-01-26 14:18:23 +01001591 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001592 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001593 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001594 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 return ret;
1597}
1598
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001599static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1600 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
Francois Romieud58d46b2011-05-03 16:38:29 +02001602 struct rtl8169_private *tp = netdev_priv(dev);
1603
Francois Romieu2b7b4312011-04-18 22:53:24 -07001604 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001605 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Francois Romieud58d46b2011-05-03 16:38:29 +02001607 if (dev->mtu > JUMBO_1K &&
1608 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1609 features &= ~NETIF_F_IP_CSUM;
1610
Michał Mirosław350fb322011-04-08 06:35:56 +00001611 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
1613
Francois Romieuda78dbf2012-01-26 14:18:23 +01001614static void __rtl8169_set_features(struct net_device *dev,
1615 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
1617 struct rtl8169_private *tp = netdev_priv(dev);
Ben Greear6bbe0212012-02-10 15:04:33 +00001618 netdev_features_t changed = features ^ dev->features;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001619 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Ben Greear6bbe0212012-02-10 15:04:33 +00001621 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)))
1622 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Ben Greear6bbe0212012-02-10 15:04:33 +00001624 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)) {
1625 if (features & NETIF_F_RXCSUM)
1626 tp->cp_cmd |= RxChkSum;
1627 else
1628 tp->cp_cmd &= ~RxChkSum;
Michał Mirosław350fb322011-04-08 06:35:56 +00001629
Ben Greear6bbe0212012-02-10 15:04:33 +00001630 if (dev->features & NETIF_F_HW_VLAN_RX)
1631 tp->cp_cmd |= RxVlan;
1632 else
1633 tp->cp_cmd &= ~RxVlan;
1634
1635 RTL_W16(CPlusCmd, tp->cp_cmd);
1636 RTL_R16(CPlusCmd);
1637 }
1638 if (changed & NETIF_F_RXALL) {
1639 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1640 if (features & NETIF_F_RXALL)
1641 tmp |= (AcceptErr | AcceptRunt);
1642 RTL_W32(RxConfig, tmp);
1643 }
Francois Romieuda78dbf2012-01-26 14:18:23 +01001644}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Francois Romieuda78dbf2012-01-26 14:18:23 +01001646static int rtl8169_set_features(struct net_device *dev,
1647 netdev_features_t features)
1648{
1649 struct rtl8169_private *tp = netdev_priv(dev);
1650
1651 rtl_lock_work(tp);
1652 __rtl8169_set_features(dev, features);
1653 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 return 0;
1656}
1657
Francois Romieuda78dbf2012-01-26 14:18:23 +01001658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1660 struct sk_buff *skb)
1661{
Jesse Grosseab6d182010-10-20 13:56:03 +00001662 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1664}
1665
Francois Romieu7a8fc772011-03-01 17:18:33 +01001666static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
1668 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Francois Romieu7a8fc772011-03-01 17:18:33 +01001670 if (opts2 & RxVlanTag)
1671 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674}
1675
Francois Romieuccdffb92008-07-26 14:26:06 +02001676static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
1678 struct rtl8169_private *tp = netdev_priv(dev);
1679 void __iomem *ioaddr = tp->mmio_addr;
1680 u32 status;
1681
1682 cmd->supported =
1683 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1684 cmd->port = PORT_FIBRE;
1685 cmd->transceiver = XCVR_INTERNAL;
1686
1687 status = RTL_R32(TBICSR);
1688 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1689 cmd->autoneg = !!(status & TBINwEnable);
1690
David Decotigny70739492011-04-27 18:32:40 +00001691 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001693
1694 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695}
1696
Francois Romieuccdffb92008-07-26 14:26:06 +02001697static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
1699 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Francois Romieuccdffb92008-07-26 14:26:06 +02001701 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
1703
1704static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1705{
1706 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001707 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Francois Romieuda78dbf2012-01-26 14:18:23 +01001709 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02001710 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001711 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Francois Romieuccdffb92008-07-26 14:26:06 +02001713 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714}
1715
1716static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1717 void *p)
1718{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001719 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Francois Romieu5b0384f2006-08-16 16:00:01 +02001721 if (regs->len > R8169_REGS_SIZE)
1722 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Francois Romieuda78dbf2012-01-26 14:18:23 +01001724 rtl_lock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001725 memcpy_fromio(p, tp->mmio_addr, regs->len);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001726 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001729static u32 rtl8169_get_msglevel(struct net_device *dev)
1730{
1731 struct rtl8169_private *tp = netdev_priv(dev);
1732
1733 return tp->msg_enable;
1734}
1735
1736static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1737{
1738 struct rtl8169_private *tp = netdev_priv(dev);
1739
1740 tp->msg_enable = value;
1741}
1742
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001743static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1744 "tx_packets",
1745 "rx_packets",
1746 "tx_errors",
1747 "rx_errors",
1748 "rx_missed",
1749 "align_errors",
1750 "tx_single_collisions",
1751 "tx_multi_collisions",
1752 "unicast",
1753 "broadcast",
1754 "multicast",
1755 "tx_aborted",
1756 "tx_underrun",
1757};
1758
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001759static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001760{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001761 switch (sset) {
1762 case ETH_SS_STATS:
1763 return ARRAY_SIZE(rtl8169_gstrings);
1764 default:
1765 return -EOPNOTSUPP;
1766 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001767}
1768
Ivan Vecera355423d2009-02-06 21:49:57 -08001769static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001770{
1771 struct rtl8169_private *tp = netdev_priv(dev);
1772 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001773 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001774 struct rtl8169_counters *counters;
1775 dma_addr_t paddr;
1776 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001777 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001778
Ivan Vecera355423d2009-02-06 21:49:57 -08001779 /*
1780 * Some chips are unable to dump tally counters when the receiver
1781 * is disabled.
1782 */
1783 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1784 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001785
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001786 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001787 if (!counters)
1788 return;
1789
1790 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001791 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001792 RTL_W32(CounterAddrLow, cmd);
1793 RTL_W32(CounterAddrLow, cmd | CounterDump);
1794
Ivan Vecera355423d2009-02-06 21:49:57 -08001795 while (wait--) {
1796 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001797 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001798 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001799 }
1800 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001801 }
1802
1803 RTL_W32(CounterAddrLow, 0);
1804 RTL_W32(CounterAddrHigh, 0);
1805
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001806 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001807}
1808
Ivan Vecera355423d2009-02-06 21:49:57 -08001809static void rtl8169_get_ethtool_stats(struct net_device *dev,
1810 struct ethtool_stats *stats, u64 *data)
1811{
1812 struct rtl8169_private *tp = netdev_priv(dev);
1813
1814 ASSERT_RTNL();
1815
1816 rtl8169_update_counters(dev);
1817
1818 data[0] = le64_to_cpu(tp->counters.tx_packets);
1819 data[1] = le64_to_cpu(tp->counters.rx_packets);
1820 data[2] = le64_to_cpu(tp->counters.tx_errors);
1821 data[3] = le32_to_cpu(tp->counters.rx_errors);
1822 data[4] = le16_to_cpu(tp->counters.rx_missed);
1823 data[5] = le16_to_cpu(tp->counters.align_errors);
1824 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1825 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1826 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1827 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1828 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1829 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1830 data[12] = le16_to_cpu(tp->counters.tx_underun);
1831}
1832
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001833static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1834{
1835 switch(stringset) {
1836 case ETH_SS_STATS:
1837 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1838 break;
1839 }
1840}
1841
Jeff Garzik7282d492006-09-13 14:30:00 -04001842static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 .get_drvinfo = rtl8169_get_drvinfo,
1844 .get_regs_len = rtl8169_get_regs_len,
1845 .get_link = ethtool_op_get_link,
1846 .get_settings = rtl8169_get_settings,
1847 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001848 .get_msglevel = rtl8169_get_msglevel,
1849 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001851 .get_wol = rtl8169_get_wol,
1852 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001853 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001854 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001855 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856};
1857
Francois Romieu07d3f512007-02-21 22:40:46 +01001858static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001859 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
Francois Romieu5d320a22011-05-08 17:47:36 +02001861 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001862 /*
1863 * The driver currently handles the 8168Bf and the 8168Be identically
1864 * but they can be identified more specifically through the test below
1865 * if needed:
1866 *
1867 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001868 *
1869 * Same thing for the 8101Eb and the 8101Ec:
1870 *
1871 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001872 */
Francois Romieu37441002011-06-17 22:58:54 +02001873 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001875 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 int mac_version;
1877 } mac_info[] = {
Hayes Wangc2218922011-09-06 16:55:18 +08001878 /* 8168F family. */
1879 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1880 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1881
hayeswang01dc7fe2011-03-21 01:50:28 +00001882 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001883 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001884 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1885 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1886 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1887
Francois Romieu5b538df2008-07-20 16:22:45 +02001888 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001889 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1890 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001891 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001892
françois romieue6de30d2011-01-03 15:08:37 +00001893 /* 8168DP family. */
1894 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1895 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001896 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001897
Francois Romieuef808d52008-06-29 13:10:54 +02001898 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001899 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001900 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001901 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001902 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001903 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1904 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001905 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001906 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001907 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001908
1909 /* 8168B family. */
1910 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1911 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1912 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1913 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1914
1915 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001916 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001917 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1918 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1919 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001920 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1921 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1922 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1923 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1924 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1925 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001926 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001927 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001928 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001929 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1930 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001931 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1932 /* FIXME: where did these entries come from ? -- FR */
1933 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1934 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1935
1936 /* 8110 family. */
1937 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1938 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1939 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1940 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1941 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1942 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1943
Jean Delvaref21b75e2009-05-26 20:54:48 -07001944 /* Catch-all */
1945 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001946 };
1947 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 u32 reg;
1949
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001950 reg = RTL_R32(TxConfig);
1951 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 p++;
1953 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001954
1955 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1956 netif_notice(tp, probe, dev,
1957 "unknown MAC, using family default\n");
1958 tp->mac_version = default_version;
1959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961
1962static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1963{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001964 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965}
1966
Francois Romieu867763c2007-08-17 18:21:58 +02001967struct phy_reg {
1968 u16 reg;
1969 u16 val;
1970};
1971
françois romieu4da19632011-01-03 15:07:55 +00001972static void rtl_writephy_batch(struct rtl8169_private *tp,
1973 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001974{
1975 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001976 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001977 regs++;
1978 }
1979}
1980
françois romieubca03d52011-01-03 15:07:31 +00001981#define PHY_READ 0x00000000
1982#define PHY_DATA_OR 0x10000000
1983#define PHY_DATA_AND 0x20000000
1984#define PHY_BJMPN 0x30000000
1985#define PHY_READ_EFUSE 0x40000000
1986#define PHY_READ_MAC_BYTE 0x50000000
1987#define PHY_WRITE_MAC_BYTE 0x60000000
1988#define PHY_CLEAR_READCOUNT 0x70000000
1989#define PHY_WRITE 0x80000000
1990#define PHY_READCOUNT_EQ_SKIP 0x90000000
1991#define PHY_COMP_EQ_SKIPN 0xa0000000
1992#define PHY_COMP_NEQ_SKIPN 0xb0000000
1993#define PHY_WRITE_PREVIOUS 0xc0000000
1994#define PHY_SKIPN 0xd0000000
1995#define PHY_DELAY_MS 0xe0000000
1996#define PHY_WRITE_ERI_WORD 0xf0000000
1997
Hayes Wang960aee62011-06-18 11:37:48 +02001998struct fw_info {
1999 u32 magic;
2000 char version[RTL_VER_SIZE];
2001 __le32 fw_start;
2002 __le32 fw_len;
2003 u8 chksum;
2004} __packed;
2005
Francois Romieu1c361ef2011-06-17 17:16:24 +02002006#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2007
2008static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002009{
Francois Romieub6ffd972011-06-17 17:00:05 +02002010 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002011 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002012 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2013 char *version = rtl_fw->version;
2014 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002015
Francois Romieu1c361ef2011-06-17 17:16:24 +02002016 if (fw->size < FW_OPCODE_SIZE)
2017 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002018
2019 if (!fw_info->magic) {
2020 size_t i, size, start;
2021 u8 checksum = 0;
2022
2023 if (fw->size < sizeof(*fw_info))
2024 goto out;
2025
2026 for (i = 0; i < fw->size; i++)
2027 checksum += fw->data[i];
2028 if (checksum != 0)
2029 goto out;
2030
2031 start = le32_to_cpu(fw_info->fw_start);
2032 if (start > fw->size)
2033 goto out;
2034
2035 size = le32_to_cpu(fw_info->fw_len);
2036 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2037 goto out;
2038
2039 memcpy(version, fw_info->version, RTL_VER_SIZE);
2040
2041 pa->code = (__le32 *)(fw->data + start);
2042 pa->size = size;
2043 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002044 if (fw->size % FW_OPCODE_SIZE)
2045 goto out;
2046
2047 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2048
2049 pa->code = (__le32 *)fw->data;
2050 pa->size = fw->size / FW_OPCODE_SIZE;
2051 }
2052 version[RTL_VER_SIZE - 1] = 0;
2053
2054 rc = true;
2055out:
2056 return rc;
2057}
2058
Francois Romieufd112f22011-06-18 00:10:29 +02002059static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2060 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002061{
Francois Romieufd112f22011-06-18 00:10:29 +02002062 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002063 size_t index;
2064
Francois Romieu1c361ef2011-06-17 17:16:24 +02002065 for (index = 0; index < pa->size; index++) {
2066 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002067 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002068
hayeswang42b82dc2011-01-10 02:07:25 +00002069 switch(action & 0xf0000000) {
2070 case PHY_READ:
2071 case PHY_DATA_OR:
2072 case PHY_DATA_AND:
2073 case PHY_READ_EFUSE:
2074 case PHY_CLEAR_READCOUNT:
2075 case PHY_WRITE:
2076 case PHY_WRITE_PREVIOUS:
2077 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002078 break;
2079
hayeswang42b82dc2011-01-10 02:07:25 +00002080 case PHY_BJMPN:
2081 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002082 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002083 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002084 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002085 }
2086 break;
2087 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002088 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002089 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002090 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002091 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002092 }
2093 break;
2094 case PHY_COMP_EQ_SKIPN:
2095 case PHY_COMP_NEQ_SKIPN:
2096 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002097 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002098 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002099 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002100 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002101 }
2102 break;
2103
2104 case PHY_READ_MAC_BYTE:
2105 case PHY_WRITE_MAC_BYTE:
2106 case PHY_WRITE_ERI_WORD:
2107 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002108 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002109 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002110 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002111 }
2112 }
Francois Romieufd112f22011-06-18 00:10:29 +02002113 rc = true;
2114out:
2115 return rc;
2116}
françois romieubca03d52011-01-03 15:07:31 +00002117
Francois Romieufd112f22011-06-18 00:10:29 +02002118static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2119{
2120 struct net_device *dev = tp->dev;
2121 int rc = -EINVAL;
2122
2123 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2124 netif_err(tp, ifup, dev, "invalid firwmare\n");
2125 goto out;
2126 }
2127
2128 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2129 rc = 0;
2130out:
2131 return rc;
2132}
2133
2134static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2135{
2136 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2137 u32 predata, count;
2138 size_t index;
2139
2140 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002141
Francois Romieu1c361ef2011-06-17 17:16:24 +02002142 for (index = 0; index < pa->size; ) {
2143 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002144 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002145 u32 regno = (action & 0x0fff0000) >> 16;
2146
2147 if (!action)
2148 break;
françois romieubca03d52011-01-03 15:07:31 +00002149
2150 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002151 case PHY_READ:
2152 predata = rtl_readphy(tp, regno);
2153 count++;
2154 index++;
françois romieubca03d52011-01-03 15:07:31 +00002155 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002156 case PHY_DATA_OR:
2157 predata |= data;
2158 index++;
2159 break;
2160 case PHY_DATA_AND:
2161 predata &= data;
2162 index++;
2163 break;
2164 case PHY_BJMPN:
2165 index -= regno;
2166 break;
2167 case PHY_READ_EFUSE:
2168 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2169 index++;
2170 break;
2171 case PHY_CLEAR_READCOUNT:
2172 count = 0;
2173 index++;
2174 break;
2175 case PHY_WRITE:
2176 rtl_writephy(tp, regno, data);
2177 index++;
2178 break;
2179 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002180 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002181 break;
2182 case PHY_COMP_EQ_SKIPN:
2183 if (predata == data)
2184 index += regno;
2185 index++;
2186 break;
2187 case PHY_COMP_NEQ_SKIPN:
2188 if (predata != data)
2189 index += regno;
2190 index++;
2191 break;
2192 case PHY_WRITE_PREVIOUS:
2193 rtl_writephy(tp, regno, predata);
2194 index++;
2195 break;
2196 case PHY_SKIPN:
2197 index += regno + 1;
2198 break;
2199 case PHY_DELAY_MS:
2200 mdelay(data);
2201 index++;
2202 break;
2203
2204 case PHY_READ_MAC_BYTE:
2205 case PHY_WRITE_MAC_BYTE:
2206 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002207 default:
2208 BUG();
2209 }
2210 }
2211}
2212
françois romieuf1e02ed2011-01-13 13:07:53 +00002213static void rtl_release_firmware(struct rtl8169_private *tp)
2214{
Francois Romieub6ffd972011-06-17 17:00:05 +02002215 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2216 release_firmware(tp->rtl_fw->fw);
2217 kfree(tp->rtl_fw);
2218 }
2219 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002220}
2221
François Romieu953a12c2011-04-24 17:38:48 +02002222static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002223{
Francois Romieub6ffd972011-06-17 17:00:05 +02002224 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002225
2226 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002227 if (!IS_ERR_OR_NULL(rtl_fw))
2228 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002229}
2230
2231static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2232{
2233 if (rtl_readphy(tp, reg) != val)
2234 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2235 else
2236 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002237}
2238
françois romieu4da19632011-01-03 15:07:55 +00002239static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002241 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002242 { 0x1f, 0x0001 },
2243 { 0x06, 0x006e },
2244 { 0x08, 0x0708 },
2245 { 0x15, 0x4000 },
2246 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
françois romieu0b9b5712009-08-10 19:44:56 +00002248 { 0x1f, 0x0001 },
2249 { 0x03, 0x00a1 },
2250 { 0x02, 0x0008 },
2251 { 0x01, 0x0120 },
2252 { 0x00, 0x1000 },
2253 { 0x04, 0x0800 },
2254 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
françois romieu0b9b5712009-08-10 19:44:56 +00002256 { 0x03, 0xff41 },
2257 { 0x02, 0xdf60 },
2258 { 0x01, 0x0140 },
2259 { 0x00, 0x0077 },
2260 { 0x04, 0x7800 },
2261 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
françois romieu0b9b5712009-08-10 19:44:56 +00002263 { 0x03, 0x802f },
2264 { 0x02, 0x4f02 },
2265 { 0x01, 0x0409 },
2266 { 0x00, 0xf0f9 },
2267 { 0x04, 0x9800 },
2268 { 0x04, 0x9000 },
2269
2270 { 0x03, 0xdf01 },
2271 { 0x02, 0xdf20 },
2272 { 0x01, 0xff95 },
2273 { 0x00, 0xba00 },
2274 { 0x04, 0xa800 },
2275 { 0x04, 0xa000 },
2276
2277 { 0x03, 0xff41 },
2278 { 0x02, 0xdf20 },
2279 { 0x01, 0x0140 },
2280 { 0x00, 0x00bb },
2281 { 0x04, 0xb800 },
2282 { 0x04, 0xb000 },
2283
2284 { 0x03, 0xdf41 },
2285 { 0x02, 0xdc60 },
2286 { 0x01, 0x6340 },
2287 { 0x00, 0x007d },
2288 { 0x04, 0xd800 },
2289 { 0x04, 0xd000 },
2290
2291 { 0x03, 0xdf01 },
2292 { 0x02, 0xdf20 },
2293 { 0x01, 0x100a },
2294 { 0x00, 0xa0ff },
2295 { 0x04, 0xf800 },
2296 { 0x04, 0xf000 },
2297
2298 { 0x1f, 0x0000 },
2299 { 0x0b, 0x0000 },
2300 { 0x00, 0x9200 }
2301 };
2302
françois romieu4da19632011-01-03 15:07:55 +00002303 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
françois romieu4da19632011-01-03 15:07:55 +00002306static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002307{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002308 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002309 { 0x1f, 0x0002 },
2310 { 0x01, 0x90d0 },
2311 { 0x1f, 0x0000 }
2312 };
2313
françois romieu4da19632011-01-03 15:07:55 +00002314 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002315}
2316
françois romieu4da19632011-01-03 15:07:55 +00002317static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002318{
2319 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002320
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002321 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2322 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002323 return;
2324
françois romieu4da19632011-01-03 15:07:55 +00002325 rtl_writephy(tp, 0x1f, 0x0001);
2326 rtl_writephy(tp, 0x10, 0xf01b);
2327 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002328}
2329
françois romieu4da19632011-01-03 15:07:55 +00002330static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002331{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002332 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002333 { 0x1f, 0x0001 },
2334 { 0x04, 0x0000 },
2335 { 0x03, 0x00a1 },
2336 { 0x02, 0x0008 },
2337 { 0x01, 0x0120 },
2338 { 0x00, 0x1000 },
2339 { 0x04, 0x0800 },
2340 { 0x04, 0x9000 },
2341 { 0x03, 0x802f },
2342 { 0x02, 0x4f02 },
2343 { 0x01, 0x0409 },
2344 { 0x00, 0xf099 },
2345 { 0x04, 0x9800 },
2346 { 0x04, 0xa000 },
2347 { 0x03, 0xdf01 },
2348 { 0x02, 0xdf20 },
2349 { 0x01, 0xff95 },
2350 { 0x00, 0xba00 },
2351 { 0x04, 0xa800 },
2352 { 0x04, 0xf000 },
2353 { 0x03, 0xdf01 },
2354 { 0x02, 0xdf20 },
2355 { 0x01, 0x101a },
2356 { 0x00, 0xa0ff },
2357 { 0x04, 0xf800 },
2358 { 0x04, 0x0000 },
2359 { 0x1f, 0x0000 },
2360
2361 { 0x1f, 0x0001 },
2362 { 0x10, 0xf41b },
2363 { 0x14, 0xfb54 },
2364 { 0x18, 0xf5c7 },
2365 { 0x1f, 0x0000 },
2366
2367 { 0x1f, 0x0001 },
2368 { 0x17, 0x0cc0 },
2369 { 0x1f, 0x0000 }
2370 };
2371
françois romieu4da19632011-01-03 15:07:55 +00002372 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002373
françois romieu4da19632011-01-03 15:07:55 +00002374 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002375}
2376
françois romieu4da19632011-01-03 15:07:55 +00002377static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002378{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002379 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002380 { 0x1f, 0x0001 },
2381 { 0x04, 0x0000 },
2382 { 0x03, 0x00a1 },
2383 { 0x02, 0x0008 },
2384 { 0x01, 0x0120 },
2385 { 0x00, 0x1000 },
2386 { 0x04, 0x0800 },
2387 { 0x04, 0x9000 },
2388 { 0x03, 0x802f },
2389 { 0x02, 0x4f02 },
2390 { 0x01, 0x0409 },
2391 { 0x00, 0xf099 },
2392 { 0x04, 0x9800 },
2393 { 0x04, 0xa000 },
2394 { 0x03, 0xdf01 },
2395 { 0x02, 0xdf20 },
2396 { 0x01, 0xff95 },
2397 { 0x00, 0xba00 },
2398 { 0x04, 0xa800 },
2399 { 0x04, 0xf000 },
2400 { 0x03, 0xdf01 },
2401 { 0x02, 0xdf20 },
2402 { 0x01, 0x101a },
2403 { 0x00, 0xa0ff },
2404 { 0x04, 0xf800 },
2405 { 0x04, 0x0000 },
2406 { 0x1f, 0x0000 },
2407
2408 { 0x1f, 0x0001 },
2409 { 0x0b, 0x8480 },
2410 { 0x1f, 0x0000 },
2411
2412 { 0x1f, 0x0001 },
2413 { 0x18, 0x67c7 },
2414 { 0x04, 0x2000 },
2415 { 0x03, 0x002f },
2416 { 0x02, 0x4360 },
2417 { 0x01, 0x0109 },
2418 { 0x00, 0x3022 },
2419 { 0x04, 0x2800 },
2420 { 0x1f, 0x0000 },
2421
2422 { 0x1f, 0x0001 },
2423 { 0x17, 0x0cc0 },
2424 { 0x1f, 0x0000 }
2425 };
2426
françois romieu4da19632011-01-03 15:07:55 +00002427 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002428}
2429
françois romieu4da19632011-01-03 15:07:55 +00002430static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002431{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002432 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002433 { 0x10, 0xf41b },
2434 { 0x1f, 0x0000 }
2435 };
2436
françois romieu4da19632011-01-03 15:07:55 +00002437 rtl_writephy(tp, 0x1f, 0x0001);
2438 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002439
françois romieu4da19632011-01-03 15:07:55 +00002440 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002441}
2442
françois romieu4da19632011-01-03 15:07:55 +00002443static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002444{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002445 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002446 { 0x1f, 0x0001 },
2447 { 0x10, 0xf41b },
2448 { 0x1f, 0x0000 }
2449 };
2450
françois romieu4da19632011-01-03 15:07:55 +00002451 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002452}
2453
françois romieu4da19632011-01-03 15:07:55 +00002454static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002455{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002456 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002457 { 0x1f, 0x0000 },
2458 { 0x1d, 0x0f00 },
2459 { 0x1f, 0x0002 },
2460 { 0x0c, 0x1ec8 },
2461 { 0x1f, 0x0000 }
2462 };
2463
françois romieu4da19632011-01-03 15:07:55 +00002464 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002465}
2466
françois romieu4da19632011-01-03 15:07:55 +00002467static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002468{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002469 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002470 { 0x1f, 0x0001 },
2471 { 0x1d, 0x3d98 },
2472 { 0x1f, 0x0000 }
2473 };
2474
françois romieu4da19632011-01-03 15:07:55 +00002475 rtl_writephy(tp, 0x1f, 0x0000);
2476 rtl_patchphy(tp, 0x14, 1 << 5);
2477 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002478
françois romieu4da19632011-01-03 15:07:55 +00002479 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002480}
2481
françois romieu4da19632011-01-03 15:07:55 +00002482static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002483{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002484 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002485 { 0x1f, 0x0001 },
2486 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002487 { 0x1f, 0x0002 },
2488 { 0x00, 0x88d4 },
2489 { 0x01, 0x82b1 },
2490 { 0x03, 0x7002 },
2491 { 0x08, 0x9e30 },
2492 { 0x09, 0x01f0 },
2493 { 0x0a, 0x5500 },
2494 { 0x0c, 0x00c8 },
2495 { 0x1f, 0x0003 },
2496 { 0x12, 0xc096 },
2497 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002498 { 0x1f, 0x0000 },
2499 { 0x1f, 0x0000 },
2500 { 0x09, 0x2000 },
2501 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002502 };
2503
françois romieu4da19632011-01-03 15:07:55 +00002504 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002505
françois romieu4da19632011-01-03 15:07:55 +00002506 rtl_patchphy(tp, 0x14, 1 << 5);
2507 rtl_patchphy(tp, 0x0d, 1 << 5);
2508 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002509}
2510
françois romieu4da19632011-01-03 15:07:55 +00002511static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002512{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002513 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002514 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002515 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002516 { 0x03, 0x802f },
2517 { 0x02, 0x4f02 },
2518 { 0x01, 0x0409 },
2519 { 0x00, 0xf099 },
2520 { 0x04, 0x9800 },
2521 { 0x04, 0x9000 },
2522 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002523 { 0x1f, 0x0002 },
2524 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002525 { 0x06, 0x0761 },
2526 { 0x1f, 0x0003 },
2527 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002528 { 0x1f, 0x0000 }
2529 };
2530
françois romieu4da19632011-01-03 15:07:55 +00002531 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002532
françois romieu4da19632011-01-03 15:07:55 +00002533 rtl_patchphy(tp, 0x16, 1 << 0);
2534 rtl_patchphy(tp, 0x14, 1 << 5);
2535 rtl_patchphy(tp, 0x0d, 1 << 5);
2536 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002537}
2538
françois romieu4da19632011-01-03 15:07:55 +00002539static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002540{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002541 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002542 { 0x1f, 0x0001 },
2543 { 0x12, 0x2300 },
2544 { 0x1d, 0x3d98 },
2545 { 0x1f, 0x0002 },
2546 { 0x0c, 0x7eb8 },
2547 { 0x06, 0x5461 },
2548 { 0x1f, 0x0003 },
2549 { 0x16, 0x0f0a },
2550 { 0x1f, 0x0000 }
2551 };
2552
françois romieu4da19632011-01-03 15:07:55 +00002553 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002554
françois romieu4da19632011-01-03 15:07:55 +00002555 rtl_patchphy(tp, 0x16, 1 << 0);
2556 rtl_patchphy(tp, 0x14, 1 << 5);
2557 rtl_patchphy(tp, 0x0d, 1 << 5);
2558 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002559}
2560
françois romieu4da19632011-01-03 15:07:55 +00002561static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002562{
françois romieu4da19632011-01-03 15:07:55 +00002563 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002564}
2565
françois romieubca03d52011-01-03 15:07:31 +00002566static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002567{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002568 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002569 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002570 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002571 { 0x06, 0x4064 },
2572 { 0x07, 0x2863 },
2573 { 0x08, 0x059c },
2574 { 0x09, 0x26b4 },
2575 { 0x0a, 0x6a19 },
2576 { 0x0b, 0xdcc8 },
2577 { 0x10, 0xf06d },
2578 { 0x14, 0x7f68 },
2579 { 0x18, 0x7fd9 },
2580 { 0x1c, 0xf0ff },
2581 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002582 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002583 { 0x12, 0xf49f },
2584 { 0x13, 0x070b },
2585 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002586 { 0x14, 0x94c0 },
2587
2588 /*
2589 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002590 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002591 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002592 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002593 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002594 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002595 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002596 { 0x06, 0x5561 },
2597
2598 /*
2599 * Can not link to 1Gbps with bad cable
2600 * Decrease SNR threshold form 21.07dB to 19.04dB
2601 */
2602 { 0x1f, 0x0001 },
2603 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002604
2605 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002606 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002607 };
françois romieubca03d52011-01-03 15:07:31 +00002608 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002609
françois romieu4da19632011-01-03 15:07:55 +00002610 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002611
françois romieubca03d52011-01-03 15:07:31 +00002612 /*
2613 * Rx Error Issue
2614 * Fine Tune Switching regulator parameter
2615 */
françois romieu4da19632011-01-03 15:07:55 +00002616 rtl_writephy(tp, 0x1f, 0x0002);
2617 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2618 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002619
françois romieudaf9df62009-10-07 12:44:20 +00002620 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002621 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002622 { 0x1f, 0x0002 },
2623 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002624 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002625 { 0x05, 0x8330 },
2626 { 0x06, 0x669a },
2627 { 0x1f, 0x0002 }
2628 };
2629 int val;
2630
françois romieu4da19632011-01-03 15:07:55 +00002631 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002632
françois romieu4da19632011-01-03 15:07:55 +00002633 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002634
2635 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002636 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002637 0x0065, 0x0066, 0x0067, 0x0068,
2638 0x0069, 0x006a, 0x006b, 0x006c
2639 };
2640 int i;
2641
françois romieu4da19632011-01-03 15:07:55 +00002642 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002643
2644 val &= 0xff00;
2645 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002646 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002647 }
2648 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002649 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002650 { 0x1f, 0x0002 },
2651 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002652 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002653 { 0x05, 0x8330 },
2654 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002655 };
2656
françois romieu4da19632011-01-03 15:07:55 +00002657 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002658 }
2659
françois romieubca03d52011-01-03 15:07:31 +00002660 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002661 rtl_writephy(tp, 0x1f, 0x0002);
2662 rtl_patchphy(tp, 0x0d, 0x0300);
2663 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002664
françois romieubca03d52011-01-03 15:07:31 +00002665 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002666 rtl_writephy(tp, 0x1f, 0x0002);
2667 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2668 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002669
françois romieu4da19632011-01-03 15:07:55 +00002670 rtl_writephy(tp, 0x1f, 0x0005);
2671 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002672
2673 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002674
françois romieu4da19632011-01-03 15:07:55 +00002675 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002676}
2677
françois romieubca03d52011-01-03 15:07:31 +00002678static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002679{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002680 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002681 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002682 { 0x1f, 0x0001 },
2683 { 0x06, 0x4064 },
2684 { 0x07, 0x2863 },
2685 { 0x08, 0x059c },
2686 { 0x09, 0x26b4 },
2687 { 0x0a, 0x6a19 },
2688 { 0x0b, 0xdcc8 },
2689 { 0x10, 0xf06d },
2690 { 0x14, 0x7f68 },
2691 { 0x18, 0x7fd9 },
2692 { 0x1c, 0xf0ff },
2693 { 0x1d, 0x3d9c },
2694 { 0x1f, 0x0003 },
2695 { 0x12, 0xf49f },
2696 { 0x13, 0x070b },
2697 { 0x1a, 0x05ad },
2698 { 0x14, 0x94c0 },
2699
françois romieubca03d52011-01-03 15:07:31 +00002700 /*
2701 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002702 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002703 */
françois romieudaf9df62009-10-07 12:44:20 +00002704 { 0x1f, 0x0002 },
2705 { 0x06, 0x5561 },
2706 { 0x1f, 0x0005 },
2707 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002708 { 0x06, 0x5561 },
2709
2710 /*
2711 * Can not link to 1Gbps with bad cable
2712 * Decrease SNR threshold form 21.07dB to 19.04dB
2713 */
2714 { 0x1f, 0x0001 },
2715 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002716
2717 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002718 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002719 };
françois romieubca03d52011-01-03 15:07:31 +00002720 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002721
françois romieu4da19632011-01-03 15:07:55 +00002722 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002723
2724 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002725 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002726 { 0x1f, 0x0002 },
2727 { 0x05, 0x669a },
2728 { 0x1f, 0x0005 },
2729 { 0x05, 0x8330 },
2730 { 0x06, 0x669a },
2731
2732 { 0x1f, 0x0002 }
2733 };
2734 int val;
2735
françois romieu4da19632011-01-03 15:07:55 +00002736 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002737
françois romieu4da19632011-01-03 15:07:55 +00002738 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002739 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002740 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002741 0x0065, 0x0066, 0x0067, 0x0068,
2742 0x0069, 0x006a, 0x006b, 0x006c
2743 };
2744 int i;
2745
françois romieu4da19632011-01-03 15:07:55 +00002746 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002747
2748 val &= 0xff00;
2749 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002750 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002751 }
2752 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002753 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002754 { 0x1f, 0x0002 },
2755 { 0x05, 0x2642 },
2756 { 0x1f, 0x0005 },
2757 { 0x05, 0x8330 },
2758 { 0x06, 0x2642 }
2759 };
2760
françois romieu4da19632011-01-03 15:07:55 +00002761 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002762 }
2763
françois romieubca03d52011-01-03 15:07:31 +00002764 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002765 rtl_writephy(tp, 0x1f, 0x0002);
2766 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2767 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002768
françois romieubca03d52011-01-03 15:07:31 +00002769 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002770 rtl_writephy(tp, 0x1f, 0x0002);
2771 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002772
françois romieu4da19632011-01-03 15:07:55 +00002773 rtl_writephy(tp, 0x1f, 0x0005);
2774 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002775
2776 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002777
françois romieu4da19632011-01-03 15:07:55 +00002778 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002779}
2780
françois romieu4da19632011-01-03 15:07:55 +00002781static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002782{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002783 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002784 { 0x1f, 0x0002 },
2785 { 0x10, 0x0008 },
2786 { 0x0d, 0x006c },
2787
2788 { 0x1f, 0x0000 },
2789 { 0x0d, 0xf880 },
2790
2791 { 0x1f, 0x0001 },
2792 { 0x17, 0x0cc0 },
2793
2794 { 0x1f, 0x0001 },
2795 { 0x0b, 0xa4d8 },
2796 { 0x09, 0x281c },
2797 { 0x07, 0x2883 },
2798 { 0x0a, 0x6b35 },
2799 { 0x1d, 0x3da4 },
2800 { 0x1c, 0xeffd },
2801 { 0x14, 0x7f52 },
2802 { 0x18, 0x7fc6 },
2803 { 0x08, 0x0601 },
2804 { 0x06, 0x4063 },
2805 { 0x10, 0xf074 },
2806 { 0x1f, 0x0003 },
2807 { 0x13, 0x0789 },
2808 { 0x12, 0xf4bd },
2809 { 0x1a, 0x04fd },
2810 { 0x14, 0x84b0 },
2811 { 0x1f, 0x0000 },
2812 { 0x00, 0x9200 },
2813
2814 { 0x1f, 0x0005 },
2815 { 0x01, 0x0340 },
2816 { 0x1f, 0x0001 },
2817 { 0x04, 0x4000 },
2818 { 0x03, 0x1d21 },
2819 { 0x02, 0x0c32 },
2820 { 0x01, 0x0200 },
2821 { 0x00, 0x5554 },
2822 { 0x04, 0x4800 },
2823 { 0x04, 0x4000 },
2824 { 0x04, 0xf000 },
2825 { 0x03, 0xdf01 },
2826 { 0x02, 0xdf20 },
2827 { 0x01, 0x101a },
2828 { 0x00, 0xa0ff },
2829 { 0x04, 0xf800 },
2830 { 0x04, 0xf000 },
2831 { 0x1f, 0x0000 },
2832
2833 { 0x1f, 0x0007 },
2834 { 0x1e, 0x0023 },
2835 { 0x16, 0x0000 },
2836 { 0x1f, 0x0000 }
2837 };
2838
françois romieu4da19632011-01-03 15:07:55 +00002839 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002840}
2841
françois romieue6de30d2011-01-03 15:08:37 +00002842static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2843{
2844 static const struct phy_reg phy_reg_init[] = {
2845 { 0x1f, 0x0001 },
2846 { 0x17, 0x0cc0 },
2847
2848 { 0x1f, 0x0007 },
2849 { 0x1e, 0x002d },
2850 { 0x18, 0x0040 },
2851 { 0x1f, 0x0000 }
2852 };
2853
2854 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2855 rtl_patchphy(tp, 0x0d, 1 << 5);
2856}
2857
Hayes Wang70090422011-07-06 15:58:06 +08002858static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002859{
2860 static const struct phy_reg phy_reg_init[] = {
2861 /* Enable Delay cap */
2862 { 0x1f, 0x0005 },
2863 { 0x05, 0x8b80 },
2864 { 0x06, 0xc896 },
2865 { 0x1f, 0x0000 },
2866
2867 /* Channel estimation fine tune */
2868 { 0x1f, 0x0001 },
2869 { 0x0b, 0x6c20 },
2870 { 0x07, 0x2872 },
2871 { 0x1c, 0xefff },
2872 { 0x1f, 0x0003 },
2873 { 0x14, 0x6420 },
2874 { 0x1f, 0x0000 },
2875
2876 /* Update PFM & 10M TX idle timer */
2877 { 0x1f, 0x0007 },
2878 { 0x1e, 0x002f },
2879 { 0x15, 0x1919 },
2880 { 0x1f, 0x0000 },
2881
2882 { 0x1f, 0x0007 },
2883 { 0x1e, 0x00ac },
2884 { 0x18, 0x0006 },
2885 { 0x1f, 0x0000 }
2886 };
2887
Francois Romieu15ecd032011-04-27 13:52:22 -07002888 rtl_apply_firmware(tp);
2889
hayeswang01dc7fe2011-03-21 01:50:28 +00002890 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2891
2892 /* DCO enable for 10M IDLE Power */
2893 rtl_writephy(tp, 0x1f, 0x0007);
2894 rtl_writephy(tp, 0x1e, 0x0023);
2895 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2896 rtl_writephy(tp, 0x1f, 0x0000);
2897
2898 /* For impedance matching */
2899 rtl_writephy(tp, 0x1f, 0x0002);
2900 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002901 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002902
2903 /* PHY auto speed down */
2904 rtl_writephy(tp, 0x1f, 0x0007);
2905 rtl_writephy(tp, 0x1e, 0x002d);
2906 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2907 rtl_writephy(tp, 0x1f, 0x0000);
2908 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2909
2910 rtl_writephy(tp, 0x1f, 0x0005);
2911 rtl_writephy(tp, 0x05, 0x8b86);
2912 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2913 rtl_writephy(tp, 0x1f, 0x0000);
2914
2915 rtl_writephy(tp, 0x1f, 0x0005);
2916 rtl_writephy(tp, 0x05, 0x8b85);
2917 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2918 rtl_writephy(tp, 0x1f, 0x0007);
2919 rtl_writephy(tp, 0x1e, 0x0020);
2920 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2921 rtl_writephy(tp, 0x1f, 0x0006);
2922 rtl_writephy(tp, 0x00, 0x5a00);
2923 rtl_writephy(tp, 0x1f, 0x0000);
2924 rtl_writephy(tp, 0x0d, 0x0007);
2925 rtl_writephy(tp, 0x0e, 0x003c);
2926 rtl_writephy(tp, 0x0d, 0x4007);
2927 rtl_writephy(tp, 0x0e, 0x0000);
2928 rtl_writephy(tp, 0x0d, 0x0000);
2929}
2930
Hayes Wang70090422011-07-06 15:58:06 +08002931static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2932{
2933 static const struct phy_reg phy_reg_init[] = {
2934 /* Enable Delay cap */
2935 { 0x1f, 0x0004 },
2936 { 0x1f, 0x0007 },
2937 { 0x1e, 0x00ac },
2938 { 0x18, 0x0006 },
2939 { 0x1f, 0x0002 },
2940 { 0x1f, 0x0000 },
2941 { 0x1f, 0x0000 },
2942
2943 /* Channel estimation fine tune */
2944 { 0x1f, 0x0003 },
2945 { 0x09, 0xa20f },
2946 { 0x1f, 0x0000 },
2947 { 0x1f, 0x0000 },
2948
2949 /* Green Setting */
2950 { 0x1f, 0x0005 },
2951 { 0x05, 0x8b5b },
2952 { 0x06, 0x9222 },
2953 { 0x05, 0x8b6d },
2954 { 0x06, 0x8000 },
2955 { 0x05, 0x8b76 },
2956 { 0x06, 0x8000 },
2957 { 0x1f, 0x0000 }
2958 };
2959
2960 rtl_apply_firmware(tp);
2961
2962 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2963
2964 /* For 4-corner performance improve */
2965 rtl_writephy(tp, 0x1f, 0x0005);
2966 rtl_writephy(tp, 0x05, 0x8b80);
2967 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2968 rtl_writephy(tp, 0x1f, 0x0000);
2969
2970 /* PHY auto speed down */
2971 rtl_writephy(tp, 0x1f, 0x0004);
2972 rtl_writephy(tp, 0x1f, 0x0007);
2973 rtl_writephy(tp, 0x1e, 0x002d);
2974 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2975 rtl_writephy(tp, 0x1f, 0x0002);
2976 rtl_writephy(tp, 0x1f, 0x0000);
2977 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2978
2979 /* improve 10M EEE waveform */
2980 rtl_writephy(tp, 0x1f, 0x0005);
2981 rtl_writephy(tp, 0x05, 0x8b86);
2982 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2983 rtl_writephy(tp, 0x1f, 0x0000);
2984
2985 /* Improve 2-pair detection performance */
2986 rtl_writephy(tp, 0x1f, 0x0005);
2987 rtl_writephy(tp, 0x05, 0x8b85);
2988 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
2989 rtl_writephy(tp, 0x1f, 0x0000);
2990
2991 /* EEE setting */
2992 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
2993 ERIAR_EXGMAC);
2994 rtl_writephy(tp, 0x1f, 0x0005);
2995 rtl_writephy(tp, 0x05, 0x8b85);
2996 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2997 rtl_writephy(tp, 0x1f, 0x0004);
2998 rtl_writephy(tp, 0x1f, 0x0007);
2999 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04003000 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003001 rtl_writephy(tp, 0x1f, 0x0002);
3002 rtl_writephy(tp, 0x1f, 0x0000);
3003 rtl_writephy(tp, 0x0d, 0x0007);
3004 rtl_writephy(tp, 0x0e, 0x003c);
3005 rtl_writephy(tp, 0x0d, 0x4007);
3006 rtl_writephy(tp, 0x0e, 0x0000);
3007 rtl_writephy(tp, 0x0d, 0x0000);
3008
3009 /* Green feature */
3010 rtl_writephy(tp, 0x1f, 0x0003);
3011 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3012 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3013 rtl_writephy(tp, 0x1f, 0x0000);
3014}
3015
Hayes Wangc2218922011-09-06 16:55:18 +08003016static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3017{
3018 static const struct phy_reg phy_reg_init[] = {
3019 /* Channel estimation fine tune */
3020 { 0x1f, 0x0003 },
3021 { 0x09, 0xa20f },
3022 { 0x1f, 0x0000 },
3023
3024 /* Modify green table for giga & fnet */
3025 { 0x1f, 0x0005 },
3026 { 0x05, 0x8b55 },
3027 { 0x06, 0x0000 },
3028 { 0x05, 0x8b5e },
3029 { 0x06, 0x0000 },
3030 { 0x05, 0x8b67 },
3031 { 0x06, 0x0000 },
3032 { 0x05, 0x8b70 },
3033 { 0x06, 0x0000 },
3034 { 0x1f, 0x0000 },
3035 { 0x1f, 0x0007 },
3036 { 0x1e, 0x0078 },
3037 { 0x17, 0x0000 },
3038 { 0x19, 0x00fb },
3039 { 0x1f, 0x0000 },
3040
3041 /* Modify green table for 10M */
3042 { 0x1f, 0x0005 },
3043 { 0x05, 0x8b79 },
3044 { 0x06, 0xaa00 },
3045 { 0x1f, 0x0000 },
3046
3047 /* Disable hiimpedance detection (RTCT) */
3048 { 0x1f, 0x0003 },
3049 { 0x01, 0x328a },
3050 { 0x1f, 0x0000 }
3051 };
3052
3053 rtl_apply_firmware(tp);
3054
3055 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3056
3057 /* For 4-corner performance improve */
3058 rtl_writephy(tp, 0x1f, 0x0005);
3059 rtl_writephy(tp, 0x05, 0x8b80);
3060 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3061 rtl_writephy(tp, 0x1f, 0x0000);
3062
3063 /* PHY auto speed down */
3064 rtl_writephy(tp, 0x1f, 0x0007);
3065 rtl_writephy(tp, 0x1e, 0x002d);
3066 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3067 rtl_writephy(tp, 0x1f, 0x0000);
3068 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3069
3070 /* Improve 10M EEE waveform */
3071 rtl_writephy(tp, 0x1f, 0x0005);
3072 rtl_writephy(tp, 0x05, 0x8b86);
3073 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3074 rtl_writephy(tp, 0x1f, 0x0000);
3075
3076 /* Improve 2-pair detection performance */
3077 rtl_writephy(tp, 0x1f, 0x0005);
3078 rtl_writephy(tp, 0x05, 0x8b85);
3079 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3080 rtl_writephy(tp, 0x1f, 0x0000);
3081}
3082
3083static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3084{
3085 rtl_apply_firmware(tp);
3086
3087 /* For 4-corner performance improve */
3088 rtl_writephy(tp, 0x1f, 0x0005);
3089 rtl_writephy(tp, 0x05, 0x8b80);
3090 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3091 rtl_writephy(tp, 0x1f, 0x0000);
3092
3093 /* PHY auto speed down */
3094 rtl_writephy(tp, 0x1f, 0x0007);
3095 rtl_writephy(tp, 0x1e, 0x002d);
3096 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3097 rtl_writephy(tp, 0x1f, 0x0000);
3098 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3099
3100 /* Improve 10M EEE waveform */
3101 rtl_writephy(tp, 0x1f, 0x0005);
3102 rtl_writephy(tp, 0x05, 0x8b86);
3103 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3104 rtl_writephy(tp, 0x1f, 0x0000);
3105}
3106
françois romieu4da19632011-01-03 15:07:55 +00003107static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003108{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003109 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003110 { 0x1f, 0x0003 },
3111 { 0x08, 0x441d },
3112 { 0x01, 0x9100 },
3113 { 0x1f, 0x0000 }
3114 };
3115
françois romieu4da19632011-01-03 15:07:55 +00003116 rtl_writephy(tp, 0x1f, 0x0000);
3117 rtl_patchphy(tp, 0x11, 1 << 12);
3118 rtl_patchphy(tp, 0x19, 1 << 13);
3119 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003120
françois romieu4da19632011-01-03 15:07:55 +00003121 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003122}
3123
Hayes Wang5a5e4442011-02-22 17:26:21 +08003124static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3125{
3126 static const struct phy_reg phy_reg_init[] = {
3127 { 0x1f, 0x0005 },
3128 { 0x1a, 0x0000 },
3129 { 0x1f, 0x0000 },
3130
3131 { 0x1f, 0x0004 },
3132 { 0x1c, 0x0000 },
3133 { 0x1f, 0x0000 },
3134
3135 { 0x1f, 0x0001 },
3136 { 0x15, 0x7701 },
3137 { 0x1f, 0x0000 }
3138 };
3139
3140 /* Disable ALDPS before ram code */
3141 rtl_writephy(tp, 0x1f, 0x0000);
3142 rtl_writephy(tp, 0x18, 0x0310);
3143 msleep(100);
3144
François Romieu953a12c2011-04-24 17:38:48 +02003145 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003146
3147 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3148}
3149
Francois Romieu5615d9f2007-08-17 17:50:46 +02003150static void rtl_hw_phy_config(struct net_device *dev)
3151{
3152 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003153
3154 rtl8169_print_mac_version(tp);
3155
3156 switch (tp->mac_version) {
3157 case RTL_GIGA_MAC_VER_01:
3158 break;
3159 case RTL_GIGA_MAC_VER_02:
3160 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003161 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003162 break;
3163 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003164 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003165 break;
françois romieu2e9558562009-08-10 19:44:19 +00003166 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003167 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003168 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003169 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003170 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003171 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003172 case RTL_GIGA_MAC_VER_07:
3173 case RTL_GIGA_MAC_VER_08:
3174 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003175 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003176 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003177 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003178 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003179 break;
3180 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003181 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003182 break;
3183 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003184 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003185 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003186 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003187 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003188 break;
3189 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003190 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003191 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003192 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003193 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003194 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003195 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003196 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003197 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003198 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003199 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003200 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003201 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003202 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003203 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003204 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003205 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003206 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003207 break;
3208 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003209 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003210 break;
3211 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003212 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003213 break;
françois romieue6de30d2011-01-03 15:08:37 +00003214 case RTL_GIGA_MAC_VER_28:
3215 rtl8168d_4_hw_phy_config(tp);
3216 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003217 case RTL_GIGA_MAC_VER_29:
3218 case RTL_GIGA_MAC_VER_30:
3219 rtl8105e_hw_phy_config(tp);
3220 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003221 case RTL_GIGA_MAC_VER_31:
3222 /* None. */
3223 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003224 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003225 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003226 rtl8168e_1_hw_phy_config(tp);
3227 break;
3228 case RTL_GIGA_MAC_VER_34:
3229 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003230 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003231 case RTL_GIGA_MAC_VER_35:
3232 rtl8168f_1_hw_phy_config(tp);
3233 break;
3234 case RTL_GIGA_MAC_VER_36:
3235 rtl8168f_2_hw_phy_config(tp);
3236 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003237
Francois Romieu5615d9f2007-08-17 17:50:46 +02003238 default:
3239 break;
3240 }
3241}
3242
Francois Romieuda78dbf2012-01-26 14:18:23 +01003243static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 struct timer_list *timer = &tp->timer;
3246 void __iomem *ioaddr = tp->mmio_addr;
3247 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3248
Francois Romieubcf0bf92006-07-26 23:14:13 +02003249 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
françois romieu4da19632011-01-03 15:07:55 +00003251 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003252 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 * A busy loop could burn quite a few cycles on nowadays CPU.
3254 * Let's delay the execution of the timer for a few ticks.
3255 */
3256 timeout = HZ/10;
3257 goto out_mod_timer;
3258 }
3259
3260 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01003261 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
Francois Romieuda78dbf2012-01-26 14:18:23 +01003263 netif_warn(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
françois romieu4da19632011-01-03 15:07:55 +00003265 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
3267out_mod_timer:
3268 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003269}
3270
3271static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3272{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003273 if (!test_and_set_bit(flag, tp->wk.flags))
3274 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003275}
3276
3277static void rtl8169_phy_timer(unsigned long __opaque)
3278{
3279 struct net_device *dev = (struct net_device *)__opaque;
3280 struct rtl8169_private *tp = netdev_priv(dev);
3281
Francois Romieu98ddf982012-01-31 10:47:34 +01003282 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283}
3284
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3286 void __iomem *ioaddr)
3287{
3288 iounmap(ioaddr);
3289 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003290 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 pci_disable_device(pdev);
3292 free_netdev(dev);
3293}
3294
Francois Romieubf793292006-11-01 00:53:05 +01003295static void rtl8169_phy_reset(struct net_device *dev,
3296 struct rtl8169_private *tp)
3297{
Francois Romieu07d3f512007-02-21 22:40:46 +01003298 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003299
françois romieu4da19632011-01-03 15:07:55 +00003300 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003301 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003302 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003303 return;
3304 msleep(1);
3305 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003306 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003307}
3308
David S. Miller8decf862011-09-22 03:23:13 -04003309static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3310{
3311 void __iomem *ioaddr = tp->mmio_addr;
3312
3313 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3314 (RTL_R8(PHYstatus) & TBI_Enable);
3315}
3316
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003317static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003319 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003320
Francois Romieu5615d9f2007-08-17 17:50:46 +02003321 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003322
Marcus Sundberg773328942008-07-10 21:28:08 +02003323 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3324 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3325 RTL_W8(0x82, 0x01);
3326 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003327
Francois Romieu6dccd162007-02-13 23:38:05 +01003328 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3329
3330 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3331 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003332
Francois Romieubcf0bf92006-07-26 23:14:13 +02003333 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003334 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3335 RTL_W8(0x82, 0x01);
3336 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003337 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003338 }
3339
Francois Romieubf793292006-11-01 00:53:05 +01003340 rtl8169_phy_reset(dev, tp);
3341
Oliver Neukum54405cd2011-01-06 21:55:13 +01003342 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003343 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3344 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3345 (tp->mii.supports_gmii ?
3346 ADVERTISED_1000baseT_Half |
3347 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003348
David S. Miller8decf862011-09-22 03:23:13 -04003349 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003350 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003351}
3352
Francois Romieu773d2022007-01-31 23:47:43 +01003353static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3354{
3355 void __iomem *ioaddr = tp->mmio_addr;
3356 u32 high;
3357 u32 low;
3358
3359 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3360 high = addr[4] | (addr[5] << 8);
3361
Francois Romieuda78dbf2012-01-26 14:18:23 +01003362 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003363
3364 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003365
Francois Romieu773d2022007-01-31 23:47:43 +01003366 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00003367 RTL_R32(MAC4);
3368
Francois Romieu78f1cd02010-03-27 19:35:46 -07003369 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00003370 RTL_R32(MAC0);
3371
françois romieuc28aa382011-08-02 03:53:43 +00003372 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3373 const struct exgmac_reg e[] = {
3374 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3375 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3376 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3377 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3378 low >> 16 },
3379 };
3380
3381 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3382 }
3383
Francois Romieu773d2022007-01-31 23:47:43 +01003384 RTL_W8(Cfg9346, Cfg9346_Lock);
3385
Francois Romieuda78dbf2012-01-26 14:18:23 +01003386 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003387}
3388
3389static int rtl_set_mac_address(struct net_device *dev, void *p)
3390{
3391 struct rtl8169_private *tp = netdev_priv(dev);
3392 struct sockaddr *addr = p;
3393
3394 if (!is_valid_ether_addr(addr->sa_data))
3395 return -EADDRNOTAVAIL;
3396
3397 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3398
3399 rtl_rar_set(tp, dev->dev_addr);
3400
3401 return 0;
3402}
3403
Francois Romieu5f787a12006-08-17 13:02:36 +02003404static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3405{
3406 struct rtl8169_private *tp = netdev_priv(dev);
3407 struct mii_ioctl_data *data = if_mii(ifr);
3408
Francois Romieu8b4ab282008-11-19 22:05:25 -08003409 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3410}
Francois Romieu5f787a12006-08-17 13:02:36 +02003411
Francois Romieucecb5fd2011-04-01 10:21:07 +02003412static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3413 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003414{
Francois Romieu5f787a12006-08-17 13:02:36 +02003415 switch (cmd) {
3416 case SIOCGMIIPHY:
3417 data->phy_id = 32; /* Internal PHY */
3418 return 0;
3419
3420 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003421 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003422 return 0;
3423
3424 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003425 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003426 return 0;
3427 }
3428 return -EOPNOTSUPP;
3429}
3430
Francois Romieu8b4ab282008-11-19 22:05:25 -08003431static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3432{
3433 return -EOPNOTSUPP;
3434}
3435
Francois Romieufbac58f2007-10-04 22:51:38 +02003436static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3437{
3438 if (tp->features & RTL_FEATURE_MSI) {
3439 pci_disable_msi(pdev);
3440 tp->features &= ~RTL_FEATURE_MSI;
3441 }
3442}
3443
françois romieuc0e45c12011-01-03 15:08:04 +00003444static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3445{
3446 struct mdio_ops *ops = &tp->mdio_ops;
3447
3448 switch (tp->mac_version) {
3449 case RTL_GIGA_MAC_VER_27:
3450 ops->write = r8168dp_1_mdio_write;
3451 ops->read = r8168dp_1_mdio_read;
3452 break;
françois romieue6de30d2011-01-03 15:08:37 +00003453 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003454 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003455 ops->write = r8168dp_2_mdio_write;
3456 ops->read = r8168dp_2_mdio_read;
3457 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003458 default:
3459 ops->write = r8169_mdio_write;
3460 ops->read = r8169_mdio_read;
3461 break;
3462 }
3463}
3464
David S. Miller1805b2f2011-10-24 18:18:09 -04003465static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3466{
3467 void __iomem *ioaddr = tp->mmio_addr;
3468
3469 switch (tp->mac_version) {
3470 case RTL_GIGA_MAC_VER_29:
3471 case RTL_GIGA_MAC_VER_30:
3472 case RTL_GIGA_MAC_VER_32:
3473 case RTL_GIGA_MAC_VER_33:
3474 case RTL_GIGA_MAC_VER_34:
3475 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3476 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3477 break;
3478 default:
3479 break;
3480 }
3481}
3482
3483static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3484{
3485 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3486 return false;
3487
3488 rtl_writephy(tp, 0x1f, 0x0000);
3489 rtl_writephy(tp, MII_BMCR, 0x0000);
3490
3491 rtl_wol_suspend_quirk(tp);
3492
3493 return true;
3494}
3495
françois romieu065c27c2011-01-03 15:08:12 +00003496static void r810x_phy_power_down(struct rtl8169_private *tp)
3497{
3498 rtl_writephy(tp, 0x1f, 0x0000);
3499 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3500}
3501
3502static void r810x_phy_power_up(struct rtl8169_private *tp)
3503{
3504 rtl_writephy(tp, 0x1f, 0x0000);
3505 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3506}
3507
3508static void r810x_pll_power_down(struct rtl8169_private *tp)
3509{
David S. Miller1805b2f2011-10-24 18:18:09 -04003510 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003511 return;
françois romieu065c27c2011-01-03 15:08:12 +00003512
3513 r810x_phy_power_down(tp);
3514}
3515
3516static void r810x_pll_power_up(struct rtl8169_private *tp)
3517{
3518 r810x_phy_power_up(tp);
3519}
3520
3521static void r8168_phy_power_up(struct rtl8169_private *tp)
3522{
3523 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003524 switch (tp->mac_version) {
3525 case RTL_GIGA_MAC_VER_11:
3526 case RTL_GIGA_MAC_VER_12:
3527 case RTL_GIGA_MAC_VER_17:
3528 case RTL_GIGA_MAC_VER_18:
3529 case RTL_GIGA_MAC_VER_19:
3530 case RTL_GIGA_MAC_VER_20:
3531 case RTL_GIGA_MAC_VER_21:
3532 case RTL_GIGA_MAC_VER_22:
3533 case RTL_GIGA_MAC_VER_23:
3534 case RTL_GIGA_MAC_VER_24:
3535 case RTL_GIGA_MAC_VER_25:
3536 case RTL_GIGA_MAC_VER_26:
3537 case RTL_GIGA_MAC_VER_27:
3538 case RTL_GIGA_MAC_VER_28:
3539 case RTL_GIGA_MAC_VER_31:
3540 rtl_writephy(tp, 0x0e, 0x0000);
3541 break;
3542 default:
3543 break;
3544 }
françois romieu065c27c2011-01-03 15:08:12 +00003545 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3546}
3547
3548static void r8168_phy_power_down(struct rtl8169_private *tp)
3549{
3550 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003551 switch (tp->mac_version) {
3552 case RTL_GIGA_MAC_VER_32:
3553 case RTL_GIGA_MAC_VER_33:
3554 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3555 break;
3556
3557 case RTL_GIGA_MAC_VER_11:
3558 case RTL_GIGA_MAC_VER_12:
3559 case RTL_GIGA_MAC_VER_17:
3560 case RTL_GIGA_MAC_VER_18:
3561 case RTL_GIGA_MAC_VER_19:
3562 case RTL_GIGA_MAC_VER_20:
3563 case RTL_GIGA_MAC_VER_21:
3564 case RTL_GIGA_MAC_VER_22:
3565 case RTL_GIGA_MAC_VER_23:
3566 case RTL_GIGA_MAC_VER_24:
3567 case RTL_GIGA_MAC_VER_25:
3568 case RTL_GIGA_MAC_VER_26:
3569 case RTL_GIGA_MAC_VER_27:
3570 case RTL_GIGA_MAC_VER_28:
3571 case RTL_GIGA_MAC_VER_31:
3572 rtl_writephy(tp, 0x0e, 0x0200);
3573 default:
3574 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3575 break;
3576 }
françois romieu065c27c2011-01-03 15:08:12 +00003577}
3578
3579static void r8168_pll_power_down(struct rtl8169_private *tp)
3580{
3581 void __iomem *ioaddr = tp->mmio_addr;
3582
Francois Romieucecb5fd2011-04-01 10:21:07 +02003583 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3584 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3585 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003586 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003587 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003588 }
françois romieu065c27c2011-01-03 15:08:12 +00003589
Francois Romieucecb5fd2011-04-01 10:21:07 +02003590 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3591 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003592 (RTL_R16(CPlusCmd) & ASF)) {
3593 return;
3594 }
3595
hayeswang01dc7fe2011-03-21 01:50:28 +00003596 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3597 tp->mac_version == RTL_GIGA_MAC_VER_33)
3598 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3599
David S. Miller1805b2f2011-10-24 18:18:09 -04003600 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003601 return;
françois romieu065c27c2011-01-03 15:08:12 +00003602
3603 r8168_phy_power_down(tp);
3604
3605 switch (tp->mac_version) {
3606 case RTL_GIGA_MAC_VER_25:
3607 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003608 case RTL_GIGA_MAC_VER_27:
3609 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003610 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003611 case RTL_GIGA_MAC_VER_32:
3612 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003613 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3614 break;
3615 }
3616}
3617
3618static void r8168_pll_power_up(struct rtl8169_private *tp)
3619{
3620 void __iomem *ioaddr = tp->mmio_addr;
3621
Francois Romieucecb5fd2011-04-01 10:21:07 +02003622 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3623 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3624 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003625 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003626 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003627 }
françois romieu065c27c2011-01-03 15:08:12 +00003628
3629 switch (tp->mac_version) {
3630 case RTL_GIGA_MAC_VER_25:
3631 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003632 case RTL_GIGA_MAC_VER_27:
3633 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003634 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003635 case RTL_GIGA_MAC_VER_32:
3636 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003637 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3638 break;
3639 }
3640
3641 r8168_phy_power_up(tp);
3642}
3643
Francois Romieud58d46b2011-05-03 16:38:29 +02003644static void rtl_generic_op(struct rtl8169_private *tp,
3645 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00003646{
3647 if (op)
3648 op(tp);
3649}
3650
3651static void rtl_pll_power_down(struct rtl8169_private *tp)
3652{
Francois Romieud58d46b2011-05-03 16:38:29 +02003653 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00003654}
3655
3656static void rtl_pll_power_up(struct rtl8169_private *tp)
3657{
Francois Romieud58d46b2011-05-03 16:38:29 +02003658 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00003659}
3660
3661static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3662{
3663 struct pll_power_ops *ops = &tp->pll_power_ops;
3664
3665 switch (tp->mac_version) {
3666 case RTL_GIGA_MAC_VER_07:
3667 case RTL_GIGA_MAC_VER_08:
3668 case RTL_GIGA_MAC_VER_09:
3669 case RTL_GIGA_MAC_VER_10:
3670 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003671 case RTL_GIGA_MAC_VER_29:
3672 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003673 ops->down = r810x_pll_power_down;
3674 ops->up = r810x_pll_power_up;
3675 break;
3676
3677 case RTL_GIGA_MAC_VER_11:
3678 case RTL_GIGA_MAC_VER_12:
3679 case RTL_GIGA_MAC_VER_17:
3680 case RTL_GIGA_MAC_VER_18:
3681 case RTL_GIGA_MAC_VER_19:
3682 case RTL_GIGA_MAC_VER_20:
3683 case RTL_GIGA_MAC_VER_21:
3684 case RTL_GIGA_MAC_VER_22:
3685 case RTL_GIGA_MAC_VER_23:
3686 case RTL_GIGA_MAC_VER_24:
3687 case RTL_GIGA_MAC_VER_25:
3688 case RTL_GIGA_MAC_VER_26:
3689 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003690 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003691 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003692 case RTL_GIGA_MAC_VER_32:
3693 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003694 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08003695 case RTL_GIGA_MAC_VER_35:
3696 case RTL_GIGA_MAC_VER_36:
françois romieu065c27c2011-01-03 15:08:12 +00003697 ops->down = r8168_pll_power_down;
3698 ops->up = r8168_pll_power_up;
3699 break;
3700
3701 default:
3702 ops->down = NULL;
3703 ops->up = NULL;
3704 break;
3705 }
3706}
3707
Hayes Wange542a222011-07-06 15:58:04 +08003708static void rtl_init_rxcfg(struct rtl8169_private *tp)
3709{
3710 void __iomem *ioaddr = tp->mmio_addr;
3711
3712 switch (tp->mac_version) {
3713 case RTL_GIGA_MAC_VER_01:
3714 case RTL_GIGA_MAC_VER_02:
3715 case RTL_GIGA_MAC_VER_03:
3716 case RTL_GIGA_MAC_VER_04:
3717 case RTL_GIGA_MAC_VER_05:
3718 case RTL_GIGA_MAC_VER_06:
3719 case RTL_GIGA_MAC_VER_10:
3720 case RTL_GIGA_MAC_VER_11:
3721 case RTL_GIGA_MAC_VER_12:
3722 case RTL_GIGA_MAC_VER_13:
3723 case RTL_GIGA_MAC_VER_14:
3724 case RTL_GIGA_MAC_VER_15:
3725 case RTL_GIGA_MAC_VER_16:
3726 case RTL_GIGA_MAC_VER_17:
3727 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3728 break;
3729 case RTL_GIGA_MAC_VER_18:
3730 case RTL_GIGA_MAC_VER_19:
3731 case RTL_GIGA_MAC_VER_20:
3732 case RTL_GIGA_MAC_VER_21:
3733 case RTL_GIGA_MAC_VER_22:
3734 case RTL_GIGA_MAC_VER_23:
3735 case RTL_GIGA_MAC_VER_24:
3736 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3737 break;
3738 default:
3739 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3740 break;
3741 }
3742}
3743
Hayes Wang92fc43b2011-07-06 15:58:03 +08003744static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3745{
3746 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3747}
3748
Francois Romieud58d46b2011-05-03 16:38:29 +02003749static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3750{
françois romieu9c5028e2012-03-02 04:43:14 +00003751 void __iomem *ioaddr = tp->mmio_addr;
3752
3753 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003754 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00003755 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003756}
3757
3758static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3759{
françois romieu9c5028e2012-03-02 04:43:14 +00003760 void __iomem *ioaddr = tp->mmio_addr;
3761
3762 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003763 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00003764 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003765}
3766
3767static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3768{
3769 void __iomem *ioaddr = tp->mmio_addr;
3770
3771 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3772 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3773 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3774}
3775
3776static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3777{
3778 void __iomem *ioaddr = tp->mmio_addr;
3779
3780 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3781 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3782 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3783}
3784
3785static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3786{
3787 void __iomem *ioaddr = tp->mmio_addr;
3788
3789 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3790}
3791
3792static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3793{
3794 void __iomem *ioaddr = tp->mmio_addr;
3795
3796 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3797}
3798
3799static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3800{
3801 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003802
3803 RTL_W8(MaxTxPacketSize, 0x3f);
3804 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3805 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003806 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003807}
3808
3809static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3810{
3811 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003812
3813 RTL_W8(MaxTxPacketSize, 0x0c);
3814 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3815 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003816 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003817}
3818
3819static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3820{
3821 rtl_tx_performance_tweak(tp->pci_dev,
3822 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3823}
3824
3825static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3826{
3827 rtl_tx_performance_tweak(tp->pci_dev,
3828 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3829}
3830
3831static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3832{
3833 void __iomem *ioaddr = tp->mmio_addr;
3834
3835 r8168b_0_hw_jumbo_enable(tp);
3836
3837 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
3838}
3839
3840static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3841{
3842 void __iomem *ioaddr = tp->mmio_addr;
3843
3844 r8168b_0_hw_jumbo_disable(tp);
3845
3846 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3847}
3848
3849static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
3850{
3851 struct jumbo_ops *ops = &tp->jumbo_ops;
3852
3853 switch (tp->mac_version) {
3854 case RTL_GIGA_MAC_VER_11:
3855 ops->disable = r8168b_0_hw_jumbo_disable;
3856 ops->enable = r8168b_0_hw_jumbo_enable;
3857 break;
3858 case RTL_GIGA_MAC_VER_12:
3859 case RTL_GIGA_MAC_VER_17:
3860 ops->disable = r8168b_1_hw_jumbo_disable;
3861 ops->enable = r8168b_1_hw_jumbo_enable;
3862 break;
3863 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
3864 case RTL_GIGA_MAC_VER_19:
3865 case RTL_GIGA_MAC_VER_20:
3866 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
3867 case RTL_GIGA_MAC_VER_22:
3868 case RTL_GIGA_MAC_VER_23:
3869 case RTL_GIGA_MAC_VER_24:
3870 case RTL_GIGA_MAC_VER_25:
3871 case RTL_GIGA_MAC_VER_26:
3872 ops->disable = r8168c_hw_jumbo_disable;
3873 ops->enable = r8168c_hw_jumbo_enable;
3874 break;
3875 case RTL_GIGA_MAC_VER_27:
3876 case RTL_GIGA_MAC_VER_28:
3877 ops->disable = r8168dp_hw_jumbo_disable;
3878 ops->enable = r8168dp_hw_jumbo_enable;
3879 break;
3880 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
3881 case RTL_GIGA_MAC_VER_32:
3882 case RTL_GIGA_MAC_VER_33:
3883 case RTL_GIGA_MAC_VER_34:
3884 ops->disable = r8168e_hw_jumbo_disable;
3885 ops->enable = r8168e_hw_jumbo_enable;
3886 break;
3887
3888 /*
3889 * No action needed for jumbo frames with 8169.
3890 * No jumbo for 810x at all.
3891 */
3892 default:
3893 ops->disable = NULL;
3894 ops->enable = NULL;
3895 break;
3896 }
3897}
3898
Francois Romieu6f43adc2011-04-29 15:05:51 +02003899static void rtl_hw_reset(struct rtl8169_private *tp)
3900{
3901 void __iomem *ioaddr = tp->mmio_addr;
3902 int i;
3903
3904 /* Soft reset the chip. */
3905 RTL_W8(ChipCmd, CmdReset);
3906
3907 /* Check that the chip has finished the reset. */
3908 for (i = 0; i < 100; i++) {
3909 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3910 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003911 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003912 }
3913}
3914
Francois Romieub6ffd972011-06-17 17:00:05 +02003915static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
3916{
3917 struct rtl_fw *rtl_fw;
3918 const char *name;
3919 int rc = -ENOMEM;
3920
3921 name = rtl_lookup_firmware_name(tp);
3922 if (!name)
3923 goto out_no_firmware;
3924
3925 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
3926 if (!rtl_fw)
3927 goto err_warn;
3928
3929 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
3930 if (rc < 0)
3931 goto err_free;
3932
Francois Romieufd112f22011-06-18 00:10:29 +02003933 rc = rtl_check_firmware(tp, rtl_fw);
3934 if (rc < 0)
3935 goto err_release_firmware;
3936
Francois Romieub6ffd972011-06-17 17:00:05 +02003937 tp->rtl_fw = rtl_fw;
3938out:
3939 return;
3940
Francois Romieufd112f22011-06-18 00:10:29 +02003941err_release_firmware:
3942 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02003943err_free:
3944 kfree(rtl_fw);
3945err_warn:
3946 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
3947 name, rc);
3948out_no_firmware:
3949 tp->rtl_fw = NULL;
3950 goto out;
3951}
3952
François Romieu953a12c2011-04-24 17:38:48 +02003953static void rtl_request_firmware(struct rtl8169_private *tp)
3954{
Francois Romieub6ffd972011-06-17 17:00:05 +02003955 if (IS_ERR(tp->rtl_fw))
3956 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02003957}
3958
Hayes Wang92fc43b2011-07-06 15:58:03 +08003959static void rtl_rx_close(struct rtl8169_private *tp)
3960{
3961 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003962
Francois Romieu1687b562011-07-19 17:21:29 +02003963 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08003964}
3965
françois romieue6de30d2011-01-03 15:08:37 +00003966static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967{
françois romieue6de30d2011-01-03 15:08:37 +00003968 void __iomem *ioaddr = tp->mmio_addr;
3969
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00003971 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972
Hayes Wang92fc43b2011-07-06 15:58:03 +08003973 rtl_rx_close(tp);
3974
Hayes Wang5d2e1952011-02-22 17:26:22 +08003975 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00003976 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3977 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00003978 while (RTL_R8(TxPoll) & NPQ)
3979 udelay(20);
Hayes Wangc2218922011-09-06 16:55:18 +08003980 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
3981 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
3982 tp->mac_version == RTL_GIGA_MAC_VER_36) {
David S. Miller8decf862011-09-22 03:23:13 -04003983 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08003984 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
3985 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08003986 } else {
3987 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
3988 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00003989 }
3990
Hayes Wang92fc43b2011-07-06 15:58:03 +08003991 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992}
3993
Francois Romieu7f796d832007-06-11 23:04:41 +02003994static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003995{
3996 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01003997
3998 /* Set DMA burst size and Interframe Gap Time */
3999 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4000 (InterFrameGap << TxInterFrameGapShift));
4001}
4002
Francois Romieu07ce4062007-02-23 23:36:39 +01004003static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004{
4005 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006
Francois Romieu07ce4062007-02-23 23:36:39 +01004007 tp->hw_start(dev);
4008
Francois Romieuda78dbf2012-01-26 14:18:23 +01004009 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004010}
4011
Francois Romieu7f796d832007-06-11 23:04:41 +02004012static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4013 void __iomem *ioaddr)
4014{
4015 /*
4016 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4017 * register to be written before TxDescAddrLow to work.
4018 * Switching from MMIO to I/O access fixes the issue as well.
4019 */
4020 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004021 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004022 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004023 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004024}
4025
4026static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4027{
4028 u16 cmd;
4029
4030 cmd = RTL_R16(CPlusCmd);
4031 RTL_W16(CPlusCmd, cmd);
4032 return cmd;
4033}
4034
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004035static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004036{
4037 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004038 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004039}
4040
Francois Romieu6dccd162007-02-13 23:38:05 +01004041static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4042{
Francois Romieu37441002011-06-17 22:58:54 +02004043 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004044 u32 mac_version;
4045 u32 clk;
4046 u32 val;
4047 } cfg2_info [] = {
4048 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4049 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4050 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4051 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004052 };
4053 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004054 unsigned int i;
4055 u32 clk;
4056
4057 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004058 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004059 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4060 RTL_W32(0x7c, p->val);
4061 break;
4062 }
4063 }
4064}
4065
Francois Romieue6b763e2012-03-08 09:35:39 +01004066static void rtl_set_rx_mode(struct net_device *dev)
4067{
4068 struct rtl8169_private *tp = netdev_priv(dev);
4069 void __iomem *ioaddr = tp->mmio_addr;
4070 u32 mc_filter[2]; /* Multicast hash filter */
4071 int rx_mode;
4072 u32 tmp = 0;
4073
4074 if (dev->flags & IFF_PROMISC) {
4075 /* Unconditionally log net taps. */
4076 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4077 rx_mode =
4078 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4079 AcceptAllPhys;
4080 mc_filter[1] = mc_filter[0] = 0xffffffff;
4081 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4082 (dev->flags & IFF_ALLMULTI)) {
4083 /* Too many to filter perfectly -- accept all multicasts. */
4084 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4085 mc_filter[1] = mc_filter[0] = 0xffffffff;
4086 } else {
4087 struct netdev_hw_addr *ha;
4088
4089 rx_mode = AcceptBroadcast | AcceptMyPhys;
4090 mc_filter[1] = mc_filter[0] = 0;
4091 netdev_for_each_mc_addr(ha, dev) {
4092 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4093 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4094 rx_mode |= AcceptMulticast;
4095 }
4096 }
4097
4098 if (dev->features & NETIF_F_RXALL)
4099 rx_mode |= (AcceptErr | AcceptRunt);
4100
4101 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4102
4103 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4104 u32 data = mc_filter[0];
4105
4106 mc_filter[0] = swab32(mc_filter[1]);
4107 mc_filter[1] = swab32(data);
4108 }
4109
4110 RTL_W32(MAR0 + 4, mc_filter[1]);
4111 RTL_W32(MAR0 + 0, mc_filter[0]);
4112
4113 RTL_W32(RxConfig, tmp);
4114}
4115
Francois Romieu07ce4062007-02-23 23:36:39 +01004116static void rtl_hw_start_8169(struct net_device *dev)
4117{
4118 struct rtl8169_private *tp = netdev_priv(dev);
4119 void __iomem *ioaddr = tp->mmio_addr;
4120 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004121
Francois Romieu9cb427b2006-11-02 00:10:16 +01004122 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4123 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4124 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4125 }
4126
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004128 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4129 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4130 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4131 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004132 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4133
Hayes Wange542a222011-07-06 15:58:04 +08004134 rtl_init_rxcfg(tp);
4135
françois romieuf0298f82011-01-03 15:07:42 +00004136 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004138 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
Francois Romieucecb5fd2011-04-01 10:21:07 +02004140 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4141 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4142 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4143 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004144 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
Francois Romieu7f796d832007-06-11 23:04:41 +02004146 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004147
Francois Romieucecb5fd2011-04-01 10:21:07 +02004148 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4149 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004150 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004152 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 }
4154
Francois Romieubcf0bf92006-07-26 23:14:13 +02004155 RTL_W16(CPlusCmd, tp->cp_cmd);
4156
Francois Romieu6dccd162007-02-13 23:38:05 +01004157 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4158
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 /*
4160 * Undocumented corner. Supposedly:
4161 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4162 */
4163 RTL_W16(IntrMitigate, 0x0000);
4164
Francois Romieu7f796d832007-06-11 23:04:41 +02004165 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004166
Francois Romieucecb5fd2011-04-01 10:21:07 +02004167 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4168 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4169 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4170 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004171 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4172 rtl_set_rx_tx_config_registers(tp);
4173 }
4174
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004176
4177 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4178 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179
4180 RTL_W32(RxMissed, 0);
4181
Francois Romieu07ce4062007-02-23 23:36:39 +01004182 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183
4184 /* no early-rx interrupts */
4185 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004186}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187
françois romieu650e8d52011-01-03 15:08:29 +00004188static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004189{
4190 u32 csi;
4191
4192 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00004193 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4194}
4195
françois romieue6de30d2011-01-03 15:08:37 +00004196static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4197{
4198 rtl_csi_access_enable(ioaddr, 0x17000000);
4199}
4200
françois romieu650e8d52011-01-03 15:08:29 +00004201static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4202{
4203 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02004204}
4205
4206struct ephy_info {
4207 unsigned int offset;
4208 u16 mask;
4209 u16 bits;
4210};
4211
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004212static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004213{
4214 u16 w;
4215
4216 while (len-- > 0) {
4217 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4218 rtl_ephy_write(ioaddr, e->offset, w);
4219 e++;
4220 }
4221}
4222
Francois Romieub726e492008-06-28 12:22:59 +02004223static void rtl_disable_clock_request(struct pci_dev *pdev)
4224{
Jon Masone44daad2011-06-27 07:46:31 +00004225 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004226
4227 if (cap) {
4228 u16 ctl;
4229
4230 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4231 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4232 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4233 }
4234}
4235
françois romieue6de30d2011-01-03 15:08:37 +00004236static void rtl_enable_clock_request(struct pci_dev *pdev)
4237{
Jon Masone44daad2011-06-27 07:46:31 +00004238 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004239
4240 if (cap) {
4241 u16 ctl;
4242
4243 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4244 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4245 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4246 }
4247}
4248
Francois Romieub726e492008-06-28 12:22:59 +02004249#define R8168_CPCMD_QUIRK_MASK (\
4250 EnableBist | \
4251 Mac_dbgo_oe | \
4252 Force_half_dup | \
4253 Force_rxflow_en | \
4254 Force_txflow_en | \
4255 Cxpl_dbg_sel | \
4256 ASF | \
4257 PktCntrDisable | \
4258 Mac_dbgo_sel)
4259
Francois Romieu219a1e92008-06-28 11:58:39 +02004260static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4261{
Francois Romieub726e492008-06-28 12:22:59 +02004262 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4263
4264 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4265
Francois Romieu2e68ae42008-06-28 12:00:55 +02004266 rtl_tx_performance_tweak(pdev,
4267 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004268}
4269
4270static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4271{
4272 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004273
françois romieuf0298f82011-01-03 15:07:42 +00004274 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004275
4276 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004277}
4278
4279static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4280{
Francois Romieub726e492008-06-28 12:22:59 +02004281 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4282
4283 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4284
Francois Romieu219a1e92008-06-28 11:58:39 +02004285 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004286
4287 rtl_disable_clock_request(pdev);
4288
4289 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004290}
4291
Francois Romieuef3386f2008-06-29 12:24:30 +02004292static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004293{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004294 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004295 { 0x01, 0, 0x0001 },
4296 { 0x02, 0x0800, 0x1000 },
4297 { 0x03, 0, 0x0042 },
4298 { 0x06, 0x0080, 0x0000 },
4299 { 0x07, 0, 0x2000 }
4300 };
4301
françois romieu650e8d52011-01-03 15:08:29 +00004302 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004303
4304 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4305
Francois Romieu219a1e92008-06-28 11:58:39 +02004306 __rtl_hw_start_8168cp(ioaddr, pdev);
4307}
4308
Francois Romieuef3386f2008-06-29 12:24:30 +02004309static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4310{
françois romieu650e8d52011-01-03 15:08:29 +00004311 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004312
4313 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4314
4315 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4316
4317 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4318}
4319
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004320static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4321{
françois romieu650e8d52011-01-03 15:08:29 +00004322 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004323
4324 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4325
4326 /* Magic. */
4327 RTL_W8(DBG_REG, 0x20);
4328
françois romieuf0298f82011-01-03 15:07:42 +00004329 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004330
4331 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4332
4333 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4334}
4335
Francois Romieu219a1e92008-06-28 11:58:39 +02004336static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4337{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004338 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004339 { 0x02, 0x0800, 0x1000 },
4340 { 0x03, 0, 0x0002 },
4341 { 0x06, 0x0080, 0x0000 }
4342 };
4343
françois romieu650e8d52011-01-03 15:08:29 +00004344 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004345
4346 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4347
4348 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4349
Francois Romieu219a1e92008-06-28 11:58:39 +02004350 __rtl_hw_start_8168cp(ioaddr, pdev);
4351}
4352
4353static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4354{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004355 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004356 { 0x01, 0, 0x0001 },
4357 { 0x03, 0x0400, 0x0220 }
4358 };
4359
françois romieu650e8d52011-01-03 15:08:29 +00004360 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004361
4362 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4363
Francois Romieu219a1e92008-06-28 11:58:39 +02004364 __rtl_hw_start_8168cp(ioaddr, pdev);
4365}
4366
Francois Romieu197ff762008-06-28 13:16:02 +02004367static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4368{
4369 rtl_hw_start_8168c_2(ioaddr, pdev);
4370}
4371
Francois Romieu6fb07052008-06-29 11:54:28 +02004372static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4373{
françois romieu650e8d52011-01-03 15:08:29 +00004374 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004375
4376 __rtl_hw_start_8168cp(ioaddr, pdev);
4377}
4378
Francois Romieu5b538df2008-07-20 16:22:45 +02004379static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4380{
françois romieu650e8d52011-01-03 15:08:29 +00004381 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004382
4383 rtl_disable_clock_request(pdev);
4384
françois romieuf0298f82011-01-03 15:07:42 +00004385 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004386
4387 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4388
4389 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4390}
4391
hayeswang4804b3b2011-03-21 01:50:29 +00004392static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4393{
4394 rtl_csi_access_enable_1(ioaddr);
4395
4396 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4397
4398 RTL_W8(MaxTxPacketSize, TxPacketMax);
4399
4400 rtl_disable_clock_request(pdev);
4401}
4402
françois romieue6de30d2011-01-03 15:08:37 +00004403static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4404{
4405 static const struct ephy_info e_info_8168d_4[] = {
4406 { 0x0b, ~0, 0x48 },
4407 { 0x19, 0x20, 0x50 },
4408 { 0x0c, ~0, 0x20 }
4409 };
4410 int i;
4411
4412 rtl_csi_access_enable_1(ioaddr);
4413
4414 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4415
4416 RTL_W8(MaxTxPacketSize, TxPacketMax);
4417
4418 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4419 const struct ephy_info *e = e_info_8168d_4 + i;
4420 u16 w;
4421
4422 w = rtl_ephy_read(ioaddr, e->offset);
4423 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4424 }
4425
4426 rtl_enable_clock_request(pdev);
4427}
4428
Hayes Wang70090422011-07-06 15:58:06 +08004429static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
hayeswang01dc7fe2011-03-21 01:50:28 +00004430{
Hayes Wang70090422011-07-06 15:58:06 +08004431 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004432 { 0x00, 0x0200, 0x0100 },
4433 { 0x00, 0x0000, 0x0004 },
4434 { 0x06, 0x0002, 0x0001 },
4435 { 0x06, 0x0000, 0x0030 },
4436 { 0x07, 0x0000, 0x2000 },
4437 { 0x00, 0x0000, 0x0020 },
4438 { 0x03, 0x5800, 0x2000 },
4439 { 0x03, 0x0000, 0x0001 },
4440 { 0x01, 0x0800, 0x1000 },
4441 { 0x07, 0x0000, 0x4000 },
4442 { 0x1e, 0x0000, 0x2000 },
4443 { 0x19, 0xffff, 0xfe6c },
4444 { 0x0a, 0x0000, 0x0040 }
4445 };
4446
4447 rtl_csi_access_enable_2(ioaddr);
4448
Hayes Wang70090422011-07-06 15:58:06 +08004449 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004450
4451 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4452
4453 RTL_W8(MaxTxPacketSize, TxPacketMax);
4454
4455 rtl_disable_clock_request(pdev);
4456
4457 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004458 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4459 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004460
Francois Romieucecb5fd2011-04-01 10:21:07 +02004461 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004462}
4463
Hayes Wang70090422011-07-06 15:58:06 +08004464static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4465{
4466 static const struct ephy_info e_info_8168e_2[] = {
4467 { 0x09, 0x0000, 0x0080 },
4468 { 0x19, 0x0000, 0x0224 }
4469 };
4470
4471 rtl_csi_access_enable_1(ioaddr);
4472
4473 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4474
4475 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4476
4477 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4478 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4479 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4480 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4481 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4482 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4483 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4484 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4485 ERIAR_EXGMAC);
4486
Hayes Wang3090bd92011-09-06 16:55:15 +08004487 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004488
4489 rtl_disable_clock_request(pdev);
4490
4491 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4492 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4493
4494 /* Adjust EEE LED frequency */
4495 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4496
4497 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4498 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4499 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4500}
4501
Hayes Wangc2218922011-09-06 16:55:18 +08004502static void rtl_hw_start_8168f_1(void __iomem *ioaddr, struct pci_dev *pdev)
4503{
4504 static const struct ephy_info e_info_8168f_1[] = {
4505 { 0x06, 0x00c0, 0x0020 },
4506 { 0x08, 0x0001, 0x0002 },
4507 { 0x09, 0x0000, 0x0080 },
4508 { 0x19, 0x0000, 0x0224 }
4509 };
4510
4511 rtl_csi_access_enable_1(ioaddr);
4512
4513 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4514
4515 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4516
4517 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4518 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4519 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4520 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4521 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4522 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4523 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4524 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4525 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4526 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
4527 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4528 ERIAR_EXGMAC);
4529
4530 RTL_W8(MaxTxPacketSize, EarlySize);
4531
4532 rtl_disable_clock_request(pdev);
4533
4534 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4535 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4536
4537 /* Adjust EEE LED frequency */
4538 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4539
4540 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4541 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4542 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4543}
4544
Francois Romieu07ce4062007-02-23 23:36:39 +01004545static void rtl_hw_start_8168(struct net_device *dev)
4546{
Francois Romieu2dd99532007-06-11 23:22:52 +02004547 struct rtl8169_private *tp = netdev_priv(dev);
4548 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004549 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004550
4551 RTL_W8(Cfg9346, Cfg9346_Unlock);
4552
françois romieuf0298f82011-01-03 15:07:42 +00004553 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004554
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004555 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004556
Francois Romieu0e485152007-02-20 00:00:26 +01004557 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004558
4559 RTL_W16(CPlusCmd, tp->cp_cmd);
4560
Francois Romieu0e485152007-02-20 00:00:26 +01004561 RTL_W16(IntrMitigate, 0x5151);
4562
4563 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00004564 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01004565 tp->event_slow |= RxFIFOOver | PCSTimeout;
4566 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01004567 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004568
4569 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4570
Francois Romieub8363902008-06-01 12:31:57 +02004571 rtl_set_rx_mode(dev);
4572
4573 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4574 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004575
4576 RTL_R8(IntrMask);
4577
Francois Romieu219a1e92008-06-28 11:58:39 +02004578 switch (tp->mac_version) {
4579 case RTL_GIGA_MAC_VER_11:
4580 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004581 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004582
4583 case RTL_GIGA_MAC_VER_12:
4584 case RTL_GIGA_MAC_VER_17:
4585 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004586 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004587
4588 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004589 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004590 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004591
4592 case RTL_GIGA_MAC_VER_19:
4593 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004594 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004595
4596 case RTL_GIGA_MAC_VER_20:
4597 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004598 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004599
Francois Romieu197ff762008-06-28 13:16:02 +02004600 case RTL_GIGA_MAC_VER_21:
4601 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004602 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004603
Francois Romieu6fb07052008-06-29 11:54:28 +02004604 case RTL_GIGA_MAC_VER_22:
4605 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004606 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004607
Francois Romieuef3386f2008-06-29 12:24:30 +02004608 case RTL_GIGA_MAC_VER_23:
4609 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004610 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004611
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004612 case RTL_GIGA_MAC_VER_24:
4613 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004614 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004615
Francois Romieu5b538df2008-07-20 16:22:45 +02004616 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004617 case RTL_GIGA_MAC_VER_26:
4618 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004619 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004620 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004621
françois romieue6de30d2011-01-03 15:08:37 +00004622 case RTL_GIGA_MAC_VER_28:
4623 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004624 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004625
hayeswang4804b3b2011-03-21 01:50:29 +00004626 case RTL_GIGA_MAC_VER_31:
4627 rtl_hw_start_8168dp(ioaddr, pdev);
4628 break;
4629
hayeswang01dc7fe2011-03-21 01:50:28 +00004630 case RTL_GIGA_MAC_VER_32:
4631 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004632 rtl_hw_start_8168e_1(ioaddr, pdev);
4633 break;
4634 case RTL_GIGA_MAC_VER_34:
4635 rtl_hw_start_8168e_2(ioaddr, pdev);
hayeswang01dc7fe2011-03-21 01:50:28 +00004636 break;
françois romieue6de30d2011-01-03 15:08:37 +00004637
Hayes Wangc2218922011-09-06 16:55:18 +08004638 case RTL_GIGA_MAC_VER_35:
4639 case RTL_GIGA_MAC_VER_36:
4640 rtl_hw_start_8168f_1(ioaddr, pdev);
4641 break;
4642
Francois Romieu219a1e92008-06-28 11:58:39 +02004643 default:
4644 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4645 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004646 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004647 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004648
Francois Romieu0e485152007-02-20 00:00:26 +01004649 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4650
Francois Romieub8363902008-06-01 12:31:57 +02004651 RTL_W8(Cfg9346, Cfg9346_Lock);
4652
Francois Romieu2dd99532007-06-11 23:22:52 +02004653 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004654}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655
Francois Romieu2857ffb2008-08-02 21:08:49 +02004656#define R810X_CPCMD_QUIRK_MASK (\
4657 EnableBist | \
4658 Mac_dbgo_oe | \
4659 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004660 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004661 Force_txflow_en | \
4662 Cxpl_dbg_sel | \
4663 ASF | \
4664 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004665 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004666
4667static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4668{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004669 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004670 { 0x01, 0, 0x6e65 },
4671 { 0x02, 0, 0x091f },
4672 { 0x03, 0, 0xc2f9 },
4673 { 0x06, 0, 0xafb5 },
4674 { 0x07, 0, 0x0e00 },
4675 { 0x19, 0, 0xec80 },
4676 { 0x01, 0, 0x2e65 },
4677 { 0x01, 0, 0x6e65 }
4678 };
4679 u8 cfg1;
4680
françois romieu650e8d52011-01-03 15:08:29 +00004681 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004682
4683 RTL_W8(DBG_REG, FIX_NAK_1);
4684
4685 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4686
4687 RTL_W8(Config1,
4688 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4689 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4690
4691 cfg1 = RTL_R8(Config1);
4692 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4693 RTL_W8(Config1, cfg1 & ~LEDS0);
4694
Francois Romieu2857ffb2008-08-02 21:08:49 +02004695 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4696}
4697
4698static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4699{
françois romieu650e8d52011-01-03 15:08:29 +00004700 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004701
4702 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4703
4704 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4705 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004706}
4707
4708static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4709{
4710 rtl_hw_start_8102e_2(ioaddr, pdev);
4711
4712 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4713}
4714
Hayes Wang5a5e4442011-02-22 17:26:21 +08004715static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4716{
4717 static const struct ephy_info e_info_8105e_1[] = {
4718 { 0x07, 0, 0x4000 },
4719 { 0x19, 0, 0x0200 },
4720 { 0x19, 0, 0x0020 },
4721 { 0x1e, 0, 0x2000 },
4722 { 0x03, 0, 0x0001 },
4723 { 0x19, 0, 0x0100 },
4724 { 0x19, 0, 0x0004 },
4725 { 0x0a, 0, 0x0020 }
4726 };
4727
Francois Romieucecb5fd2011-04-01 10:21:07 +02004728 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004729 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4730
Francois Romieucecb5fd2011-04-01 10:21:07 +02004731 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004732 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4733
4734 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08004735 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004736
4737 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4738}
4739
4740static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4741{
4742 rtl_hw_start_8105e_1(ioaddr, pdev);
4743 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4744}
4745
Francois Romieu07ce4062007-02-23 23:36:39 +01004746static void rtl_hw_start_8101(struct net_device *dev)
4747{
Francois Romieucdf1a602007-06-11 23:29:50 +02004748 struct rtl8169_private *tp = netdev_priv(dev);
4749 void __iomem *ioaddr = tp->mmio_addr;
4750 struct pci_dev *pdev = tp->pci_dev;
4751
Francois Romieuda78dbf2012-01-26 14:18:23 +01004752 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
4753 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00004754
Francois Romieucecb5fd2011-04-01 10:21:07 +02004755 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4756 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00004757 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02004758
4759 if (cap) {
4760 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4761 PCI_EXP_DEVCTL_NOSNOOP_EN);
4762 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004763 }
4764
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004765 RTL_W8(Cfg9346, Cfg9346_Unlock);
4766
Francois Romieu2857ffb2008-08-02 21:08:49 +02004767 switch (tp->mac_version) {
4768 case RTL_GIGA_MAC_VER_07:
4769 rtl_hw_start_8102e_1(ioaddr, pdev);
4770 break;
4771
4772 case RTL_GIGA_MAC_VER_08:
4773 rtl_hw_start_8102e_3(ioaddr, pdev);
4774 break;
4775
4776 case RTL_GIGA_MAC_VER_09:
4777 rtl_hw_start_8102e_2(ioaddr, pdev);
4778 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004779
4780 case RTL_GIGA_MAC_VER_29:
4781 rtl_hw_start_8105e_1(ioaddr, pdev);
4782 break;
4783 case RTL_GIGA_MAC_VER_30:
4784 rtl_hw_start_8105e_2(ioaddr, pdev);
4785 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004786 }
4787
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004788 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004789
françois romieuf0298f82011-01-03 15:07:42 +00004790 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004791
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004792 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004793
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004794 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004795 RTL_W16(CPlusCmd, tp->cp_cmd);
4796
4797 RTL_W16(IntrMitigate, 0x0000);
4798
4799 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4800
4801 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4802 rtl_set_rx_tx_config_registers(tp);
4803
Francois Romieucdf1a602007-06-11 23:29:50 +02004804 RTL_R8(IntrMask);
4805
Francois Romieucdf1a602007-06-11 23:29:50 +02004806 rtl_set_rx_mode(dev);
4807
4808 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809}
4810
4811static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4812{
Francois Romieud58d46b2011-05-03 16:38:29 +02004813 struct rtl8169_private *tp = netdev_priv(dev);
4814
4815 if (new_mtu < ETH_ZLEN ||
4816 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 return -EINVAL;
4818
Francois Romieud58d46b2011-05-03 16:38:29 +02004819 if (new_mtu > ETH_DATA_LEN)
4820 rtl_hw_jumbo_enable(tp);
4821 else
4822 rtl_hw_jumbo_disable(tp);
4823
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004825 netdev_update_features(dev);
4826
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004827 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828}
4829
4830static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4831{
Al Viro95e09182007-12-22 18:55:39 +00004832 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4834}
4835
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004836static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4837 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004839 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004840 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004841
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004842 kfree(*data_buff);
4843 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 rtl8169_make_unusable_by_asic(desc);
4845}
4846
4847static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4848{
4849 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4850
4851 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4852}
4853
4854static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4855 u32 rx_buf_sz)
4856{
4857 desc->addr = cpu_to_le64(mapping);
4858 wmb();
4859 rtl8169_mark_to_asic(desc, rx_buf_sz);
4860}
4861
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004862static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004864 return (void *)ALIGN((long)data, 16);
4865}
4866
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004867static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4868 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004869{
4870 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004872 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004873 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004874 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004876 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4877 if (!data)
4878 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004879
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004880 if (rtl8169_align(data) != data) {
4881 kfree(data);
4882 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4883 if (!data)
4884 return NULL;
4885 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004886
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004887 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004888 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004889 if (unlikely(dma_mapping_error(d, mapping))) {
4890 if (net_ratelimit())
4891 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004892 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894
4895 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004896 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004897
4898err_out:
4899 kfree(data);
4900 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901}
4902
4903static void rtl8169_rx_clear(struct rtl8169_private *tp)
4904{
Francois Romieu07d3f512007-02-21 22:40:46 +01004905 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906
4907 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004908 if (tp->Rx_databuff[i]) {
4909 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 tp->RxDescArray + i);
4911 }
4912 }
4913}
4914
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004915static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004917 desc->opts1 |= cpu_to_le32(RingEnd);
4918}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004919
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004920static int rtl8169_rx_fill(struct rtl8169_private *tp)
4921{
4922 unsigned int i;
4923
4924 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004925 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004926
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004927 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004929
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004930 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004931 if (!data) {
4932 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004933 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004934 }
4935 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004938 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4939 return 0;
4940
4941err_out:
4942 rtl8169_rx_clear(tp);
4943 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944}
4945
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946static int rtl8169_init_ring(struct net_device *dev)
4947{
4948 struct rtl8169_private *tp = netdev_priv(dev);
4949
4950 rtl8169_init_ring_indexes(tp);
4951
4952 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004953 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004955 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956}
4957
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004958static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 struct TxDesc *desc)
4960{
4961 unsigned int len = tx_skb->len;
4962
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004963 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4964
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 desc->opts1 = 0x00;
4966 desc->opts2 = 0x00;
4967 desc->addr = 0x00;
4968 tx_skb->len = 0;
4969}
4970
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004971static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4972 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973{
4974 unsigned int i;
4975
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004976 for (i = 0; i < n; i++) {
4977 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 struct ring_info *tx_skb = tp->tx_skb + entry;
4979 unsigned int len = tx_skb->len;
4980
4981 if (len) {
4982 struct sk_buff *skb = tx_skb->skb;
4983
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004984 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 tp->TxDescArray + entry);
4986 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004987 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 dev_kfree_skb(skb);
4989 tx_skb->skb = NULL;
4990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 }
4992 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004993}
4994
4995static void rtl8169_tx_clear(struct rtl8169_private *tp)
4996{
4997 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 tp->cur_tx = tp->dirty_tx = 0;
Igor Maravic036dafa2012-03-05 00:01:25 +01004999 netdev_reset_queue(tp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000}
5001
Francois Romieu4422bcd2012-01-26 11:23:32 +01005002static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003{
David Howellsc4028952006-11-22 14:57:56 +00005004 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005005 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006
Francois Romieuda78dbf2012-01-26 14:18:23 +01005007 napi_disable(&tp->napi);
5008 netif_stop_queue(dev);
5009 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010
françois romieuc7c2c392011-12-04 20:30:52 +00005011 rtl8169_hw_reset(tp);
5012
Francois Romieu56de4142011-03-15 17:29:31 +01005013 for (i = 0; i < NUM_RX_DESC; i++)
5014 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5015
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005017 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018
Francois Romieuda78dbf2012-01-26 14:18:23 +01005019 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01005020 rtl_hw_start(dev);
5021 netif_wake_queue(dev);
5022 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023}
5024
5025static void rtl8169_tx_timeout(struct net_device *dev)
5026{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005027 struct rtl8169_private *tp = netdev_priv(dev);
5028
5029 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030}
5031
5032static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005033 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034{
5035 struct skb_shared_info *info = skb_shinfo(skb);
5036 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005037 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005038 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039
5040 entry = tp->cur_tx;
5041 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005042 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 dma_addr_t mapping;
5044 u32 status, len;
5045 void *addr;
5046
5047 entry = (entry + 1) % NUM_TX_DESC;
5048
5049 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005050 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005051 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005052 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005053 if (unlikely(dma_mapping_error(d, mapping))) {
5054 if (net_ratelimit())
5055 netif_err(tp, drv, tp->dev,
5056 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005057 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059
Francois Romieucecb5fd2011-04-01 10:21:07 +02005060 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005061 status = opts[0] | len |
5062 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063
5064 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005065 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 txd->addr = cpu_to_le64(mapping);
5067
5068 tp->tx_skb[entry].len = len;
5069 }
5070
5071 if (cur_frag) {
5072 tp->tx_skb[entry].skb = skb;
5073 txd->opts1 |= cpu_to_le32(LastFrag);
5074 }
5075
5076 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005077
5078err_out:
5079 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5080 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081}
5082
Francois Romieu2b7b4312011-04-18 22:53:24 -07005083static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5084 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005086 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005087 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005088 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089
Francois Romieu2b7b4312011-04-18 22:53:24 -07005090 if (mss) {
5091 opts[0] |= TD_LSO;
5092 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5093 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005094 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
5096 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005097 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005099 opts[offset] |= info->checksum.udp;
5100 else
5101 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103}
5104
Stephen Hemminger613573252009-08-31 19:50:58 +00005105static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5106 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107{
5108 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005109 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 struct TxDesc *txd = tp->TxDescArray + entry;
5111 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005112 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 dma_addr_t mapping;
5114 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005115 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005116 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005117
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005119 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005120 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 }
5122
5123 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005124 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005126 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005127 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005128 if (unlikely(dma_mapping_error(d, mapping))) {
5129 if (net_ratelimit())
5130 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005131 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133
5134 tp->tx_skb[entry].len = len;
5135 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
Francois Romieu2b7b4312011-04-18 22:53:24 -07005137 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5138 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005139
Francois Romieu2b7b4312011-04-18 22:53:24 -07005140 rtl8169_tso_csum(tp, skb, opts);
5141
5142 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005143 if (frags < 0)
5144 goto err_dma_1;
5145 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005146 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005147 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005148 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005149 tp->tx_skb[entry].skb = skb;
5150 }
5151
Francois Romieu2b7b4312011-04-18 22:53:24 -07005152 txd->opts2 = cpu_to_le32(opts[1]);
5153
Igor Maravic036dafa2012-03-05 00:01:25 +01005154 netdev_sent_queue(dev, skb->len);
5155
Richard Cochran5047fb52012-03-10 07:29:42 +00005156 skb_tx_timestamp(skb);
5157
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 wmb();
5159
Francois Romieucecb5fd2011-04-01 10:21:07 +02005160 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005161 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 txd->opts1 = cpu_to_le32(status);
5163
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 tp->cur_tx += frags + 1;
5165
David Dillow4c020a92010-03-03 16:33:10 +00005166 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167
Francois Romieucecb5fd2011-04-01 10:21:07 +02005168 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169
Francois Romieuda78dbf2012-01-26 14:18:23 +01005170 mmiowb();
5171
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01005173 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5174 * not miss a ring update when it notices a stopped queue.
5175 */
5176 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01005178 /* Sync with rtl_tx:
5179 * - publish queue status and cur_tx ring index (write barrier)
5180 * - refresh dirty_tx ring index (read barrier).
5181 * May the current thread have a pessimistic view of the ring
5182 * status and forget to wake up queue, a racing rtl_tx thread
5183 * can't.
5184 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005185 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
5187 netif_wake_queue(dev);
5188 }
5189
Stephen Hemminger613573252009-08-31 19:50:58 +00005190 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005192err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005193 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005194err_dma_0:
5195 dev_kfree_skb(skb);
5196 dev->stats.tx_dropped++;
5197 return NETDEV_TX_OK;
5198
5199err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005201 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005202 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203}
5204
5205static void rtl8169_pcierr_interrupt(struct net_device *dev)
5206{
5207 struct rtl8169_private *tp = netdev_priv(dev);
5208 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 u16 pci_status, pci_cmd;
5210
5211 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5212 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5213
Joe Perchesbf82c182010-02-09 11:49:50 +00005214 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5215 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216
5217 /*
5218 * The recovery sequence below admits a very elaborated explanation:
5219 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005220 * - I did not see what else could be done;
5221 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 *
5223 * Feel free to adjust to your needs.
5224 */
Francois Romieua27993f2006-12-18 00:04:19 +01005225 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005226 pci_cmd &= ~PCI_COMMAND_PARITY;
5227 else
5228 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5229
5230 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231
5232 pci_write_config_word(pdev, PCI_STATUS,
5233 pci_status & (PCI_STATUS_DETECTED_PARITY |
5234 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5235 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5236
5237 /* The infamous DAC f*ckup only happens at boot time */
5238 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005239 void __iomem *ioaddr = tp->mmio_addr;
5240
Joe Perchesbf82c182010-02-09 11:49:50 +00005241 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 tp->cp_cmd &= ~PCIDAC;
5243 RTL_W16(CPlusCmd, tp->cp_cmd);
5244 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 }
5246
françois romieue6de30d2011-01-03 15:08:37 +00005247 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005248
Francois Romieu98ddf982012-01-31 10:47:34 +01005249 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250}
5251
Igor Maravic036dafa2012-03-05 00:01:25 +01005252struct rtl_txc {
5253 int packets;
5254 int bytes;
5255};
5256
Francois Romieuda78dbf2012-01-26 14:18:23 +01005257static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258{
Igor Maravic036dafa2012-03-05 00:01:25 +01005259 struct rtl8169_stats *tx_stats = &tp->tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 unsigned int dirty_tx, tx_left;
Igor Maravic036dafa2012-03-05 00:01:25 +01005261 struct rtl_txc txc = { 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 dirty_tx = tp->dirty_tx;
5264 smp_rmb();
5265 tx_left = tp->cur_tx - dirty_tx;
5266
5267 while (tx_left > 0) {
5268 unsigned int entry = dirty_tx % NUM_TX_DESC;
5269 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 u32 status;
5271
5272 rmb();
5273 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5274 if (status & DescOwn)
5275 break;
5276
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005277 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5278 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 if (status & LastFrag) {
Igor Maravic036dafa2012-03-05 00:01:25 +01005280 struct sk_buff *skb = tx_skb->skb;
5281
5282 txc.packets++;
5283 txc.bytes += skb->len;
5284 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 tx_skb->skb = NULL;
5286 }
5287 dirty_tx++;
5288 tx_left--;
5289 }
5290
Igor Maravic036dafa2012-03-05 00:01:25 +01005291 u64_stats_update_begin(&tx_stats->syncp);
5292 tx_stats->packets += txc.packets;
5293 tx_stats->bytes += txc.bytes;
5294 u64_stats_update_end(&tx_stats->syncp);
5295
5296 netdev_completed_queue(dev, txc.packets, txc.bytes);
5297
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 if (tp->dirty_tx != dirty_tx) {
5299 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005300 /* Sync with rtl8169_start_xmit:
5301 * - publish dirty_tx ring index (write barrier)
5302 * - refresh cur_tx ring index and queue status (read barrier)
5303 * May the current thread miss the stopped queue condition,
5304 * a racing xmit thread can only have a right view of the
5305 * ring status.
5306 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005307 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 if (netif_queue_stopped(dev) &&
5309 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5310 netif_wake_queue(dev);
5311 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005312 /*
5313 * 8168 hack: TxPoll requests are lost when the Tx packets are
5314 * too close. Let's kick an extra TxPoll request when a burst
5315 * of start_xmit activity is detected (if it is not detected,
5316 * it is slow enough). -- FR
5317 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005318 if (tp->cur_tx != dirty_tx) {
5319 void __iomem *ioaddr = tp->mmio_addr;
5320
Francois Romieud78ae2d2007-08-26 20:08:19 +02005321 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 }
5324}
5325
Francois Romieu126fa4b2005-05-12 20:09:17 -04005326static inline int rtl8169_fragmented_frame(u32 status)
5327{
5328 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5329}
5330
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005331static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 u32 status = opts1 & RxProtoMask;
5334
5335 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005336 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 skb->ip_summed = CHECKSUM_UNNECESSARY;
5338 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005339 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340}
5341
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005342static struct sk_buff *rtl8169_try_rx_copy(void *data,
5343 struct rtl8169_private *tp,
5344 int pkt_size,
5345 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005347 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005348 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005350 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005351 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005352 prefetch(data);
5353 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5354 if (skb)
5355 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005356 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5357
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005358 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359}
5360
Francois Romieuda78dbf2012-01-26 14:18:23 +01005361static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362{
5363 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005364 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 cur_rx = tp->cur_rx;
5367 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005368 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005370 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005372 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 u32 status;
5374
5375 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005376 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377
5378 if (status & DescOwn)
5379 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005380 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005381 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5382 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005383 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005385 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005387 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005388 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005389 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005390 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005391 }
Ben Greear6bbe0212012-02-10 15:04:33 +00005392 if ((status & (RxRUNT | RxCRC)) &&
5393 !(status & (RxRWT | RxFOVF)) &&
5394 (dev->features & NETIF_F_RXALL))
5395 goto process_pkt;
5396
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005397 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005399 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00005400 dma_addr_t addr;
5401 int pkt_size;
5402
5403process_pkt:
5404 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00005405 if (likely(!(dev->features & NETIF_F_RXFCS)))
5406 pkt_size = (status & 0x00003fff) - 4;
5407 else
5408 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409
Francois Romieu126fa4b2005-05-12 20:09:17 -04005410 /*
5411 * The driver does not support incoming fragmented
5412 * frames. They are seen as a symptom of over-mtu
5413 * sized frames.
5414 */
5415 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005416 dev->stats.rx_dropped++;
5417 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005418 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005419 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005420 }
5421
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005422 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5423 tp, pkt_size, addr);
5424 rtl8169_mark_to_asic(desc, rx_buf_sz);
5425 if (!skb) {
5426 dev->stats.rx_dropped++;
5427 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 }
5429
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005430 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 skb_put(skb, pkt_size);
5432 skb->protocol = eth_type_trans(skb, dev);
5433
Francois Romieu7a8fc772011-03-01 17:18:33 +01005434 rtl8169_rx_vlan_tag(desc, skb);
5435
Francois Romieu56de4142011-03-15 17:29:31 +01005436 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437
Junchang Wang8027aa22012-03-04 23:30:32 +01005438 u64_stats_update_begin(&tp->rx_stats.syncp);
5439 tp->rx_stats.packets++;
5440 tp->rx_stats.bytes += pkt_size;
5441 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005443
5444 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005445 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005446 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5447 desc->opts2 = 0;
5448 cur_rx++;
5449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 }
5451
5452 count = cur_rx - tp->cur_rx;
5453 tp->cur_rx = cur_rx;
5454
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005455 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456
5457 return count;
5458}
5459
Francois Romieu07d3f512007-02-21 22:40:46 +01005460static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461{
Francois Romieu07d3f512007-02-21 22:40:46 +01005462 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005465 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005467 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005468 if (status && status != 0xffff) {
5469 status &= RTL_EVENT_NAPI | tp->event_slow;
5470 if (status) {
5471 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00005472
Francois Romieuda78dbf2012-01-26 14:18:23 +01005473 rtl_irq_disable(tp);
5474 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 return IRQ_RETVAL(handled);
5478}
5479
Francois Romieuda78dbf2012-01-26 14:18:23 +01005480/*
5481 * Workqueue context.
5482 */
5483static void rtl_slow_event_work(struct rtl8169_private *tp)
5484{
5485 struct net_device *dev = tp->dev;
5486 u16 status;
5487
5488 status = rtl_get_events(tp) & tp->event_slow;
5489 rtl_ack_events(tp, status);
5490
5491 if (unlikely(status & RxFIFOOver)) {
5492 switch (tp->mac_version) {
5493 /* Work around for rx fifo overflow */
5494 case RTL_GIGA_MAC_VER_11:
5495 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01005496 /* XXX - Hack alert. See rtl_task(). */
5497 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005498 default:
5499 break;
5500 }
5501 }
5502
5503 if (unlikely(status & SYSErr))
5504 rtl8169_pcierr_interrupt(dev);
5505
5506 if (status & LinkChg)
5507 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
5508
5509 napi_disable(&tp->napi);
5510 rtl_irq_disable(tp);
5511
5512 napi_enable(&tp->napi);
5513 napi_schedule(&tp->napi);
5514}
5515
Francois Romieu4422bcd2012-01-26 11:23:32 +01005516static void rtl_task(struct work_struct *work)
5517{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005518 static const struct {
5519 int bitnr;
5520 void (*action)(struct rtl8169_private *);
5521 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01005522 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005523 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
5524 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
5525 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
5526 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01005527 struct rtl8169_private *tp =
5528 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005529 struct net_device *dev = tp->dev;
5530 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01005531
Francois Romieuda78dbf2012-01-26 14:18:23 +01005532 rtl_lock_work(tp);
5533
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005534 if (!netif_running(dev) ||
5535 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01005536 goto out_unlock;
5537
5538 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
5539 bool pending;
5540
Francois Romieuda78dbf2012-01-26 14:18:23 +01005541 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005542 if (pending)
5543 rtl_work[i].action(tp);
5544 }
5545
5546out_unlock:
5547 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01005548}
5549
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005550static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005552 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5553 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005554 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
5555 int work_done= 0;
5556 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557
Francois Romieuda78dbf2012-01-26 14:18:23 +01005558 status = rtl_get_events(tp);
5559 rtl_ack_events(tp, status & ~tp->event_slow);
5560
5561 if (status & RTL_EVENT_NAPI_RX)
5562 work_done = rtl_rx(dev, tp, (u32) budget);
5563
5564 if (status & RTL_EVENT_NAPI_TX)
5565 rtl_tx(dev, tp);
5566
5567 if (status & tp->event_slow) {
5568 enable_mask &= ~tp->event_slow;
5569
5570 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
5571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005573 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005574 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005575
Francois Romieuda78dbf2012-01-26 14:18:23 +01005576 rtl_irq_enable(tp, enable_mask);
5577 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 }
5579
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005580 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582
Francois Romieu523a6092008-09-10 22:28:56 +02005583static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5584{
5585 struct rtl8169_private *tp = netdev_priv(dev);
5586
5587 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5588 return;
5589
5590 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5591 RTL_W32(RxMissed, 0);
5592}
5593
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594static void rtl8169_down(struct net_device *dev)
5595{
5596 struct rtl8169_private *tp = netdev_priv(dev);
5597 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598
Francois Romieu4876cc12011-03-11 21:07:11 +01005599 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005601 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005602 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603
Hayes Wang92fc43b2011-07-06 15:58:03 +08005604 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005605 /*
5606 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01005607 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
5608 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005609 */
Francois Romieu523a6092008-09-10 22:28:56 +02005610 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005613 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 rtl8169_tx_clear(tp);
5616
5617 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005618
5619 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620}
5621
5622static int rtl8169_close(struct net_device *dev)
5623{
5624 struct rtl8169_private *tp = netdev_priv(dev);
5625 struct pci_dev *pdev = tp->pci_dev;
5626
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005627 pm_runtime_get_sync(&pdev->dev);
5628
Francois Romieucecb5fd2011-04-01 10:21:07 +02005629 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005630 rtl8169_update_counters(dev);
5631
Francois Romieuda78dbf2012-01-26 14:18:23 +01005632 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005633 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005634
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005636 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637
Francois Romieu92a7c4e2012-03-10 10:42:12 +01005638 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005640 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5641 tp->RxPhyAddr);
5642 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5643 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 tp->TxDescArray = NULL;
5645 tp->RxDescArray = NULL;
5646
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005647 pm_runtime_put_sync(&pdev->dev);
5648
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 return 0;
5650}
5651
Francois Romieudc1c00c2012-03-08 10:06:18 +01005652#ifdef CONFIG_NET_POLL_CONTROLLER
5653static void rtl8169_netpoll(struct net_device *dev)
5654{
5655 struct rtl8169_private *tp = netdev_priv(dev);
5656
5657 rtl8169_interrupt(tp->pci_dev->irq, dev);
5658}
5659#endif
5660
Francois Romieudf43ac72012-03-08 09:48:40 +01005661static int rtl_open(struct net_device *dev)
5662{
5663 struct rtl8169_private *tp = netdev_priv(dev);
5664 void __iomem *ioaddr = tp->mmio_addr;
5665 struct pci_dev *pdev = tp->pci_dev;
5666 int retval = -ENOMEM;
5667
5668 pm_runtime_get_sync(&pdev->dev);
5669
5670 /*
5671 * Rx and Tx desscriptors needs 256 bytes alignment.
5672 * dma_alloc_coherent provides more.
5673 */
5674 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
5675 &tp->TxPhyAddr, GFP_KERNEL);
5676 if (!tp->TxDescArray)
5677 goto err_pm_runtime_put;
5678
5679 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
5680 &tp->RxPhyAddr, GFP_KERNEL);
5681 if (!tp->RxDescArray)
5682 goto err_free_tx_0;
5683
5684 retval = rtl8169_init_ring(dev);
5685 if (retval < 0)
5686 goto err_free_rx_1;
5687
5688 INIT_WORK(&tp->wk.work, rtl_task);
5689
5690 smp_mb();
5691
5692 rtl_request_firmware(tp);
5693
Francois Romieu92a7c4e2012-03-10 10:42:12 +01005694 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01005695 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
5696 dev->name, dev);
5697 if (retval < 0)
5698 goto err_release_fw_2;
5699
5700 rtl_lock_work(tp);
5701
5702 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
5703
5704 napi_enable(&tp->napi);
5705
5706 rtl8169_init_phy(dev, tp);
5707
5708 __rtl8169_set_features(dev, dev->features);
5709
5710 rtl_pll_power_up(tp);
5711
5712 rtl_hw_start(dev);
5713
5714 netif_start_queue(dev);
5715
5716 rtl_unlock_work(tp);
5717
5718 tp->saved_wolopts = 0;
5719 pm_runtime_put_noidle(&pdev->dev);
5720
5721 rtl8169_check_link_status(dev, tp, ioaddr);
5722out:
5723 return retval;
5724
5725err_release_fw_2:
5726 rtl_release_firmware(tp);
5727 rtl8169_rx_clear(tp);
5728err_free_rx_1:
5729 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5730 tp->RxPhyAddr);
5731 tp->RxDescArray = NULL;
5732err_free_tx_0:
5733 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5734 tp->TxPhyAddr);
5735 tp->TxDescArray = NULL;
5736err_pm_runtime_put:
5737 pm_runtime_put_noidle(&pdev->dev);
5738 goto out;
5739}
5740
Junchang Wang8027aa22012-03-04 23:30:32 +01005741static struct rtnl_link_stats64 *
5742rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743{
5744 struct rtl8169_private *tp = netdev_priv(dev);
5745 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01005746 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747
Francois Romieuda78dbf2012-01-26 14:18:23 +01005748 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02005749 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02005750
Junchang Wang8027aa22012-03-04 23:30:32 +01005751 do {
5752 start = u64_stats_fetch_begin_bh(&tp->rx_stats.syncp);
5753 stats->rx_packets = tp->rx_stats.packets;
5754 stats->rx_bytes = tp->rx_stats.bytes;
5755 } while (u64_stats_fetch_retry_bh(&tp->rx_stats.syncp, start));
5756
5757
5758 do {
5759 start = u64_stats_fetch_begin_bh(&tp->tx_stats.syncp);
5760 stats->tx_packets = tp->tx_stats.packets;
5761 stats->tx_bytes = tp->tx_stats.bytes;
5762 } while (u64_stats_fetch_retry_bh(&tp->tx_stats.syncp, start));
5763
5764 stats->rx_dropped = dev->stats.rx_dropped;
5765 stats->tx_dropped = dev->stats.tx_dropped;
5766 stats->rx_length_errors = dev->stats.rx_length_errors;
5767 stats->rx_errors = dev->stats.rx_errors;
5768 stats->rx_crc_errors = dev->stats.rx_crc_errors;
5769 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
5770 stats->rx_missed_errors = dev->stats.rx_missed_errors;
5771
5772 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773}
5774
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005775static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005776{
françois romieu065c27c2011-01-03 15:08:12 +00005777 struct rtl8169_private *tp = netdev_priv(dev);
5778
Francois Romieu5d06a992006-02-23 00:47:58 +01005779 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005780 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005781
5782 netif_device_detach(dev);
5783 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005784
5785 rtl_lock_work(tp);
5786 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005787 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005788 rtl_unlock_work(tp);
5789
5790 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005791}
Francois Romieu5d06a992006-02-23 00:47:58 +01005792
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005793#ifdef CONFIG_PM
5794
5795static int rtl8169_suspend(struct device *device)
5796{
5797 struct pci_dev *pdev = to_pci_dev(device);
5798 struct net_device *dev = pci_get_drvdata(pdev);
5799
5800 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005801
Francois Romieu5d06a992006-02-23 00:47:58 +01005802 return 0;
5803}
5804
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005805static void __rtl8169_resume(struct net_device *dev)
5806{
françois romieu065c27c2011-01-03 15:08:12 +00005807 struct rtl8169_private *tp = netdev_priv(dev);
5808
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005809 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005810
5811 rtl_pll_power_up(tp);
5812
Artem Savkovcff4c162012-04-03 10:29:11 +00005813 rtl_lock_work(tp);
5814 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005815 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00005816 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005817
Francois Romieu98ddf982012-01-31 10:47:34 +01005818 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005819}
5820
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005821static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005822{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005823 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005824 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005825 struct rtl8169_private *tp = netdev_priv(dev);
5826
5827 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005828
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005829 if (netif_running(dev))
5830 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005831
Francois Romieu5d06a992006-02-23 00:47:58 +01005832 return 0;
5833}
5834
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005835static int rtl8169_runtime_suspend(struct device *device)
5836{
5837 struct pci_dev *pdev = to_pci_dev(device);
5838 struct net_device *dev = pci_get_drvdata(pdev);
5839 struct rtl8169_private *tp = netdev_priv(dev);
5840
5841 if (!tp->TxDescArray)
5842 return 0;
5843
Francois Romieuda78dbf2012-01-26 14:18:23 +01005844 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005845 tp->saved_wolopts = __rtl8169_get_wol(tp);
5846 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005847 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005848
5849 rtl8169_net_suspend(dev);
5850
5851 return 0;
5852}
5853
5854static int rtl8169_runtime_resume(struct device *device)
5855{
5856 struct pci_dev *pdev = to_pci_dev(device);
5857 struct net_device *dev = pci_get_drvdata(pdev);
5858 struct rtl8169_private *tp = netdev_priv(dev);
5859
5860 if (!tp->TxDescArray)
5861 return 0;
5862
Francois Romieuda78dbf2012-01-26 14:18:23 +01005863 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005864 __rtl8169_set_wol(tp, tp->saved_wolopts);
5865 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005866 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005867
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005868 rtl8169_init_phy(dev, tp);
5869
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005870 __rtl8169_resume(dev);
5871
5872 return 0;
5873}
5874
5875static int rtl8169_runtime_idle(struct device *device)
5876{
5877 struct pci_dev *pdev = to_pci_dev(device);
5878 struct net_device *dev = pci_get_drvdata(pdev);
5879 struct rtl8169_private *tp = netdev_priv(dev);
5880
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005881 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005882}
5883
Alexey Dobriyan47145212009-12-14 18:00:08 -08005884static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005885 .suspend = rtl8169_suspend,
5886 .resume = rtl8169_resume,
5887 .freeze = rtl8169_suspend,
5888 .thaw = rtl8169_resume,
5889 .poweroff = rtl8169_suspend,
5890 .restore = rtl8169_resume,
5891 .runtime_suspend = rtl8169_runtime_suspend,
5892 .runtime_resume = rtl8169_runtime_resume,
5893 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005894};
5895
5896#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5897
5898#else /* !CONFIG_PM */
5899
5900#define RTL8169_PM_OPS NULL
5901
5902#endif /* !CONFIG_PM */
5903
David S. Miller1805b2f2011-10-24 18:18:09 -04005904static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
5905{
5906 void __iomem *ioaddr = tp->mmio_addr;
5907
5908 /* WoL fails with 8168b when the receiver is disabled. */
5909 switch (tp->mac_version) {
5910 case RTL_GIGA_MAC_VER_11:
5911 case RTL_GIGA_MAC_VER_12:
5912 case RTL_GIGA_MAC_VER_17:
5913 pci_clear_master(tp->pci_dev);
5914
5915 RTL_W8(ChipCmd, CmdRxEnb);
5916 /* PCI commit */
5917 RTL_R8(ChipCmd);
5918 break;
5919 default:
5920 break;
5921 }
5922}
5923
Francois Romieu1765f952008-09-13 17:21:40 +02005924static void rtl_shutdown(struct pci_dev *pdev)
5925{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005926 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005927 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00005928 struct device *d = &pdev->dev;
5929
5930 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02005931
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005932 rtl8169_net_suspend(dev);
5933
Francois Romieucecb5fd2011-04-01 10:21:07 +02005934 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08005935 rtl_rar_set(tp, dev->perm_addr);
5936
Hayes Wang92fc43b2011-07-06 15:58:03 +08005937 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00005938
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005939 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04005940 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
5941 rtl_wol_suspend_quirk(tp);
5942 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00005943 }
5944
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005945 pci_wake_from_d3(pdev, true);
5946 pci_set_power_state(pdev, PCI_D3hot);
5947 }
françois romieu2a15cd22012-03-06 01:14:12 +00005948
5949 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005950}
Francois Romieu5d06a992006-02-23 00:47:58 +01005951
Francois Romieue27566e2012-03-08 09:54:01 +01005952static void __devexit rtl_remove_one(struct pci_dev *pdev)
5953{
5954 struct net_device *dev = pci_get_drvdata(pdev);
5955 struct rtl8169_private *tp = netdev_priv(dev);
5956
5957 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
5958 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5959 tp->mac_version == RTL_GIGA_MAC_VER_31) {
5960 rtl8168_driver_stop(tp);
5961 }
5962
5963 cancel_work_sync(&tp->wk.work);
5964
5965 unregister_netdev(dev);
5966
5967 rtl_release_firmware(tp);
5968
5969 if (pci_dev_run_wake(pdev))
5970 pm_runtime_get_noresume(&pdev->dev);
5971
5972 /* restore original MAC address */
5973 rtl_rar_set(tp, dev->perm_addr);
5974
5975 rtl_disable_msi(pdev, tp);
5976 rtl8169_release_board(pdev, dev, tp->mmio_addr);
5977 pci_set_drvdata(pdev, NULL);
5978}
5979
Francois Romieufa9c3852012-03-08 10:01:50 +01005980static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01005981 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01005982 .ndo_stop = rtl8169_close,
5983 .ndo_get_stats64 = rtl8169_get_stats64,
5984 .ndo_start_xmit = rtl8169_start_xmit,
5985 .ndo_tx_timeout = rtl8169_tx_timeout,
5986 .ndo_validate_addr = eth_validate_addr,
5987 .ndo_change_mtu = rtl8169_change_mtu,
5988 .ndo_fix_features = rtl8169_fix_features,
5989 .ndo_set_features = rtl8169_set_features,
5990 .ndo_set_mac_address = rtl_set_mac_address,
5991 .ndo_do_ioctl = rtl8169_ioctl,
5992 .ndo_set_rx_mode = rtl_set_rx_mode,
5993#ifdef CONFIG_NET_POLL_CONTROLLER
5994 .ndo_poll_controller = rtl8169_netpoll,
5995#endif
5996
5997};
5998
Francois Romieu31fa8b12012-03-08 10:09:40 +01005999static const struct rtl_cfg_info {
6000 void (*hw_start)(struct net_device *);
6001 unsigned int region;
6002 unsigned int align;
6003 u16 event_slow;
6004 unsigned features;
6005 u8 default_ver;
6006} rtl_cfg_infos [] = {
6007 [RTL_CFG_0] = {
6008 .hw_start = rtl_hw_start_8169,
6009 .region = 1,
6010 .align = 0,
6011 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
6012 .features = RTL_FEATURE_GMII,
6013 .default_ver = RTL_GIGA_MAC_VER_01,
6014 },
6015 [RTL_CFG_1] = {
6016 .hw_start = rtl_hw_start_8168,
6017 .region = 2,
6018 .align = 8,
6019 .event_slow = SYSErr | LinkChg | RxOverflow,
6020 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
6021 .default_ver = RTL_GIGA_MAC_VER_11,
6022 },
6023 [RTL_CFG_2] = {
6024 .hw_start = rtl_hw_start_8101,
6025 .region = 2,
6026 .align = 8,
6027 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
6028 PCSTimeout,
6029 .features = RTL_FEATURE_MSI,
6030 .default_ver = RTL_GIGA_MAC_VER_13,
6031 }
6032};
6033
6034/* Cfg9346_Unlock assumed. */
6035static unsigned rtl_try_msi(struct rtl8169_private *tp,
6036 const struct rtl_cfg_info *cfg)
6037{
6038 void __iomem *ioaddr = tp->mmio_addr;
6039 unsigned msi = 0;
6040 u8 cfg2;
6041
6042 cfg2 = RTL_R8(Config2) & ~MSIEnable;
6043 if (cfg->features & RTL_FEATURE_MSI) {
6044 if (pci_enable_msi(tp->pci_dev)) {
6045 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
6046 } else {
6047 cfg2 |= MSIEnable;
6048 msi = RTL_FEATURE_MSI;
6049 }
6050 }
6051 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6052 RTL_W8(Config2, cfg2);
6053 return msi;
6054}
6055
Francois Romieu3b6cf252012-03-08 09:59:04 +01006056static int __devinit
6057rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6058{
6059 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
6060 const unsigned int region = cfg->region;
6061 struct rtl8169_private *tp;
6062 struct mii_if_info *mii;
6063 struct net_device *dev;
6064 void __iomem *ioaddr;
6065 int chipset, i;
6066 int rc;
6067
6068 if (netif_msg_drv(&debug)) {
6069 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
6070 MODULENAME, RTL8169_VERSION);
6071 }
6072
6073 dev = alloc_etherdev(sizeof (*tp));
6074 if (!dev) {
6075 rc = -ENOMEM;
6076 goto out;
6077 }
6078
6079 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01006080 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006081 tp = netdev_priv(dev);
6082 tp->dev = dev;
6083 tp->pci_dev = pdev;
6084 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
6085
6086 mii = &tp->mii;
6087 mii->dev = dev;
6088 mii->mdio_read = rtl_mdio_read;
6089 mii->mdio_write = rtl_mdio_write;
6090 mii->phy_id_mask = 0x1f;
6091 mii->reg_num_mask = 0x1f;
6092 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
6093
6094 /* disable ASPM completely as that cause random device stop working
6095 * problems as well as full system hangs for some PCIe devices users */
6096 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6097 PCIE_LINK_STATE_CLKPM);
6098
6099 /* enable device (incl. PCI PM wakeup and hotplug setup) */
6100 rc = pci_enable_device(pdev);
6101 if (rc < 0) {
6102 netif_err(tp, probe, dev, "enable failure\n");
6103 goto err_out_free_dev_1;
6104 }
6105
6106 if (pci_set_mwi(pdev) < 0)
6107 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
6108
6109 /* make sure PCI base addr 1 is MMIO */
6110 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
6111 netif_err(tp, probe, dev,
6112 "region #%d not an MMIO resource, aborting\n",
6113 region);
6114 rc = -ENODEV;
6115 goto err_out_mwi_2;
6116 }
6117
6118 /* check for weird/broken PCI region reporting */
6119 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
6120 netif_err(tp, probe, dev,
6121 "Invalid PCI region size(s), aborting\n");
6122 rc = -ENODEV;
6123 goto err_out_mwi_2;
6124 }
6125
6126 rc = pci_request_regions(pdev, MODULENAME);
6127 if (rc < 0) {
6128 netif_err(tp, probe, dev, "could not request regions\n");
6129 goto err_out_mwi_2;
6130 }
6131
6132 tp->cp_cmd = RxChkSum;
6133
6134 if ((sizeof(dma_addr_t) > 4) &&
6135 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
6136 tp->cp_cmd |= PCIDAC;
6137 dev->features |= NETIF_F_HIGHDMA;
6138 } else {
6139 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6140 if (rc < 0) {
6141 netif_err(tp, probe, dev, "DMA configuration failed\n");
6142 goto err_out_free_res_3;
6143 }
6144 }
6145
6146 /* ioremap MMIO region */
6147 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
6148 if (!ioaddr) {
6149 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
6150 rc = -EIO;
6151 goto err_out_free_res_3;
6152 }
6153 tp->mmio_addr = ioaddr;
6154
6155 if (!pci_is_pcie(pdev))
6156 netif_info(tp, probe, dev, "not PCI Express\n");
6157
6158 /* Identify chip attached to board */
6159 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
6160
6161 rtl_init_rxcfg(tp);
6162
6163 rtl_irq_disable(tp);
6164
6165 rtl_hw_reset(tp);
6166
6167 rtl_ack_events(tp, 0xffff);
6168
6169 pci_set_master(pdev);
6170
6171 /*
6172 * Pretend we are using VLANs; This bypasses a nasty bug where
6173 * Interrupts stop flowing on high load on 8110SCd controllers.
6174 */
6175 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6176 tp->cp_cmd |= RxVlan;
6177
6178 rtl_init_mdio_ops(tp);
6179 rtl_init_pll_power_ops(tp);
6180 rtl_init_jumbo_ops(tp);
6181
6182 rtl8169_print_mac_version(tp);
6183
6184 chipset = tp->mac_version;
6185 tp->txd_version = rtl_chip_infos[chipset].txd_version;
6186
6187 RTL_W8(Cfg9346, Cfg9346_Unlock);
6188 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
6189 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
6190 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
6191 tp->features |= RTL_FEATURE_WOL;
6192 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
6193 tp->features |= RTL_FEATURE_WOL;
6194 tp->features |= rtl_try_msi(tp, cfg);
6195 RTL_W8(Cfg9346, Cfg9346_Lock);
6196
6197 if (rtl_tbi_enabled(tp)) {
6198 tp->set_speed = rtl8169_set_speed_tbi;
6199 tp->get_settings = rtl8169_gset_tbi;
6200 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
6201 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
6202 tp->link_ok = rtl8169_tbi_link_ok;
6203 tp->do_ioctl = rtl_tbi_ioctl;
6204 } else {
6205 tp->set_speed = rtl8169_set_speed_xmii;
6206 tp->get_settings = rtl8169_gset_xmii;
6207 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
6208 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
6209 tp->link_ok = rtl8169_xmii_link_ok;
6210 tp->do_ioctl = rtl_xmii_ioctl;
6211 }
6212
6213 mutex_init(&tp->wk.mutex);
6214
6215 /* Get MAC address */
6216 for (i = 0; i < ETH_ALEN; i++)
6217 dev->dev_addr[i] = RTL_R8(MAC0 + i);
6218 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
6219
6220 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
6221 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006222
6223 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
6224
6225 /* don't enable SG, IP_CSUM and TSO by default - it might not work
6226 * properly for all devices */
6227 dev->features |= NETIF_F_RXCSUM |
6228 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6229
6230 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6231 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6232 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6233 NETIF_F_HIGHDMA;
6234
6235 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6236 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
6237 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
6238
6239 dev->hw_features |= NETIF_F_RXALL;
6240 dev->hw_features |= NETIF_F_RXFCS;
6241
6242 tp->hw_start = cfg->hw_start;
6243 tp->event_slow = cfg->event_slow;
6244
6245 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
6246 ~(RxBOVF | RxFOVF) : ~0;
6247
6248 init_timer(&tp->timer);
6249 tp->timer.data = (unsigned long) dev;
6250 tp->timer.function = rtl8169_phy_timer;
6251
6252 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
6253
6254 rc = register_netdev(dev);
6255 if (rc < 0)
6256 goto err_out_msi_4;
6257
6258 pci_set_drvdata(pdev, dev);
6259
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006260 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
6261 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
6262 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006263 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
6264 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
6265 "tx checksumming: %s]\n",
6266 rtl_chip_infos[chipset].jumbo_max,
6267 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
6268 }
6269
6270 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6271 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6272 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6273 rtl8168_driver_start(tp);
6274 }
6275
6276 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
6277
6278 if (pci_dev_run_wake(pdev))
6279 pm_runtime_put_noidle(&pdev->dev);
6280
6281 netif_carrier_off(dev);
6282
6283out:
6284 return rc;
6285
6286err_out_msi_4:
6287 rtl_disable_msi(pdev, tp);
6288 iounmap(ioaddr);
6289err_out_free_res_3:
6290 pci_release_regions(pdev);
6291err_out_mwi_2:
6292 pci_clear_mwi(pdev);
6293 pci_disable_device(pdev);
6294err_out_free_dev_1:
6295 free_netdev(dev);
6296 goto out;
6297}
6298
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299static struct pci_driver rtl8169_pci_driver = {
6300 .name = MODULENAME,
6301 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01006302 .probe = rtl_init_one,
Francois Romieue27566e2012-03-08 09:54:01 +01006303 .remove = __devexit_p(rtl_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02006304 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006305 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306};
6307
Francois Romieu07d3f512007-02-21 22:40:46 +01006308static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309{
Jeff Garzik29917622006-08-19 17:48:59 -04006310 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311}
6312
Francois Romieu07d3f512007-02-21 22:40:46 +01006313static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314{
6315 pci_unregister_driver(&rtl8169_pci_driver);
6316}
6317
6318module_init(rtl8169_init_module);
6319module_exit(rtl8169_cleanup_module);