blob: 28a08616569f89d66832be5ef4ebc627906f7f63 [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"
Hayes Wang7e18dca2012-03-30 14:33:02 +080047#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#ifdef RTL8169_DEBUG
50#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020051 if (!(expr)) { \
52 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070053 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020054 }
Joe Perches06fa7352007-10-18 21:15:00 +020055#define dprintk(fmt, args...) \
56 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#else
58#define assert(expr) do {} while (0)
59#define dprintk(fmt, args...) do {} while (0)
60#endif /* RTL8169_DEBUG */
61
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020062#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070063 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define TX_BUFFS_AVAIL(tp) \
66 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
69 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050070static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Francois Romieu9c14cea2008-07-05 00:21:15 +020072#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
75#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
76
77#define R8169_REGS_SIZE 256
78#define R8169_NAPI_WEIGHT 64
79#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
80#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
81#define RX_BUF_SIZE 1536 /* Rx Buffer size */
82#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
83#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
84
85#define RTL8169_TX_TIMEOUT (6*HZ)
86#define RTL8169_PHY_TIMEOUT (10*HZ)
87
françois romieuea8dbdd2009-03-15 01:10:50 +000088#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
89#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020090#define RTL_EEPROM_SIG_ADDR 0x0000
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* write/read MMIO register */
93#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
94#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
95#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
96#define RTL_R8(reg) readb (ioaddr + (reg))
97#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +000098#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200101 RTL_GIGA_MAC_VER_01 = 0,
102 RTL_GIGA_MAC_VER_02,
103 RTL_GIGA_MAC_VER_03,
104 RTL_GIGA_MAC_VER_04,
105 RTL_GIGA_MAC_VER_05,
106 RTL_GIGA_MAC_VER_06,
107 RTL_GIGA_MAC_VER_07,
108 RTL_GIGA_MAC_VER_08,
109 RTL_GIGA_MAC_VER_09,
110 RTL_GIGA_MAC_VER_10,
111 RTL_GIGA_MAC_VER_11,
112 RTL_GIGA_MAC_VER_12,
113 RTL_GIGA_MAC_VER_13,
114 RTL_GIGA_MAC_VER_14,
115 RTL_GIGA_MAC_VER_15,
116 RTL_GIGA_MAC_VER_16,
117 RTL_GIGA_MAC_VER_17,
118 RTL_GIGA_MAC_VER_18,
119 RTL_GIGA_MAC_VER_19,
120 RTL_GIGA_MAC_VER_20,
121 RTL_GIGA_MAC_VER_21,
122 RTL_GIGA_MAC_VER_22,
123 RTL_GIGA_MAC_VER_23,
124 RTL_GIGA_MAC_VER_24,
125 RTL_GIGA_MAC_VER_25,
126 RTL_GIGA_MAC_VER_26,
127 RTL_GIGA_MAC_VER_27,
128 RTL_GIGA_MAC_VER_28,
129 RTL_GIGA_MAC_VER_29,
130 RTL_GIGA_MAC_VER_30,
131 RTL_GIGA_MAC_VER_31,
132 RTL_GIGA_MAC_VER_32,
133 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800134 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800135 RTL_GIGA_MAC_VER_35,
136 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800137 RTL_GIGA_MAC_VER_37,
Francois Romieu85bffe62011-04-27 08:22:39 +0200138 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139};
140
Francois Romieu2b7b4312011-04-18 22:53:24 -0700141enum rtl_tx_desc_version {
142 RTL_TD_0 = 0,
143 RTL_TD_1 = 1,
144};
145
Francois Romieud58d46b2011-05-03 16:38:29 +0200146#define JUMBO_1K ETH_DATA_LEN
147#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
148#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
149#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
150#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
151
152#define _R(NAME,TD,FW,SZ,B) { \
153 .name = NAME, \
154 .txd_version = TD, \
155 .fw_name = FW, \
156 .jumbo_max = SZ, \
157 .jumbo_tx_csum = B \
158}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800160static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700162 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200163 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200164 u16 jumbo_max;
165 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200166} rtl_chip_infos[] = {
167 /* PCI devices. */
168 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200169 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200170 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200171 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200172 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200173 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200174 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200175 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200176 [RTL_GIGA_MAC_VER_05] =
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 [RTL_GIGA_MAC_VER_06] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200179 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200180 /* PCI-E devices. */
181 [RTL_GIGA_MAC_VER_07] =
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_08] =
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_09] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200186 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200187 [RTL_GIGA_MAC_VER_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200188 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200189 [RTL_GIGA_MAC_VER_11] =
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_12] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200192 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200193 [RTL_GIGA_MAC_VER_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200194 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200195 [RTL_GIGA_MAC_VER_14] =
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_15] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200198 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200199 [RTL_GIGA_MAC_VER_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200200 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200201 [RTL_GIGA_MAC_VER_17] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200202 _R("RTL8168b/8111b", RTL_TD_1, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200203 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200204 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200205 [RTL_GIGA_MAC_VER_19] =
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_20] =
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_21] =
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_22] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200212 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200213 [RTL_GIGA_MAC_VER_23] =
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_24] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200216 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200217 [RTL_GIGA_MAC_VER_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200218 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
219 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200220 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200221 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
222 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200223 [RTL_GIGA_MAC_VER_27] =
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_28] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200226 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200227 [RTL_GIGA_MAC_VER_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200228 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
229 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200230 [RTL_GIGA_MAC_VER_30] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200231 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
232 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200233 [RTL_GIGA_MAC_VER_31] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200234 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200235 [RTL_GIGA_MAC_VER_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200236 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
237 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200238 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200239 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
240 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800241 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200242 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
243 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800244 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200245 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
246 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800247 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200248 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
249 JUMBO_9K, false),
Hayes Wang7e18dca2012-03-30 14:33:02 +0800250 [RTL_GIGA_MAC_VER_37] =
251 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
252 JUMBO_1K, true),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253};
254#undef _R
255
Francois Romieubcf0bf92006-07-26 23:14:13 +0200256enum cfg_version {
257 RTL_CFG_0 = 0x00,
258 RTL_CFG_1,
259 RTL_CFG_2
260};
261
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000262static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200263 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200264 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200265 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100266 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200267 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
268 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000269 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200270 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200271 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
272 { PCI_VENDOR_ID_LINKSYS, 0x1032,
273 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100274 { 0x0001, 0x8168,
275 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 {0,},
277};
278
279MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
280
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000281static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700282static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200283static struct {
284 u32 msg_enable;
285} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Francois Romieu07d3f512007-02-21 22:40:46 +0100287enum rtl_registers {
288 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100289 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100290 MAR0 = 8, /* Multicast filter. */
291 CounterAddrLow = 0x10,
292 CounterAddrHigh = 0x14,
293 TxDescStartAddrLow = 0x20,
294 TxDescStartAddrHigh = 0x24,
295 TxHDescStartAddrLow = 0x28,
296 TxHDescStartAddrHigh = 0x2c,
297 FLASH = 0x30,
298 ERSR = 0x36,
299 ChipCmd = 0x37,
300 TxPoll = 0x38,
301 IntrMask = 0x3c,
302 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700303
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800304 TxConfig = 0x40,
305#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
306#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
307
308 RxConfig = 0x44,
309#define RX128_INT_EN (1 << 15) /* 8111c and later */
310#define RX_MULTI_EN (1 << 14) /* 8111c only */
311#define RXCFG_FIFO_SHIFT 13
312 /* No threshold before first PCI xfer */
313#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
314#define RXCFG_DMA_SHIFT 8
315 /* Unlimited maximum PCI burst. */
316#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700317
Francois Romieu07d3f512007-02-21 22:40:46 +0100318 RxMissed = 0x4c,
319 Cfg9346 = 0x50,
320 Config0 = 0x51,
321 Config1 = 0x52,
322 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200323#define PME_SIGNAL (1 << 5) /* 8168c and later */
324
Francois Romieu07d3f512007-02-21 22:40:46 +0100325 Config3 = 0x54,
326 Config4 = 0x55,
327 Config5 = 0x56,
328 MultiIntr = 0x5c,
329 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100330 PHYstatus = 0x6c,
331 RxMaxSize = 0xda,
332 CPlusCmd = 0xe0,
333 IntrMitigate = 0xe2,
334 RxDescAddrLow = 0xe4,
335 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000336 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
337
338#define NoEarlyTx 0x3f /* Max value : no early transmit. */
339
340 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
341
342#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800343#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000344
Francois Romieu07d3f512007-02-21 22:40:46 +0100345 FuncEvent = 0xf0,
346 FuncEventMask = 0xf4,
347 FuncPresetState = 0xf8,
348 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349};
350
Francois Romieuf162a5d2008-06-01 22:37:49 +0200351enum rtl8110_registers {
352 TBICSR = 0x64,
353 TBI_ANAR = 0x68,
354 TBI_LPAR = 0x6a,
355};
356
357enum rtl8168_8101_registers {
358 CSIDR = 0x64,
359 CSIAR = 0x68,
360#define CSIAR_FLAG 0x80000000
361#define CSIAR_WRITE_CMD 0x80000000
362#define CSIAR_BYTE_ENABLE 0x0f
363#define CSIAR_BYTE_ENABLE_SHIFT 12
364#define CSIAR_ADDR_MASK 0x0fff
Hayes Wang7e18dca2012-03-30 14:33:02 +0800365#define CSIAR_FUNC_CARD 0x00000000
366#define CSIAR_FUNC_SDIO 0x00010000
367#define CSIAR_FUNC_NIC 0x00020000
françois romieu065c27c2011-01-03 15:08:12 +0000368 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200369 EPHYAR = 0x80,
370#define EPHYAR_FLAG 0x80000000
371#define EPHYAR_WRITE_CMD 0x80000000
372#define EPHYAR_REG_MASK 0x1f
373#define EPHYAR_REG_SHIFT 16
374#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800375 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800376#define PFM_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200377 DBG_REG = 0xd1,
378#define FIX_NAK_1 (1 << 4)
379#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800380 TWSI = 0xd2,
381 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800382#define NOW_IS_OOB (1 << 7)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800383#define EN_NDP (1 << 3)
384#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000385 EFUSEAR = 0xdc,
386#define EFUSEAR_FLAG 0x80000000
387#define EFUSEAR_WRITE_CMD 0x80000000
388#define EFUSEAR_READ_CMD 0x00000000
389#define EFUSEAR_REG_MASK 0x03ff
390#define EFUSEAR_REG_SHIFT 8
391#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200392};
393
françois romieuc0e45c12011-01-03 15:08:04 +0000394enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800395 LED_FREQ = 0x1a,
396 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000397 ERIDR = 0x70,
398 ERIAR = 0x74,
399#define ERIAR_FLAG 0x80000000
400#define ERIAR_WRITE_CMD 0x80000000
401#define ERIAR_READ_CMD 0x00000000
402#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000403#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800404#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
405#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
406#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
407#define ERIAR_MASK_SHIFT 12
408#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
409#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
410#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000411 EPHY_RXER_NUM = 0x7c,
412 OCPDR = 0xb0, /* OCP GPHY access */
413#define OCPDR_WRITE_CMD 0x80000000
414#define OCPDR_READ_CMD 0x00000000
415#define OCPDR_REG_MASK 0x7f
416#define OCPDR_GPHY_REG_SHIFT 16
417#define OCPDR_DATA_MASK 0xffff
418 OCPAR = 0xb4,
419#define OCPAR_FLAG 0x80000000
420#define OCPAR_GPHY_WRITE_CMD 0x8000f060
421#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000422 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
423 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200424#define TXPLA_RST (1 << 29)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800425#define PWM_EN (1 << 22)
françois romieuc0e45c12011-01-03 15:08:04 +0000426};
427
Francois Romieu07d3f512007-02-21 22:40:46 +0100428enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100430 SYSErr = 0x8000,
431 PCSTimeout = 0x4000,
432 SWInt = 0x0100,
433 TxDescUnavail = 0x0080,
434 RxFIFOOver = 0x0040,
435 LinkChg = 0x0020,
436 RxOverflow = 0x0010,
437 TxErr = 0x0008,
438 TxOK = 0x0004,
439 RxErr = 0x0002,
440 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400443 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200444 RxFOVF = (1 << 23),
445 RxRWT = (1 << 22),
446 RxRES = (1 << 21),
447 RxRUNT = (1 << 20),
448 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800451 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100452 CmdReset = 0x10,
453 CmdRxEnb = 0x08,
454 CmdTxEnb = 0x04,
455 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Francois Romieu275391a2007-02-23 23:50:28 +0100457 /* TXPoll register p.5 */
458 HPQ = 0x80, /* Poll cmd on the high prio queue */
459 NPQ = 0x40, /* Poll cmd on the low prio queue */
460 FSWInt = 0x01, /* Forced software interrupt */
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100463 Cfg9346_Lock = 0x00,
464 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100467 AcceptErr = 0x20,
468 AcceptRunt = 0x10,
469 AcceptBroadcast = 0x08,
470 AcceptMulticast = 0x04,
471 AcceptMyPhys = 0x02,
472 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200473#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 /* TxConfigBits */
476 TxInterFrameGapShift = 24,
477 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
478
Francois Romieu5d06a992006-02-23 00:47:58 +0100479 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200480 LEDS1 = (1 << 7),
481 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200482 Speed_down = (1 << 4),
483 MEMMAP = (1 << 3),
484 IOMAP = (1 << 2),
485 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100486 PMEnable = (1 << 0), /* Power Management Enable */
487
Francois Romieu6dccd162007-02-13 23:38:05 +0100488 /* Config2 register p. 25 */
françois romieu2ca6cf02011-12-15 08:37:43 +0000489 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100490 PCI_Clock_66MHz = 0x01,
491 PCI_Clock_33MHz = 0x00,
492
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100493 /* Config3 register p.25 */
494 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
495 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200496 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200497 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100498
Francois Romieud58d46b2011-05-03 16:38:29 +0200499 /* Config4 register */
500 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
501
Francois Romieu5d06a992006-02-23 00:47:58 +0100502 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100503 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
504 MWF = (1 << 5), /* Accept Multicast wakeup frame */
505 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200506 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100507 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100508 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 /* TBICSR p.28 */
511 TBIReset = 0x80000000,
512 TBILoopback = 0x40000000,
513 TBINwEnable = 0x20000000,
514 TBINwRestart = 0x10000000,
515 TBILinkOk = 0x02000000,
516 TBINwComplete = 0x01000000,
517
518 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200519 EnableBist = (1 << 15), // 8168 8101
520 Mac_dbgo_oe = (1 << 14), // 8168 8101
521 Normal_mode = (1 << 13), // unused
522 Force_half_dup = (1 << 12), // 8168 8101
523 Force_rxflow_en = (1 << 11), // 8168 8101
524 Force_txflow_en = (1 << 10), // 8168 8101
525 Cxpl_dbg_sel = (1 << 9), // 8168 8101
526 ASF = (1 << 8), // 8168 8101
527 PktCntrDisable = (1 << 7), // 8168 8101
528 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 RxVlan = (1 << 6),
530 RxChkSum = (1 << 5),
531 PCIDAC = (1 << 4),
532 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100533 INTT_0 = 0x0000, // 8168
534 INTT_1 = 0x0001, // 8168
535 INTT_2 = 0x0002, // 8168
536 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100539 TBI_Enable = 0x80,
540 TxFlowCtrl = 0x40,
541 RxFlowCtrl = 0x20,
542 _1000bpsF = 0x10,
543 _100bps = 0x08,
544 _10bps = 0x04,
545 LinkStatus = 0x02,
546 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100549 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200550
551 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100552 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553};
554
Francois Romieu2b7b4312011-04-18 22:53:24 -0700555enum rtl_desc_bit {
556 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
558 RingEnd = (1 << 30), /* End of descriptor ring */
559 FirstFrag = (1 << 29), /* First segment of a packet */
560 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700561};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Francois Romieu2b7b4312011-04-18 22:53:24 -0700563/* Generic case. */
564enum rtl_tx_desc_bit {
565 /* First doubleword. */
566 TD_LSO = (1 << 27), /* Large Send Offload */
567#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Francois Romieu2b7b4312011-04-18 22:53:24 -0700569 /* Second doubleword. */
570 TxVlanTag = (1 << 17), /* Add VLAN tag */
571};
572
573/* 8169, 8168b and 810x except 8102e. */
574enum rtl_tx_desc_bit_0 {
575 /* First doubleword. */
576#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
577 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
578 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
579 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
580};
581
582/* 8102e, 8168c and beyond. */
583enum rtl_tx_desc_bit_1 {
584 /* Second doubleword. */
585#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
586 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
587 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
588 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
589};
590
591static const struct rtl_tx_desc_info {
592 struct {
593 u32 udp;
594 u32 tcp;
595 } checksum;
596 u16 mss_shift;
597 u16 opts_offset;
598} tx_desc_info [] = {
599 [RTL_TD_0] = {
600 .checksum = {
601 .udp = TD0_IP_CS | TD0_UDP_CS,
602 .tcp = TD0_IP_CS | TD0_TCP_CS
603 },
604 .mss_shift = TD0_MSS_SHIFT,
605 .opts_offset = 0
606 },
607 [RTL_TD_1] = {
608 .checksum = {
609 .udp = TD1_IP_CS | TD1_UDP_CS,
610 .tcp = TD1_IP_CS | TD1_TCP_CS
611 },
612 .mss_shift = TD1_MSS_SHIFT,
613 .opts_offset = 1
614 }
615};
616
617enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /* Rx private */
619 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
620 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
621
622#define RxProtoUDP (PID1)
623#define RxProtoTCP (PID0)
624#define RxProtoIP (PID1 | PID0)
625#define RxProtoMask RxProtoIP
626
627 IPFail = (1 << 16), /* IP checksum failed */
628 UDPFail = (1 << 15), /* UDP/IP checksum failed */
629 TCPFail = (1 << 14), /* TCP/IP checksum failed */
630 RxVlanTag = (1 << 16), /* VLAN tag available */
631};
632
633#define RsvdMask 0x3fffc000
634
635struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200636 __le32 opts1;
637 __le32 opts2;
638 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639};
640
641struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200642 __le32 opts1;
643 __le32 opts2;
644 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645};
646
647struct ring_info {
648 struct sk_buff *skb;
649 u32 len;
650 u8 __pad[sizeof(void *) - sizeof(u32)];
651};
652
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200653enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200654 RTL_FEATURE_WOL = (1 << 0),
655 RTL_FEATURE_MSI = (1 << 1),
656 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200657};
658
Ivan Vecera355423d2009-02-06 21:49:57 -0800659struct rtl8169_counters {
660 __le64 tx_packets;
661 __le64 rx_packets;
662 __le64 tx_errors;
663 __le32 rx_errors;
664 __le16 rx_missed;
665 __le16 align_errors;
666 __le32 tx_one_collision;
667 __le32 tx_multi_collision;
668 __le64 rx_unicast;
669 __le64 rx_broadcast;
670 __le32 rx_multicast;
671 __le16 tx_aborted;
672 __le16 tx_underun;
673};
674
Francois Romieuda78dbf2012-01-26 14:18:23 +0100675enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100676 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100677 RTL_FLAG_TASK_SLOW_PENDING,
678 RTL_FLAG_TASK_RESET_PENDING,
679 RTL_FLAG_TASK_PHY_PENDING,
680 RTL_FLAG_MAX
681};
682
Junchang Wang8027aa22012-03-04 23:30:32 +0100683struct rtl8169_stats {
684 u64 packets;
685 u64 bytes;
686 struct u64_stats_sync syncp;
687};
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689struct rtl8169_private {
690 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200691 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000692 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700693 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200694 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700695 u16 txd_version;
696 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
698 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
699 u32 dirty_rx;
700 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100701 struct rtl8169_stats rx_stats;
702 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
704 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
705 dma_addr_t TxPhyAddr;
706 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000707 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 struct timer_list timer;
710 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100711
712 u16 event_slow;
françois romieuc0e45c12011-01-03 15:08:04 +0000713
714 struct mdio_ops {
715 void (*write)(void __iomem *, int, int);
716 int (*read)(void __iomem *, int);
717 } mdio_ops;
718
françois romieu065c27c2011-01-03 15:08:12 +0000719 struct pll_power_ops {
720 void (*down)(struct rtl8169_private *);
721 void (*up)(struct rtl8169_private *);
722 } pll_power_ops;
723
Francois Romieud58d46b2011-05-03 16:38:29 +0200724 struct jumbo_ops {
725 void (*enable)(struct rtl8169_private *);
726 void (*disable)(struct rtl8169_private *);
727 } jumbo_ops;
728
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800729 struct csi_ops {
730 void (*write)(void __iomem *, int, int);
731 u32 (*read)(void __iomem *, int);
732 } csi_ops;
733
Oliver Neukum54405cd2011-01-06 21:55:13 +0100734 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200735 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000736 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100737 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000738 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800740 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100741
742 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100743 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
744 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100745 struct work_struct work;
746 } wk;
747
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200748 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200749
750 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800751 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000752 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400753 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000754
Francois Romieub6ffd972011-06-17 17:00:05 +0200755 struct rtl_fw {
756 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200757
758#define RTL_VER_SIZE 32
759
760 char version[RTL_VER_SIZE];
761
762 struct rtl_fw_phy_action {
763 __le32 *code;
764 size_t size;
765 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200766 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300767#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768};
769
Ralf Baechle979b6c12005-06-13 14:30:40 -0700770MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700773MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200774module_param_named(debug, debug.msg_enable, int, 0);
775MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776MODULE_LICENSE("GPL");
777MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000778MODULE_FIRMWARE(FIRMWARE_8168D_1);
779MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000780MODULE_FIRMWARE(FIRMWARE_8168E_1);
781MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400782MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800783MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800784MODULE_FIRMWARE(FIRMWARE_8168F_1);
785MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800786MODULE_FIRMWARE(FIRMWARE_8402_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Francois Romieuda78dbf2012-01-26 14:18:23 +0100788static void rtl_lock_work(struct rtl8169_private *tp)
789{
790 mutex_lock(&tp->wk.mutex);
791}
792
793static void rtl_unlock_work(struct rtl8169_private *tp)
794{
795 mutex_unlock(&tp->wk.mutex);
796}
797
Francois Romieud58d46b2011-05-03 16:38:29 +0200798static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
799{
800 int cap = pci_pcie_cap(pdev);
801
802 if (cap) {
803 u16 ctl;
804
805 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
806 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
807 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
808 }
809}
810
françois romieub646d902011-01-03 15:08:21 +0000811static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
812{
813 void __iomem *ioaddr = tp->mmio_addr;
814 int i;
815
816 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
817 for (i = 0; i < 20; i++) {
818 udelay(100);
819 if (RTL_R32(OCPAR) & OCPAR_FLAG)
820 break;
821 }
822 return RTL_R32(OCPDR);
823}
824
825static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
826{
827 void __iomem *ioaddr = tp->mmio_addr;
828 int i;
829
830 RTL_W32(OCPDR, data);
831 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
832 for (i = 0; i < 20; i++) {
833 udelay(100);
834 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
835 break;
836 }
837}
838
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800839static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000840{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800841 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000842 int i;
843
844 RTL_W8(ERIDR, cmd);
845 RTL_W32(ERIAR, 0x800010e8);
846 msleep(2);
847 for (i = 0; i < 5; i++) {
848 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200849 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000850 break;
851 }
852
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800853 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000854}
855
856#define OOB_CMD_RESET 0x00
857#define OOB_CMD_DRIVER_START 0x05
858#define OOB_CMD_DRIVER_STOP 0x06
859
Francois Romieucecb5fd2011-04-01 10:21:07 +0200860static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
861{
862 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
863}
864
françois romieub646d902011-01-03 15:08:21 +0000865static void rtl8168_driver_start(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_START);
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)
françois romieub646d902011-01-03 15:08:21 +0000877 break;
878 }
879}
880
881static void rtl8168_driver_stop(struct rtl8169_private *tp)
882{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200883 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000884 int i;
885
886 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
887
Francois Romieucecb5fd2011-04-01 10:21:07 +0200888 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000889
françois romieub646d902011-01-03 15:08:21 +0000890 for (i = 0; i < 10; i++) {
891 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000892 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000893 break;
894 }
895}
896
hayeswang4804b3b2011-03-21 01:50:29 +0000897static int r8168dp_check_dash(struct rtl8169_private *tp)
898{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200899 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000900
Francois Romieucecb5fd2011-04-01 10:21:07 +0200901 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000902}
françois romieub646d902011-01-03 15:08:21 +0000903
françois romieu4da19632011-01-03 15:07:55 +0000904static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
906 int i;
907
Francois Romieua6baf3a2007-11-08 23:23:21 +0100908 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Francois Romieu23714082006-01-29 00:49:09 +0100910 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100911 /*
912 * Check if the RTL8169 has completed writing to the specified
913 * MII register.
914 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200915 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 break;
Francois Romieu23714082006-01-29 00:49:09 +0100917 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700919 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700920 * According to hardware specs a 20us delay is required after write
921 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700922 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700923 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
françois romieu4da19632011-01-03 15:07:55 +0000926static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
928 int i, value = -1;
929
Francois Romieua6baf3a2007-11-08 23:23:21 +0100930 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Francois Romieu23714082006-01-29 00:49:09 +0100932 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100933 /*
934 * Check if the RTL8169 has completed retrieving data from
935 * the specified MII register.
936 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100938 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 break;
940 }
Francois Romieu23714082006-01-29 00:49:09 +0100941 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700943 /*
944 * According to hardware specs a 20us delay is required after read
945 * complete indication, but before sending next command.
946 */
947 udelay(20);
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return value;
950}
951
françois romieuc0e45c12011-01-03 15:08:04 +0000952static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
953{
954 int i;
955
956 RTL_W32(OCPDR, data |
957 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
958 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
959 RTL_W32(EPHY_RXER_NUM, 0);
960
961 for (i = 0; i < 100; i++) {
962 mdelay(1);
963 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
964 break;
965 }
966}
967
968static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
969{
970 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
971 (value & OCPDR_DATA_MASK));
972}
973
974static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
975{
976 int i;
977
978 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
979
980 mdelay(1);
981 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
982 RTL_W32(EPHY_RXER_NUM, 0);
983
984 for (i = 0; i < 100; i++) {
985 mdelay(1);
986 if (RTL_R32(OCPAR) & OCPAR_FLAG)
987 break;
988 }
989
990 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
991}
992
françois romieue6de30d2011-01-03 15:08:37 +0000993#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
994
995static void r8168dp_2_mdio_start(void __iomem *ioaddr)
996{
997 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
998}
999
1000static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1001{
1002 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1003}
1004
1005static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
1006{
1007 r8168dp_2_mdio_start(ioaddr);
1008
1009 r8169_mdio_write(ioaddr, reg_addr, value);
1010
1011 r8168dp_2_mdio_stop(ioaddr);
1012}
1013
1014static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
1015{
1016 int value;
1017
1018 r8168dp_2_mdio_start(ioaddr);
1019
1020 value = r8169_mdio_read(ioaddr, reg_addr);
1021
1022 r8168dp_2_mdio_stop(ioaddr);
1023
1024 return value;
1025}
1026
françois romieu4da19632011-01-03 15:07:55 +00001027static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001028{
françois romieuc0e45c12011-01-03 15:08:04 +00001029 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001030}
1031
françois romieu4da19632011-01-03 15:07:55 +00001032static int rtl_readphy(struct rtl8169_private *tp, int location)
1033{
françois romieuc0e45c12011-01-03 15:08:04 +00001034 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +00001035}
1036
1037static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1038{
1039 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1040}
1041
1042static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001043{
1044 int val;
1045
françois romieu4da19632011-01-03 15:07:55 +00001046 val = rtl_readphy(tp, reg_addr);
1047 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001048}
1049
Francois Romieuccdffb92008-07-26 14:26:06 +02001050static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1051 int val)
1052{
1053 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001054
françois romieu4da19632011-01-03 15:07:55 +00001055 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001056}
1057
1058static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1059{
1060 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001061
françois romieu4da19632011-01-03 15:07:55 +00001062 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001063}
1064
Francois Romieudacf8152008-08-02 20:44:13 +02001065static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
1066{
1067 unsigned int i;
1068
1069 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1070 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1071
1072 for (i = 0; i < 100; i++) {
1073 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1074 break;
1075 udelay(10);
1076 }
1077}
1078
1079static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1080{
1081 u16 value = 0xffff;
1082 unsigned int i;
1083
1084 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1085
1086 for (i = 0; i < 100; i++) {
1087 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1088 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1089 break;
1090 }
1091 udelay(10);
1092 }
1093
1094 return value;
1095}
1096
Hayes Wang133ac402011-07-06 15:58:05 +08001097static
1098void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1099{
1100 unsigned int i;
1101
1102 BUG_ON((addr & 3) || (mask == 0));
1103 RTL_W32(ERIDR, val);
1104 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1105
1106 for (i = 0; i < 100; i++) {
1107 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1108 break;
1109 udelay(100);
1110 }
1111}
1112
1113static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1114{
1115 u32 value = ~0x00;
1116 unsigned int i;
1117
1118 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1119
1120 for (i = 0; i < 100; i++) {
1121 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1122 value = RTL_R32(ERIDR);
1123 break;
1124 }
1125 udelay(100);
1126 }
1127
1128 return value;
1129}
1130
1131static void
1132rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1133{
1134 u32 val;
1135
1136 val = rtl_eri_read(ioaddr, addr, type);
1137 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1138}
1139
françois romieuc28aa382011-08-02 03:53:43 +00001140struct exgmac_reg {
1141 u16 addr;
1142 u16 mask;
1143 u32 val;
1144};
1145
1146static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1147 const struct exgmac_reg *r, int len)
1148{
1149 while (len-- > 0) {
1150 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1151 r++;
1152 }
1153}
1154
françois romieudaf9df62009-10-07 12:44:20 +00001155static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1156{
1157 u8 value = 0xff;
1158 unsigned int i;
1159
1160 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1161
1162 for (i = 0; i < 300; i++) {
1163 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1164 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1165 break;
1166 }
1167 udelay(100);
1168 }
1169
1170 return value;
1171}
1172
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001173static u16 rtl_get_events(struct rtl8169_private *tp)
1174{
1175 void __iomem *ioaddr = tp->mmio_addr;
1176
1177 return RTL_R16(IntrStatus);
1178}
1179
1180static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1181{
1182 void __iomem *ioaddr = tp->mmio_addr;
1183
1184 RTL_W16(IntrStatus, bits);
1185 mmiowb();
1186}
1187
1188static void rtl_irq_disable(struct rtl8169_private *tp)
1189{
1190 void __iomem *ioaddr = tp->mmio_addr;
1191
1192 RTL_W16(IntrMask, 0);
1193 mmiowb();
1194}
1195
Francois Romieu3e990ff2012-01-26 12:50:01 +01001196static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1197{
1198 void __iomem *ioaddr = tp->mmio_addr;
1199
1200 RTL_W16(IntrMask, bits);
1201}
1202
Francois Romieuda78dbf2012-01-26 14:18:23 +01001203#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1204#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1205#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1206
1207static void rtl_irq_enable_all(struct rtl8169_private *tp)
1208{
1209 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1210}
1211
françois romieu811fd302011-12-04 20:30:45 +00001212static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
françois romieu811fd302011-12-04 20:30:45 +00001214 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001216 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001217 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001218 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
françois romieu4da19632011-01-03 15:07:55 +00001221static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
françois romieu4da19632011-01-03 15:07:55 +00001223 void __iomem *ioaddr = tp->mmio_addr;
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return RTL_R32(TBICSR) & TBIReset;
1226}
1227
françois romieu4da19632011-01-03 15:07:55 +00001228static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
françois romieu4da19632011-01-03 15:07:55 +00001230 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
1233static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1234{
1235 return RTL_R32(TBICSR) & TBILinkOk;
1236}
1237
1238static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1239{
1240 return RTL_R8(PHYstatus) & LinkStatus;
1241}
1242
françois romieu4da19632011-01-03 15:07:55 +00001243static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
françois romieu4da19632011-01-03 15:07:55 +00001245 void __iomem *ioaddr = tp->mmio_addr;
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1248}
1249
françois romieu4da19632011-01-03 15:07:55 +00001250static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
1252 unsigned int val;
1253
françois romieu4da19632011-01-03 15:07:55 +00001254 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1255 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256}
1257
Hayes Wang70090422011-07-06 15:58:06 +08001258static void rtl_link_chg_patch(struct rtl8169_private *tp)
1259{
1260 void __iomem *ioaddr = tp->mmio_addr;
1261 struct net_device *dev = tp->dev;
1262
1263 if (!netif_running(dev))
1264 return;
1265
1266 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1267 if (RTL_R8(PHYstatus) & _1000bpsF) {
1268 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1269 0x00000011, ERIAR_EXGMAC);
1270 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1271 0x00000005, ERIAR_EXGMAC);
1272 } else if (RTL_R8(PHYstatus) & _100bps) {
1273 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1274 0x0000001f, ERIAR_EXGMAC);
1275 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1276 0x00000005, ERIAR_EXGMAC);
1277 } else {
1278 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1279 0x0000001f, ERIAR_EXGMAC);
1280 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1281 0x0000003f, ERIAR_EXGMAC);
1282 }
1283 /* Reset packet filter */
1284 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1285 ERIAR_EXGMAC);
1286 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1287 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001288 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1289 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1290 if (RTL_R8(PHYstatus) & _1000bpsF) {
1291 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1292 0x00000011, 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 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001301 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1302 if (RTL_R8(PHYstatus) & _10bps) {
1303 rtl_eri_write(ioaddr, 0x1d0, ERIAR_MASK_0011,
1304 0x4d02, ERIAR_EXGMAC);
1305 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_0011,
1306 0x0060, ERIAR_EXGMAC);
1307 } else {
1308 rtl_eri_write(ioaddr, 0x1d0, ERIAR_MASK_0011,
1309 0x0000, ERIAR_EXGMAC);
1310 }
Hayes Wang70090422011-07-06 15:58:06 +08001311 }
1312}
1313
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001314static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001315 struct rtl8169_private *tp,
1316 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001319 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001320 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001321 if (pm)
1322 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001324 if (net_ratelimit())
1325 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001326 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001328 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001329 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001330 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001334static void rtl8169_check_link_status(struct net_device *dev,
1335 struct rtl8169_private *tp,
1336 void __iomem *ioaddr)
1337{
1338 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1339}
1340
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001341#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1342
1343static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1344{
1345 void __iomem *ioaddr = tp->mmio_addr;
1346 u8 options;
1347 u32 wolopts = 0;
1348
1349 options = RTL_R8(Config1);
1350 if (!(options & PMEnable))
1351 return 0;
1352
1353 options = RTL_R8(Config3);
1354 if (options & LinkUp)
1355 wolopts |= WAKE_PHY;
1356 if (options & MagicPacket)
1357 wolopts |= WAKE_MAGIC;
1358
1359 options = RTL_R8(Config5);
1360 if (options & UWF)
1361 wolopts |= WAKE_UCAST;
1362 if (options & BWF)
1363 wolopts |= WAKE_BCAST;
1364 if (options & MWF)
1365 wolopts |= WAKE_MCAST;
1366
1367 return wolopts;
1368}
1369
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001370static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1371{
1372 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001373
Francois Romieuda78dbf2012-01-26 14:18:23 +01001374 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001375
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001376 wol->supported = WAKE_ANY;
1377 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001378
Francois Romieuda78dbf2012-01-26 14:18:23 +01001379 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001380}
1381
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001382static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001383{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001384 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001385 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001386 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001387 u32 opt;
1388 u16 reg;
1389 u8 mask;
1390 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001391 { WAKE_PHY, Config3, LinkUp },
1392 { WAKE_MAGIC, Config3, MagicPacket },
1393 { WAKE_UCAST, Config5, UWF },
1394 { WAKE_BCAST, Config5, BWF },
1395 { WAKE_MCAST, Config5, MWF },
1396 { WAKE_ANY, Config5, LanWake }
1397 };
Francois Romieu851e6022012-04-17 11:10:11 +02001398 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001399
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001400 RTL_W8(Cfg9346, Cfg9346_Unlock);
1401
1402 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
Francois Romieu851e6022012-04-17 11:10:11 +02001403 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001404 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001405 options |= cfg[i].mask;
1406 RTL_W8(cfg[i].reg, options);
1407 }
1408
Francois Romieu851e6022012-04-17 11:10:11 +02001409 switch (tp->mac_version) {
1410 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1411 options = RTL_R8(Config1) & ~PMEnable;
1412 if (wolopts)
1413 options |= PMEnable;
1414 RTL_W8(Config1, options);
1415 break;
1416 default:
Francois Romieud387b422012-04-17 11:12:01 +02001417 options = RTL_R8(Config2) & ~PME_SIGNAL;
1418 if (wolopts)
1419 options |= PME_SIGNAL;
1420 RTL_W8(Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001421 break;
1422 }
1423
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001424 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001425}
1426
1427static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1428{
1429 struct rtl8169_private *tp = netdev_priv(dev);
1430
Francois Romieuda78dbf2012-01-26 14:18:23 +01001431 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001432
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001433 if (wol->wolopts)
1434 tp->features |= RTL_FEATURE_WOL;
1435 else
1436 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001437 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001438
1439 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001440
françois romieuea809072010-11-08 13:23:58 +00001441 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1442
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001443 return 0;
1444}
1445
Francois Romieu31bd2042011-04-26 18:58:59 +02001446static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1447{
Francois Romieu85bffe62011-04-27 08:22:39 +02001448 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001449}
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451static void rtl8169_get_drvinfo(struct net_device *dev,
1452 struct ethtool_drvinfo *info)
1453{
1454 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001455 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Rick Jones68aad782011-11-07 13:29:27 +00001457 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1458 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1459 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001460 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001461 if (!IS_ERR_OR_NULL(rtl_fw))
1462 strlcpy(info->fw_version, rtl_fw->version,
1463 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
1466static int rtl8169_get_regs_len(struct net_device *dev)
1467{
1468 return R8169_REGS_SIZE;
1469}
1470
1471static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001472 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
1474 struct rtl8169_private *tp = netdev_priv(dev);
1475 void __iomem *ioaddr = tp->mmio_addr;
1476 int ret = 0;
1477 u32 reg;
1478
1479 reg = RTL_R32(TBICSR);
1480 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1481 (duplex == DUPLEX_FULL)) {
1482 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1483 } else if (autoneg == AUTONEG_ENABLE)
1484 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1485 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001486 netif_warn(tp, link, dev,
1487 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 ret = -EOPNOTSUPP;
1489 }
1490
1491 return ret;
1492}
1493
1494static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001495 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
1497 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001498 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001499 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Hayes Wang716b50a2011-02-22 17:26:18 +08001501 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001504 int auto_nego;
1505
françois romieu4da19632011-01-03 15:07:55 +00001506 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001507 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1508 ADVERTISE_100HALF | ADVERTISE_100FULL);
1509
1510 if (adv & ADVERTISED_10baseT_Half)
1511 auto_nego |= ADVERTISE_10HALF;
1512 if (adv & ADVERTISED_10baseT_Full)
1513 auto_nego |= ADVERTISE_10FULL;
1514 if (adv & ADVERTISED_100baseT_Half)
1515 auto_nego |= ADVERTISE_100HALF;
1516 if (adv & ADVERTISED_100baseT_Full)
1517 auto_nego |= ADVERTISE_100FULL;
1518
françois romieu3577aa12009-05-19 10:46:48 +00001519 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1520
françois romieu4da19632011-01-03 15:07:55 +00001521 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001522 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1523
1524 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001525 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001526 if (adv & ADVERTISED_1000baseT_Half)
1527 giga_ctrl |= ADVERTISE_1000HALF;
1528 if (adv & ADVERTISED_1000baseT_Full)
1529 giga_ctrl |= ADVERTISE_1000FULL;
1530 } else if (adv & (ADVERTISED_1000baseT_Half |
1531 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001532 netif_info(tp, link, dev,
1533 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001534 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
françois romieu3577aa12009-05-19 10:46:48 +00001537 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001538
françois romieu4da19632011-01-03 15:07:55 +00001539 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1540 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001541 } else {
1542 giga_ctrl = 0;
1543
1544 if (speed == SPEED_10)
1545 bmcr = 0;
1546 else if (speed == SPEED_100)
1547 bmcr = BMCR_SPEED100;
1548 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001549 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001550
1551 if (duplex == DUPLEX_FULL)
1552 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001553 }
1554
françois romieu4da19632011-01-03 15:07:55 +00001555 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001556
Francois Romieucecb5fd2011-04-01 10:21:07 +02001557 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1558 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001559 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001560 rtl_writephy(tp, 0x17, 0x2138);
1561 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001562 } else {
françois romieu4da19632011-01-03 15:07:55 +00001563 rtl_writephy(tp, 0x17, 0x2108);
1564 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001565 }
1566 }
1567
Oliver Neukum54405cd2011-01-06 21:55:13 +01001568 rc = 0;
1569out:
1570 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
1572
1573static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001574 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
1576 struct rtl8169_private *tp = netdev_priv(dev);
1577 int ret;
1578
Oliver Neukum54405cd2011-01-06 21:55:13 +01001579 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001580 if (ret < 0)
1581 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Francois Romieu4876cc12011-03-11 21:07:11 +01001583 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1584 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001586 }
1587out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 return ret;
1589}
1590
1591static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1592{
1593 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 int ret;
1595
Francois Romieu4876cc12011-03-11 21:07:11 +01001596 del_timer_sync(&tp->timer);
1597
Francois Romieuda78dbf2012-01-26 14:18:23 +01001598 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001599 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001600 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001601 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return ret;
1604}
1605
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001606static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1607 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
Francois Romieud58d46b2011-05-03 16:38:29 +02001609 struct rtl8169_private *tp = netdev_priv(dev);
1610
Francois Romieu2b7b4312011-04-18 22:53:24 -07001611 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001612 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Francois Romieud58d46b2011-05-03 16:38:29 +02001614 if (dev->mtu > JUMBO_1K &&
1615 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1616 features &= ~NETIF_F_IP_CSUM;
1617
Michał Mirosław350fb322011-04-08 06:35:56 +00001618 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
Francois Romieuda78dbf2012-01-26 14:18:23 +01001621static void __rtl8169_set_features(struct net_device *dev,
1622 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
1624 struct rtl8169_private *tp = netdev_priv(dev);
Ben Greear6bbe0212012-02-10 15:04:33 +00001625 netdev_features_t changed = features ^ dev->features;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001626 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Ben Greear6bbe0212012-02-10 15:04:33 +00001628 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)))
1629 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Ben Greear6bbe0212012-02-10 15:04:33 +00001631 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)) {
1632 if (features & NETIF_F_RXCSUM)
1633 tp->cp_cmd |= RxChkSum;
1634 else
1635 tp->cp_cmd &= ~RxChkSum;
Michał Mirosław350fb322011-04-08 06:35:56 +00001636
Ben Greear6bbe0212012-02-10 15:04:33 +00001637 if (dev->features & NETIF_F_HW_VLAN_RX)
1638 tp->cp_cmd |= RxVlan;
1639 else
1640 tp->cp_cmd &= ~RxVlan;
1641
1642 RTL_W16(CPlusCmd, tp->cp_cmd);
1643 RTL_R16(CPlusCmd);
1644 }
1645 if (changed & NETIF_F_RXALL) {
1646 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1647 if (features & NETIF_F_RXALL)
1648 tmp |= (AcceptErr | AcceptRunt);
1649 RTL_W32(RxConfig, tmp);
1650 }
Francois Romieuda78dbf2012-01-26 14:18:23 +01001651}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Francois Romieuda78dbf2012-01-26 14:18:23 +01001653static int rtl8169_set_features(struct net_device *dev,
1654 netdev_features_t features)
1655{
1656 struct rtl8169_private *tp = netdev_priv(dev);
1657
1658 rtl_lock_work(tp);
1659 __rtl8169_set_features(dev, features);
1660 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 return 0;
1663}
1664
Francois Romieuda78dbf2012-01-26 14:18:23 +01001665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1667 struct sk_buff *skb)
1668{
Jesse Grosseab6d182010-10-20 13:56:03 +00001669 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1671}
1672
Francois Romieu7a8fc772011-03-01 17:18:33 +01001673static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
1675 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Francois Romieu7a8fc772011-03-01 17:18:33 +01001677 if (opts2 & RxVlanTag)
1678 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
Francois Romieuccdffb92008-07-26 14:26:06 +02001683static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
1685 struct rtl8169_private *tp = netdev_priv(dev);
1686 void __iomem *ioaddr = tp->mmio_addr;
1687 u32 status;
1688
1689 cmd->supported =
1690 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1691 cmd->port = PORT_FIBRE;
1692 cmd->transceiver = XCVR_INTERNAL;
1693
1694 status = RTL_R32(TBICSR);
1695 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1696 cmd->autoneg = !!(status & TBINwEnable);
1697
David Decotigny70739492011-04-27 18:32:40 +00001698 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001700
1701 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
1703
Francois Romieuccdffb92008-07-26 14:26:06 +02001704static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
1706 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Francois Romieuccdffb92008-07-26 14:26:06 +02001708 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709}
1710
1711static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1712{
1713 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001714 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Francois Romieuda78dbf2012-01-26 14:18:23 +01001716 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02001717 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001718 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Francois Romieuccdffb92008-07-26 14:26:06 +02001720 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721}
1722
1723static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1724 void *p)
1725{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001726 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Francois Romieu5b0384f2006-08-16 16:00:01 +02001728 if (regs->len > R8169_REGS_SIZE)
1729 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Francois Romieuda78dbf2012-01-26 14:18:23 +01001731 rtl_lock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001732 memcpy_fromio(p, tp->mmio_addr, regs->len);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001733 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734}
1735
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001736static u32 rtl8169_get_msglevel(struct net_device *dev)
1737{
1738 struct rtl8169_private *tp = netdev_priv(dev);
1739
1740 return tp->msg_enable;
1741}
1742
1743static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1744{
1745 struct rtl8169_private *tp = netdev_priv(dev);
1746
1747 tp->msg_enable = value;
1748}
1749
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001750static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1751 "tx_packets",
1752 "rx_packets",
1753 "tx_errors",
1754 "rx_errors",
1755 "rx_missed",
1756 "align_errors",
1757 "tx_single_collisions",
1758 "tx_multi_collisions",
1759 "unicast",
1760 "broadcast",
1761 "multicast",
1762 "tx_aborted",
1763 "tx_underrun",
1764};
1765
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001766static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001767{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001768 switch (sset) {
1769 case ETH_SS_STATS:
1770 return ARRAY_SIZE(rtl8169_gstrings);
1771 default:
1772 return -EOPNOTSUPP;
1773 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001774}
1775
Ivan Vecera355423d2009-02-06 21:49:57 -08001776static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001777{
1778 struct rtl8169_private *tp = netdev_priv(dev);
1779 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001780 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001781 struct rtl8169_counters *counters;
1782 dma_addr_t paddr;
1783 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001784 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001785
Ivan Vecera355423d2009-02-06 21:49:57 -08001786 /*
1787 * Some chips are unable to dump tally counters when the receiver
1788 * is disabled.
1789 */
1790 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1791 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001792
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001793 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001794 if (!counters)
1795 return;
1796
1797 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001798 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001799 RTL_W32(CounterAddrLow, cmd);
1800 RTL_W32(CounterAddrLow, cmd | CounterDump);
1801
Ivan Vecera355423d2009-02-06 21:49:57 -08001802 while (wait--) {
1803 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001804 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001805 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001806 }
1807 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001808 }
1809
1810 RTL_W32(CounterAddrLow, 0);
1811 RTL_W32(CounterAddrHigh, 0);
1812
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001813 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001814}
1815
Ivan Vecera355423d2009-02-06 21:49:57 -08001816static void rtl8169_get_ethtool_stats(struct net_device *dev,
1817 struct ethtool_stats *stats, u64 *data)
1818{
1819 struct rtl8169_private *tp = netdev_priv(dev);
1820
1821 ASSERT_RTNL();
1822
1823 rtl8169_update_counters(dev);
1824
1825 data[0] = le64_to_cpu(tp->counters.tx_packets);
1826 data[1] = le64_to_cpu(tp->counters.rx_packets);
1827 data[2] = le64_to_cpu(tp->counters.tx_errors);
1828 data[3] = le32_to_cpu(tp->counters.rx_errors);
1829 data[4] = le16_to_cpu(tp->counters.rx_missed);
1830 data[5] = le16_to_cpu(tp->counters.align_errors);
1831 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1832 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1833 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1834 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1835 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1836 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1837 data[12] = le16_to_cpu(tp->counters.tx_underun);
1838}
1839
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001840static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1841{
1842 switch(stringset) {
1843 case ETH_SS_STATS:
1844 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1845 break;
1846 }
1847}
1848
Jeff Garzik7282d492006-09-13 14:30:00 -04001849static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 .get_drvinfo = rtl8169_get_drvinfo,
1851 .get_regs_len = rtl8169_get_regs_len,
1852 .get_link = ethtool_op_get_link,
1853 .get_settings = rtl8169_get_settings,
1854 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001855 .get_msglevel = rtl8169_get_msglevel,
1856 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001858 .get_wol = rtl8169_get_wol,
1859 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001860 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001861 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001862 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00001863 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864};
1865
Francois Romieu07d3f512007-02-21 22:40:46 +01001866static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001867 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
Francois Romieu5d320a22011-05-08 17:47:36 +02001869 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001870 /*
1871 * The driver currently handles the 8168Bf and the 8168Be identically
1872 * but they can be identified more specifically through the test below
1873 * if needed:
1874 *
1875 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001876 *
1877 * Same thing for the 8101Eb and the 8101Ec:
1878 *
1879 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001880 */
Francois Romieu37441002011-06-17 22:58:54 +02001881 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001883 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 int mac_version;
1885 } mac_info[] = {
Hayes Wangc2218922011-09-06 16:55:18 +08001886 /* 8168F family. */
1887 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1888 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1889
hayeswang01dc7fe2011-03-21 01:50:28 +00001890 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001891 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001892 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1893 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1894 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1895
Francois Romieu5b538df2008-07-20 16:22:45 +02001896 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001897 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1898 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001899 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001900
françois romieue6de30d2011-01-03 15:08:37 +00001901 /* 8168DP family. */
1902 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1903 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001904 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001905
Francois Romieuef808d52008-06-29 13:10:54 +02001906 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001907 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001908 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001909 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001910 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001911 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1912 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001913 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001914 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001915 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001916
1917 /* 8168B family. */
1918 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1919 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1920 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1921 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1922
1923 /* 8101 family. */
Hayes Wang7e18dca2012-03-30 14:33:02 +08001924 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00001925 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001926 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1927 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1928 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001929 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1930 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1931 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1932 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1933 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1934 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001935 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001936 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001937 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001938 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1939 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001940 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1941 /* FIXME: where did these entries come from ? -- FR */
1942 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1943 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1944
1945 /* 8110 family. */
1946 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1947 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1948 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1949 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1950 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1951 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1952
Jean Delvaref21b75e2009-05-26 20:54:48 -07001953 /* Catch-all */
1954 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001955 };
1956 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 u32 reg;
1958
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001959 reg = RTL_R32(TxConfig);
1960 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 p++;
1962 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001963
1964 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1965 netif_notice(tp, probe, dev,
1966 "unknown MAC, using family default\n");
1967 tp->mac_version = default_version;
1968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
1970
1971static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1972{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001973 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974}
1975
Francois Romieu867763c2007-08-17 18:21:58 +02001976struct phy_reg {
1977 u16 reg;
1978 u16 val;
1979};
1980
françois romieu4da19632011-01-03 15:07:55 +00001981static void rtl_writephy_batch(struct rtl8169_private *tp,
1982 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001983{
1984 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001985 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001986 regs++;
1987 }
1988}
1989
françois romieubca03d52011-01-03 15:07:31 +00001990#define PHY_READ 0x00000000
1991#define PHY_DATA_OR 0x10000000
1992#define PHY_DATA_AND 0x20000000
1993#define PHY_BJMPN 0x30000000
1994#define PHY_READ_EFUSE 0x40000000
1995#define PHY_READ_MAC_BYTE 0x50000000
1996#define PHY_WRITE_MAC_BYTE 0x60000000
1997#define PHY_CLEAR_READCOUNT 0x70000000
1998#define PHY_WRITE 0x80000000
1999#define PHY_READCOUNT_EQ_SKIP 0x90000000
2000#define PHY_COMP_EQ_SKIPN 0xa0000000
2001#define PHY_COMP_NEQ_SKIPN 0xb0000000
2002#define PHY_WRITE_PREVIOUS 0xc0000000
2003#define PHY_SKIPN 0xd0000000
2004#define PHY_DELAY_MS 0xe0000000
2005#define PHY_WRITE_ERI_WORD 0xf0000000
2006
Hayes Wang960aee62011-06-18 11:37:48 +02002007struct fw_info {
2008 u32 magic;
2009 char version[RTL_VER_SIZE];
2010 __le32 fw_start;
2011 __le32 fw_len;
2012 u8 chksum;
2013} __packed;
2014
Francois Romieu1c361ef2011-06-17 17:16:24 +02002015#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2016
2017static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002018{
Francois Romieub6ffd972011-06-17 17:00:05 +02002019 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002020 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002021 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2022 char *version = rtl_fw->version;
2023 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002024
Francois Romieu1c361ef2011-06-17 17:16:24 +02002025 if (fw->size < FW_OPCODE_SIZE)
2026 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002027
2028 if (!fw_info->magic) {
2029 size_t i, size, start;
2030 u8 checksum = 0;
2031
2032 if (fw->size < sizeof(*fw_info))
2033 goto out;
2034
2035 for (i = 0; i < fw->size; i++)
2036 checksum += fw->data[i];
2037 if (checksum != 0)
2038 goto out;
2039
2040 start = le32_to_cpu(fw_info->fw_start);
2041 if (start > fw->size)
2042 goto out;
2043
2044 size = le32_to_cpu(fw_info->fw_len);
2045 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2046 goto out;
2047
2048 memcpy(version, fw_info->version, RTL_VER_SIZE);
2049
2050 pa->code = (__le32 *)(fw->data + start);
2051 pa->size = size;
2052 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002053 if (fw->size % FW_OPCODE_SIZE)
2054 goto out;
2055
2056 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2057
2058 pa->code = (__le32 *)fw->data;
2059 pa->size = fw->size / FW_OPCODE_SIZE;
2060 }
2061 version[RTL_VER_SIZE - 1] = 0;
2062
2063 rc = true;
2064out:
2065 return rc;
2066}
2067
Francois Romieufd112f22011-06-18 00:10:29 +02002068static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2069 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002070{
Francois Romieufd112f22011-06-18 00:10:29 +02002071 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002072 size_t index;
2073
Francois Romieu1c361ef2011-06-17 17:16:24 +02002074 for (index = 0; index < pa->size; index++) {
2075 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002076 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002077
hayeswang42b82dc2011-01-10 02:07:25 +00002078 switch(action & 0xf0000000) {
2079 case PHY_READ:
2080 case PHY_DATA_OR:
2081 case PHY_DATA_AND:
2082 case PHY_READ_EFUSE:
2083 case PHY_CLEAR_READCOUNT:
2084 case PHY_WRITE:
2085 case PHY_WRITE_PREVIOUS:
2086 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002087 break;
2088
hayeswang42b82dc2011-01-10 02:07:25 +00002089 case PHY_BJMPN:
2090 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002091 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002092 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002093 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002094 }
2095 break;
2096 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002097 if (index + 2 >= 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 case PHY_COMP_EQ_SKIPN:
2104 case PHY_COMP_NEQ_SKIPN:
2105 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002106 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002107 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002108 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002109 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002110 }
2111 break;
2112
2113 case PHY_READ_MAC_BYTE:
2114 case PHY_WRITE_MAC_BYTE:
2115 case PHY_WRITE_ERI_WORD:
2116 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002117 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002118 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002119 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002120 }
2121 }
Francois Romieufd112f22011-06-18 00:10:29 +02002122 rc = true;
2123out:
2124 return rc;
2125}
françois romieubca03d52011-01-03 15:07:31 +00002126
Francois Romieufd112f22011-06-18 00:10:29 +02002127static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2128{
2129 struct net_device *dev = tp->dev;
2130 int rc = -EINVAL;
2131
2132 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2133 netif_err(tp, ifup, dev, "invalid firwmare\n");
2134 goto out;
2135 }
2136
2137 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2138 rc = 0;
2139out:
2140 return rc;
2141}
2142
2143static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2144{
2145 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2146 u32 predata, count;
2147 size_t index;
2148
2149 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002150
Francois Romieu1c361ef2011-06-17 17:16:24 +02002151 for (index = 0; index < pa->size; ) {
2152 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002153 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002154 u32 regno = (action & 0x0fff0000) >> 16;
2155
2156 if (!action)
2157 break;
françois romieubca03d52011-01-03 15:07:31 +00002158
2159 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002160 case PHY_READ:
2161 predata = rtl_readphy(tp, regno);
2162 count++;
2163 index++;
françois romieubca03d52011-01-03 15:07:31 +00002164 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002165 case PHY_DATA_OR:
2166 predata |= data;
2167 index++;
2168 break;
2169 case PHY_DATA_AND:
2170 predata &= data;
2171 index++;
2172 break;
2173 case PHY_BJMPN:
2174 index -= regno;
2175 break;
2176 case PHY_READ_EFUSE:
2177 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2178 index++;
2179 break;
2180 case PHY_CLEAR_READCOUNT:
2181 count = 0;
2182 index++;
2183 break;
2184 case PHY_WRITE:
2185 rtl_writephy(tp, regno, data);
2186 index++;
2187 break;
2188 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002189 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002190 break;
2191 case PHY_COMP_EQ_SKIPN:
2192 if (predata == data)
2193 index += regno;
2194 index++;
2195 break;
2196 case PHY_COMP_NEQ_SKIPN:
2197 if (predata != data)
2198 index += regno;
2199 index++;
2200 break;
2201 case PHY_WRITE_PREVIOUS:
2202 rtl_writephy(tp, regno, predata);
2203 index++;
2204 break;
2205 case PHY_SKIPN:
2206 index += regno + 1;
2207 break;
2208 case PHY_DELAY_MS:
2209 mdelay(data);
2210 index++;
2211 break;
2212
2213 case PHY_READ_MAC_BYTE:
2214 case PHY_WRITE_MAC_BYTE:
2215 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002216 default:
2217 BUG();
2218 }
2219 }
2220}
2221
françois romieuf1e02ed2011-01-13 13:07:53 +00002222static void rtl_release_firmware(struct rtl8169_private *tp)
2223{
Francois Romieub6ffd972011-06-17 17:00:05 +02002224 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2225 release_firmware(tp->rtl_fw->fw);
2226 kfree(tp->rtl_fw);
2227 }
2228 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002229}
2230
François Romieu953a12c2011-04-24 17:38:48 +02002231static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002232{
Francois Romieub6ffd972011-06-17 17:00:05 +02002233 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002234
2235 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002236 if (!IS_ERR_OR_NULL(rtl_fw))
2237 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002238}
2239
2240static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2241{
2242 if (rtl_readphy(tp, reg) != val)
2243 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2244 else
2245 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002246}
2247
françois romieu4da19632011-01-03 15:07:55 +00002248static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002250 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002251 { 0x1f, 0x0001 },
2252 { 0x06, 0x006e },
2253 { 0x08, 0x0708 },
2254 { 0x15, 0x4000 },
2255 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
françois romieu0b9b5712009-08-10 19:44:56 +00002257 { 0x1f, 0x0001 },
2258 { 0x03, 0x00a1 },
2259 { 0x02, 0x0008 },
2260 { 0x01, 0x0120 },
2261 { 0x00, 0x1000 },
2262 { 0x04, 0x0800 },
2263 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
françois romieu0b9b5712009-08-10 19:44:56 +00002265 { 0x03, 0xff41 },
2266 { 0x02, 0xdf60 },
2267 { 0x01, 0x0140 },
2268 { 0x00, 0x0077 },
2269 { 0x04, 0x7800 },
2270 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
françois romieu0b9b5712009-08-10 19:44:56 +00002272 { 0x03, 0x802f },
2273 { 0x02, 0x4f02 },
2274 { 0x01, 0x0409 },
2275 { 0x00, 0xf0f9 },
2276 { 0x04, 0x9800 },
2277 { 0x04, 0x9000 },
2278
2279 { 0x03, 0xdf01 },
2280 { 0x02, 0xdf20 },
2281 { 0x01, 0xff95 },
2282 { 0x00, 0xba00 },
2283 { 0x04, 0xa800 },
2284 { 0x04, 0xa000 },
2285
2286 { 0x03, 0xff41 },
2287 { 0x02, 0xdf20 },
2288 { 0x01, 0x0140 },
2289 { 0x00, 0x00bb },
2290 { 0x04, 0xb800 },
2291 { 0x04, 0xb000 },
2292
2293 { 0x03, 0xdf41 },
2294 { 0x02, 0xdc60 },
2295 { 0x01, 0x6340 },
2296 { 0x00, 0x007d },
2297 { 0x04, 0xd800 },
2298 { 0x04, 0xd000 },
2299
2300 { 0x03, 0xdf01 },
2301 { 0x02, 0xdf20 },
2302 { 0x01, 0x100a },
2303 { 0x00, 0xa0ff },
2304 { 0x04, 0xf800 },
2305 { 0x04, 0xf000 },
2306
2307 { 0x1f, 0x0000 },
2308 { 0x0b, 0x0000 },
2309 { 0x00, 0x9200 }
2310 };
2311
françois romieu4da19632011-01-03 15:07:55 +00002312 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313}
2314
françois romieu4da19632011-01-03 15:07:55 +00002315static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002316{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002317 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002318 { 0x1f, 0x0002 },
2319 { 0x01, 0x90d0 },
2320 { 0x1f, 0x0000 }
2321 };
2322
françois romieu4da19632011-01-03 15:07:55 +00002323 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002324}
2325
françois romieu4da19632011-01-03 15:07:55 +00002326static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002327{
2328 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002329
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002330 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2331 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002332 return;
2333
françois romieu4da19632011-01-03 15:07:55 +00002334 rtl_writephy(tp, 0x1f, 0x0001);
2335 rtl_writephy(tp, 0x10, 0xf01b);
2336 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002337}
2338
françois romieu4da19632011-01-03 15:07:55 +00002339static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002340{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002341 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002342 { 0x1f, 0x0001 },
2343 { 0x04, 0x0000 },
2344 { 0x03, 0x00a1 },
2345 { 0x02, 0x0008 },
2346 { 0x01, 0x0120 },
2347 { 0x00, 0x1000 },
2348 { 0x04, 0x0800 },
2349 { 0x04, 0x9000 },
2350 { 0x03, 0x802f },
2351 { 0x02, 0x4f02 },
2352 { 0x01, 0x0409 },
2353 { 0x00, 0xf099 },
2354 { 0x04, 0x9800 },
2355 { 0x04, 0xa000 },
2356 { 0x03, 0xdf01 },
2357 { 0x02, 0xdf20 },
2358 { 0x01, 0xff95 },
2359 { 0x00, 0xba00 },
2360 { 0x04, 0xa800 },
2361 { 0x04, 0xf000 },
2362 { 0x03, 0xdf01 },
2363 { 0x02, 0xdf20 },
2364 { 0x01, 0x101a },
2365 { 0x00, 0xa0ff },
2366 { 0x04, 0xf800 },
2367 { 0x04, 0x0000 },
2368 { 0x1f, 0x0000 },
2369
2370 { 0x1f, 0x0001 },
2371 { 0x10, 0xf41b },
2372 { 0x14, 0xfb54 },
2373 { 0x18, 0xf5c7 },
2374 { 0x1f, 0x0000 },
2375
2376 { 0x1f, 0x0001 },
2377 { 0x17, 0x0cc0 },
2378 { 0x1f, 0x0000 }
2379 };
2380
françois romieu4da19632011-01-03 15:07:55 +00002381 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002382
françois romieu4da19632011-01-03 15:07:55 +00002383 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002384}
2385
françois romieu4da19632011-01-03 15:07:55 +00002386static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002387{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002388 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002389 { 0x1f, 0x0001 },
2390 { 0x04, 0x0000 },
2391 { 0x03, 0x00a1 },
2392 { 0x02, 0x0008 },
2393 { 0x01, 0x0120 },
2394 { 0x00, 0x1000 },
2395 { 0x04, 0x0800 },
2396 { 0x04, 0x9000 },
2397 { 0x03, 0x802f },
2398 { 0x02, 0x4f02 },
2399 { 0x01, 0x0409 },
2400 { 0x00, 0xf099 },
2401 { 0x04, 0x9800 },
2402 { 0x04, 0xa000 },
2403 { 0x03, 0xdf01 },
2404 { 0x02, 0xdf20 },
2405 { 0x01, 0xff95 },
2406 { 0x00, 0xba00 },
2407 { 0x04, 0xa800 },
2408 { 0x04, 0xf000 },
2409 { 0x03, 0xdf01 },
2410 { 0x02, 0xdf20 },
2411 { 0x01, 0x101a },
2412 { 0x00, 0xa0ff },
2413 { 0x04, 0xf800 },
2414 { 0x04, 0x0000 },
2415 { 0x1f, 0x0000 },
2416
2417 { 0x1f, 0x0001 },
2418 { 0x0b, 0x8480 },
2419 { 0x1f, 0x0000 },
2420
2421 { 0x1f, 0x0001 },
2422 { 0x18, 0x67c7 },
2423 { 0x04, 0x2000 },
2424 { 0x03, 0x002f },
2425 { 0x02, 0x4360 },
2426 { 0x01, 0x0109 },
2427 { 0x00, 0x3022 },
2428 { 0x04, 0x2800 },
2429 { 0x1f, 0x0000 },
2430
2431 { 0x1f, 0x0001 },
2432 { 0x17, 0x0cc0 },
2433 { 0x1f, 0x0000 }
2434 };
2435
françois romieu4da19632011-01-03 15:07:55 +00002436 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002437}
2438
françois romieu4da19632011-01-03 15:07:55 +00002439static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002440{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002441 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002442 { 0x10, 0xf41b },
2443 { 0x1f, 0x0000 }
2444 };
2445
françois romieu4da19632011-01-03 15:07:55 +00002446 rtl_writephy(tp, 0x1f, 0x0001);
2447 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002448
françois romieu4da19632011-01-03 15:07:55 +00002449 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002450}
2451
françois romieu4da19632011-01-03 15:07:55 +00002452static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002453{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002454 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002455 { 0x1f, 0x0001 },
2456 { 0x10, 0xf41b },
2457 { 0x1f, 0x0000 }
2458 };
2459
françois romieu4da19632011-01-03 15:07:55 +00002460 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002461}
2462
françois romieu4da19632011-01-03 15:07:55 +00002463static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002464{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002465 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002466 { 0x1f, 0x0000 },
2467 { 0x1d, 0x0f00 },
2468 { 0x1f, 0x0002 },
2469 { 0x0c, 0x1ec8 },
2470 { 0x1f, 0x0000 }
2471 };
2472
françois romieu4da19632011-01-03 15:07:55 +00002473 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002474}
2475
françois romieu4da19632011-01-03 15:07:55 +00002476static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002477{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002478 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002479 { 0x1f, 0x0001 },
2480 { 0x1d, 0x3d98 },
2481 { 0x1f, 0x0000 }
2482 };
2483
françois romieu4da19632011-01-03 15:07:55 +00002484 rtl_writephy(tp, 0x1f, 0x0000);
2485 rtl_patchphy(tp, 0x14, 1 << 5);
2486 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002487
françois romieu4da19632011-01-03 15:07:55 +00002488 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002489}
2490
françois romieu4da19632011-01-03 15:07:55 +00002491static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002492{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002493 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002494 { 0x1f, 0x0001 },
2495 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002496 { 0x1f, 0x0002 },
2497 { 0x00, 0x88d4 },
2498 { 0x01, 0x82b1 },
2499 { 0x03, 0x7002 },
2500 { 0x08, 0x9e30 },
2501 { 0x09, 0x01f0 },
2502 { 0x0a, 0x5500 },
2503 { 0x0c, 0x00c8 },
2504 { 0x1f, 0x0003 },
2505 { 0x12, 0xc096 },
2506 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002507 { 0x1f, 0x0000 },
2508 { 0x1f, 0x0000 },
2509 { 0x09, 0x2000 },
2510 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002511 };
2512
françois romieu4da19632011-01-03 15:07:55 +00002513 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002514
françois romieu4da19632011-01-03 15:07:55 +00002515 rtl_patchphy(tp, 0x14, 1 << 5);
2516 rtl_patchphy(tp, 0x0d, 1 << 5);
2517 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002518}
2519
françois romieu4da19632011-01-03 15:07:55 +00002520static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002521{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002522 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002523 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002524 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002525 { 0x03, 0x802f },
2526 { 0x02, 0x4f02 },
2527 { 0x01, 0x0409 },
2528 { 0x00, 0xf099 },
2529 { 0x04, 0x9800 },
2530 { 0x04, 0x9000 },
2531 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002532 { 0x1f, 0x0002 },
2533 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002534 { 0x06, 0x0761 },
2535 { 0x1f, 0x0003 },
2536 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002537 { 0x1f, 0x0000 }
2538 };
2539
françois romieu4da19632011-01-03 15:07:55 +00002540 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002541
françois romieu4da19632011-01-03 15:07:55 +00002542 rtl_patchphy(tp, 0x16, 1 << 0);
2543 rtl_patchphy(tp, 0x14, 1 << 5);
2544 rtl_patchphy(tp, 0x0d, 1 << 5);
2545 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002546}
2547
françois romieu4da19632011-01-03 15:07:55 +00002548static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002549{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002550 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002551 { 0x1f, 0x0001 },
2552 { 0x12, 0x2300 },
2553 { 0x1d, 0x3d98 },
2554 { 0x1f, 0x0002 },
2555 { 0x0c, 0x7eb8 },
2556 { 0x06, 0x5461 },
2557 { 0x1f, 0x0003 },
2558 { 0x16, 0x0f0a },
2559 { 0x1f, 0x0000 }
2560 };
2561
françois romieu4da19632011-01-03 15:07:55 +00002562 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002563
françois romieu4da19632011-01-03 15:07:55 +00002564 rtl_patchphy(tp, 0x16, 1 << 0);
2565 rtl_patchphy(tp, 0x14, 1 << 5);
2566 rtl_patchphy(tp, 0x0d, 1 << 5);
2567 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002568}
2569
françois romieu4da19632011-01-03 15:07:55 +00002570static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002571{
françois romieu4da19632011-01-03 15:07:55 +00002572 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002573}
2574
françois romieubca03d52011-01-03 15:07:31 +00002575static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002576{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002577 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002578 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002579 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002580 { 0x06, 0x4064 },
2581 { 0x07, 0x2863 },
2582 { 0x08, 0x059c },
2583 { 0x09, 0x26b4 },
2584 { 0x0a, 0x6a19 },
2585 { 0x0b, 0xdcc8 },
2586 { 0x10, 0xf06d },
2587 { 0x14, 0x7f68 },
2588 { 0x18, 0x7fd9 },
2589 { 0x1c, 0xf0ff },
2590 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002591 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002592 { 0x12, 0xf49f },
2593 { 0x13, 0x070b },
2594 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002595 { 0x14, 0x94c0 },
2596
2597 /*
2598 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002599 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002600 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002601 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002602 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002603 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002604 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002605 { 0x06, 0x5561 },
2606
2607 /*
2608 * Can not link to 1Gbps with bad cable
2609 * Decrease SNR threshold form 21.07dB to 19.04dB
2610 */
2611 { 0x1f, 0x0001 },
2612 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002613
2614 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002615 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002616 };
françois romieubca03d52011-01-03 15:07:31 +00002617 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002618
françois romieu4da19632011-01-03 15:07:55 +00002619 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002620
françois romieubca03d52011-01-03 15:07:31 +00002621 /*
2622 * Rx Error Issue
2623 * Fine Tune Switching regulator parameter
2624 */
françois romieu4da19632011-01-03 15:07:55 +00002625 rtl_writephy(tp, 0x1f, 0x0002);
2626 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2627 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002628
françois romieudaf9df62009-10-07 12:44:20 +00002629 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002630 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002631 { 0x1f, 0x0002 },
2632 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002633 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002634 { 0x05, 0x8330 },
2635 { 0x06, 0x669a },
2636 { 0x1f, 0x0002 }
2637 };
2638 int val;
2639
françois romieu4da19632011-01-03 15:07:55 +00002640 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002641
françois romieu4da19632011-01-03 15:07:55 +00002642 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002643
2644 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002645 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002646 0x0065, 0x0066, 0x0067, 0x0068,
2647 0x0069, 0x006a, 0x006b, 0x006c
2648 };
2649 int i;
2650
françois romieu4da19632011-01-03 15:07:55 +00002651 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002652
2653 val &= 0xff00;
2654 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002655 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002656 }
2657 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002658 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002659 { 0x1f, 0x0002 },
2660 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002661 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002662 { 0x05, 0x8330 },
2663 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002664 };
2665
françois romieu4da19632011-01-03 15:07:55 +00002666 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002667 }
2668
françois romieubca03d52011-01-03 15:07:31 +00002669 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002670 rtl_writephy(tp, 0x1f, 0x0002);
2671 rtl_patchphy(tp, 0x0d, 0x0300);
2672 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002673
françois romieubca03d52011-01-03 15:07:31 +00002674 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002675 rtl_writephy(tp, 0x1f, 0x0002);
2676 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2677 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002678
françois romieu4da19632011-01-03 15:07:55 +00002679 rtl_writephy(tp, 0x1f, 0x0005);
2680 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002681
2682 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002683
françois romieu4da19632011-01-03 15:07:55 +00002684 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002685}
2686
françois romieubca03d52011-01-03 15:07:31 +00002687static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002688{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002689 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002690 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002691 { 0x1f, 0x0001 },
2692 { 0x06, 0x4064 },
2693 { 0x07, 0x2863 },
2694 { 0x08, 0x059c },
2695 { 0x09, 0x26b4 },
2696 { 0x0a, 0x6a19 },
2697 { 0x0b, 0xdcc8 },
2698 { 0x10, 0xf06d },
2699 { 0x14, 0x7f68 },
2700 { 0x18, 0x7fd9 },
2701 { 0x1c, 0xf0ff },
2702 { 0x1d, 0x3d9c },
2703 { 0x1f, 0x0003 },
2704 { 0x12, 0xf49f },
2705 { 0x13, 0x070b },
2706 { 0x1a, 0x05ad },
2707 { 0x14, 0x94c0 },
2708
françois romieubca03d52011-01-03 15:07:31 +00002709 /*
2710 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002711 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002712 */
françois romieudaf9df62009-10-07 12:44:20 +00002713 { 0x1f, 0x0002 },
2714 { 0x06, 0x5561 },
2715 { 0x1f, 0x0005 },
2716 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002717 { 0x06, 0x5561 },
2718
2719 /*
2720 * Can not link to 1Gbps with bad cable
2721 * Decrease SNR threshold form 21.07dB to 19.04dB
2722 */
2723 { 0x1f, 0x0001 },
2724 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002725
2726 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002727 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002728 };
françois romieubca03d52011-01-03 15:07:31 +00002729 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002730
françois romieu4da19632011-01-03 15:07:55 +00002731 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002732
2733 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002734 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002735 { 0x1f, 0x0002 },
2736 { 0x05, 0x669a },
2737 { 0x1f, 0x0005 },
2738 { 0x05, 0x8330 },
2739 { 0x06, 0x669a },
2740
2741 { 0x1f, 0x0002 }
2742 };
2743 int val;
2744
françois romieu4da19632011-01-03 15:07:55 +00002745 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002746
françois romieu4da19632011-01-03 15:07:55 +00002747 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002748 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002749 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002750 0x0065, 0x0066, 0x0067, 0x0068,
2751 0x0069, 0x006a, 0x006b, 0x006c
2752 };
2753 int i;
2754
françois romieu4da19632011-01-03 15:07:55 +00002755 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002756
2757 val &= 0xff00;
2758 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002759 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002760 }
2761 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002762 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002763 { 0x1f, 0x0002 },
2764 { 0x05, 0x2642 },
2765 { 0x1f, 0x0005 },
2766 { 0x05, 0x8330 },
2767 { 0x06, 0x2642 }
2768 };
2769
françois romieu4da19632011-01-03 15:07:55 +00002770 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002771 }
2772
françois romieubca03d52011-01-03 15:07:31 +00002773 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002774 rtl_writephy(tp, 0x1f, 0x0002);
2775 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2776 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002777
françois romieubca03d52011-01-03 15:07:31 +00002778 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002779 rtl_writephy(tp, 0x1f, 0x0002);
2780 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002781
françois romieu4da19632011-01-03 15:07:55 +00002782 rtl_writephy(tp, 0x1f, 0x0005);
2783 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002784
2785 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002786
françois romieu4da19632011-01-03 15:07:55 +00002787 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002788}
2789
françois romieu4da19632011-01-03 15:07:55 +00002790static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002791{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002792 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002793 { 0x1f, 0x0002 },
2794 { 0x10, 0x0008 },
2795 { 0x0d, 0x006c },
2796
2797 { 0x1f, 0x0000 },
2798 { 0x0d, 0xf880 },
2799
2800 { 0x1f, 0x0001 },
2801 { 0x17, 0x0cc0 },
2802
2803 { 0x1f, 0x0001 },
2804 { 0x0b, 0xa4d8 },
2805 { 0x09, 0x281c },
2806 { 0x07, 0x2883 },
2807 { 0x0a, 0x6b35 },
2808 { 0x1d, 0x3da4 },
2809 { 0x1c, 0xeffd },
2810 { 0x14, 0x7f52 },
2811 { 0x18, 0x7fc6 },
2812 { 0x08, 0x0601 },
2813 { 0x06, 0x4063 },
2814 { 0x10, 0xf074 },
2815 { 0x1f, 0x0003 },
2816 { 0x13, 0x0789 },
2817 { 0x12, 0xf4bd },
2818 { 0x1a, 0x04fd },
2819 { 0x14, 0x84b0 },
2820 { 0x1f, 0x0000 },
2821 { 0x00, 0x9200 },
2822
2823 { 0x1f, 0x0005 },
2824 { 0x01, 0x0340 },
2825 { 0x1f, 0x0001 },
2826 { 0x04, 0x4000 },
2827 { 0x03, 0x1d21 },
2828 { 0x02, 0x0c32 },
2829 { 0x01, 0x0200 },
2830 { 0x00, 0x5554 },
2831 { 0x04, 0x4800 },
2832 { 0x04, 0x4000 },
2833 { 0x04, 0xf000 },
2834 { 0x03, 0xdf01 },
2835 { 0x02, 0xdf20 },
2836 { 0x01, 0x101a },
2837 { 0x00, 0xa0ff },
2838 { 0x04, 0xf800 },
2839 { 0x04, 0xf000 },
2840 { 0x1f, 0x0000 },
2841
2842 { 0x1f, 0x0007 },
2843 { 0x1e, 0x0023 },
2844 { 0x16, 0x0000 },
2845 { 0x1f, 0x0000 }
2846 };
2847
françois romieu4da19632011-01-03 15:07:55 +00002848 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002849}
2850
françois romieue6de30d2011-01-03 15:08:37 +00002851static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2852{
2853 static const struct phy_reg phy_reg_init[] = {
2854 { 0x1f, 0x0001 },
2855 { 0x17, 0x0cc0 },
2856
2857 { 0x1f, 0x0007 },
2858 { 0x1e, 0x002d },
2859 { 0x18, 0x0040 },
2860 { 0x1f, 0x0000 }
2861 };
2862
2863 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2864 rtl_patchphy(tp, 0x0d, 1 << 5);
2865}
2866
Hayes Wang70090422011-07-06 15:58:06 +08002867static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002868{
2869 static const struct phy_reg phy_reg_init[] = {
2870 /* Enable Delay cap */
2871 { 0x1f, 0x0005 },
2872 { 0x05, 0x8b80 },
2873 { 0x06, 0xc896 },
2874 { 0x1f, 0x0000 },
2875
2876 /* Channel estimation fine tune */
2877 { 0x1f, 0x0001 },
2878 { 0x0b, 0x6c20 },
2879 { 0x07, 0x2872 },
2880 { 0x1c, 0xefff },
2881 { 0x1f, 0x0003 },
2882 { 0x14, 0x6420 },
2883 { 0x1f, 0x0000 },
2884
2885 /* Update PFM & 10M TX idle timer */
2886 { 0x1f, 0x0007 },
2887 { 0x1e, 0x002f },
2888 { 0x15, 0x1919 },
2889 { 0x1f, 0x0000 },
2890
2891 { 0x1f, 0x0007 },
2892 { 0x1e, 0x00ac },
2893 { 0x18, 0x0006 },
2894 { 0x1f, 0x0000 }
2895 };
2896
Francois Romieu15ecd032011-04-27 13:52:22 -07002897 rtl_apply_firmware(tp);
2898
hayeswang01dc7fe2011-03-21 01:50:28 +00002899 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2900
2901 /* DCO enable for 10M IDLE Power */
2902 rtl_writephy(tp, 0x1f, 0x0007);
2903 rtl_writephy(tp, 0x1e, 0x0023);
2904 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2905 rtl_writephy(tp, 0x1f, 0x0000);
2906
2907 /* For impedance matching */
2908 rtl_writephy(tp, 0x1f, 0x0002);
2909 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002910 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002911
2912 /* PHY auto speed down */
2913 rtl_writephy(tp, 0x1f, 0x0007);
2914 rtl_writephy(tp, 0x1e, 0x002d);
2915 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2916 rtl_writephy(tp, 0x1f, 0x0000);
2917 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2918
2919 rtl_writephy(tp, 0x1f, 0x0005);
2920 rtl_writephy(tp, 0x05, 0x8b86);
2921 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2922 rtl_writephy(tp, 0x1f, 0x0000);
2923
2924 rtl_writephy(tp, 0x1f, 0x0005);
2925 rtl_writephy(tp, 0x05, 0x8b85);
2926 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2927 rtl_writephy(tp, 0x1f, 0x0007);
2928 rtl_writephy(tp, 0x1e, 0x0020);
2929 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2930 rtl_writephy(tp, 0x1f, 0x0006);
2931 rtl_writephy(tp, 0x00, 0x5a00);
2932 rtl_writephy(tp, 0x1f, 0x0000);
2933 rtl_writephy(tp, 0x0d, 0x0007);
2934 rtl_writephy(tp, 0x0e, 0x003c);
2935 rtl_writephy(tp, 0x0d, 0x4007);
2936 rtl_writephy(tp, 0x0e, 0x0000);
2937 rtl_writephy(tp, 0x0d, 0x0000);
2938}
2939
Hayes Wang70090422011-07-06 15:58:06 +08002940static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2941{
2942 static const struct phy_reg phy_reg_init[] = {
2943 /* Enable Delay cap */
2944 { 0x1f, 0x0004 },
2945 { 0x1f, 0x0007 },
2946 { 0x1e, 0x00ac },
2947 { 0x18, 0x0006 },
2948 { 0x1f, 0x0002 },
2949 { 0x1f, 0x0000 },
2950 { 0x1f, 0x0000 },
2951
2952 /* Channel estimation fine tune */
2953 { 0x1f, 0x0003 },
2954 { 0x09, 0xa20f },
2955 { 0x1f, 0x0000 },
2956 { 0x1f, 0x0000 },
2957
2958 /* Green Setting */
2959 { 0x1f, 0x0005 },
2960 { 0x05, 0x8b5b },
2961 { 0x06, 0x9222 },
2962 { 0x05, 0x8b6d },
2963 { 0x06, 0x8000 },
2964 { 0x05, 0x8b76 },
2965 { 0x06, 0x8000 },
2966 { 0x1f, 0x0000 }
2967 };
2968
2969 rtl_apply_firmware(tp);
2970
2971 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2972
2973 /* For 4-corner performance improve */
2974 rtl_writephy(tp, 0x1f, 0x0005);
2975 rtl_writephy(tp, 0x05, 0x8b80);
2976 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2977 rtl_writephy(tp, 0x1f, 0x0000);
2978
2979 /* PHY auto speed down */
2980 rtl_writephy(tp, 0x1f, 0x0004);
2981 rtl_writephy(tp, 0x1f, 0x0007);
2982 rtl_writephy(tp, 0x1e, 0x002d);
2983 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2984 rtl_writephy(tp, 0x1f, 0x0002);
2985 rtl_writephy(tp, 0x1f, 0x0000);
2986 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2987
2988 /* improve 10M EEE waveform */
2989 rtl_writephy(tp, 0x1f, 0x0005);
2990 rtl_writephy(tp, 0x05, 0x8b86);
2991 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2992 rtl_writephy(tp, 0x1f, 0x0000);
2993
2994 /* Improve 2-pair detection performance */
2995 rtl_writephy(tp, 0x1f, 0x0005);
2996 rtl_writephy(tp, 0x05, 0x8b85);
2997 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
2998 rtl_writephy(tp, 0x1f, 0x0000);
2999
3000 /* EEE setting */
3001 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
3002 ERIAR_EXGMAC);
3003 rtl_writephy(tp, 0x1f, 0x0005);
3004 rtl_writephy(tp, 0x05, 0x8b85);
3005 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3006 rtl_writephy(tp, 0x1f, 0x0004);
3007 rtl_writephy(tp, 0x1f, 0x0007);
3008 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04003009 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003010 rtl_writephy(tp, 0x1f, 0x0002);
3011 rtl_writephy(tp, 0x1f, 0x0000);
3012 rtl_writephy(tp, 0x0d, 0x0007);
3013 rtl_writephy(tp, 0x0e, 0x003c);
3014 rtl_writephy(tp, 0x0d, 0x4007);
3015 rtl_writephy(tp, 0x0e, 0x0000);
3016 rtl_writephy(tp, 0x0d, 0x0000);
3017
3018 /* Green feature */
3019 rtl_writephy(tp, 0x1f, 0x0003);
3020 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3021 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3022 rtl_writephy(tp, 0x1f, 0x0000);
3023}
3024
Hayes Wang5f886e02012-03-30 14:33:03 +08003025static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3026{
3027 /* For 4-corner performance improve */
3028 rtl_writephy(tp, 0x1f, 0x0005);
3029 rtl_writephy(tp, 0x05, 0x8b80);
3030 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3031 rtl_writephy(tp, 0x1f, 0x0000);
3032
3033 /* PHY auto speed down */
3034 rtl_writephy(tp, 0x1f, 0x0007);
3035 rtl_writephy(tp, 0x1e, 0x002d);
3036 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3037 rtl_writephy(tp, 0x1f, 0x0000);
3038 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3039
3040 /* Improve 10M EEE waveform */
3041 rtl_writephy(tp, 0x1f, 0x0005);
3042 rtl_writephy(tp, 0x05, 0x8b86);
3043 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3044 rtl_writephy(tp, 0x1f, 0x0000);
3045}
3046
Hayes Wangc2218922011-09-06 16:55:18 +08003047static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3048{
3049 static const struct phy_reg phy_reg_init[] = {
3050 /* Channel estimation fine tune */
3051 { 0x1f, 0x0003 },
3052 { 0x09, 0xa20f },
3053 { 0x1f, 0x0000 },
3054
3055 /* Modify green table for giga & fnet */
3056 { 0x1f, 0x0005 },
3057 { 0x05, 0x8b55 },
3058 { 0x06, 0x0000 },
3059 { 0x05, 0x8b5e },
3060 { 0x06, 0x0000 },
3061 { 0x05, 0x8b67 },
3062 { 0x06, 0x0000 },
3063 { 0x05, 0x8b70 },
3064 { 0x06, 0x0000 },
3065 { 0x1f, 0x0000 },
3066 { 0x1f, 0x0007 },
3067 { 0x1e, 0x0078 },
3068 { 0x17, 0x0000 },
3069 { 0x19, 0x00fb },
3070 { 0x1f, 0x0000 },
3071
3072 /* Modify green table for 10M */
3073 { 0x1f, 0x0005 },
3074 { 0x05, 0x8b79 },
3075 { 0x06, 0xaa00 },
3076 { 0x1f, 0x0000 },
3077
3078 /* Disable hiimpedance detection (RTCT) */
3079 { 0x1f, 0x0003 },
3080 { 0x01, 0x328a },
3081 { 0x1f, 0x0000 }
3082 };
3083
3084 rtl_apply_firmware(tp);
3085
3086 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3087
Hayes Wang5f886e02012-03-30 14:33:03 +08003088 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003089
3090 /* Improve 2-pair detection performance */
3091 rtl_writephy(tp, 0x1f, 0x0005);
3092 rtl_writephy(tp, 0x05, 0x8b85);
3093 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3094 rtl_writephy(tp, 0x1f, 0x0000);
3095}
3096
3097static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3098{
3099 rtl_apply_firmware(tp);
3100
Hayes Wang5f886e02012-03-30 14:33:03 +08003101 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003102}
3103
françois romieu4da19632011-01-03 15:07:55 +00003104static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003105{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003106 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003107 { 0x1f, 0x0003 },
3108 { 0x08, 0x441d },
3109 { 0x01, 0x9100 },
3110 { 0x1f, 0x0000 }
3111 };
3112
françois romieu4da19632011-01-03 15:07:55 +00003113 rtl_writephy(tp, 0x1f, 0x0000);
3114 rtl_patchphy(tp, 0x11, 1 << 12);
3115 rtl_patchphy(tp, 0x19, 1 << 13);
3116 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003117
françois romieu4da19632011-01-03 15:07:55 +00003118 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003119}
3120
Hayes Wang5a5e4442011-02-22 17:26:21 +08003121static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3122{
3123 static const struct phy_reg phy_reg_init[] = {
3124 { 0x1f, 0x0005 },
3125 { 0x1a, 0x0000 },
3126 { 0x1f, 0x0000 },
3127
3128 { 0x1f, 0x0004 },
3129 { 0x1c, 0x0000 },
3130 { 0x1f, 0x0000 },
3131
3132 { 0x1f, 0x0001 },
3133 { 0x15, 0x7701 },
3134 { 0x1f, 0x0000 }
3135 };
3136
3137 /* Disable ALDPS before ram code */
3138 rtl_writephy(tp, 0x1f, 0x0000);
3139 rtl_writephy(tp, 0x18, 0x0310);
3140 msleep(100);
3141
François Romieu953a12c2011-04-24 17:38:48 +02003142 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003143
3144 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3145}
3146
Hayes Wang7e18dca2012-03-30 14:33:02 +08003147static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3148{
3149 void __iomem *ioaddr = tp->mmio_addr;
3150
3151 /* Disable ALDPS before setting firmware */
3152 rtl_writephy(tp, 0x1f, 0x0000);
3153 rtl_writephy(tp, 0x18, 0x0310);
3154 msleep(20);
3155
3156 rtl_apply_firmware(tp);
3157
3158 /* EEE setting */
3159 rtl_eri_write(ioaddr, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3160 rtl_writephy(tp, 0x1f, 0x0004);
3161 rtl_writephy(tp, 0x10, 0x401f);
3162 rtl_writephy(tp, 0x19, 0x7030);
3163 rtl_writephy(tp, 0x1f, 0x0000);
3164}
3165
Francois Romieu5615d9f2007-08-17 17:50:46 +02003166static void rtl_hw_phy_config(struct net_device *dev)
3167{
3168 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003169
3170 rtl8169_print_mac_version(tp);
3171
3172 switch (tp->mac_version) {
3173 case RTL_GIGA_MAC_VER_01:
3174 break;
3175 case RTL_GIGA_MAC_VER_02:
3176 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003177 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003178 break;
3179 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003180 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003181 break;
françois romieu2e9558562009-08-10 19:44:19 +00003182 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003183 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003184 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003185 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003186 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003187 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003188 case RTL_GIGA_MAC_VER_07:
3189 case RTL_GIGA_MAC_VER_08:
3190 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003191 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003192 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003193 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003194 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003195 break;
3196 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003197 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003198 break;
3199 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003200 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003201 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003202 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003203 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003204 break;
3205 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003206 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003207 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003208 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003209 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003210 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003211 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003212 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003213 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003214 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003215 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003216 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003217 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003218 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003219 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003220 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003221 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003222 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003223 break;
3224 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003225 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003226 break;
3227 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003228 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003229 break;
françois romieue6de30d2011-01-03 15:08:37 +00003230 case RTL_GIGA_MAC_VER_28:
3231 rtl8168d_4_hw_phy_config(tp);
3232 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003233 case RTL_GIGA_MAC_VER_29:
3234 case RTL_GIGA_MAC_VER_30:
3235 rtl8105e_hw_phy_config(tp);
3236 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003237 case RTL_GIGA_MAC_VER_31:
3238 /* None. */
3239 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003240 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003241 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003242 rtl8168e_1_hw_phy_config(tp);
3243 break;
3244 case RTL_GIGA_MAC_VER_34:
3245 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003246 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003247 case RTL_GIGA_MAC_VER_35:
3248 rtl8168f_1_hw_phy_config(tp);
3249 break;
3250 case RTL_GIGA_MAC_VER_36:
3251 rtl8168f_2_hw_phy_config(tp);
3252 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003253
Hayes Wang7e18dca2012-03-30 14:33:02 +08003254 case RTL_GIGA_MAC_VER_37:
3255 rtl8402_hw_phy_config(tp);
3256 break;
3257
Francois Romieu5615d9f2007-08-17 17:50:46 +02003258 default:
3259 break;
3260 }
3261}
3262
Francois Romieuda78dbf2012-01-26 14:18:23 +01003263static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 struct timer_list *timer = &tp->timer;
3266 void __iomem *ioaddr = tp->mmio_addr;
3267 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3268
Francois Romieubcf0bf92006-07-26 23:14:13 +02003269 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
françois romieu4da19632011-01-03 15:07:55 +00003271 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003272 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 * A busy loop could burn quite a few cycles on nowadays CPU.
3274 * Let's delay the execution of the timer for a few ticks.
3275 */
3276 timeout = HZ/10;
3277 goto out_mod_timer;
3278 }
3279
3280 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01003281 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Francois Romieuda78dbf2012-01-26 14:18:23 +01003283 netif_warn(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
françois romieu4da19632011-01-03 15:07:55 +00003285 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
3287out_mod_timer:
3288 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003289}
3290
3291static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3292{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003293 if (!test_and_set_bit(flag, tp->wk.flags))
3294 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003295}
3296
3297static void rtl8169_phy_timer(unsigned long __opaque)
3298{
3299 struct net_device *dev = (struct net_device *)__opaque;
3300 struct rtl8169_private *tp = netdev_priv(dev);
3301
Francois Romieu98ddf982012-01-31 10:47:34 +01003302 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303}
3304
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3306 void __iomem *ioaddr)
3307{
3308 iounmap(ioaddr);
3309 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003310 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 pci_disable_device(pdev);
3312 free_netdev(dev);
3313}
3314
Francois Romieubf793292006-11-01 00:53:05 +01003315static void rtl8169_phy_reset(struct net_device *dev,
3316 struct rtl8169_private *tp)
3317{
Francois Romieu07d3f512007-02-21 22:40:46 +01003318 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003319
françois romieu4da19632011-01-03 15:07:55 +00003320 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003321 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003322 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003323 return;
3324 msleep(1);
3325 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003326 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003327}
3328
David S. Miller8decf862011-09-22 03:23:13 -04003329static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3330{
3331 void __iomem *ioaddr = tp->mmio_addr;
3332
3333 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3334 (RTL_R8(PHYstatus) & TBI_Enable);
3335}
3336
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003337static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003339 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003340
Francois Romieu5615d9f2007-08-17 17:50:46 +02003341 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003342
Marcus Sundberg773328942008-07-10 21:28:08 +02003343 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3344 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3345 RTL_W8(0x82, 0x01);
3346 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003347
Francois Romieu6dccd162007-02-13 23:38:05 +01003348 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3349
3350 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3351 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003352
Francois Romieubcf0bf92006-07-26 23:14:13 +02003353 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003354 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3355 RTL_W8(0x82, 0x01);
3356 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003357 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003358 }
3359
Francois Romieubf793292006-11-01 00:53:05 +01003360 rtl8169_phy_reset(dev, tp);
3361
Oliver Neukum54405cd2011-01-06 21:55:13 +01003362 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003363 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3364 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3365 (tp->mii.supports_gmii ?
3366 ADVERTISED_1000baseT_Half |
3367 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003368
David S. Miller8decf862011-09-22 03:23:13 -04003369 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003370 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003371}
3372
Francois Romieu773d2022007-01-31 23:47:43 +01003373static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3374{
3375 void __iomem *ioaddr = tp->mmio_addr;
3376 u32 high;
3377 u32 low;
3378
3379 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3380 high = addr[4] | (addr[5] << 8);
3381
Francois Romieuda78dbf2012-01-26 14:18:23 +01003382 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003383
3384 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003385
Francois Romieu773d2022007-01-31 23:47:43 +01003386 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00003387 RTL_R32(MAC4);
3388
Francois Romieu78f1cd02010-03-27 19:35:46 -07003389 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00003390 RTL_R32(MAC0);
3391
françois romieuc28aa382011-08-02 03:53:43 +00003392 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3393 const struct exgmac_reg e[] = {
3394 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3395 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3396 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3397 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3398 low >> 16 },
3399 };
3400
3401 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3402 }
3403
Francois Romieu773d2022007-01-31 23:47:43 +01003404 RTL_W8(Cfg9346, Cfg9346_Lock);
3405
Francois Romieuda78dbf2012-01-26 14:18:23 +01003406 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003407}
3408
3409static int rtl_set_mac_address(struct net_device *dev, void *p)
3410{
3411 struct rtl8169_private *tp = netdev_priv(dev);
3412 struct sockaddr *addr = p;
3413
3414 if (!is_valid_ether_addr(addr->sa_data))
3415 return -EADDRNOTAVAIL;
3416
3417 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3418
3419 rtl_rar_set(tp, dev->dev_addr);
3420
3421 return 0;
3422}
3423
Francois Romieu5f787a12006-08-17 13:02:36 +02003424static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3425{
3426 struct rtl8169_private *tp = netdev_priv(dev);
3427 struct mii_ioctl_data *data = if_mii(ifr);
3428
Francois Romieu8b4ab282008-11-19 22:05:25 -08003429 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3430}
Francois Romieu5f787a12006-08-17 13:02:36 +02003431
Francois Romieucecb5fd2011-04-01 10:21:07 +02003432static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3433 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003434{
Francois Romieu5f787a12006-08-17 13:02:36 +02003435 switch (cmd) {
3436 case SIOCGMIIPHY:
3437 data->phy_id = 32; /* Internal PHY */
3438 return 0;
3439
3440 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003441 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003442 return 0;
3443
3444 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003445 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003446 return 0;
3447 }
3448 return -EOPNOTSUPP;
3449}
3450
Francois Romieu8b4ab282008-11-19 22:05:25 -08003451static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3452{
3453 return -EOPNOTSUPP;
3454}
3455
Francois Romieufbac58f2007-10-04 22:51:38 +02003456static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3457{
3458 if (tp->features & RTL_FEATURE_MSI) {
3459 pci_disable_msi(pdev);
3460 tp->features &= ~RTL_FEATURE_MSI;
3461 }
3462}
3463
françois romieuc0e45c12011-01-03 15:08:04 +00003464static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3465{
3466 struct mdio_ops *ops = &tp->mdio_ops;
3467
3468 switch (tp->mac_version) {
3469 case RTL_GIGA_MAC_VER_27:
3470 ops->write = r8168dp_1_mdio_write;
3471 ops->read = r8168dp_1_mdio_read;
3472 break;
françois romieue6de30d2011-01-03 15:08:37 +00003473 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003474 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003475 ops->write = r8168dp_2_mdio_write;
3476 ops->read = r8168dp_2_mdio_read;
3477 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003478 default:
3479 ops->write = r8169_mdio_write;
3480 ops->read = r8169_mdio_read;
3481 break;
3482 }
3483}
3484
David S. Miller1805b2f2011-10-24 18:18:09 -04003485static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3486{
3487 void __iomem *ioaddr = tp->mmio_addr;
3488
3489 switch (tp->mac_version) {
3490 case RTL_GIGA_MAC_VER_29:
3491 case RTL_GIGA_MAC_VER_30:
3492 case RTL_GIGA_MAC_VER_32:
3493 case RTL_GIGA_MAC_VER_33:
3494 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08003495 case RTL_GIGA_MAC_VER_37:
David S. Miller1805b2f2011-10-24 18:18:09 -04003496 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3497 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3498 break;
3499 default:
3500 break;
3501 }
3502}
3503
3504static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3505{
3506 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3507 return false;
3508
3509 rtl_writephy(tp, 0x1f, 0x0000);
3510 rtl_writephy(tp, MII_BMCR, 0x0000);
3511
3512 rtl_wol_suspend_quirk(tp);
3513
3514 return true;
3515}
3516
françois romieu065c27c2011-01-03 15:08:12 +00003517static void r810x_phy_power_down(struct rtl8169_private *tp)
3518{
3519 rtl_writephy(tp, 0x1f, 0x0000);
3520 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3521}
3522
3523static void r810x_phy_power_up(struct rtl8169_private *tp)
3524{
3525 rtl_writephy(tp, 0x1f, 0x0000);
3526 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3527}
3528
3529static void r810x_pll_power_down(struct rtl8169_private *tp)
3530{
Hayes Wang00042992012-03-30 14:33:00 +08003531 void __iomem *ioaddr = tp->mmio_addr;
3532
David S. Miller1805b2f2011-10-24 18:18:09 -04003533 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003534 return;
françois romieu065c27c2011-01-03 15:08:12 +00003535
3536 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003537
3538 switch (tp->mac_version) {
3539 case RTL_GIGA_MAC_VER_07:
3540 case RTL_GIGA_MAC_VER_08:
3541 case RTL_GIGA_MAC_VER_09:
3542 case RTL_GIGA_MAC_VER_10:
3543 case RTL_GIGA_MAC_VER_13:
3544 case RTL_GIGA_MAC_VER_16:
3545 break;
3546 default:
3547 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3548 break;
3549 }
françois romieu065c27c2011-01-03 15:08:12 +00003550}
3551
3552static void r810x_pll_power_up(struct rtl8169_private *tp)
3553{
Hayes Wang00042992012-03-30 14:33:00 +08003554 void __iomem *ioaddr = tp->mmio_addr;
3555
françois romieu065c27c2011-01-03 15:08:12 +00003556 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003557
3558 switch (tp->mac_version) {
3559 case RTL_GIGA_MAC_VER_07:
3560 case RTL_GIGA_MAC_VER_08:
3561 case RTL_GIGA_MAC_VER_09:
3562 case RTL_GIGA_MAC_VER_10:
3563 case RTL_GIGA_MAC_VER_13:
3564 case RTL_GIGA_MAC_VER_16:
3565 break;
3566 default:
3567 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3568 break;
3569 }
françois romieu065c27c2011-01-03 15:08:12 +00003570}
3571
3572static void r8168_phy_power_up(struct rtl8169_private *tp)
3573{
3574 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003575 switch (tp->mac_version) {
3576 case RTL_GIGA_MAC_VER_11:
3577 case RTL_GIGA_MAC_VER_12:
3578 case RTL_GIGA_MAC_VER_17:
3579 case RTL_GIGA_MAC_VER_18:
3580 case RTL_GIGA_MAC_VER_19:
3581 case RTL_GIGA_MAC_VER_20:
3582 case RTL_GIGA_MAC_VER_21:
3583 case RTL_GIGA_MAC_VER_22:
3584 case RTL_GIGA_MAC_VER_23:
3585 case RTL_GIGA_MAC_VER_24:
3586 case RTL_GIGA_MAC_VER_25:
3587 case RTL_GIGA_MAC_VER_26:
3588 case RTL_GIGA_MAC_VER_27:
3589 case RTL_GIGA_MAC_VER_28:
3590 case RTL_GIGA_MAC_VER_31:
3591 rtl_writephy(tp, 0x0e, 0x0000);
3592 break;
3593 default:
3594 break;
3595 }
françois romieu065c27c2011-01-03 15:08:12 +00003596 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3597}
3598
3599static void r8168_phy_power_down(struct rtl8169_private *tp)
3600{
3601 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003602 switch (tp->mac_version) {
3603 case RTL_GIGA_MAC_VER_32:
3604 case RTL_GIGA_MAC_VER_33:
3605 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3606 break;
3607
3608 case RTL_GIGA_MAC_VER_11:
3609 case RTL_GIGA_MAC_VER_12:
3610 case RTL_GIGA_MAC_VER_17:
3611 case RTL_GIGA_MAC_VER_18:
3612 case RTL_GIGA_MAC_VER_19:
3613 case RTL_GIGA_MAC_VER_20:
3614 case RTL_GIGA_MAC_VER_21:
3615 case RTL_GIGA_MAC_VER_22:
3616 case RTL_GIGA_MAC_VER_23:
3617 case RTL_GIGA_MAC_VER_24:
3618 case RTL_GIGA_MAC_VER_25:
3619 case RTL_GIGA_MAC_VER_26:
3620 case RTL_GIGA_MAC_VER_27:
3621 case RTL_GIGA_MAC_VER_28:
3622 case RTL_GIGA_MAC_VER_31:
3623 rtl_writephy(tp, 0x0e, 0x0200);
3624 default:
3625 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3626 break;
3627 }
françois romieu065c27c2011-01-03 15:08:12 +00003628}
3629
3630static void r8168_pll_power_down(struct rtl8169_private *tp)
3631{
3632 void __iomem *ioaddr = tp->mmio_addr;
3633
Francois Romieucecb5fd2011-04-01 10:21:07 +02003634 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3635 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3636 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003637 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003638 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003639 }
françois romieu065c27c2011-01-03 15:08:12 +00003640
Francois Romieucecb5fd2011-04-01 10:21:07 +02003641 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3642 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003643 (RTL_R16(CPlusCmd) & ASF)) {
3644 return;
3645 }
3646
hayeswang01dc7fe2011-03-21 01:50:28 +00003647 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3648 tp->mac_version == RTL_GIGA_MAC_VER_33)
3649 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3650
David S. Miller1805b2f2011-10-24 18:18:09 -04003651 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003652 return;
françois romieu065c27c2011-01-03 15:08:12 +00003653
3654 r8168_phy_power_down(tp);
3655
3656 switch (tp->mac_version) {
3657 case RTL_GIGA_MAC_VER_25:
3658 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003659 case RTL_GIGA_MAC_VER_27:
3660 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003661 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003662 case RTL_GIGA_MAC_VER_32:
3663 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003664 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3665 break;
3666 }
3667}
3668
3669static void r8168_pll_power_up(struct rtl8169_private *tp)
3670{
3671 void __iomem *ioaddr = tp->mmio_addr;
3672
françois romieu065c27c2011-01-03 15:08:12 +00003673 switch (tp->mac_version) {
3674 case RTL_GIGA_MAC_VER_25:
3675 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003676 case RTL_GIGA_MAC_VER_27:
3677 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003678 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003679 case RTL_GIGA_MAC_VER_32:
3680 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003681 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3682 break;
3683 }
3684
3685 r8168_phy_power_up(tp);
3686}
3687
Francois Romieud58d46b2011-05-03 16:38:29 +02003688static void rtl_generic_op(struct rtl8169_private *tp,
3689 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00003690{
3691 if (op)
3692 op(tp);
3693}
3694
3695static void rtl_pll_power_down(struct rtl8169_private *tp)
3696{
Francois Romieud58d46b2011-05-03 16:38:29 +02003697 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00003698}
3699
3700static void rtl_pll_power_up(struct rtl8169_private *tp)
3701{
Francois Romieud58d46b2011-05-03 16:38:29 +02003702 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00003703}
3704
3705static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3706{
3707 struct pll_power_ops *ops = &tp->pll_power_ops;
3708
3709 switch (tp->mac_version) {
3710 case RTL_GIGA_MAC_VER_07:
3711 case RTL_GIGA_MAC_VER_08:
3712 case RTL_GIGA_MAC_VER_09:
3713 case RTL_GIGA_MAC_VER_10:
3714 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003715 case RTL_GIGA_MAC_VER_29:
3716 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08003717 case RTL_GIGA_MAC_VER_37:
françois romieu065c27c2011-01-03 15:08:12 +00003718 ops->down = r810x_pll_power_down;
3719 ops->up = r810x_pll_power_up;
3720 break;
3721
3722 case RTL_GIGA_MAC_VER_11:
3723 case RTL_GIGA_MAC_VER_12:
3724 case RTL_GIGA_MAC_VER_17:
3725 case RTL_GIGA_MAC_VER_18:
3726 case RTL_GIGA_MAC_VER_19:
3727 case RTL_GIGA_MAC_VER_20:
3728 case RTL_GIGA_MAC_VER_21:
3729 case RTL_GIGA_MAC_VER_22:
3730 case RTL_GIGA_MAC_VER_23:
3731 case RTL_GIGA_MAC_VER_24:
3732 case RTL_GIGA_MAC_VER_25:
3733 case RTL_GIGA_MAC_VER_26:
3734 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003735 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003736 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003737 case RTL_GIGA_MAC_VER_32:
3738 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003739 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08003740 case RTL_GIGA_MAC_VER_35:
3741 case RTL_GIGA_MAC_VER_36:
françois romieu065c27c2011-01-03 15:08:12 +00003742 ops->down = r8168_pll_power_down;
3743 ops->up = r8168_pll_power_up;
3744 break;
3745
3746 default:
3747 ops->down = NULL;
3748 ops->up = NULL;
3749 break;
3750 }
3751}
3752
Hayes Wange542a222011-07-06 15:58:04 +08003753static void rtl_init_rxcfg(struct rtl8169_private *tp)
3754{
3755 void __iomem *ioaddr = tp->mmio_addr;
3756
3757 switch (tp->mac_version) {
3758 case RTL_GIGA_MAC_VER_01:
3759 case RTL_GIGA_MAC_VER_02:
3760 case RTL_GIGA_MAC_VER_03:
3761 case RTL_GIGA_MAC_VER_04:
3762 case RTL_GIGA_MAC_VER_05:
3763 case RTL_GIGA_MAC_VER_06:
3764 case RTL_GIGA_MAC_VER_10:
3765 case RTL_GIGA_MAC_VER_11:
3766 case RTL_GIGA_MAC_VER_12:
3767 case RTL_GIGA_MAC_VER_13:
3768 case RTL_GIGA_MAC_VER_14:
3769 case RTL_GIGA_MAC_VER_15:
3770 case RTL_GIGA_MAC_VER_16:
3771 case RTL_GIGA_MAC_VER_17:
3772 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3773 break;
3774 case RTL_GIGA_MAC_VER_18:
3775 case RTL_GIGA_MAC_VER_19:
3776 case RTL_GIGA_MAC_VER_20:
3777 case RTL_GIGA_MAC_VER_21:
3778 case RTL_GIGA_MAC_VER_22:
3779 case RTL_GIGA_MAC_VER_23:
3780 case RTL_GIGA_MAC_VER_24:
3781 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3782 break;
3783 default:
3784 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3785 break;
3786 }
3787}
3788
Hayes Wang92fc43b2011-07-06 15:58:03 +08003789static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3790{
3791 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3792}
3793
Francois Romieud58d46b2011-05-03 16:38:29 +02003794static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3795{
françois romieu9c5028e2012-03-02 04:43:14 +00003796 void __iomem *ioaddr = tp->mmio_addr;
3797
3798 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003799 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00003800 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003801}
3802
3803static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3804{
françois romieu9c5028e2012-03-02 04:43:14 +00003805 void __iomem *ioaddr = tp->mmio_addr;
3806
3807 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003808 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00003809 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003810}
3811
3812static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3813{
3814 void __iomem *ioaddr = tp->mmio_addr;
3815
3816 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3817 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3818 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3819}
3820
3821static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3822{
3823 void __iomem *ioaddr = tp->mmio_addr;
3824
3825 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3826 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3827 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3828}
3829
3830static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3831{
3832 void __iomem *ioaddr = tp->mmio_addr;
3833
3834 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3835}
3836
3837static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3838{
3839 void __iomem *ioaddr = tp->mmio_addr;
3840
3841 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3842}
3843
3844static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3845{
3846 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003847
3848 RTL_W8(MaxTxPacketSize, 0x3f);
3849 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3850 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003851 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003852}
3853
3854static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3855{
3856 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003857
3858 RTL_W8(MaxTxPacketSize, 0x0c);
3859 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3860 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003861 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003862}
3863
3864static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3865{
3866 rtl_tx_performance_tweak(tp->pci_dev,
3867 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3868}
3869
3870static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3871{
3872 rtl_tx_performance_tweak(tp->pci_dev,
3873 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3874}
3875
3876static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3877{
3878 void __iomem *ioaddr = tp->mmio_addr;
3879
3880 r8168b_0_hw_jumbo_enable(tp);
3881
3882 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
3883}
3884
3885static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3886{
3887 void __iomem *ioaddr = tp->mmio_addr;
3888
3889 r8168b_0_hw_jumbo_disable(tp);
3890
3891 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3892}
3893
3894static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
3895{
3896 struct jumbo_ops *ops = &tp->jumbo_ops;
3897
3898 switch (tp->mac_version) {
3899 case RTL_GIGA_MAC_VER_11:
3900 ops->disable = r8168b_0_hw_jumbo_disable;
3901 ops->enable = r8168b_0_hw_jumbo_enable;
3902 break;
3903 case RTL_GIGA_MAC_VER_12:
3904 case RTL_GIGA_MAC_VER_17:
3905 ops->disable = r8168b_1_hw_jumbo_disable;
3906 ops->enable = r8168b_1_hw_jumbo_enable;
3907 break;
3908 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
3909 case RTL_GIGA_MAC_VER_19:
3910 case RTL_GIGA_MAC_VER_20:
3911 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
3912 case RTL_GIGA_MAC_VER_22:
3913 case RTL_GIGA_MAC_VER_23:
3914 case RTL_GIGA_MAC_VER_24:
3915 case RTL_GIGA_MAC_VER_25:
3916 case RTL_GIGA_MAC_VER_26:
3917 ops->disable = r8168c_hw_jumbo_disable;
3918 ops->enable = r8168c_hw_jumbo_enable;
3919 break;
3920 case RTL_GIGA_MAC_VER_27:
3921 case RTL_GIGA_MAC_VER_28:
3922 ops->disable = r8168dp_hw_jumbo_disable;
3923 ops->enable = r8168dp_hw_jumbo_enable;
3924 break;
3925 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
3926 case RTL_GIGA_MAC_VER_32:
3927 case RTL_GIGA_MAC_VER_33:
3928 case RTL_GIGA_MAC_VER_34:
3929 ops->disable = r8168e_hw_jumbo_disable;
3930 ops->enable = r8168e_hw_jumbo_enable;
3931 break;
3932
3933 /*
3934 * No action needed for jumbo frames with 8169.
3935 * No jumbo for 810x at all.
3936 */
3937 default:
3938 ops->disable = NULL;
3939 ops->enable = NULL;
3940 break;
3941 }
3942}
3943
Francois Romieu6f43adc2011-04-29 15:05:51 +02003944static void rtl_hw_reset(struct rtl8169_private *tp)
3945{
3946 void __iomem *ioaddr = tp->mmio_addr;
3947 int i;
3948
3949 /* Soft reset the chip. */
3950 RTL_W8(ChipCmd, CmdReset);
3951
3952 /* Check that the chip has finished the reset. */
3953 for (i = 0; i < 100; i++) {
3954 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3955 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003956 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003957 }
3958}
3959
Francois Romieub6ffd972011-06-17 17:00:05 +02003960static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
3961{
3962 struct rtl_fw *rtl_fw;
3963 const char *name;
3964 int rc = -ENOMEM;
3965
3966 name = rtl_lookup_firmware_name(tp);
3967 if (!name)
3968 goto out_no_firmware;
3969
3970 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
3971 if (!rtl_fw)
3972 goto err_warn;
3973
3974 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
3975 if (rc < 0)
3976 goto err_free;
3977
Francois Romieufd112f22011-06-18 00:10:29 +02003978 rc = rtl_check_firmware(tp, rtl_fw);
3979 if (rc < 0)
3980 goto err_release_firmware;
3981
Francois Romieub6ffd972011-06-17 17:00:05 +02003982 tp->rtl_fw = rtl_fw;
3983out:
3984 return;
3985
Francois Romieufd112f22011-06-18 00:10:29 +02003986err_release_firmware:
3987 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02003988err_free:
3989 kfree(rtl_fw);
3990err_warn:
3991 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
3992 name, rc);
3993out_no_firmware:
3994 tp->rtl_fw = NULL;
3995 goto out;
3996}
3997
François Romieu953a12c2011-04-24 17:38:48 +02003998static void rtl_request_firmware(struct rtl8169_private *tp)
3999{
Francois Romieub6ffd972011-06-17 17:00:05 +02004000 if (IS_ERR(tp->rtl_fw))
4001 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004002}
4003
Hayes Wang92fc43b2011-07-06 15:58:03 +08004004static void rtl_rx_close(struct rtl8169_private *tp)
4005{
4006 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004007
Francois Romieu1687b562011-07-19 17:21:29 +02004008 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004009}
4010
françois romieue6de30d2011-01-03 15:08:37 +00004011static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012{
françois romieue6de30d2011-01-03 15:08:37 +00004013 void __iomem *ioaddr = tp->mmio_addr;
4014
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004016 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017
Hayes Wang92fc43b2011-07-06 15:58:03 +08004018 rtl_rx_close(tp);
4019
Hayes Wang5d2e1952011-02-22 17:26:22 +08004020 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004021 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4022 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00004023 while (RTL_R8(TxPoll) & NPQ)
4024 udelay(20);
Hayes Wangc2218922011-09-06 16:55:18 +08004025 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4026 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
Hayes Wang7e18dca2012-03-30 14:33:02 +08004027 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
4028 tp->mac_version == RTL_GIGA_MAC_VER_37) {
David S. Miller8decf862011-09-22 03:23:13 -04004029 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08004030 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4031 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004032 } else {
4033 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4034 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004035 }
4036
Hayes Wang92fc43b2011-07-06 15:58:03 +08004037 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038}
4039
Francois Romieu7f796d832007-06-11 23:04:41 +02004040static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004041{
4042 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004043
4044 /* Set DMA burst size and Interframe Gap Time */
4045 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4046 (InterFrameGap << TxInterFrameGapShift));
4047}
4048
Francois Romieu07ce4062007-02-23 23:36:39 +01004049static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050{
4051 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
Francois Romieu07ce4062007-02-23 23:36:39 +01004053 tp->hw_start(dev);
4054
Francois Romieuda78dbf2012-01-26 14:18:23 +01004055 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004056}
4057
Francois Romieu7f796d832007-06-11 23:04:41 +02004058static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4059 void __iomem *ioaddr)
4060{
4061 /*
4062 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4063 * register to be written before TxDescAddrLow to work.
4064 * Switching from MMIO to I/O access fixes the issue as well.
4065 */
4066 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004067 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004068 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004069 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004070}
4071
4072static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4073{
4074 u16 cmd;
4075
4076 cmd = RTL_R16(CPlusCmd);
4077 RTL_W16(CPlusCmd, cmd);
4078 return cmd;
4079}
4080
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004081static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004082{
4083 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004084 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004085}
4086
Francois Romieu6dccd162007-02-13 23:38:05 +01004087static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4088{
Francois Romieu37441002011-06-17 22:58:54 +02004089 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004090 u32 mac_version;
4091 u32 clk;
4092 u32 val;
4093 } cfg2_info [] = {
4094 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4095 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4096 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4097 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004098 };
4099 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004100 unsigned int i;
4101 u32 clk;
4102
4103 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004104 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004105 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4106 RTL_W32(0x7c, p->val);
4107 break;
4108 }
4109 }
4110}
4111
Francois Romieue6b763e2012-03-08 09:35:39 +01004112static void rtl_set_rx_mode(struct net_device *dev)
4113{
4114 struct rtl8169_private *tp = netdev_priv(dev);
4115 void __iomem *ioaddr = tp->mmio_addr;
4116 u32 mc_filter[2]; /* Multicast hash filter */
4117 int rx_mode;
4118 u32 tmp = 0;
4119
4120 if (dev->flags & IFF_PROMISC) {
4121 /* Unconditionally log net taps. */
4122 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4123 rx_mode =
4124 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4125 AcceptAllPhys;
4126 mc_filter[1] = mc_filter[0] = 0xffffffff;
4127 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4128 (dev->flags & IFF_ALLMULTI)) {
4129 /* Too many to filter perfectly -- accept all multicasts. */
4130 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4131 mc_filter[1] = mc_filter[0] = 0xffffffff;
4132 } else {
4133 struct netdev_hw_addr *ha;
4134
4135 rx_mode = AcceptBroadcast | AcceptMyPhys;
4136 mc_filter[1] = mc_filter[0] = 0;
4137 netdev_for_each_mc_addr(ha, dev) {
4138 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4139 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4140 rx_mode |= AcceptMulticast;
4141 }
4142 }
4143
4144 if (dev->features & NETIF_F_RXALL)
4145 rx_mode |= (AcceptErr | AcceptRunt);
4146
4147 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4148
4149 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4150 u32 data = mc_filter[0];
4151
4152 mc_filter[0] = swab32(mc_filter[1]);
4153 mc_filter[1] = swab32(data);
4154 }
4155
4156 RTL_W32(MAR0 + 4, mc_filter[1]);
4157 RTL_W32(MAR0 + 0, mc_filter[0]);
4158
4159 RTL_W32(RxConfig, tmp);
4160}
4161
Francois Romieu07ce4062007-02-23 23:36:39 +01004162static void rtl_hw_start_8169(struct net_device *dev)
4163{
4164 struct rtl8169_private *tp = netdev_priv(dev);
4165 void __iomem *ioaddr = tp->mmio_addr;
4166 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004167
Francois Romieu9cb427b2006-11-02 00:10:16 +01004168 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4169 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4170 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4171 }
4172
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004174 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4175 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4176 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4177 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004178 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4179
Hayes Wange542a222011-07-06 15:58:04 +08004180 rtl_init_rxcfg(tp);
4181
françois romieuf0298f82011-01-03 15:07:42 +00004182 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004184 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185
Francois Romieucecb5fd2011-04-01 10:21:07 +02004186 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4187 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4188 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4189 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004190 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191
Francois Romieu7f796d832007-06-11 23:04:41 +02004192 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004193
Francois Romieucecb5fd2011-04-01 10:21:07 +02004194 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4195 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004196 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004198 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 }
4200
Francois Romieubcf0bf92006-07-26 23:14:13 +02004201 RTL_W16(CPlusCmd, tp->cp_cmd);
4202
Francois Romieu6dccd162007-02-13 23:38:05 +01004203 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4204
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 /*
4206 * Undocumented corner. Supposedly:
4207 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4208 */
4209 RTL_W16(IntrMitigate, 0x0000);
4210
Francois Romieu7f796d832007-06-11 23:04:41 +02004211 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004212
Francois Romieucecb5fd2011-04-01 10:21:07 +02004213 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4214 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4215 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4216 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004217 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4218 rtl_set_rx_tx_config_registers(tp);
4219 }
4220
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004222
4223 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4224 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225
4226 RTL_W32(RxMissed, 0);
4227
Francois Romieu07ce4062007-02-23 23:36:39 +01004228 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229
4230 /* no early-rx interrupts */
4231 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004232}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004234static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
4235{
4236 if (tp->csi_ops.write)
4237 tp->csi_ops.write(tp->mmio_addr, addr, value);
4238}
4239
4240static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
4241{
4242 if (tp->csi_ops.read)
4243 return tp->csi_ops.read(tp->mmio_addr, addr);
4244 else
4245 return ~0;
4246}
4247
4248static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004249{
4250 u32 csi;
4251
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004252 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4253 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00004254}
4255
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004256static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004257{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004258 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00004259}
4260
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004261static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00004262{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004263 rtl_csi_access_enable(tp, 0x27000000);
4264}
4265
4266static void r8169_csi_write(void __iomem *ioaddr, int addr, int value)
4267{
4268 unsigned int i;
4269
4270 RTL_W32(CSIDR, value);
4271 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4272 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4273
4274 for (i = 0; i < 100; i++) {
4275 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
4276 break;
4277 udelay(10);
4278 }
4279}
4280
4281static u32 r8169_csi_read(void __iomem *ioaddr, int addr)
4282{
4283 u32 value = ~0x00;
4284 unsigned int i;
4285
4286 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
4287 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4288
4289 for (i = 0; i < 100; i++) {
4290 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
4291 value = RTL_R32(CSIDR);
4292 break;
4293 }
4294 udelay(10);
4295 }
4296
4297 return value;
4298}
4299
Hayes Wang7e18dca2012-03-30 14:33:02 +08004300static void r8402_csi_write(void __iomem *ioaddr, int addr, int value)
4301{
4302 unsigned int i;
4303
4304 RTL_W32(CSIDR, value);
4305 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4306 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
4307 CSIAR_FUNC_NIC);
4308
4309 for (i = 0; i < 100; i++) {
4310 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
4311 break;
4312 udelay(10);
4313 }
4314}
4315
4316static u32 r8402_csi_read(void __iomem *ioaddr, int addr)
4317{
4318 u32 value = ~0x00;
4319 unsigned int i;
4320
4321 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
4322 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4323
4324 for (i = 0; i < 100; i++) {
4325 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
4326 value = RTL_R32(CSIDR);
4327 break;
4328 }
4329 udelay(10);
4330 }
4331
4332 return value;
4333}
4334
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004335static void __devinit rtl_init_csi_ops(struct rtl8169_private *tp)
4336{
4337 struct csi_ops *ops = &tp->csi_ops;
4338
4339 switch (tp->mac_version) {
4340 case RTL_GIGA_MAC_VER_01:
4341 case RTL_GIGA_MAC_VER_02:
4342 case RTL_GIGA_MAC_VER_03:
4343 case RTL_GIGA_MAC_VER_04:
4344 case RTL_GIGA_MAC_VER_05:
4345 case RTL_GIGA_MAC_VER_06:
4346 case RTL_GIGA_MAC_VER_10:
4347 case RTL_GIGA_MAC_VER_11:
4348 case RTL_GIGA_MAC_VER_12:
4349 case RTL_GIGA_MAC_VER_13:
4350 case RTL_GIGA_MAC_VER_14:
4351 case RTL_GIGA_MAC_VER_15:
4352 case RTL_GIGA_MAC_VER_16:
4353 case RTL_GIGA_MAC_VER_17:
4354 ops->write = NULL;
4355 ops->read = NULL;
4356 break;
4357
Hayes Wang7e18dca2012-03-30 14:33:02 +08004358 case RTL_GIGA_MAC_VER_37:
4359 ops->write = r8402_csi_write;
4360 ops->read = r8402_csi_read;
4361 break;
4362
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004363 default:
4364 ops->write = r8169_csi_write;
4365 ops->read = r8169_csi_read;
4366 break;
4367 }
Francois Romieudacf8152008-08-02 20:44:13 +02004368}
4369
4370struct ephy_info {
4371 unsigned int offset;
4372 u16 mask;
4373 u16 bits;
4374};
4375
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004376static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004377{
4378 u16 w;
4379
4380 while (len-- > 0) {
4381 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4382 rtl_ephy_write(ioaddr, e->offset, w);
4383 e++;
4384 }
4385}
4386
Francois Romieub726e492008-06-28 12:22:59 +02004387static void rtl_disable_clock_request(struct pci_dev *pdev)
4388{
Jon Masone44daad2011-06-27 07:46:31 +00004389 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004390
4391 if (cap) {
4392 u16 ctl;
4393
4394 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4395 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4396 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4397 }
4398}
4399
françois romieue6de30d2011-01-03 15:08:37 +00004400static void rtl_enable_clock_request(struct pci_dev *pdev)
4401{
Jon Masone44daad2011-06-27 07:46:31 +00004402 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004403
4404 if (cap) {
4405 u16 ctl;
4406
4407 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4408 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4409 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4410 }
4411}
4412
Francois Romieub726e492008-06-28 12:22:59 +02004413#define R8168_CPCMD_QUIRK_MASK (\
4414 EnableBist | \
4415 Mac_dbgo_oe | \
4416 Force_half_dup | \
4417 Force_rxflow_en | \
4418 Force_txflow_en | \
4419 Cxpl_dbg_sel | \
4420 ASF | \
4421 PktCntrDisable | \
4422 Mac_dbgo_sel)
4423
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004424static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004425{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004426 void __iomem *ioaddr = tp->mmio_addr;
4427 struct pci_dev *pdev = tp->pci_dev;
4428
Francois Romieub726e492008-06-28 12:22:59 +02004429 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4430
4431 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4432
Francois Romieu2e68ae42008-06-28 12:00:55 +02004433 rtl_tx_performance_tweak(pdev,
4434 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004435}
4436
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004437static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004438{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004439 void __iomem *ioaddr = tp->mmio_addr;
4440
4441 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004442
françois romieuf0298f82011-01-03 15:07:42 +00004443 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004444
4445 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004446}
4447
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004448static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004449{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004450 void __iomem *ioaddr = tp->mmio_addr;
4451 struct pci_dev *pdev = tp->pci_dev;
4452
Francois Romieub726e492008-06-28 12:22:59 +02004453 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4454
4455 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4456
Francois Romieu219a1e92008-06-28 11:58:39 +02004457 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004458
4459 rtl_disable_clock_request(pdev);
4460
4461 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004462}
4463
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004464static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004465{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004466 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004467 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004468 { 0x01, 0, 0x0001 },
4469 { 0x02, 0x0800, 0x1000 },
4470 { 0x03, 0, 0x0042 },
4471 { 0x06, 0x0080, 0x0000 },
4472 { 0x07, 0, 0x2000 }
4473 };
4474
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004475 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004476
4477 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4478
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004479 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004480}
4481
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004482static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02004483{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004484 void __iomem *ioaddr = tp->mmio_addr;
4485 struct pci_dev *pdev = tp->pci_dev;
4486
4487 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004488
4489 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4490
4491 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4492
4493 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4494}
4495
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004496static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004497{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004498 void __iomem *ioaddr = tp->mmio_addr;
4499 struct pci_dev *pdev = tp->pci_dev;
4500
4501 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004502
4503 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4504
4505 /* Magic. */
4506 RTL_W8(DBG_REG, 0x20);
4507
françois romieuf0298f82011-01-03 15:07:42 +00004508 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004509
4510 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4511
4512 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4513}
4514
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004515static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004516{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004517 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004518 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004519 { 0x02, 0x0800, 0x1000 },
4520 { 0x03, 0, 0x0002 },
4521 { 0x06, 0x0080, 0x0000 }
4522 };
4523
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004524 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004525
4526 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4527
4528 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4529
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004530 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004531}
4532
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004533static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004534{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004535 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004536 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004537 { 0x01, 0, 0x0001 },
4538 { 0x03, 0x0400, 0x0220 }
4539 };
4540
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004541 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004542
4543 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4544
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004545 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004546}
4547
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004548static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02004549{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004550 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004551}
4552
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004553static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02004554{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004555 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004556
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004557 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004558}
4559
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004560static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02004561{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004562 void __iomem *ioaddr = tp->mmio_addr;
4563 struct pci_dev *pdev = tp->pci_dev;
4564
4565 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004566
4567 rtl_disable_clock_request(pdev);
4568
françois romieuf0298f82011-01-03 15:07:42 +00004569 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004570
4571 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4572
4573 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4574}
4575
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004576static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00004577{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004578 void __iomem *ioaddr = tp->mmio_addr;
4579 struct pci_dev *pdev = tp->pci_dev;
4580
4581 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004582
4583 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4584
4585 RTL_W8(MaxTxPacketSize, TxPacketMax);
4586
4587 rtl_disable_clock_request(pdev);
4588}
4589
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004590static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004591{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004592 void __iomem *ioaddr = tp->mmio_addr;
4593 struct pci_dev *pdev = tp->pci_dev;
françois romieue6de30d2011-01-03 15:08:37 +00004594 static const struct ephy_info e_info_8168d_4[] = {
4595 { 0x0b, ~0, 0x48 },
4596 { 0x19, 0x20, 0x50 },
4597 { 0x0c, ~0, 0x20 }
4598 };
4599 int i;
4600
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004601 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004602
4603 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4604
4605 RTL_W8(MaxTxPacketSize, TxPacketMax);
4606
4607 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4608 const struct ephy_info *e = e_info_8168d_4 + i;
4609 u16 w;
4610
4611 w = rtl_ephy_read(ioaddr, e->offset);
4612 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4613 }
4614
4615 rtl_enable_clock_request(pdev);
4616}
4617
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004618static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00004619{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004620 void __iomem *ioaddr = tp->mmio_addr;
4621 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08004622 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004623 { 0x00, 0x0200, 0x0100 },
4624 { 0x00, 0x0000, 0x0004 },
4625 { 0x06, 0x0002, 0x0001 },
4626 { 0x06, 0x0000, 0x0030 },
4627 { 0x07, 0x0000, 0x2000 },
4628 { 0x00, 0x0000, 0x0020 },
4629 { 0x03, 0x5800, 0x2000 },
4630 { 0x03, 0x0000, 0x0001 },
4631 { 0x01, 0x0800, 0x1000 },
4632 { 0x07, 0x0000, 0x4000 },
4633 { 0x1e, 0x0000, 0x2000 },
4634 { 0x19, 0xffff, 0xfe6c },
4635 { 0x0a, 0x0000, 0x0040 }
4636 };
4637
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004638 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004639
Hayes Wang70090422011-07-06 15:58:06 +08004640 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004641
4642 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4643
4644 RTL_W8(MaxTxPacketSize, TxPacketMax);
4645
4646 rtl_disable_clock_request(pdev);
4647
4648 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004649 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4650 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004651
Francois Romieucecb5fd2011-04-01 10:21:07 +02004652 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004653}
4654
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004655static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08004656{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004657 void __iomem *ioaddr = tp->mmio_addr;
4658 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08004659 static const struct ephy_info e_info_8168e_2[] = {
4660 { 0x09, 0x0000, 0x0080 },
4661 { 0x19, 0x0000, 0x0224 }
4662 };
4663
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004664 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08004665
4666 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4667
4668 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4669
4670 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4671 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4672 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4673 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4674 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4675 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4676 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4677 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4678 ERIAR_EXGMAC);
4679
Hayes Wang3090bd92011-09-06 16:55:15 +08004680 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004681
4682 rtl_disable_clock_request(pdev);
4683
4684 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4685 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4686
4687 /* Adjust EEE LED frequency */
4688 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4689
4690 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4691 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4692 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4693}
4694
Hayes Wang5f886e02012-03-30 14:33:03 +08004695static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08004696{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004697 void __iomem *ioaddr = tp->mmio_addr;
4698 struct pci_dev *pdev = tp->pci_dev;
Hayes Wangc2218922011-09-06 16:55:18 +08004699
Hayes Wang5f886e02012-03-30 14:33:03 +08004700 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08004701
4702 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4703
4704 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4705 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4706 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4707 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4708 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4709 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4710 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4711 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4712 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4713 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08004714
4715 RTL_W8(MaxTxPacketSize, EarlySize);
4716
4717 rtl_disable_clock_request(pdev);
4718
4719 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4720 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
Hayes Wangc2218922011-09-06 16:55:18 +08004721 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4722 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4723 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4724}
4725
Hayes Wang5f886e02012-03-30 14:33:03 +08004726static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
4727{
4728 void __iomem *ioaddr = tp->mmio_addr;
4729 static const struct ephy_info e_info_8168f_1[] = {
4730 { 0x06, 0x00c0, 0x0020 },
4731 { 0x08, 0x0001, 0x0002 },
4732 { 0x09, 0x0000, 0x0080 },
4733 { 0x19, 0x0000, 0x0224 }
4734 };
4735
4736 rtl_hw_start_8168f(tp);
4737
4738 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4739
4740 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4741 ERIAR_EXGMAC);
4742
4743 /* Adjust EEE LED frequency */
4744 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4745}
4746
Francois Romieu07ce4062007-02-23 23:36:39 +01004747static void rtl_hw_start_8168(struct net_device *dev)
4748{
Francois Romieu2dd99532007-06-11 23:22:52 +02004749 struct rtl8169_private *tp = netdev_priv(dev);
4750 void __iomem *ioaddr = tp->mmio_addr;
4751
4752 RTL_W8(Cfg9346, Cfg9346_Unlock);
4753
françois romieuf0298f82011-01-03 15:07:42 +00004754 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004755
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004756 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004757
Francois Romieu0e485152007-02-20 00:00:26 +01004758 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004759
4760 RTL_W16(CPlusCmd, tp->cp_cmd);
4761
Francois Romieu0e485152007-02-20 00:00:26 +01004762 RTL_W16(IntrMitigate, 0x5151);
4763
4764 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00004765 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01004766 tp->event_slow |= RxFIFOOver | PCSTimeout;
4767 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01004768 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004769
4770 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4771
Francois Romieub8363902008-06-01 12:31:57 +02004772 rtl_set_rx_mode(dev);
4773
4774 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4775 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004776
4777 RTL_R8(IntrMask);
4778
Francois Romieu219a1e92008-06-28 11:58:39 +02004779 switch (tp->mac_version) {
4780 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004781 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004782 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004783
4784 case RTL_GIGA_MAC_VER_12:
4785 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004786 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004787 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004788
4789 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004790 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004791 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004792
4793 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004794 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004795 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004796
4797 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004798 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004799 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004800
Francois Romieu197ff762008-06-28 13:16:02 +02004801 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004802 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004803 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004804
Francois Romieu6fb07052008-06-29 11:54:28 +02004805 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004806 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004807 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004808
Francois Romieuef3386f2008-06-29 12:24:30 +02004809 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004810 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004811 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004812
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004813 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004814 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004815 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004816
Francois Romieu5b538df2008-07-20 16:22:45 +02004817 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004818 case RTL_GIGA_MAC_VER_26:
4819 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004820 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004821 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004822
françois romieue6de30d2011-01-03 15:08:37 +00004823 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004824 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004825 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004826
hayeswang4804b3b2011-03-21 01:50:29 +00004827 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004828 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004829 break;
4830
hayeswang01dc7fe2011-03-21 01:50:28 +00004831 case RTL_GIGA_MAC_VER_32:
4832 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004833 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08004834 break;
4835 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004836 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004837 break;
françois romieue6de30d2011-01-03 15:08:37 +00004838
Hayes Wangc2218922011-09-06 16:55:18 +08004839 case RTL_GIGA_MAC_VER_35:
4840 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004841 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08004842 break;
4843
Francois Romieu219a1e92008-06-28 11:58:39 +02004844 default:
4845 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4846 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004847 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004848 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004849
Francois Romieu0e485152007-02-20 00:00:26 +01004850 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4851
Francois Romieub8363902008-06-01 12:31:57 +02004852 RTL_W8(Cfg9346, Cfg9346_Lock);
4853
Francois Romieu2dd99532007-06-11 23:22:52 +02004854 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004855}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856
Francois Romieu2857ffb2008-08-02 21:08:49 +02004857#define R810X_CPCMD_QUIRK_MASK (\
4858 EnableBist | \
4859 Mac_dbgo_oe | \
4860 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004861 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004862 Force_txflow_en | \
4863 Cxpl_dbg_sel | \
4864 ASF | \
4865 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004866 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004867
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004868static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004869{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004870 void __iomem *ioaddr = tp->mmio_addr;
4871 struct pci_dev *pdev = tp->pci_dev;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004872 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004873 { 0x01, 0, 0x6e65 },
4874 { 0x02, 0, 0x091f },
4875 { 0x03, 0, 0xc2f9 },
4876 { 0x06, 0, 0xafb5 },
4877 { 0x07, 0, 0x0e00 },
4878 { 0x19, 0, 0xec80 },
4879 { 0x01, 0, 0x2e65 },
4880 { 0x01, 0, 0x6e65 }
4881 };
4882 u8 cfg1;
4883
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004884 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004885
4886 RTL_W8(DBG_REG, FIX_NAK_1);
4887
4888 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4889
4890 RTL_W8(Config1,
4891 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4892 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4893
4894 cfg1 = RTL_R8(Config1);
4895 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4896 RTL_W8(Config1, cfg1 & ~LEDS0);
4897
Francois Romieu2857ffb2008-08-02 21:08:49 +02004898 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4899}
4900
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004901static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004902{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004903 void __iomem *ioaddr = tp->mmio_addr;
4904 struct pci_dev *pdev = tp->pci_dev;
4905
4906 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004907
4908 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4909
4910 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4911 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004912}
4913
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004914static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004915{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004916 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004917
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004918 rtl_ephy_write(tp->mmio_addr, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004919}
4920
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004921static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08004922{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004923 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004924 static const struct ephy_info e_info_8105e_1[] = {
4925 { 0x07, 0, 0x4000 },
4926 { 0x19, 0, 0x0200 },
4927 { 0x19, 0, 0x0020 },
4928 { 0x1e, 0, 0x2000 },
4929 { 0x03, 0, 0x0001 },
4930 { 0x19, 0, 0x0100 },
4931 { 0x19, 0, 0x0004 },
4932 { 0x0a, 0, 0x0020 }
4933 };
4934
Francois Romieucecb5fd2011-04-01 10:21:07 +02004935 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004936 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4937
Francois Romieucecb5fd2011-04-01 10:21:07 +02004938 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004939 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4940
4941 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08004942 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004943
4944 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4945}
4946
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004947static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08004948{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004949 void __iomem *ioaddr = tp->mmio_addr;
4950
4951 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004952 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4953}
4954
Hayes Wang7e18dca2012-03-30 14:33:02 +08004955static void rtl_hw_start_8402(struct rtl8169_private *tp)
4956{
4957 void __iomem *ioaddr = tp->mmio_addr;
4958 static const struct ephy_info e_info_8402[] = {
4959 { 0x19, 0xffff, 0xff64 },
4960 { 0x1e, 0, 0x4000 }
4961 };
4962
4963 rtl_csi_access_enable_2(tp);
4964
4965 /* Force LAN exit from ASPM if Rx/Tx are not idle */
4966 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4967
4968 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4969 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4970
4971 rtl_ephy_init(ioaddr, e_info_8402, ARRAY_SIZE(e_info_8402));
4972
4973 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
4974
4975 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
4976 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
4977 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4978 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4979 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4980 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4981 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00,
4982 ERIAR_EXGMAC);
4983}
4984
Francois Romieu07ce4062007-02-23 23:36:39 +01004985static void rtl_hw_start_8101(struct net_device *dev)
4986{
Francois Romieucdf1a602007-06-11 23:29:50 +02004987 struct rtl8169_private *tp = netdev_priv(dev);
4988 void __iomem *ioaddr = tp->mmio_addr;
4989 struct pci_dev *pdev = tp->pci_dev;
4990
Francois Romieuda78dbf2012-01-26 14:18:23 +01004991 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
4992 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00004993
Francois Romieucecb5fd2011-04-01 10:21:07 +02004994 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4995 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00004996 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02004997
4998 if (cap) {
4999 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
5000 PCI_EXP_DEVCTL_NOSNOOP_EN);
5001 }
Francois Romieucdf1a602007-06-11 23:29:50 +02005002 }
5003
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005004 RTL_W8(Cfg9346, Cfg9346_Unlock);
5005
Francois Romieu2857ffb2008-08-02 21:08:49 +02005006 switch (tp->mac_version) {
5007 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005008 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005009 break;
5010
5011 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005012 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005013 break;
5014
5015 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005016 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005017 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005018
5019 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005020 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005021 break;
5022 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005023 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005024 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005025
5026 case RTL_GIGA_MAC_VER_37:
5027 rtl_hw_start_8402(tp);
5028 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02005029 }
5030
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005031 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02005032
françois romieuf0298f82011-01-03 15:07:42 +00005033 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02005034
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005035 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02005036
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005037 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02005038 RTL_W16(CPlusCmd, tp->cp_cmd);
5039
5040 RTL_W16(IntrMitigate, 0x0000);
5041
5042 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5043
5044 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5045 rtl_set_rx_tx_config_registers(tp);
5046
Francois Romieucdf1a602007-06-11 23:29:50 +02005047 RTL_R8(IntrMask);
5048
Francois Romieucdf1a602007-06-11 23:29:50 +02005049 rtl_set_rx_mode(dev);
5050
5051 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052}
5053
5054static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5055{
Francois Romieud58d46b2011-05-03 16:38:29 +02005056 struct rtl8169_private *tp = netdev_priv(dev);
5057
5058 if (new_mtu < ETH_ZLEN ||
5059 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 return -EINVAL;
5061
Francois Romieud58d46b2011-05-03 16:38:29 +02005062 if (new_mtu > ETH_DATA_LEN)
5063 rtl_hw_jumbo_enable(tp);
5064 else
5065 rtl_hw_jumbo_disable(tp);
5066
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005068 netdev_update_features(dev);
5069
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005070 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071}
5072
5073static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5074{
Al Viro95e09182007-12-22 18:55:39 +00005075 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5077}
5078
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005079static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5080 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005082 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005083 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005084
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005085 kfree(*data_buff);
5086 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 rtl8169_make_unusable_by_asic(desc);
5088}
5089
5090static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5091{
5092 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5093
5094 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5095}
5096
5097static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5098 u32 rx_buf_sz)
5099{
5100 desc->addr = cpu_to_le64(mapping);
5101 wmb();
5102 rtl8169_mark_to_asic(desc, rx_buf_sz);
5103}
5104
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005105static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005107 return (void *)ALIGN((long)data, 16);
5108}
5109
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005110static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5111 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005112{
5113 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005115 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005116 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005117 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005119 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5120 if (!data)
5121 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005122
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005123 if (rtl8169_align(data) != data) {
5124 kfree(data);
5125 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5126 if (!data)
5127 return NULL;
5128 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005129
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005130 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005131 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005132 if (unlikely(dma_mapping_error(d, mapping))) {
5133 if (net_ratelimit())
5134 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005135 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137
5138 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005139 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005140
5141err_out:
5142 kfree(data);
5143 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144}
5145
5146static void rtl8169_rx_clear(struct rtl8169_private *tp)
5147{
Francois Romieu07d3f512007-02-21 22:40:46 +01005148 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149
5150 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005151 if (tp->Rx_databuff[i]) {
5152 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 tp->RxDescArray + i);
5154 }
5155 }
5156}
5157
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005158static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005160 desc->opts1 |= cpu_to_le32(RingEnd);
5161}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005162
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005163static int rtl8169_rx_fill(struct rtl8169_private *tp)
5164{
5165 unsigned int i;
5166
5167 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005168 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005169
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005170 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005172
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005173 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005174 if (!data) {
5175 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005176 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005177 }
5178 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005181 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5182 return 0;
5183
5184err_out:
5185 rtl8169_rx_clear(tp);
5186 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187}
5188
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189static int rtl8169_init_ring(struct net_device *dev)
5190{
5191 struct rtl8169_private *tp = netdev_priv(dev);
5192
5193 rtl8169_init_ring_indexes(tp);
5194
5195 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005196 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005198 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199}
5200
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005201static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 struct TxDesc *desc)
5203{
5204 unsigned int len = tx_skb->len;
5205
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005206 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5207
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 desc->opts1 = 0x00;
5209 desc->opts2 = 0x00;
5210 desc->addr = 0x00;
5211 tx_skb->len = 0;
5212}
5213
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005214static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5215 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216{
5217 unsigned int i;
5218
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005219 for (i = 0; i < n; i++) {
5220 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 struct ring_info *tx_skb = tp->tx_skb + entry;
5222 unsigned int len = tx_skb->len;
5223
5224 if (len) {
5225 struct sk_buff *skb = tx_skb->skb;
5226
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005227 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 tp->TxDescArray + entry);
5229 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005230 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 dev_kfree_skb(skb);
5232 tx_skb->skb = NULL;
5233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 }
5235 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005236}
5237
5238static void rtl8169_tx_clear(struct rtl8169_private *tp)
5239{
5240 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 tp->cur_tx = tp->dirty_tx = 0;
Igor Maravic036dafa2012-03-05 00:01:25 +01005242 netdev_reset_queue(tp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243}
5244
Francois Romieu4422bcd2012-01-26 11:23:32 +01005245static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246{
David Howellsc4028952006-11-22 14:57:56 +00005247 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005248 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249
Francois Romieuda78dbf2012-01-26 14:18:23 +01005250 napi_disable(&tp->napi);
5251 netif_stop_queue(dev);
5252 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253
françois romieuc7c2c392011-12-04 20:30:52 +00005254 rtl8169_hw_reset(tp);
5255
Francois Romieu56de4142011-03-15 17:29:31 +01005256 for (i = 0; i < NUM_RX_DESC; i++)
5257 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5258
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005260 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261
Francois Romieuda78dbf2012-01-26 14:18:23 +01005262 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01005263 rtl_hw_start(dev);
5264 netif_wake_queue(dev);
5265 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266}
5267
5268static void rtl8169_tx_timeout(struct net_device *dev)
5269{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005270 struct rtl8169_private *tp = netdev_priv(dev);
5271
5272 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273}
5274
5275static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005276 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277{
5278 struct skb_shared_info *info = skb_shinfo(skb);
5279 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005280 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005281 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282
5283 entry = tp->cur_tx;
5284 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005285 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 dma_addr_t mapping;
5287 u32 status, len;
5288 void *addr;
5289
5290 entry = (entry + 1) % NUM_TX_DESC;
5291
5292 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005293 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005294 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005295 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005296 if (unlikely(dma_mapping_error(d, mapping))) {
5297 if (net_ratelimit())
5298 netif_err(tp, drv, tp->dev,
5299 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005300 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302
Francois Romieucecb5fd2011-04-01 10:21:07 +02005303 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005304 status = opts[0] | len |
5305 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306
5307 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005308 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 txd->addr = cpu_to_le64(mapping);
5310
5311 tp->tx_skb[entry].len = len;
5312 }
5313
5314 if (cur_frag) {
5315 tp->tx_skb[entry].skb = skb;
5316 txd->opts1 |= cpu_to_le32(LastFrag);
5317 }
5318
5319 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005320
5321err_out:
5322 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5323 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324}
5325
Francois Romieu2b7b4312011-04-18 22:53:24 -07005326static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5327 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005329 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005330 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005331 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332
Francois Romieu2b7b4312011-04-18 22:53:24 -07005333 if (mss) {
5334 opts[0] |= TD_LSO;
5335 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5336 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005337 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338
5339 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005340 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005342 opts[offset] |= info->checksum.udp;
5343 else
5344 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346}
5347
Stephen Hemminger613573252009-08-31 19:50:58 +00005348static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5349 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350{
5351 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005352 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 struct TxDesc *txd = tp->TxDescArray + entry;
5354 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005355 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 dma_addr_t mapping;
5357 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005358 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005359 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005360
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005362 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005363 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 }
5365
5366 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005367 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005369 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005370 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005371 if (unlikely(dma_mapping_error(d, mapping))) {
5372 if (net_ratelimit())
5373 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005374 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376
5377 tp->tx_skb[entry].len = len;
5378 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379
Francois Romieu2b7b4312011-04-18 22:53:24 -07005380 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5381 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005382
Francois Romieu2b7b4312011-04-18 22:53:24 -07005383 rtl8169_tso_csum(tp, skb, opts);
5384
5385 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005386 if (frags < 0)
5387 goto err_dma_1;
5388 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005389 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005390 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005391 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005392 tp->tx_skb[entry].skb = skb;
5393 }
5394
Francois Romieu2b7b4312011-04-18 22:53:24 -07005395 txd->opts2 = cpu_to_le32(opts[1]);
5396
Igor Maravic036dafa2012-03-05 00:01:25 +01005397 netdev_sent_queue(dev, skb->len);
5398
Richard Cochran5047fb52012-03-10 07:29:42 +00005399 skb_tx_timestamp(skb);
5400
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 wmb();
5402
Francois Romieucecb5fd2011-04-01 10:21:07 +02005403 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005404 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 txd->opts1 = cpu_to_le32(status);
5406
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 tp->cur_tx += frags + 1;
5408
David Dillow4c020a92010-03-03 16:33:10 +00005409 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410
Francois Romieucecb5fd2011-04-01 10:21:07 +02005411 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412
Francois Romieuda78dbf2012-01-26 14:18:23 +01005413 mmiowb();
5414
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01005416 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5417 * not miss a ring update when it notices a stopped queue.
5418 */
5419 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01005421 /* Sync with rtl_tx:
5422 * - publish queue status and cur_tx ring index (write barrier)
5423 * - refresh dirty_tx ring index (read barrier).
5424 * May the current thread have a pessimistic view of the ring
5425 * status and forget to wake up queue, a racing rtl_tx thread
5426 * can't.
5427 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005428 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
5430 netif_wake_queue(dev);
5431 }
5432
Stephen Hemminger613573252009-08-31 19:50:58 +00005433 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005435err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005436 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005437err_dma_0:
5438 dev_kfree_skb(skb);
5439 dev->stats.tx_dropped++;
5440 return NETDEV_TX_OK;
5441
5442err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005444 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005445 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446}
5447
5448static void rtl8169_pcierr_interrupt(struct net_device *dev)
5449{
5450 struct rtl8169_private *tp = netdev_priv(dev);
5451 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 u16 pci_status, pci_cmd;
5453
5454 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5455 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5456
Joe Perchesbf82c182010-02-09 11:49:50 +00005457 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5458 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459
5460 /*
5461 * The recovery sequence below admits a very elaborated explanation:
5462 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005463 * - I did not see what else could be done;
5464 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 *
5466 * Feel free to adjust to your needs.
5467 */
Francois Romieua27993f2006-12-18 00:04:19 +01005468 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005469 pci_cmd &= ~PCI_COMMAND_PARITY;
5470 else
5471 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5472
5473 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474
5475 pci_write_config_word(pdev, PCI_STATUS,
5476 pci_status & (PCI_STATUS_DETECTED_PARITY |
5477 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5478 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5479
5480 /* The infamous DAC f*ckup only happens at boot time */
5481 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005482 void __iomem *ioaddr = tp->mmio_addr;
5483
Joe Perchesbf82c182010-02-09 11:49:50 +00005484 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 tp->cp_cmd &= ~PCIDAC;
5486 RTL_W16(CPlusCmd, tp->cp_cmd);
5487 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 }
5489
françois romieue6de30d2011-01-03 15:08:37 +00005490 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005491
Francois Romieu98ddf982012-01-31 10:47:34 +01005492 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493}
5494
Igor Maravic036dafa2012-03-05 00:01:25 +01005495struct rtl_txc {
5496 int packets;
5497 int bytes;
5498};
5499
Francois Romieuda78dbf2012-01-26 14:18:23 +01005500static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501{
Igor Maravic036dafa2012-03-05 00:01:25 +01005502 struct rtl8169_stats *tx_stats = &tp->tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 unsigned int dirty_tx, tx_left;
Igor Maravic036dafa2012-03-05 00:01:25 +01005504 struct rtl_txc txc = { 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 dirty_tx = tp->dirty_tx;
5507 smp_rmb();
5508 tx_left = tp->cur_tx - dirty_tx;
5509
5510 while (tx_left > 0) {
5511 unsigned int entry = dirty_tx % NUM_TX_DESC;
5512 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 u32 status;
5514
5515 rmb();
5516 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5517 if (status & DescOwn)
5518 break;
5519
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005520 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5521 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 if (status & LastFrag) {
Igor Maravic036dafa2012-03-05 00:01:25 +01005523 struct sk_buff *skb = tx_skb->skb;
5524
5525 txc.packets++;
5526 txc.bytes += skb->len;
5527 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 tx_skb->skb = NULL;
5529 }
5530 dirty_tx++;
5531 tx_left--;
5532 }
5533
Igor Maravic036dafa2012-03-05 00:01:25 +01005534 u64_stats_update_begin(&tx_stats->syncp);
5535 tx_stats->packets += txc.packets;
5536 tx_stats->bytes += txc.bytes;
5537 u64_stats_update_end(&tx_stats->syncp);
5538
5539 netdev_completed_queue(dev, txc.packets, txc.bytes);
5540
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 if (tp->dirty_tx != dirty_tx) {
5542 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005543 /* Sync with rtl8169_start_xmit:
5544 * - publish dirty_tx ring index (write barrier)
5545 * - refresh cur_tx ring index and queue status (read barrier)
5546 * May the current thread miss the stopped queue condition,
5547 * a racing xmit thread can only have a right view of the
5548 * ring status.
5549 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005550 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 if (netif_queue_stopped(dev) &&
5552 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5553 netif_wake_queue(dev);
5554 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005555 /*
5556 * 8168 hack: TxPoll requests are lost when the Tx packets are
5557 * too close. Let's kick an extra TxPoll request when a burst
5558 * of start_xmit activity is detected (if it is not detected,
5559 * it is slow enough). -- FR
5560 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005561 if (tp->cur_tx != dirty_tx) {
5562 void __iomem *ioaddr = tp->mmio_addr;
5563
Francois Romieud78ae2d2007-08-26 20:08:19 +02005564 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 }
5567}
5568
Francois Romieu126fa4b2005-05-12 20:09:17 -04005569static inline int rtl8169_fragmented_frame(u32 status)
5570{
5571 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5572}
5573
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005574static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 u32 status = opts1 & RxProtoMask;
5577
5578 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005579 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 skb->ip_summed = CHECKSUM_UNNECESSARY;
5581 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005582 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583}
5584
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005585static struct sk_buff *rtl8169_try_rx_copy(void *data,
5586 struct rtl8169_private *tp,
5587 int pkt_size,
5588 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005590 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005591 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005593 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005594 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005595 prefetch(data);
5596 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5597 if (skb)
5598 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005599 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5600
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005601 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602}
5603
Francois Romieuda78dbf2012-01-26 14:18:23 +01005604static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605{
5606 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005607 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 cur_rx = tp->cur_rx;
5610 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005611 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005613 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005615 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 u32 status;
5617
5618 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005619 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620
5621 if (status & DescOwn)
5622 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005623 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005624 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5625 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005626 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005628 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005630 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005631 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005632 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005633 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005634 }
Ben Greear6bbe0212012-02-10 15:04:33 +00005635 if ((status & (RxRUNT | RxCRC)) &&
5636 !(status & (RxRWT | RxFOVF)) &&
5637 (dev->features & NETIF_F_RXALL))
5638 goto process_pkt;
5639
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005640 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005642 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00005643 dma_addr_t addr;
5644 int pkt_size;
5645
5646process_pkt:
5647 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00005648 if (likely(!(dev->features & NETIF_F_RXFCS)))
5649 pkt_size = (status & 0x00003fff) - 4;
5650 else
5651 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652
Francois Romieu126fa4b2005-05-12 20:09:17 -04005653 /*
5654 * The driver does not support incoming fragmented
5655 * frames. They are seen as a symptom of over-mtu
5656 * sized frames.
5657 */
5658 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005659 dev->stats.rx_dropped++;
5660 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005661 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005662 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005663 }
5664
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005665 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5666 tp, pkt_size, addr);
5667 rtl8169_mark_to_asic(desc, rx_buf_sz);
5668 if (!skb) {
5669 dev->stats.rx_dropped++;
5670 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 }
5672
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005673 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 skb_put(skb, pkt_size);
5675 skb->protocol = eth_type_trans(skb, dev);
5676
Francois Romieu7a8fc772011-03-01 17:18:33 +01005677 rtl8169_rx_vlan_tag(desc, skb);
5678
Francois Romieu56de4142011-03-15 17:29:31 +01005679 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680
Junchang Wang8027aa22012-03-04 23:30:32 +01005681 u64_stats_update_begin(&tp->rx_stats.syncp);
5682 tp->rx_stats.packets++;
5683 tp->rx_stats.bytes += pkt_size;
5684 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005686
5687 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005688 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005689 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5690 desc->opts2 = 0;
5691 cur_rx++;
5692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 }
5694
5695 count = cur_rx - tp->cur_rx;
5696 tp->cur_rx = cur_rx;
5697
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005698 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699
5700 return count;
5701}
5702
Francois Romieu07d3f512007-02-21 22:40:46 +01005703static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704{
Francois Romieu07d3f512007-02-21 22:40:46 +01005705 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005708 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005710 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005711 if (status && status != 0xffff) {
5712 status &= RTL_EVENT_NAPI | tp->event_slow;
5713 if (status) {
5714 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00005715
Francois Romieuda78dbf2012-01-26 14:18:23 +01005716 rtl_irq_disable(tp);
5717 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 return IRQ_RETVAL(handled);
5721}
5722
Francois Romieuda78dbf2012-01-26 14:18:23 +01005723/*
5724 * Workqueue context.
5725 */
5726static void rtl_slow_event_work(struct rtl8169_private *tp)
5727{
5728 struct net_device *dev = tp->dev;
5729 u16 status;
5730
5731 status = rtl_get_events(tp) & tp->event_slow;
5732 rtl_ack_events(tp, status);
5733
5734 if (unlikely(status & RxFIFOOver)) {
5735 switch (tp->mac_version) {
5736 /* Work around for rx fifo overflow */
5737 case RTL_GIGA_MAC_VER_11:
5738 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01005739 /* XXX - Hack alert. See rtl_task(). */
5740 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005741 default:
5742 break;
5743 }
5744 }
5745
5746 if (unlikely(status & SYSErr))
5747 rtl8169_pcierr_interrupt(dev);
5748
5749 if (status & LinkChg)
5750 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
5751
5752 napi_disable(&tp->napi);
5753 rtl_irq_disable(tp);
5754
5755 napi_enable(&tp->napi);
5756 napi_schedule(&tp->napi);
5757}
5758
Francois Romieu4422bcd2012-01-26 11:23:32 +01005759static void rtl_task(struct work_struct *work)
5760{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005761 static const struct {
5762 int bitnr;
5763 void (*action)(struct rtl8169_private *);
5764 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01005765 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005766 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
5767 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
5768 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
5769 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01005770 struct rtl8169_private *tp =
5771 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005772 struct net_device *dev = tp->dev;
5773 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01005774
Francois Romieuda78dbf2012-01-26 14:18:23 +01005775 rtl_lock_work(tp);
5776
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005777 if (!netif_running(dev) ||
5778 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01005779 goto out_unlock;
5780
5781 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
5782 bool pending;
5783
Francois Romieuda78dbf2012-01-26 14:18:23 +01005784 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005785 if (pending)
5786 rtl_work[i].action(tp);
5787 }
5788
5789out_unlock:
5790 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01005791}
5792
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005793static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005795 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5796 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005797 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
5798 int work_done= 0;
5799 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800
Francois Romieuda78dbf2012-01-26 14:18:23 +01005801 status = rtl_get_events(tp);
5802 rtl_ack_events(tp, status & ~tp->event_slow);
5803
5804 if (status & RTL_EVENT_NAPI_RX)
5805 work_done = rtl_rx(dev, tp, (u32) budget);
5806
5807 if (status & RTL_EVENT_NAPI_TX)
5808 rtl_tx(dev, tp);
5809
5810 if (status & tp->event_slow) {
5811 enable_mask &= ~tp->event_slow;
5812
5813 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
5814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005816 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005817 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005818
Francois Romieuda78dbf2012-01-26 14:18:23 +01005819 rtl_irq_enable(tp, enable_mask);
5820 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 }
5822
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005823 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825
Francois Romieu523a6092008-09-10 22:28:56 +02005826static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5827{
5828 struct rtl8169_private *tp = netdev_priv(dev);
5829
5830 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5831 return;
5832
5833 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5834 RTL_W32(RxMissed, 0);
5835}
5836
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837static void rtl8169_down(struct net_device *dev)
5838{
5839 struct rtl8169_private *tp = netdev_priv(dev);
5840 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841
Francois Romieu4876cc12011-03-11 21:07:11 +01005842 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005844 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005845 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846
Hayes Wang92fc43b2011-07-06 15:58:03 +08005847 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005848 /*
5849 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01005850 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
5851 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005852 */
Francois Romieu523a6092008-09-10 22:28:56 +02005853 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005856 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 rtl8169_tx_clear(tp);
5859
5860 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005861
5862 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863}
5864
5865static int rtl8169_close(struct net_device *dev)
5866{
5867 struct rtl8169_private *tp = netdev_priv(dev);
5868 struct pci_dev *pdev = tp->pci_dev;
5869
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005870 pm_runtime_get_sync(&pdev->dev);
5871
Francois Romieucecb5fd2011-04-01 10:21:07 +02005872 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005873 rtl8169_update_counters(dev);
5874
Francois Romieuda78dbf2012-01-26 14:18:23 +01005875 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005876 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005877
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005879 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880
Francois Romieu92a7c4e2012-03-10 10:42:12 +01005881 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005883 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5884 tp->RxPhyAddr);
5885 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5886 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887 tp->TxDescArray = NULL;
5888 tp->RxDescArray = NULL;
5889
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005890 pm_runtime_put_sync(&pdev->dev);
5891
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 return 0;
5893}
5894
Francois Romieudc1c00c2012-03-08 10:06:18 +01005895#ifdef CONFIG_NET_POLL_CONTROLLER
5896static void rtl8169_netpoll(struct net_device *dev)
5897{
5898 struct rtl8169_private *tp = netdev_priv(dev);
5899
5900 rtl8169_interrupt(tp->pci_dev->irq, dev);
5901}
5902#endif
5903
Francois Romieudf43ac72012-03-08 09:48:40 +01005904static int rtl_open(struct net_device *dev)
5905{
5906 struct rtl8169_private *tp = netdev_priv(dev);
5907 void __iomem *ioaddr = tp->mmio_addr;
5908 struct pci_dev *pdev = tp->pci_dev;
5909 int retval = -ENOMEM;
5910
5911 pm_runtime_get_sync(&pdev->dev);
5912
5913 /*
5914 * Rx and Tx desscriptors needs 256 bytes alignment.
5915 * dma_alloc_coherent provides more.
5916 */
5917 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
5918 &tp->TxPhyAddr, GFP_KERNEL);
5919 if (!tp->TxDescArray)
5920 goto err_pm_runtime_put;
5921
5922 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
5923 &tp->RxPhyAddr, GFP_KERNEL);
5924 if (!tp->RxDescArray)
5925 goto err_free_tx_0;
5926
5927 retval = rtl8169_init_ring(dev);
5928 if (retval < 0)
5929 goto err_free_rx_1;
5930
5931 INIT_WORK(&tp->wk.work, rtl_task);
5932
5933 smp_mb();
5934
5935 rtl_request_firmware(tp);
5936
Francois Romieu92a7c4e2012-03-10 10:42:12 +01005937 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01005938 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
5939 dev->name, dev);
5940 if (retval < 0)
5941 goto err_release_fw_2;
5942
5943 rtl_lock_work(tp);
5944
5945 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
5946
5947 napi_enable(&tp->napi);
5948
5949 rtl8169_init_phy(dev, tp);
5950
5951 __rtl8169_set_features(dev, dev->features);
5952
5953 rtl_pll_power_up(tp);
5954
5955 rtl_hw_start(dev);
5956
5957 netif_start_queue(dev);
5958
5959 rtl_unlock_work(tp);
5960
5961 tp->saved_wolopts = 0;
5962 pm_runtime_put_noidle(&pdev->dev);
5963
5964 rtl8169_check_link_status(dev, tp, ioaddr);
5965out:
5966 return retval;
5967
5968err_release_fw_2:
5969 rtl_release_firmware(tp);
5970 rtl8169_rx_clear(tp);
5971err_free_rx_1:
5972 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5973 tp->RxPhyAddr);
5974 tp->RxDescArray = NULL;
5975err_free_tx_0:
5976 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5977 tp->TxPhyAddr);
5978 tp->TxDescArray = NULL;
5979err_pm_runtime_put:
5980 pm_runtime_put_noidle(&pdev->dev);
5981 goto out;
5982}
5983
Junchang Wang8027aa22012-03-04 23:30:32 +01005984static struct rtnl_link_stats64 *
5985rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986{
5987 struct rtl8169_private *tp = netdev_priv(dev);
5988 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01005989 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990
Francois Romieuda78dbf2012-01-26 14:18:23 +01005991 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02005992 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02005993
Junchang Wang8027aa22012-03-04 23:30:32 +01005994 do {
5995 start = u64_stats_fetch_begin_bh(&tp->rx_stats.syncp);
5996 stats->rx_packets = tp->rx_stats.packets;
5997 stats->rx_bytes = tp->rx_stats.bytes;
5998 } while (u64_stats_fetch_retry_bh(&tp->rx_stats.syncp, start));
5999
6000
6001 do {
6002 start = u64_stats_fetch_begin_bh(&tp->tx_stats.syncp);
6003 stats->tx_packets = tp->tx_stats.packets;
6004 stats->tx_bytes = tp->tx_stats.bytes;
6005 } while (u64_stats_fetch_retry_bh(&tp->tx_stats.syncp, start));
6006
6007 stats->rx_dropped = dev->stats.rx_dropped;
6008 stats->tx_dropped = dev->stats.tx_dropped;
6009 stats->rx_length_errors = dev->stats.rx_length_errors;
6010 stats->rx_errors = dev->stats.rx_errors;
6011 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6012 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6013 stats->rx_missed_errors = dev->stats.rx_missed_errors;
6014
6015 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016}
6017
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006018static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006019{
françois romieu065c27c2011-01-03 15:08:12 +00006020 struct rtl8169_private *tp = netdev_priv(dev);
6021
Francois Romieu5d06a992006-02-23 00:47:58 +01006022 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006023 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006024
6025 netif_device_detach(dev);
6026 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006027
6028 rtl_lock_work(tp);
6029 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006030 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006031 rtl_unlock_work(tp);
6032
6033 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006034}
Francois Romieu5d06a992006-02-23 00:47:58 +01006035
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006036#ifdef CONFIG_PM
6037
6038static int rtl8169_suspend(struct device *device)
6039{
6040 struct pci_dev *pdev = to_pci_dev(device);
6041 struct net_device *dev = pci_get_drvdata(pdev);
6042
6043 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02006044
Francois Romieu5d06a992006-02-23 00:47:58 +01006045 return 0;
6046}
6047
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006048static void __rtl8169_resume(struct net_device *dev)
6049{
françois romieu065c27c2011-01-03 15:08:12 +00006050 struct rtl8169_private *tp = netdev_priv(dev);
6051
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006052 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006053
6054 rtl_pll_power_up(tp);
6055
Artem Savkovcff4c162012-04-03 10:29:11 +00006056 rtl_lock_work(tp);
6057 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006058 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00006059 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006060
Francois Romieu98ddf982012-01-31 10:47:34 +01006061 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006062}
6063
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006064static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006065{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006066 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01006067 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006068 struct rtl8169_private *tp = netdev_priv(dev);
6069
6070 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01006071
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006072 if (netif_running(dev))
6073 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006074
Francois Romieu5d06a992006-02-23 00:47:58 +01006075 return 0;
6076}
6077
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006078static int rtl8169_runtime_suspend(struct device *device)
6079{
6080 struct pci_dev *pdev = to_pci_dev(device);
6081 struct net_device *dev = pci_get_drvdata(pdev);
6082 struct rtl8169_private *tp = netdev_priv(dev);
6083
6084 if (!tp->TxDescArray)
6085 return 0;
6086
Francois Romieuda78dbf2012-01-26 14:18:23 +01006087 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006088 tp->saved_wolopts = __rtl8169_get_wol(tp);
6089 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006090 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006091
6092 rtl8169_net_suspend(dev);
6093
6094 return 0;
6095}
6096
6097static int rtl8169_runtime_resume(struct device *device)
6098{
6099 struct pci_dev *pdev = to_pci_dev(device);
6100 struct net_device *dev = pci_get_drvdata(pdev);
6101 struct rtl8169_private *tp = netdev_priv(dev);
6102
6103 if (!tp->TxDescArray)
6104 return 0;
6105
Francois Romieuda78dbf2012-01-26 14:18:23 +01006106 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006107 __rtl8169_set_wol(tp, tp->saved_wolopts);
6108 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006109 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006110
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006111 rtl8169_init_phy(dev, tp);
6112
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006113 __rtl8169_resume(dev);
6114
6115 return 0;
6116}
6117
6118static int rtl8169_runtime_idle(struct device *device)
6119{
6120 struct pci_dev *pdev = to_pci_dev(device);
6121 struct net_device *dev = pci_get_drvdata(pdev);
6122 struct rtl8169_private *tp = netdev_priv(dev);
6123
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00006124 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006125}
6126
Alexey Dobriyan47145212009-12-14 18:00:08 -08006127static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006128 .suspend = rtl8169_suspend,
6129 .resume = rtl8169_resume,
6130 .freeze = rtl8169_suspend,
6131 .thaw = rtl8169_resume,
6132 .poweroff = rtl8169_suspend,
6133 .restore = rtl8169_resume,
6134 .runtime_suspend = rtl8169_runtime_suspend,
6135 .runtime_resume = rtl8169_runtime_resume,
6136 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006137};
6138
6139#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6140
6141#else /* !CONFIG_PM */
6142
6143#define RTL8169_PM_OPS NULL
6144
6145#endif /* !CONFIG_PM */
6146
David S. Miller1805b2f2011-10-24 18:18:09 -04006147static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6148{
6149 void __iomem *ioaddr = tp->mmio_addr;
6150
6151 /* WoL fails with 8168b when the receiver is disabled. */
6152 switch (tp->mac_version) {
6153 case RTL_GIGA_MAC_VER_11:
6154 case RTL_GIGA_MAC_VER_12:
6155 case RTL_GIGA_MAC_VER_17:
6156 pci_clear_master(tp->pci_dev);
6157
6158 RTL_W8(ChipCmd, CmdRxEnb);
6159 /* PCI commit */
6160 RTL_R8(ChipCmd);
6161 break;
6162 default:
6163 break;
6164 }
6165}
6166
Francois Romieu1765f952008-09-13 17:21:40 +02006167static void rtl_shutdown(struct pci_dev *pdev)
6168{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006169 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006170 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00006171 struct device *d = &pdev->dev;
6172
6173 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02006174
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006175 rtl8169_net_suspend(dev);
6176
Francois Romieucecb5fd2011-04-01 10:21:07 +02006177 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006178 rtl_rar_set(tp, dev->perm_addr);
6179
Hayes Wang92fc43b2011-07-06 15:58:03 +08006180 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006181
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006182 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006183 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6184 rtl_wol_suspend_quirk(tp);
6185 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006186 }
6187
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006188 pci_wake_from_d3(pdev, true);
6189 pci_set_power_state(pdev, PCI_D3hot);
6190 }
françois romieu2a15cd22012-03-06 01:14:12 +00006191
6192 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006193}
Francois Romieu5d06a992006-02-23 00:47:58 +01006194
Francois Romieue27566e2012-03-08 09:54:01 +01006195static void __devexit rtl_remove_one(struct pci_dev *pdev)
6196{
6197 struct net_device *dev = pci_get_drvdata(pdev);
6198 struct rtl8169_private *tp = netdev_priv(dev);
6199
6200 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6201 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6202 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6203 rtl8168_driver_stop(tp);
6204 }
6205
6206 cancel_work_sync(&tp->wk.work);
6207
6208 unregister_netdev(dev);
6209
6210 rtl_release_firmware(tp);
6211
6212 if (pci_dev_run_wake(pdev))
6213 pm_runtime_get_noresume(&pdev->dev);
6214
6215 /* restore original MAC address */
6216 rtl_rar_set(tp, dev->perm_addr);
6217
6218 rtl_disable_msi(pdev, tp);
6219 rtl8169_release_board(pdev, dev, tp->mmio_addr);
6220 pci_set_drvdata(pdev, NULL);
6221}
6222
Francois Romieufa9c3852012-03-08 10:01:50 +01006223static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01006224 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01006225 .ndo_stop = rtl8169_close,
6226 .ndo_get_stats64 = rtl8169_get_stats64,
6227 .ndo_start_xmit = rtl8169_start_xmit,
6228 .ndo_tx_timeout = rtl8169_tx_timeout,
6229 .ndo_validate_addr = eth_validate_addr,
6230 .ndo_change_mtu = rtl8169_change_mtu,
6231 .ndo_fix_features = rtl8169_fix_features,
6232 .ndo_set_features = rtl8169_set_features,
6233 .ndo_set_mac_address = rtl_set_mac_address,
6234 .ndo_do_ioctl = rtl8169_ioctl,
6235 .ndo_set_rx_mode = rtl_set_rx_mode,
6236#ifdef CONFIG_NET_POLL_CONTROLLER
6237 .ndo_poll_controller = rtl8169_netpoll,
6238#endif
6239
6240};
6241
Francois Romieu31fa8b12012-03-08 10:09:40 +01006242static const struct rtl_cfg_info {
6243 void (*hw_start)(struct net_device *);
6244 unsigned int region;
6245 unsigned int align;
6246 u16 event_slow;
6247 unsigned features;
6248 u8 default_ver;
6249} rtl_cfg_infos [] = {
6250 [RTL_CFG_0] = {
6251 .hw_start = rtl_hw_start_8169,
6252 .region = 1,
6253 .align = 0,
6254 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
6255 .features = RTL_FEATURE_GMII,
6256 .default_ver = RTL_GIGA_MAC_VER_01,
6257 },
6258 [RTL_CFG_1] = {
6259 .hw_start = rtl_hw_start_8168,
6260 .region = 2,
6261 .align = 8,
6262 .event_slow = SYSErr | LinkChg | RxOverflow,
6263 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
6264 .default_ver = RTL_GIGA_MAC_VER_11,
6265 },
6266 [RTL_CFG_2] = {
6267 .hw_start = rtl_hw_start_8101,
6268 .region = 2,
6269 .align = 8,
6270 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
6271 PCSTimeout,
6272 .features = RTL_FEATURE_MSI,
6273 .default_ver = RTL_GIGA_MAC_VER_13,
6274 }
6275};
6276
6277/* Cfg9346_Unlock assumed. */
6278static unsigned rtl_try_msi(struct rtl8169_private *tp,
6279 const struct rtl_cfg_info *cfg)
6280{
6281 void __iomem *ioaddr = tp->mmio_addr;
6282 unsigned msi = 0;
6283 u8 cfg2;
6284
6285 cfg2 = RTL_R8(Config2) & ~MSIEnable;
6286 if (cfg->features & RTL_FEATURE_MSI) {
6287 if (pci_enable_msi(tp->pci_dev)) {
6288 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
6289 } else {
6290 cfg2 |= MSIEnable;
6291 msi = RTL_FEATURE_MSI;
6292 }
6293 }
6294 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6295 RTL_W8(Config2, cfg2);
6296 return msi;
6297}
6298
Francois Romieu3b6cf252012-03-08 09:59:04 +01006299static int __devinit
6300rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6301{
6302 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
6303 const unsigned int region = cfg->region;
6304 struct rtl8169_private *tp;
6305 struct mii_if_info *mii;
6306 struct net_device *dev;
6307 void __iomem *ioaddr;
6308 int chipset, i;
6309 int rc;
6310
6311 if (netif_msg_drv(&debug)) {
6312 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
6313 MODULENAME, RTL8169_VERSION);
6314 }
6315
6316 dev = alloc_etherdev(sizeof (*tp));
6317 if (!dev) {
6318 rc = -ENOMEM;
6319 goto out;
6320 }
6321
6322 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01006323 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006324 tp = netdev_priv(dev);
6325 tp->dev = dev;
6326 tp->pci_dev = pdev;
6327 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
6328
6329 mii = &tp->mii;
6330 mii->dev = dev;
6331 mii->mdio_read = rtl_mdio_read;
6332 mii->mdio_write = rtl_mdio_write;
6333 mii->phy_id_mask = 0x1f;
6334 mii->reg_num_mask = 0x1f;
6335 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
6336
6337 /* disable ASPM completely as that cause random device stop working
6338 * problems as well as full system hangs for some PCIe devices users */
6339 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6340 PCIE_LINK_STATE_CLKPM);
6341
6342 /* enable device (incl. PCI PM wakeup and hotplug setup) */
6343 rc = pci_enable_device(pdev);
6344 if (rc < 0) {
6345 netif_err(tp, probe, dev, "enable failure\n");
6346 goto err_out_free_dev_1;
6347 }
6348
6349 if (pci_set_mwi(pdev) < 0)
6350 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
6351
6352 /* make sure PCI base addr 1 is MMIO */
6353 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
6354 netif_err(tp, probe, dev,
6355 "region #%d not an MMIO resource, aborting\n",
6356 region);
6357 rc = -ENODEV;
6358 goto err_out_mwi_2;
6359 }
6360
6361 /* check for weird/broken PCI region reporting */
6362 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
6363 netif_err(tp, probe, dev,
6364 "Invalid PCI region size(s), aborting\n");
6365 rc = -ENODEV;
6366 goto err_out_mwi_2;
6367 }
6368
6369 rc = pci_request_regions(pdev, MODULENAME);
6370 if (rc < 0) {
6371 netif_err(tp, probe, dev, "could not request regions\n");
6372 goto err_out_mwi_2;
6373 }
6374
6375 tp->cp_cmd = RxChkSum;
6376
6377 if ((sizeof(dma_addr_t) > 4) &&
6378 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
6379 tp->cp_cmd |= PCIDAC;
6380 dev->features |= NETIF_F_HIGHDMA;
6381 } else {
6382 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6383 if (rc < 0) {
6384 netif_err(tp, probe, dev, "DMA configuration failed\n");
6385 goto err_out_free_res_3;
6386 }
6387 }
6388
6389 /* ioremap MMIO region */
6390 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
6391 if (!ioaddr) {
6392 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
6393 rc = -EIO;
6394 goto err_out_free_res_3;
6395 }
6396 tp->mmio_addr = ioaddr;
6397
6398 if (!pci_is_pcie(pdev))
6399 netif_info(tp, probe, dev, "not PCI Express\n");
6400
6401 /* Identify chip attached to board */
6402 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
6403
6404 rtl_init_rxcfg(tp);
6405
6406 rtl_irq_disable(tp);
6407
6408 rtl_hw_reset(tp);
6409
6410 rtl_ack_events(tp, 0xffff);
6411
6412 pci_set_master(pdev);
6413
6414 /*
6415 * Pretend we are using VLANs; This bypasses a nasty bug where
6416 * Interrupts stop flowing on high load on 8110SCd controllers.
6417 */
6418 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6419 tp->cp_cmd |= RxVlan;
6420
6421 rtl_init_mdio_ops(tp);
6422 rtl_init_pll_power_ops(tp);
6423 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006424 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006425
6426 rtl8169_print_mac_version(tp);
6427
6428 chipset = tp->mac_version;
6429 tp->txd_version = rtl_chip_infos[chipset].txd_version;
6430
6431 RTL_W8(Cfg9346, Cfg9346_Unlock);
6432 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
6433 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
6434 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
6435 tp->features |= RTL_FEATURE_WOL;
6436 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
6437 tp->features |= RTL_FEATURE_WOL;
6438 tp->features |= rtl_try_msi(tp, cfg);
6439 RTL_W8(Cfg9346, Cfg9346_Lock);
6440
6441 if (rtl_tbi_enabled(tp)) {
6442 tp->set_speed = rtl8169_set_speed_tbi;
6443 tp->get_settings = rtl8169_gset_tbi;
6444 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
6445 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
6446 tp->link_ok = rtl8169_tbi_link_ok;
6447 tp->do_ioctl = rtl_tbi_ioctl;
6448 } else {
6449 tp->set_speed = rtl8169_set_speed_xmii;
6450 tp->get_settings = rtl8169_gset_xmii;
6451 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
6452 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
6453 tp->link_ok = rtl8169_xmii_link_ok;
6454 tp->do_ioctl = rtl_xmii_ioctl;
6455 }
6456
6457 mutex_init(&tp->wk.mutex);
6458
6459 /* Get MAC address */
6460 for (i = 0; i < ETH_ALEN; i++)
6461 dev->dev_addr[i] = RTL_R8(MAC0 + i);
6462 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
6463
6464 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
6465 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006466
6467 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
6468
6469 /* don't enable SG, IP_CSUM and TSO by default - it might not work
6470 * properly for all devices */
6471 dev->features |= NETIF_F_RXCSUM |
6472 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6473
6474 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6475 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6476 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6477 NETIF_F_HIGHDMA;
6478
6479 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6480 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
6481 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
6482
6483 dev->hw_features |= NETIF_F_RXALL;
6484 dev->hw_features |= NETIF_F_RXFCS;
6485
6486 tp->hw_start = cfg->hw_start;
6487 tp->event_slow = cfg->event_slow;
6488
6489 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
6490 ~(RxBOVF | RxFOVF) : ~0;
6491
6492 init_timer(&tp->timer);
6493 tp->timer.data = (unsigned long) dev;
6494 tp->timer.function = rtl8169_phy_timer;
6495
6496 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
6497
6498 rc = register_netdev(dev);
6499 if (rc < 0)
6500 goto err_out_msi_4;
6501
6502 pci_set_drvdata(pdev, dev);
6503
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006504 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
6505 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
6506 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006507 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
6508 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
6509 "tx checksumming: %s]\n",
6510 rtl_chip_infos[chipset].jumbo_max,
6511 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
6512 }
6513
6514 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6515 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6516 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6517 rtl8168_driver_start(tp);
6518 }
6519
6520 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
6521
6522 if (pci_dev_run_wake(pdev))
6523 pm_runtime_put_noidle(&pdev->dev);
6524
6525 netif_carrier_off(dev);
6526
6527out:
6528 return rc;
6529
6530err_out_msi_4:
6531 rtl_disable_msi(pdev, tp);
6532 iounmap(ioaddr);
6533err_out_free_res_3:
6534 pci_release_regions(pdev);
6535err_out_mwi_2:
6536 pci_clear_mwi(pdev);
6537 pci_disable_device(pdev);
6538err_out_free_dev_1:
6539 free_netdev(dev);
6540 goto out;
6541}
6542
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543static struct pci_driver rtl8169_pci_driver = {
6544 .name = MODULENAME,
6545 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01006546 .probe = rtl_init_one,
Francois Romieue27566e2012-03-08 09:54:01 +01006547 .remove = __devexit_p(rtl_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02006548 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006549 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550};
6551
Francois Romieu07d3f512007-02-21 22:40:46 +01006552static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553{
Jeff Garzik29917622006-08-19 17:48:59 -04006554 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555}
6556
Francois Romieu07d3f512007-02-21 22:40:46 +01006557static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558{
6559 pci_unregister_driver(&rtl8169_pci_driver);
6560}
6561
6562module_init(rtl8169_init_module);
6563module_exit(rtl8169_cleanup_module);