blob: adf496d0ce48382ed30874268684c7b0bcf823cc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Francois Romieu07d3f512007-02-21 22:40:46 +01002 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3 *
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
7 *
8 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/delay.h>
17#include <linux/ethtool.h>
18#include <linux/mii.h>
19#include <linux/if_vlan.h>
20#include <linux/crc32.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/tcp.h>
24#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000025#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000027#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000028#include <linux/firmware.h>
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +000029#include <linux/pci-aspm.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040030#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <asm/io.h>
33#include <asm/irq.h>
34
Francois Romieu865c6522008-05-11 14:51:00 +020035#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define MODULENAME "r8169"
37#define PFX MODULENAME ": "
38
françois romieubca03d52011-01-03 15:07:31 +000039#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
40#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000041#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
42#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080043#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080044#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
45#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080046#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#ifdef RTL8169_DEBUG
49#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020050 if (!(expr)) { \
51 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070052 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020053 }
Joe Perches06fa7352007-10-18 21:15:00 +020054#define dprintk(fmt, args...) \
55 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#else
57#define assert(expr) do {} while (0)
58#define dprintk(fmt, args...) do {} while (0)
59#endif /* RTL8169_DEBUG */
60
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020061#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070062 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020063
Julien Ducourthial477206a2012-05-09 00:00:06 +020064#define TX_SLOTS_AVAIL(tp) \
65 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
66
67/* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
68#define TX_FRAGS_READY_FOR(tp,nr_frags) \
69 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
72 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050073static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Francois Romieu9c14cea2008-07-05 00:21:15 +020075#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
78#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
79
80#define R8169_REGS_SIZE 256
81#define R8169_NAPI_WEIGHT 64
82#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
83#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
84#define RX_BUF_SIZE 1536 /* Rx Buffer size */
85#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
86#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
87
88#define RTL8169_TX_TIMEOUT (6*HZ)
89#define RTL8169_PHY_TIMEOUT (10*HZ)
90
françois romieuea8dbdd2009-03-15 01:10:50 +000091#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
92#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020093#define RTL_EEPROM_SIG_ADDR 0x0000
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/* write/read MMIO register */
96#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
97#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
98#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
99#define RTL_R8(reg) readb (ioaddr + (reg))
100#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +0000101#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200104 RTL_GIGA_MAC_VER_01 = 0,
105 RTL_GIGA_MAC_VER_02,
106 RTL_GIGA_MAC_VER_03,
107 RTL_GIGA_MAC_VER_04,
108 RTL_GIGA_MAC_VER_05,
109 RTL_GIGA_MAC_VER_06,
110 RTL_GIGA_MAC_VER_07,
111 RTL_GIGA_MAC_VER_08,
112 RTL_GIGA_MAC_VER_09,
113 RTL_GIGA_MAC_VER_10,
114 RTL_GIGA_MAC_VER_11,
115 RTL_GIGA_MAC_VER_12,
116 RTL_GIGA_MAC_VER_13,
117 RTL_GIGA_MAC_VER_14,
118 RTL_GIGA_MAC_VER_15,
119 RTL_GIGA_MAC_VER_16,
120 RTL_GIGA_MAC_VER_17,
121 RTL_GIGA_MAC_VER_18,
122 RTL_GIGA_MAC_VER_19,
123 RTL_GIGA_MAC_VER_20,
124 RTL_GIGA_MAC_VER_21,
125 RTL_GIGA_MAC_VER_22,
126 RTL_GIGA_MAC_VER_23,
127 RTL_GIGA_MAC_VER_24,
128 RTL_GIGA_MAC_VER_25,
129 RTL_GIGA_MAC_VER_26,
130 RTL_GIGA_MAC_VER_27,
131 RTL_GIGA_MAC_VER_28,
132 RTL_GIGA_MAC_VER_29,
133 RTL_GIGA_MAC_VER_30,
134 RTL_GIGA_MAC_VER_31,
135 RTL_GIGA_MAC_VER_32,
136 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800137 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800138 RTL_GIGA_MAC_VER_35,
139 RTL_GIGA_MAC_VER_36,
Francois Romieu85bffe62011-04-27 08:22:39 +0200140 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
Francois Romieu2b7b4312011-04-18 22:53:24 -0700143enum rtl_tx_desc_version {
144 RTL_TD_0 = 0,
145 RTL_TD_1 = 1,
146};
147
Francois Romieud58d46b2011-05-03 16:38:29 +0200148#define JUMBO_1K ETH_DATA_LEN
149#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
150#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
151#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
152#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
153
154#define _R(NAME,TD,FW,SZ,B) { \
155 .name = NAME, \
156 .txd_version = TD, \
157 .fw_name = FW, \
158 .jumbo_max = SZ, \
159 .jumbo_tx_csum = B \
160}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800162static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700164 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200165 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200166 u16 jumbo_max;
167 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200168} rtl_chip_infos[] = {
169 /* PCI devices. */
170 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200171 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200172 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200173 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200174 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200175 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200176 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200177 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200178 [RTL_GIGA_MAC_VER_05] =
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 [RTL_GIGA_MAC_VER_06] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200181 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200182 /* PCI-E devices. */
183 [RTL_GIGA_MAC_VER_07] =
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_08] =
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_09] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200188 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200189 [RTL_GIGA_MAC_VER_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200190 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200191 [RTL_GIGA_MAC_VER_11] =
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_12] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200194 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200195 [RTL_GIGA_MAC_VER_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200196 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200197 [RTL_GIGA_MAC_VER_14] =
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_15] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200200 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200201 [RTL_GIGA_MAC_VER_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200202 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200203 [RTL_GIGA_MAC_VER_17] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200204 _R("RTL8168b/8111b", RTL_TD_1, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200205 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200206 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200207 [RTL_GIGA_MAC_VER_19] =
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_20] =
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_21] =
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_22] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200214 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200215 [RTL_GIGA_MAC_VER_23] =
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_24] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200218 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200219 [RTL_GIGA_MAC_VER_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200220 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
221 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200222 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200223 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
224 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200225 [RTL_GIGA_MAC_VER_27] =
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_28] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200228 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200229 [RTL_GIGA_MAC_VER_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200230 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
231 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200232 [RTL_GIGA_MAC_VER_30] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200233 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
234 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200235 [RTL_GIGA_MAC_VER_31] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200236 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200237 [RTL_GIGA_MAC_VER_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200238 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
239 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200240 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200241 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
242 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800243 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200244 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
245 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800246 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200247 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
248 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800249 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200250 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
251 JUMBO_9K, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252};
253#undef _R
254
Francois Romieubcf0bf92006-07-26 23:14:13 +0200255enum cfg_version {
256 RTL_CFG_0 = 0x00,
257 RTL_CFG_1,
258 RTL_CFG_2
259};
260
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000261static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200262 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200263 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200264 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100265 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200266 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
267 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000268 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200269 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200270 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
271 { PCI_VENDOR_ID_LINKSYS, 0x1032,
272 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100273 { 0x0001, 0x8168,
274 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 {0,},
276};
277
278MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
279
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000280static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700281static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200282static struct {
283 u32 msg_enable;
284} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Francois Romieu07d3f512007-02-21 22:40:46 +0100286enum rtl_registers {
287 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100288 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100289 MAR0 = 8, /* Multicast filter. */
290 CounterAddrLow = 0x10,
291 CounterAddrHigh = 0x14,
292 TxDescStartAddrLow = 0x20,
293 TxDescStartAddrHigh = 0x24,
294 TxHDescStartAddrLow = 0x28,
295 TxHDescStartAddrHigh = 0x2c,
296 FLASH = 0x30,
297 ERSR = 0x36,
298 ChipCmd = 0x37,
299 TxPoll = 0x38,
300 IntrMask = 0x3c,
301 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700302
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800303 TxConfig = 0x40,
304#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
305#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
306
307 RxConfig = 0x44,
308#define RX128_INT_EN (1 << 15) /* 8111c and later */
309#define RX_MULTI_EN (1 << 14) /* 8111c only */
310#define RXCFG_FIFO_SHIFT 13
311 /* No threshold before first PCI xfer */
312#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
313#define RXCFG_DMA_SHIFT 8
314 /* Unlimited maximum PCI burst. */
315#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700316
Francois Romieu07d3f512007-02-21 22:40:46 +0100317 RxMissed = 0x4c,
318 Cfg9346 = 0x50,
319 Config0 = 0x51,
320 Config1 = 0x52,
321 Config2 = 0x53,
322 Config3 = 0x54,
323 Config4 = 0x55,
324 Config5 = 0x56,
325 MultiIntr = 0x5c,
326 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100327 PHYstatus = 0x6c,
328 RxMaxSize = 0xda,
329 CPlusCmd = 0xe0,
330 IntrMitigate = 0xe2,
331 RxDescAddrLow = 0xe4,
332 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000333 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
334
335#define NoEarlyTx 0x3f /* Max value : no early transmit. */
336
337 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
338
339#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800340#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000341
Francois Romieu07d3f512007-02-21 22:40:46 +0100342 FuncEvent = 0xf0,
343 FuncEventMask = 0xf4,
344 FuncPresetState = 0xf8,
345 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346};
347
Francois Romieuf162a5d2008-06-01 22:37:49 +0200348enum rtl8110_registers {
349 TBICSR = 0x64,
350 TBI_ANAR = 0x68,
351 TBI_LPAR = 0x6a,
352};
353
354enum rtl8168_8101_registers {
355 CSIDR = 0x64,
356 CSIAR = 0x68,
357#define CSIAR_FLAG 0x80000000
358#define CSIAR_WRITE_CMD 0x80000000
359#define CSIAR_BYTE_ENABLE 0x0f
360#define CSIAR_BYTE_ENABLE_SHIFT 12
361#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000362 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200363 EPHYAR = 0x80,
364#define EPHYAR_FLAG 0x80000000
365#define EPHYAR_WRITE_CMD 0x80000000
366#define EPHYAR_REG_MASK 0x1f
367#define EPHYAR_REG_SHIFT 16
368#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800369 DLLPR = 0xd0,
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800370#define PFM_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200371 DBG_REG = 0xd1,
372#define FIX_NAK_1 (1 << 4)
373#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800374 TWSI = 0xd2,
375 MCU = 0xd3,
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800376#define NOW_IS_OOB (1 << 7)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800377#define EN_NDP (1 << 3)
378#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000379 EFUSEAR = 0xdc,
380#define EFUSEAR_FLAG 0x80000000
381#define EFUSEAR_WRITE_CMD 0x80000000
382#define EFUSEAR_READ_CMD 0x00000000
383#define EFUSEAR_REG_MASK 0x03ff
384#define EFUSEAR_REG_SHIFT 8
385#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200386};
387
françois romieuc0e45c12011-01-03 15:08:04 +0000388enum rtl8168_registers {
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800389 LED_FREQ = 0x1a,
390 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000391 ERIDR = 0x70,
392 ERIAR = 0x74,
393#define ERIAR_FLAG 0x80000000
394#define ERIAR_WRITE_CMD 0x80000000
395#define ERIAR_READ_CMD 0x00000000
396#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000397#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800398#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
399#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
400#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
401#define ERIAR_MASK_SHIFT 12
402#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
403#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
404#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000405 EPHY_RXER_NUM = 0x7c,
406 OCPDR = 0xb0, /* OCP GPHY access */
407#define OCPDR_WRITE_CMD 0x80000000
408#define OCPDR_READ_CMD 0x00000000
409#define OCPDR_REG_MASK 0x7f
410#define OCPDR_GPHY_REG_SHIFT 16
411#define OCPDR_DATA_MASK 0xffff
412 OCPAR = 0xb4,
413#define OCPAR_FLAG 0x80000000
414#define OCPAR_GPHY_WRITE_CMD 0x8000f060
415#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000416 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
417 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200418#define TXPLA_RST (1 << 29)
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800419#define PWM_EN (1 << 22)
françois romieuc0e45c12011-01-03 15:08:04 +0000420};
421
Francois Romieu07d3f512007-02-21 22:40:46 +0100422enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100424 SYSErr = 0x8000,
425 PCSTimeout = 0x4000,
426 SWInt = 0x0100,
427 TxDescUnavail = 0x0080,
428 RxFIFOOver = 0x0040,
429 LinkChg = 0x0020,
430 RxOverflow = 0x0010,
431 TxErr = 0x0008,
432 TxOK = 0x0004,
433 RxErr = 0x0002,
434 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400437 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200438 RxFOVF = (1 << 23),
439 RxRWT = (1 << 22),
440 RxRES = (1 << 21),
441 RxRUNT = (1 << 20),
442 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 /* ChipCmdBits */
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800445 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100446 CmdReset = 0x10,
447 CmdRxEnb = 0x08,
448 CmdTxEnb = 0x04,
449 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Francois Romieu275391a2007-02-23 23:50:28 +0100451 /* TXPoll register p.5 */
452 HPQ = 0x80, /* Poll cmd on the high prio queue */
453 NPQ = 0x40, /* Poll cmd on the low prio queue */
454 FSWInt = 0x01, /* Forced software interrupt */
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100457 Cfg9346_Lock = 0x00,
458 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100461 AcceptErr = 0x20,
462 AcceptRunt = 0x10,
463 AcceptBroadcast = 0x08,
464 AcceptMulticast = 0x04,
465 AcceptMyPhys = 0x02,
466 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200467#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /* TxConfigBits */
470 TxInterFrameGapShift = 24,
471 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
472
Francois Romieu5d06a992006-02-23 00:47:58 +0100473 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200474 LEDS1 = (1 << 7),
475 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200476 Speed_down = (1 << 4),
477 MEMMAP = (1 << 3),
478 IOMAP = (1 << 2),
479 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100480 PMEnable = (1 << 0), /* Power Management Enable */
481
Francois Romieu6dccd162007-02-13 23:38:05 +0100482 /* Config2 register p. 25 */
françois romieu2ca6cf02011-12-15 08:37:43 +0000483 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100484 PCI_Clock_66MHz = 0x01,
485 PCI_Clock_33MHz = 0x00,
486
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100487 /* Config3 register p.25 */
488 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
489 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200490 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200491 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100492
Francois Romieud58d46b2011-05-03 16:38:29 +0200493 /* Config4 register */
494 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
495
Francois Romieu5d06a992006-02-23 00:47:58 +0100496 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100497 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
498 MWF = (1 << 5), /* Accept Multicast wakeup frame */
499 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200500 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100501 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100502 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /* TBICSR p.28 */
505 TBIReset = 0x80000000,
506 TBILoopback = 0x40000000,
507 TBINwEnable = 0x20000000,
508 TBINwRestart = 0x10000000,
509 TBILinkOk = 0x02000000,
510 TBINwComplete = 0x01000000,
511
512 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200513 EnableBist = (1 << 15), // 8168 8101
514 Mac_dbgo_oe = (1 << 14), // 8168 8101
515 Normal_mode = (1 << 13), // unused
516 Force_half_dup = (1 << 12), // 8168 8101
517 Force_rxflow_en = (1 << 11), // 8168 8101
518 Force_txflow_en = (1 << 10), // 8168 8101
519 Cxpl_dbg_sel = (1 << 9), // 8168 8101
520 ASF = (1 << 8), // 8168 8101
521 PktCntrDisable = (1 << 7), // 8168 8101
522 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 RxVlan = (1 << 6),
524 RxChkSum = (1 << 5),
525 PCIDAC = (1 << 4),
526 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100527 INTT_0 = 0x0000, // 8168
528 INTT_1 = 0x0001, // 8168
529 INTT_2 = 0x0002, // 8168
530 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100533 TBI_Enable = 0x80,
534 TxFlowCtrl = 0x40,
535 RxFlowCtrl = 0x20,
536 _1000bpsF = 0x10,
537 _100bps = 0x08,
538 _10bps = 0x04,
539 LinkStatus = 0x02,
540 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100543 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200544
545 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100546 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547};
548
Francois Romieu2b7b4312011-04-18 22:53:24 -0700549enum rtl_desc_bit {
550 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
552 RingEnd = (1 << 30), /* End of descriptor ring */
553 FirstFrag = (1 << 29), /* First segment of a packet */
554 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700555};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Francois Romieu2b7b4312011-04-18 22:53:24 -0700557/* Generic case. */
558enum rtl_tx_desc_bit {
559 /* First doubleword. */
560 TD_LSO = (1 << 27), /* Large Send Offload */
561#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Francois Romieu2b7b4312011-04-18 22:53:24 -0700563 /* Second doubleword. */
564 TxVlanTag = (1 << 17), /* Add VLAN tag */
565};
566
567/* 8169, 8168b and 810x except 8102e. */
568enum rtl_tx_desc_bit_0 {
569 /* First doubleword. */
570#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
571 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
572 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
573 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
574};
575
576/* 8102e, 8168c and beyond. */
577enum rtl_tx_desc_bit_1 {
578 /* Second doubleword. */
579#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
580 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
581 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
582 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
583};
584
585static const struct rtl_tx_desc_info {
586 struct {
587 u32 udp;
588 u32 tcp;
589 } checksum;
590 u16 mss_shift;
591 u16 opts_offset;
592} tx_desc_info [] = {
593 [RTL_TD_0] = {
594 .checksum = {
595 .udp = TD0_IP_CS | TD0_UDP_CS,
596 .tcp = TD0_IP_CS | TD0_TCP_CS
597 },
598 .mss_shift = TD0_MSS_SHIFT,
599 .opts_offset = 0
600 },
601 [RTL_TD_1] = {
602 .checksum = {
603 .udp = TD1_IP_CS | TD1_UDP_CS,
604 .tcp = TD1_IP_CS | TD1_TCP_CS
605 },
606 .mss_shift = TD1_MSS_SHIFT,
607 .opts_offset = 1
608 }
609};
610
611enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /* Rx private */
613 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
614 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
615
616#define RxProtoUDP (PID1)
617#define RxProtoTCP (PID0)
618#define RxProtoIP (PID1 | PID0)
619#define RxProtoMask RxProtoIP
620
621 IPFail = (1 << 16), /* IP checksum failed */
622 UDPFail = (1 << 15), /* UDP/IP checksum failed */
623 TCPFail = (1 << 14), /* TCP/IP checksum failed */
624 RxVlanTag = (1 << 16), /* VLAN tag available */
625};
626
627#define RsvdMask 0x3fffc000
628
629struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200630 __le32 opts1;
631 __le32 opts2;
632 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633};
634
635struct RxDesc {
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 ring_info {
642 struct sk_buff *skb;
643 u32 len;
644 u8 __pad[sizeof(void *) - sizeof(u32)];
645};
646
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200647enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200648 RTL_FEATURE_WOL = (1 << 0),
649 RTL_FEATURE_MSI = (1 << 1),
650 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200651};
652
Ivan Vecera355423d2009-02-06 21:49:57 -0800653struct rtl8169_counters {
654 __le64 tx_packets;
655 __le64 rx_packets;
656 __le64 tx_errors;
657 __le32 rx_errors;
658 __le16 rx_missed;
659 __le16 align_errors;
660 __le32 tx_one_collision;
661 __le32 tx_multi_collision;
662 __le64 rx_unicast;
663 __le64 rx_broadcast;
664 __le32 rx_multicast;
665 __le16 tx_aborted;
666 __le16 tx_underun;
667};
668
Francois Romieuda78dbf2012-01-26 14:18:23 +0100669enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100670 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100671 RTL_FLAG_TASK_SLOW_PENDING,
672 RTL_FLAG_TASK_RESET_PENDING,
673 RTL_FLAG_TASK_PHY_PENDING,
674 RTL_FLAG_MAX
675};
676
Junchang Wang8027aa22012-03-04 23:30:32 +0100677struct rtl8169_stats {
678 u64 packets;
679 u64 bytes;
680 struct u64_stats_sync syncp;
681};
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683struct rtl8169_private {
684 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200685 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000686 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700687 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200688 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700689 u16 txd_version;
690 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
692 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
693 u32 dirty_rx;
694 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100695 struct rtl8169_stats rx_stats;
696 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
698 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
699 dma_addr_t TxPhyAddr;
700 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000701 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 struct timer_list timer;
704 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100705
706 u16 event_slow;
françois romieuc0e45c12011-01-03 15:08:04 +0000707
708 struct mdio_ops {
709 void (*write)(void __iomem *, int, int);
710 int (*read)(void __iomem *, int);
711 } mdio_ops;
712
françois romieu065c27c2011-01-03 15:08:12 +0000713 struct pll_power_ops {
714 void (*down)(struct rtl8169_private *);
715 void (*up)(struct rtl8169_private *);
716 } pll_power_ops;
717
Francois Romieud58d46b2011-05-03 16:38:29 +0200718 struct jumbo_ops {
719 void (*enable)(struct rtl8169_private *);
720 void (*disable)(struct rtl8169_private *);
721 } jumbo_ops;
722
Oliver Neukum54405cd2011-01-06 21:55:13 +0100723 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200724 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000725 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100726 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000727 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800729 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100730
731 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100732 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
733 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100734 struct work_struct work;
735 } wk;
736
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200737 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200738
739 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800740 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000741 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400742 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000743
Francois Romieub6ffd972011-06-17 17:00:05 +0200744 struct rtl_fw {
745 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200746
747#define RTL_VER_SIZE 32
748
749 char version[RTL_VER_SIZE];
750
751 struct rtl_fw_phy_action {
752 __le32 *code;
753 size_t size;
754 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200755 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300756#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757};
758
Ralf Baechle979b6c12005-06-13 14:30:40 -0700759MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700762MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200763module_param_named(debug, debug.msg_enable, int, 0);
764MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765MODULE_LICENSE("GPL");
766MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000767MODULE_FIRMWARE(FIRMWARE_8168D_1);
768MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000769MODULE_FIRMWARE(FIRMWARE_8168E_1);
770MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400771MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800772MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800773MODULE_FIRMWARE(FIRMWARE_8168F_1);
774MODULE_FIRMWARE(FIRMWARE_8168F_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Francois Romieuda78dbf2012-01-26 14:18:23 +0100776static void rtl_lock_work(struct rtl8169_private *tp)
777{
778 mutex_lock(&tp->wk.mutex);
779}
780
781static void rtl_unlock_work(struct rtl8169_private *tp)
782{
783 mutex_unlock(&tp->wk.mutex);
784}
785
Francois Romieud58d46b2011-05-03 16:38:29 +0200786static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
787{
788 int cap = pci_pcie_cap(pdev);
789
790 if (cap) {
791 u16 ctl;
792
793 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
794 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
795 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
796 }
797}
798
françois romieub646d902011-01-03 15:08:21 +0000799static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
800{
801 void __iomem *ioaddr = tp->mmio_addr;
802 int i;
803
804 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
805 for (i = 0; i < 20; i++) {
806 udelay(100);
807 if (RTL_R32(OCPAR) & OCPAR_FLAG)
808 break;
809 }
810 return RTL_R32(OCPDR);
811}
812
813static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
814{
815 void __iomem *ioaddr = tp->mmio_addr;
816 int i;
817
818 RTL_W32(OCPDR, data);
819 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
820 for (i = 0; i < 20; i++) {
821 udelay(100);
822 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
823 break;
824 }
825}
826
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800827static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000828{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800829 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000830 int i;
831
832 RTL_W8(ERIDR, cmd);
833 RTL_W32(ERIAR, 0x800010e8);
834 msleep(2);
835 for (i = 0; i < 5; i++) {
836 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200837 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000838 break;
839 }
840
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800841 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000842}
843
844#define OOB_CMD_RESET 0x00
845#define OOB_CMD_DRIVER_START 0x05
846#define OOB_CMD_DRIVER_STOP 0x06
847
Francois Romieucecb5fd2011-04-01 10:21:07 +0200848static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
849{
850 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
851}
852
françois romieub646d902011-01-03 15:08:21 +0000853static void rtl8168_driver_start(struct rtl8169_private *tp)
854{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200855 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000856 int i;
857
858 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
859
Francois Romieucecb5fd2011-04-01 10:21:07 +0200860 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000861
françois romieub646d902011-01-03 15:08:21 +0000862 for (i = 0; i < 10; i++) {
863 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000864 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000865 break;
866 }
867}
868
869static void rtl8168_driver_stop(struct rtl8169_private *tp)
870{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200871 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000872 int i;
873
874 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
875
Francois Romieucecb5fd2011-04-01 10:21:07 +0200876 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000877
françois romieub646d902011-01-03 15:08:21 +0000878 for (i = 0; i < 10; i++) {
879 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000880 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000881 break;
882 }
883}
884
hayeswang4804b3b2011-03-21 01:50:29 +0000885static int r8168dp_check_dash(struct rtl8169_private *tp)
886{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200887 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000888
Francois Romieucecb5fd2011-04-01 10:21:07 +0200889 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000890}
françois romieub646d902011-01-03 15:08:21 +0000891
françois romieu4da19632011-01-03 15:07:55 +0000892static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 int i;
895
Francois Romieua6baf3a2007-11-08 23:23:21 +0100896 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Francois Romieu23714082006-01-29 00:49:09 +0100898 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100899 /*
900 * Check if the RTL8169 has completed writing to the specified
901 * MII register.
902 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200903 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 break;
Francois Romieu23714082006-01-29 00:49:09 +0100905 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700907 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700908 * According to hardware specs a 20us delay is required after write
909 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700910 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700911 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
françois romieu4da19632011-01-03 15:07:55 +0000914static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 int i, value = -1;
917
Francois Romieua6baf3a2007-11-08 23:23:21 +0100918 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Francois Romieu23714082006-01-29 00:49:09 +0100920 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100921 /*
922 * Check if the RTL8169 has completed retrieving data from
923 * the specified MII register.
924 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100926 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 break;
928 }
Francois Romieu23714082006-01-29 00:49:09 +0100929 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700931 /*
932 * According to hardware specs a 20us delay is required after read
933 * complete indication, but before sending next command.
934 */
935 udelay(20);
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return value;
938}
939
françois romieuc0e45c12011-01-03 15:08:04 +0000940static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
941{
942 int i;
943
944 RTL_W32(OCPDR, data |
945 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
946 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
947 RTL_W32(EPHY_RXER_NUM, 0);
948
949 for (i = 0; i < 100; i++) {
950 mdelay(1);
951 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
952 break;
953 }
954}
955
956static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
957{
958 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
959 (value & OCPDR_DATA_MASK));
960}
961
962static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
963{
964 int i;
965
966 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
967
968 mdelay(1);
969 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
970 RTL_W32(EPHY_RXER_NUM, 0);
971
972 for (i = 0; i < 100; i++) {
973 mdelay(1);
974 if (RTL_R32(OCPAR) & OCPAR_FLAG)
975 break;
976 }
977
978 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
979}
980
françois romieue6de30d2011-01-03 15:08:37 +0000981#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
982
983static void r8168dp_2_mdio_start(void __iomem *ioaddr)
984{
985 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
986}
987
988static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
989{
990 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
991}
992
993static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
994{
995 r8168dp_2_mdio_start(ioaddr);
996
997 r8169_mdio_write(ioaddr, reg_addr, value);
998
999 r8168dp_2_mdio_stop(ioaddr);
1000}
1001
1002static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
1003{
1004 int value;
1005
1006 r8168dp_2_mdio_start(ioaddr);
1007
1008 value = r8169_mdio_read(ioaddr, reg_addr);
1009
1010 r8168dp_2_mdio_stop(ioaddr);
1011
1012 return value;
1013}
1014
françois romieu4da19632011-01-03 15:07:55 +00001015static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001016{
françois romieuc0e45c12011-01-03 15:08:04 +00001017 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001018}
1019
françois romieu4da19632011-01-03 15:07:55 +00001020static int rtl_readphy(struct rtl8169_private *tp, int location)
1021{
françois romieuc0e45c12011-01-03 15:08:04 +00001022 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +00001023}
1024
1025static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1026{
1027 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1028}
1029
1030static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001031{
1032 int val;
1033
françois romieu4da19632011-01-03 15:07:55 +00001034 val = rtl_readphy(tp, reg_addr);
1035 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001036}
1037
Francois Romieuccdffb92008-07-26 14:26:06 +02001038static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1039 int val)
1040{
1041 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001042
françois romieu4da19632011-01-03 15:07:55 +00001043 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001044}
1045
1046static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1047{
1048 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001049
françois romieu4da19632011-01-03 15:07:55 +00001050 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001051}
1052
Francois Romieudacf8152008-08-02 20:44:13 +02001053static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
1054{
1055 unsigned int i;
1056
1057 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1058 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1059
1060 for (i = 0; i < 100; i++) {
1061 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1062 break;
1063 udelay(10);
1064 }
1065}
1066
1067static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1068{
1069 u16 value = 0xffff;
1070 unsigned int i;
1071
1072 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1073
1074 for (i = 0; i < 100; i++) {
1075 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1076 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1077 break;
1078 }
1079 udelay(10);
1080 }
1081
1082 return value;
1083}
1084
1085static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1086{
1087 unsigned int i;
1088
1089 RTL_W32(CSIDR, value);
1090 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1091 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1092
1093 for (i = 0; i < 100; i++) {
1094 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1095 break;
1096 udelay(10);
1097 }
1098}
1099
1100static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1101{
1102 u32 value = ~0x00;
1103 unsigned int i;
1104
1105 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1106 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1107
1108 for (i = 0; i < 100; i++) {
1109 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1110 value = RTL_R32(CSIDR);
1111 break;
1112 }
1113 udelay(10);
1114 }
1115
1116 return value;
1117}
1118
Hayes Wang133ac402011-07-06 15:58:05 +08001119static
1120void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1121{
1122 unsigned int i;
1123
1124 BUG_ON((addr & 3) || (mask == 0));
1125 RTL_W32(ERIDR, val);
1126 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1127
1128 for (i = 0; i < 100; i++) {
1129 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1130 break;
1131 udelay(100);
1132 }
1133}
1134
1135static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1136{
1137 u32 value = ~0x00;
1138 unsigned int i;
1139
1140 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1141
1142 for (i = 0; i < 100; i++) {
1143 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1144 value = RTL_R32(ERIDR);
1145 break;
1146 }
1147 udelay(100);
1148 }
1149
1150 return value;
1151}
1152
1153static void
1154rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1155{
1156 u32 val;
1157
1158 val = rtl_eri_read(ioaddr, addr, type);
1159 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1160}
1161
françois romieuc28aa382011-08-02 03:53:43 +00001162struct exgmac_reg {
1163 u16 addr;
1164 u16 mask;
1165 u32 val;
1166};
1167
1168static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1169 const struct exgmac_reg *r, int len)
1170{
1171 while (len-- > 0) {
1172 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1173 r++;
1174 }
1175}
1176
françois romieudaf9df62009-10-07 12:44:20 +00001177static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1178{
1179 u8 value = 0xff;
1180 unsigned int i;
1181
1182 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1183
1184 for (i = 0; i < 300; i++) {
1185 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1186 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1187 break;
1188 }
1189 udelay(100);
1190 }
1191
1192 return value;
1193}
1194
Francois Romieu9085cdf2012-01-26 12:59:08 +01001195static u16 rtl_get_events(struct rtl8169_private *tp)
1196{
1197 void __iomem *ioaddr = tp->mmio_addr;
1198
1199 return RTL_R16(IntrStatus);
1200}
1201
1202static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1203{
1204 void __iomem *ioaddr = tp->mmio_addr;
1205
1206 RTL_W16(IntrStatus, bits);
1207 mmiowb();
1208}
1209
1210static void rtl_irq_disable(struct rtl8169_private *tp)
1211{
1212 void __iomem *ioaddr = tp->mmio_addr;
1213
1214 RTL_W16(IntrMask, 0);
1215 mmiowb();
1216}
1217
Francois Romieu3e990ff2012-01-26 12:50:01 +01001218static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1219{
1220 void __iomem *ioaddr = tp->mmio_addr;
1221
1222 RTL_W16(IntrMask, bits);
1223}
1224
Francois Romieuda78dbf2012-01-26 14:18:23 +01001225#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1226#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1227#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1228
1229static void rtl_irq_enable_all(struct rtl8169_private *tp)
1230{
1231 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1232}
1233
françois romieu811fd302011-12-04 20:30:45 +00001234static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
françois romieu811fd302011-12-04 20:30:45 +00001236 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Francois Romieu9085cdf2012-01-26 12:59:08 +01001238 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001239 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001240 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
françois romieu4da19632011-01-03 15:07:55 +00001243static unsigned int rtl8169_tbi_reset_pending(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 return RTL_R32(TBICSR) & TBIReset;
1248}
1249
françois romieu4da19632011-01-03 15:07:55 +00001250static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
françois romieu4da19632011-01-03 15:07:55 +00001252 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
1255static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1256{
1257 return RTL_R32(TBICSR) & TBILinkOk;
1258}
1259
1260static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1261{
1262 return RTL_R8(PHYstatus) & LinkStatus;
1263}
1264
françois romieu4da19632011-01-03 15:07:55 +00001265static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
françois romieu4da19632011-01-03 15:07:55 +00001267 void __iomem *ioaddr = tp->mmio_addr;
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1270}
1271
françois romieu4da19632011-01-03 15:07:55 +00001272static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
1274 unsigned int val;
1275
françois romieu4da19632011-01-03 15:07:55 +00001276 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1277 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
Hayes Wang70090422011-07-06 15:58:06 +08001280static void rtl_link_chg_patch(struct rtl8169_private *tp)
1281{
1282 void __iomem *ioaddr = tp->mmio_addr;
1283 struct net_device *dev = tp->dev;
1284
1285 if (!netif_running(dev))
1286 return;
1287
1288 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1289 if (RTL_R8(PHYstatus) & _1000bpsF) {
1290 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1291 0x00000011, ERIAR_EXGMAC);
1292 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1293 0x00000005, ERIAR_EXGMAC);
1294 } else if (RTL_R8(PHYstatus) & _100bps) {
1295 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1296 0x0000001f, ERIAR_EXGMAC);
1297 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1298 0x00000005, ERIAR_EXGMAC);
1299 } else {
1300 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1301 0x0000001f, ERIAR_EXGMAC);
1302 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1303 0x0000003f, ERIAR_EXGMAC);
1304 }
1305 /* Reset packet filter */
1306 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1307 ERIAR_EXGMAC);
1308 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1309 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001310 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1311 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1312 if (RTL_R8(PHYstatus) & _1000bpsF) {
1313 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1314 0x00000011, ERIAR_EXGMAC);
1315 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1316 0x00000005, ERIAR_EXGMAC);
1317 } else {
1318 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1319 0x0000001f, ERIAR_EXGMAC);
1320 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1321 0x0000003f, ERIAR_EXGMAC);
1322 }
Hayes Wang70090422011-07-06 15:58:06 +08001323 }
1324}
1325
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001326static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001327 struct rtl8169_private *tp,
1328 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001331 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001332 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001333 if (pm)
1334 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001336 if (net_ratelimit())
1337 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001338 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001340 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001341 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001342 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001346static void rtl8169_check_link_status(struct net_device *dev,
1347 struct rtl8169_private *tp,
1348 void __iomem *ioaddr)
1349{
1350 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1351}
1352
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001353#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1354
1355static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1356{
1357 void __iomem *ioaddr = tp->mmio_addr;
1358 u8 options;
1359 u32 wolopts = 0;
1360
1361 options = RTL_R8(Config1);
1362 if (!(options & PMEnable))
1363 return 0;
1364
1365 options = RTL_R8(Config3);
1366 if (options & LinkUp)
1367 wolopts |= WAKE_PHY;
1368 if (options & MagicPacket)
1369 wolopts |= WAKE_MAGIC;
1370
1371 options = RTL_R8(Config5);
1372 if (options & UWF)
1373 wolopts |= WAKE_UCAST;
1374 if (options & BWF)
1375 wolopts |= WAKE_BCAST;
1376 if (options & MWF)
1377 wolopts |= WAKE_MCAST;
1378
1379 return wolopts;
1380}
1381
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001382static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1383{
1384 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001385
Francois Romieuda78dbf2012-01-26 14:18:23 +01001386 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001387
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001388 wol->supported = WAKE_ANY;
1389 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001390
Francois Romieuda78dbf2012-01-26 14:18:23 +01001391 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001392}
1393
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001394static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001395{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001396 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001397 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001398 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001399 u32 opt;
1400 u16 reg;
1401 u8 mask;
1402 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001403 { WAKE_PHY, Config3, LinkUp },
1404 { WAKE_MAGIC, Config3, MagicPacket },
1405 { WAKE_UCAST, Config5, UWF },
1406 { WAKE_BCAST, Config5, BWF },
1407 { WAKE_MCAST, Config5, MWF },
1408 { WAKE_ANY, Config5, LanWake }
1409 };
Francois Romieu34ffca42012-10-06 11:19:52 +02001410 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001411
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001412 RTL_W8(Cfg9346, Cfg9346_Unlock);
1413
1414 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
Francois Romieu34ffca42012-10-06 11:19:52 +02001415 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001416 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001417 options |= cfg[i].mask;
1418 RTL_W8(cfg[i].reg, options);
1419 }
1420
Francois Romieu34ffca42012-10-06 11:19:52 +02001421 switch (tp->mac_version) {
1422 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1423 options = RTL_R8(Config1) & ~PMEnable;
1424 if (wolopts)
1425 options |= PMEnable;
1426 RTL_W8(Config1, options);
1427 break;
1428 default:
1429 break;
1430 }
1431
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001432 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001433}
1434
1435static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1436{
1437 struct rtl8169_private *tp = netdev_priv(dev);
1438
Francois Romieuda78dbf2012-01-26 14:18:23 +01001439 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001440
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001441 if (wol->wolopts)
1442 tp->features |= RTL_FEATURE_WOL;
1443 else
1444 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001445 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001446
1447 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001448
françois romieuea809072010-11-08 13:23:58 +00001449 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1450
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001451 return 0;
1452}
1453
Francois Romieu31bd2042011-04-26 18:58:59 +02001454static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1455{
Francois Romieu85bffe62011-04-27 08:22:39 +02001456 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001457}
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459static void rtl8169_get_drvinfo(struct net_device *dev,
1460 struct ethtool_drvinfo *info)
1461{
1462 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001463 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Rick Jones68aad782011-11-07 13:29:27 +00001465 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1466 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1467 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001468 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001469 if (!IS_ERR_OR_NULL(rtl_fw))
1470 strlcpy(info->fw_version, rtl_fw->version,
1471 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472}
1473
1474static int rtl8169_get_regs_len(struct net_device *dev)
1475{
1476 return R8169_REGS_SIZE;
1477}
1478
1479static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001480 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 struct rtl8169_private *tp = netdev_priv(dev);
1483 void __iomem *ioaddr = tp->mmio_addr;
1484 int ret = 0;
1485 u32 reg;
1486
1487 reg = RTL_R32(TBICSR);
1488 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1489 (duplex == DUPLEX_FULL)) {
1490 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1491 } else if (autoneg == AUTONEG_ENABLE)
1492 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1493 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001494 netif_warn(tp, link, dev,
1495 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 ret = -EOPNOTSUPP;
1497 }
1498
1499 return ret;
1500}
1501
1502static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001503 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
1505 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001506 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001507 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Hayes Wang716b50a2011-02-22 17:26:18 +08001509 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001512 int auto_nego;
1513
françois romieu4da19632011-01-03 15:07:55 +00001514 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001515 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1516 ADVERTISE_100HALF | ADVERTISE_100FULL);
1517
1518 if (adv & ADVERTISED_10baseT_Half)
1519 auto_nego |= ADVERTISE_10HALF;
1520 if (adv & ADVERTISED_10baseT_Full)
1521 auto_nego |= ADVERTISE_10FULL;
1522 if (adv & ADVERTISED_100baseT_Half)
1523 auto_nego |= ADVERTISE_100HALF;
1524 if (adv & ADVERTISED_100baseT_Full)
1525 auto_nego |= ADVERTISE_100FULL;
1526
françois romieu3577aa12009-05-19 10:46:48 +00001527 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1528
françois romieu4da19632011-01-03 15:07:55 +00001529 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001530 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1531
1532 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001533 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001534 if (adv & ADVERTISED_1000baseT_Half)
1535 giga_ctrl |= ADVERTISE_1000HALF;
1536 if (adv & ADVERTISED_1000baseT_Full)
1537 giga_ctrl |= ADVERTISE_1000FULL;
1538 } else if (adv & (ADVERTISED_1000baseT_Half |
1539 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001540 netif_info(tp, link, dev,
1541 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001542 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
françois romieu3577aa12009-05-19 10:46:48 +00001545 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001546
françois romieu4da19632011-01-03 15:07:55 +00001547 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1548 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001549 } else {
1550 giga_ctrl = 0;
1551
1552 if (speed == SPEED_10)
1553 bmcr = 0;
1554 else if (speed == SPEED_100)
1555 bmcr = BMCR_SPEED100;
1556 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001557 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001558
1559 if (duplex == DUPLEX_FULL)
1560 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001561 }
1562
françois romieu4da19632011-01-03 15:07:55 +00001563 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001564
Francois Romieucecb5fd2011-04-01 10:21:07 +02001565 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1566 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001567 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001568 rtl_writephy(tp, 0x17, 0x2138);
1569 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001570 } else {
françois romieu4da19632011-01-03 15:07:55 +00001571 rtl_writephy(tp, 0x17, 0x2108);
1572 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001573 }
1574 }
1575
Oliver Neukum54405cd2011-01-06 21:55:13 +01001576 rc = 0;
1577out:
1578 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
1581static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001582 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
1584 struct rtl8169_private *tp = netdev_priv(dev);
1585 int ret;
1586
Oliver Neukum54405cd2011-01-06 21:55:13 +01001587 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001588 if (ret < 0)
1589 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Francois Romieu4876cc12011-03-11 21:07:11 +01001591 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1592 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001594 }
1595out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 return ret;
1597}
1598
1599static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1600{
1601 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 int ret;
1603
Francois Romieu4876cc12011-03-11 21:07:11 +01001604 del_timer_sync(&tp->timer);
1605
Francois Romieuda78dbf2012-01-26 14:18:23 +01001606 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001607 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001608 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001609 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 return ret;
1612}
1613
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001614static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1615 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
Francois Romieud58d46b2011-05-03 16:38:29 +02001617 struct rtl8169_private *tp = netdev_priv(dev);
1618
Francois Romieu2b7b4312011-04-18 22:53:24 -07001619 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001620 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Francois Romieud58d46b2011-05-03 16:38:29 +02001622 if (dev->mtu > JUMBO_1K &&
1623 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1624 features &= ~NETIF_F_IP_CSUM;
1625
Michał Mirosław350fb322011-04-08 06:35:56 +00001626 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627}
1628
Francois Romieuda78dbf2012-01-26 14:18:23 +01001629static void __rtl8169_set_features(struct net_device *dev,
1630 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
1632 struct rtl8169_private *tp = netdev_priv(dev);
Ben Greear6bbe0212012-02-10 15:04:33 +00001633 netdev_features_t changed = features ^ dev->features;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001634 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Ben Greear6bbe0212012-02-10 15:04:33 +00001636 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)))
1637 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Ben Greear6bbe0212012-02-10 15:04:33 +00001639 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)) {
1640 if (features & NETIF_F_RXCSUM)
1641 tp->cp_cmd |= RxChkSum;
1642 else
1643 tp->cp_cmd &= ~RxChkSum;
Michał Mirosław350fb322011-04-08 06:35:56 +00001644
Ben Greear6bbe0212012-02-10 15:04:33 +00001645 if (dev->features & NETIF_F_HW_VLAN_RX)
1646 tp->cp_cmd |= RxVlan;
1647 else
1648 tp->cp_cmd &= ~RxVlan;
1649
1650 RTL_W16(CPlusCmd, tp->cp_cmd);
1651 RTL_R16(CPlusCmd);
1652 }
1653 if (changed & NETIF_F_RXALL) {
1654 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1655 if (features & NETIF_F_RXALL)
1656 tmp |= (AcceptErr | AcceptRunt);
1657 RTL_W32(RxConfig, tmp);
1658 }
Francois Romieuda78dbf2012-01-26 14:18:23 +01001659}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Francois Romieuda78dbf2012-01-26 14:18:23 +01001661static int rtl8169_set_features(struct net_device *dev,
1662 netdev_features_t features)
1663{
1664 struct rtl8169_private *tp = netdev_priv(dev);
1665
1666 rtl_lock_work(tp);
1667 __rtl8169_set_features(dev, features);
1668 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670 return 0;
1671}
1672
Francois Romieuda78dbf2012-01-26 14:18:23 +01001673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1675 struct sk_buff *skb)
1676{
Jesse Grosseab6d182010-10-20 13:56:03 +00001677 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1679}
1680
Francois Romieu7a8fc772011-03-01 17:18:33 +01001681static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
1683 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Francois Romieu7a8fc772011-03-01 17:18:33 +01001685 if (opts2 & RxVlanTag)
1686 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
Francois Romieuccdffb92008-07-26 14:26:06 +02001691static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693 struct rtl8169_private *tp = netdev_priv(dev);
1694 void __iomem *ioaddr = tp->mmio_addr;
1695 u32 status;
1696
1697 cmd->supported =
1698 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1699 cmd->port = PORT_FIBRE;
1700 cmd->transceiver = XCVR_INTERNAL;
1701
1702 status = RTL_R32(TBICSR);
1703 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1704 cmd->autoneg = !!(status & TBINwEnable);
1705
David Decotigny70739492011-04-27 18:32:40 +00001706 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001708
1709 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710}
1711
Francois Romieuccdffb92008-07-26 14:26:06 +02001712static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Francois Romieuccdffb92008-07-26 14:26:06 +02001716 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
1719static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1720{
1721 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001722 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Francois Romieuda78dbf2012-01-26 14:18:23 +01001724 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02001725 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001726 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Francois Romieuccdffb92008-07-26 14:26:06 +02001728 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729}
1730
1731static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1732 void *p)
1733{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001734 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Francois Romieu5b0384f2006-08-16 16:00:01 +02001736 if (regs->len > R8169_REGS_SIZE)
1737 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Francois Romieuda78dbf2012-01-26 14:18:23 +01001739 rtl_lock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001740 memcpy_fromio(p, tp->mmio_addr, regs->len);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001741 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742}
1743
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001744static u32 rtl8169_get_msglevel(struct net_device *dev)
1745{
1746 struct rtl8169_private *tp = netdev_priv(dev);
1747
1748 return tp->msg_enable;
1749}
1750
1751static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1752{
1753 struct rtl8169_private *tp = netdev_priv(dev);
1754
1755 tp->msg_enable = value;
1756}
1757
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001758static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1759 "tx_packets",
1760 "rx_packets",
1761 "tx_errors",
1762 "rx_errors",
1763 "rx_missed",
1764 "align_errors",
1765 "tx_single_collisions",
1766 "tx_multi_collisions",
1767 "unicast",
1768 "broadcast",
1769 "multicast",
1770 "tx_aborted",
1771 "tx_underrun",
1772};
1773
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001774static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001775{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001776 switch (sset) {
1777 case ETH_SS_STATS:
1778 return ARRAY_SIZE(rtl8169_gstrings);
1779 default:
1780 return -EOPNOTSUPP;
1781 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001782}
1783
Ivan Vecera355423d2009-02-06 21:49:57 -08001784static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001785{
1786 struct rtl8169_private *tp = netdev_priv(dev);
1787 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001788 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001789 struct rtl8169_counters *counters;
1790 dma_addr_t paddr;
1791 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001792 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001793
Ivan Vecera355423d2009-02-06 21:49:57 -08001794 /*
1795 * Some chips are unable to dump tally counters when the receiver
1796 * is disabled.
1797 */
1798 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1799 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001800
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001801 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001802 if (!counters)
1803 return;
1804
1805 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001806 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001807 RTL_W32(CounterAddrLow, cmd);
1808 RTL_W32(CounterAddrLow, cmd | CounterDump);
1809
Ivan Vecera355423d2009-02-06 21:49:57 -08001810 while (wait--) {
1811 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001812 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001813 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001814 }
1815 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001816 }
1817
1818 RTL_W32(CounterAddrLow, 0);
1819 RTL_W32(CounterAddrHigh, 0);
1820
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001821 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001822}
1823
Ivan Vecera355423d2009-02-06 21:49:57 -08001824static void rtl8169_get_ethtool_stats(struct net_device *dev,
1825 struct ethtool_stats *stats, u64 *data)
1826{
1827 struct rtl8169_private *tp = netdev_priv(dev);
1828
1829 ASSERT_RTNL();
1830
1831 rtl8169_update_counters(dev);
1832
1833 data[0] = le64_to_cpu(tp->counters.tx_packets);
1834 data[1] = le64_to_cpu(tp->counters.rx_packets);
1835 data[2] = le64_to_cpu(tp->counters.tx_errors);
1836 data[3] = le32_to_cpu(tp->counters.rx_errors);
1837 data[4] = le16_to_cpu(tp->counters.rx_missed);
1838 data[5] = le16_to_cpu(tp->counters.align_errors);
1839 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1840 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1841 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1842 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1843 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1844 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1845 data[12] = le16_to_cpu(tp->counters.tx_underun);
1846}
1847
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001848static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1849{
1850 switch(stringset) {
1851 case ETH_SS_STATS:
1852 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1853 break;
1854 }
1855}
1856
Jeff Garzik7282d492006-09-13 14:30:00 -04001857static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 .get_drvinfo = rtl8169_get_drvinfo,
1859 .get_regs_len = rtl8169_get_regs_len,
1860 .get_link = ethtool_op_get_link,
1861 .get_settings = rtl8169_get_settings,
1862 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001863 .get_msglevel = rtl8169_get_msglevel,
1864 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001866 .get_wol = rtl8169_get_wol,
1867 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001868 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001869 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001870 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871};
1872
Francois Romieu07d3f512007-02-21 22:40:46 +01001873static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001874 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
Francois Romieu5d320a22011-05-08 17:47:36 +02001876 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001877 /*
1878 * The driver currently handles the 8168Bf and the 8168Be identically
1879 * but they can be identified more specifically through the test below
1880 * if needed:
1881 *
1882 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001883 *
1884 * Same thing for the 8101Eb and the 8101Ec:
1885 *
1886 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001887 */
Francois Romieu37441002011-06-17 22:58:54 +02001888 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001890 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 int mac_version;
1892 } mac_info[] = {
Hayes Wangc2218922011-09-06 16:55:18 +08001893 /* 8168F family. */
1894 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1895 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1896
hayeswang01dc7fe2011-03-21 01:50:28 +00001897 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001898 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001899 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1900 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1901 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1902
Francois Romieu5b538df2008-07-20 16:22:45 +02001903 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001904 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1905 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001906 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001907
françois romieue6de30d2011-01-03 15:08:37 +00001908 /* 8168DP family. */
1909 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1910 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001911 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001912
Francois Romieuef808d52008-06-29 13:10:54 +02001913 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001914 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001915 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001916 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001917 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001918 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1919 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001920 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001921 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001922 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001923
1924 /* 8168B family. */
1925 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1926 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1927 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1928 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1929
1930 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001931 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001932 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1933 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1934 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001935 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1936 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1937 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1938 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1939 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1940 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001941 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001942 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001943 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001944 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1945 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001946 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1947 /* FIXME: where did these entries come from ? -- FR */
1948 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1949 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1950
1951 /* 8110 family. */
1952 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1953 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1954 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1955 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1956 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1957 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1958
Jean Delvaref21b75e2009-05-26 20:54:48 -07001959 /* Catch-all */
1960 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001961 };
1962 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 u32 reg;
1964
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001965 reg = RTL_R32(TxConfig);
1966 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 p++;
1968 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001969
1970 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1971 netif_notice(tp, probe, dev,
1972 "unknown MAC, using family default\n");
1973 tp->mac_version = default_version;
1974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975}
1976
1977static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1978{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001979 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980}
1981
Francois Romieu867763c2007-08-17 18:21:58 +02001982struct phy_reg {
1983 u16 reg;
1984 u16 val;
1985};
1986
françois romieu4da19632011-01-03 15:07:55 +00001987static void rtl_writephy_batch(struct rtl8169_private *tp,
1988 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001989{
1990 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001991 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001992 regs++;
1993 }
1994}
1995
françois romieubca03d52011-01-03 15:07:31 +00001996#define PHY_READ 0x00000000
1997#define PHY_DATA_OR 0x10000000
1998#define PHY_DATA_AND 0x20000000
1999#define PHY_BJMPN 0x30000000
2000#define PHY_READ_EFUSE 0x40000000
2001#define PHY_READ_MAC_BYTE 0x50000000
2002#define PHY_WRITE_MAC_BYTE 0x60000000
2003#define PHY_CLEAR_READCOUNT 0x70000000
2004#define PHY_WRITE 0x80000000
2005#define PHY_READCOUNT_EQ_SKIP 0x90000000
2006#define PHY_COMP_EQ_SKIPN 0xa0000000
2007#define PHY_COMP_NEQ_SKIPN 0xb0000000
2008#define PHY_WRITE_PREVIOUS 0xc0000000
2009#define PHY_SKIPN 0xd0000000
2010#define PHY_DELAY_MS 0xe0000000
2011#define PHY_WRITE_ERI_WORD 0xf0000000
2012
Hayes Wang960aee62011-06-18 11:37:48 +02002013struct fw_info {
2014 u32 magic;
2015 char version[RTL_VER_SIZE];
2016 __le32 fw_start;
2017 __le32 fw_len;
2018 u8 chksum;
2019} __packed;
2020
Francois Romieu1c361ef2011-06-17 17:16:24 +02002021#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2022
2023static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002024{
Francois Romieub6ffd972011-06-17 17:00:05 +02002025 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002026 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002027 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2028 char *version = rtl_fw->version;
2029 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002030
Francois Romieu1c361ef2011-06-17 17:16:24 +02002031 if (fw->size < FW_OPCODE_SIZE)
2032 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002033
2034 if (!fw_info->magic) {
2035 size_t i, size, start;
2036 u8 checksum = 0;
2037
2038 if (fw->size < sizeof(*fw_info))
2039 goto out;
2040
2041 for (i = 0; i < fw->size; i++)
2042 checksum += fw->data[i];
2043 if (checksum != 0)
2044 goto out;
2045
2046 start = le32_to_cpu(fw_info->fw_start);
2047 if (start > fw->size)
2048 goto out;
2049
2050 size = le32_to_cpu(fw_info->fw_len);
2051 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2052 goto out;
2053
2054 memcpy(version, fw_info->version, RTL_VER_SIZE);
2055
2056 pa->code = (__le32 *)(fw->data + start);
2057 pa->size = size;
2058 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002059 if (fw->size % FW_OPCODE_SIZE)
2060 goto out;
2061
2062 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2063
2064 pa->code = (__le32 *)fw->data;
2065 pa->size = fw->size / FW_OPCODE_SIZE;
2066 }
2067 version[RTL_VER_SIZE - 1] = 0;
2068
2069 rc = true;
2070out:
2071 return rc;
2072}
2073
Francois Romieufd112f22011-06-18 00:10:29 +02002074static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2075 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002076{
Francois Romieufd112f22011-06-18 00:10:29 +02002077 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002078 size_t index;
2079
Francois Romieu1c361ef2011-06-17 17:16:24 +02002080 for (index = 0; index < pa->size; index++) {
2081 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002082 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002083
hayeswang42b82dc2011-01-10 02:07:25 +00002084 switch(action & 0xf0000000) {
2085 case PHY_READ:
2086 case PHY_DATA_OR:
2087 case PHY_DATA_AND:
2088 case PHY_READ_EFUSE:
2089 case PHY_CLEAR_READCOUNT:
2090 case PHY_WRITE:
2091 case PHY_WRITE_PREVIOUS:
2092 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002093 break;
2094
hayeswang42b82dc2011-01-10 02:07:25 +00002095 case PHY_BJMPN:
2096 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002097 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002098 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002099 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002100 }
2101 break;
2102 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002103 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002104 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002105 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002106 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002107 }
2108 break;
2109 case PHY_COMP_EQ_SKIPN:
2110 case PHY_COMP_NEQ_SKIPN:
2111 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002112 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002113 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002114 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002115 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002116 }
2117 break;
2118
2119 case PHY_READ_MAC_BYTE:
2120 case PHY_WRITE_MAC_BYTE:
2121 case PHY_WRITE_ERI_WORD:
2122 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002123 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002124 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002125 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002126 }
2127 }
Francois Romieufd112f22011-06-18 00:10:29 +02002128 rc = true;
2129out:
2130 return rc;
2131}
françois romieubca03d52011-01-03 15:07:31 +00002132
Francois Romieufd112f22011-06-18 00:10:29 +02002133static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2134{
2135 struct net_device *dev = tp->dev;
2136 int rc = -EINVAL;
2137
2138 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2139 netif_err(tp, ifup, dev, "invalid firwmare\n");
2140 goto out;
2141 }
2142
2143 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2144 rc = 0;
2145out:
2146 return rc;
2147}
2148
2149static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2150{
2151 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2152 u32 predata, count;
2153 size_t index;
2154
2155 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002156
Francois Romieu1c361ef2011-06-17 17:16:24 +02002157 for (index = 0; index < pa->size; ) {
2158 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002159 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002160 u32 regno = (action & 0x0fff0000) >> 16;
2161
2162 if (!action)
2163 break;
françois romieubca03d52011-01-03 15:07:31 +00002164
2165 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002166 case PHY_READ:
2167 predata = rtl_readphy(tp, regno);
2168 count++;
2169 index++;
françois romieubca03d52011-01-03 15:07:31 +00002170 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002171 case PHY_DATA_OR:
2172 predata |= data;
2173 index++;
2174 break;
2175 case PHY_DATA_AND:
2176 predata &= data;
2177 index++;
2178 break;
2179 case PHY_BJMPN:
2180 index -= regno;
2181 break;
2182 case PHY_READ_EFUSE:
2183 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2184 index++;
2185 break;
2186 case PHY_CLEAR_READCOUNT:
2187 count = 0;
2188 index++;
2189 break;
2190 case PHY_WRITE:
2191 rtl_writephy(tp, regno, data);
2192 index++;
2193 break;
2194 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002195 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002196 break;
2197 case PHY_COMP_EQ_SKIPN:
2198 if (predata == data)
2199 index += regno;
2200 index++;
2201 break;
2202 case PHY_COMP_NEQ_SKIPN:
2203 if (predata != data)
2204 index += regno;
2205 index++;
2206 break;
2207 case PHY_WRITE_PREVIOUS:
2208 rtl_writephy(tp, regno, predata);
2209 index++;
2210 break;
2211 case PHY_SKIPN:
2212 index += regno + 1;
2213 break;
2214 case PHY_DELAY_MS:
2215 mdelay(data);
2216 index++;
2217 break;
2218
2219 case PHY_READ_MAC_BYTE:
2220 case PHY_WRITE_MAC_BYTE:
2221 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002222 default:
2223 BUG();
2224 }
2225 }
2226}
2227
françois romieuf1e02ed2011-01-13 13:07:53 +00002228static void rtl_release_firmware(struct rtl8169_private *tp)
2229{
Francois Romieub6ffd972011-06-17 17:00:05 +02002230 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2231 release_firmware(tp->rtl_fw->fw);
2232 kfree(tp->rtl_fw);
2233 }
2234 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002235}
2236
François Romieu953a12c2011-04-24 17:38:48 +02002237static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002238{
Francois Romieub6ffd972011-06-17 17:00:05 +02002239 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002240
2241 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002242 if (!IS_ERR_OR_NULL(rtl_fw))
2243 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002244}
2245
2246static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2247{
2248 if (rtl_readphy(tp, reg) != val)
2249 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2250 else
2251 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002252}
2253
françois romieu4da19632011-01-03 15:07:55 +00002254static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002256 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002257 { 0x1f, 0x0001 },
2258 { 0x06, 0x006e },
2259 { 0x08, 0x0708 },
2260 { 0x15, 0x4000 },
2261 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
françois romieu0b9b5712009-08-10 19:44:56 +00002263 { 0x1f, 0x0001 },
2264 { 0x03, 0x00a1 },
2265 { 0x02, 0x0008 },
2266 { 0x01, 0x0120 },
2267 { 0x00, 0x1000 },
2268 { 0x04, 0x0800 },
2269 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
françois romieu0b9b5712009-08-10 19:44:56 +00002271 { 0x03, 0xff41 },
2272 { 0x02, 0xdf60 },
2273 { 0x01, 0x0140 },
2274 { 0x00, 0x0077 },
2275 { 0x04, 0x7800 },
2276 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
françois romieu0b9b5712009-08-10 19:44:56 +00002278 { 0x03, 0x802f },
2279 { 0x02, 0x4f02 },
2280 { 0x01, 0x0409 },
2281 { 0x00, 0xf0f9 },
2282 { 0x04, 0x9800 },
2283 { 0x04, 0x9000 },
2284
2285 { 0x03, 0xdf01 },
2286 { 0x02, 0xdf20 },
2287 { 0x01, 0xff95 },
2288 { 0x00, 0xba00 },
2289 { 0x04, 0xa800 },
2290 { 0x04, 0xa000 },
2291
2292 { 0x03, 0xff41 },
2293 { 0x02, 0xdf20 },
2294 { 0x01, 0x0140 },
2295 { 0x00, 0x00bb },
2296 { 0x04, 0xb800 },
2297 { 0x04, 0xb000 },
2298
2299 { 0x03, 0xdf41 },
2300 { 0x02, 0xdc60 },
2301 { 0x01, 0x6340 },
2302 { 0x00, 0x007d },
2303 { 0x04, 0xd800 },
2304 { 0x04, 0xd000 },
2305
2306 { 0x03, 0xdf01 },
2307 { 0x02, 0xdf20 },
2308 { 0x01, 0x100a },
2309 { 0x00, 0xa0ff },
2310 { 0x04, 0xf800 },
2311 { 0x04, 0xf000 },
2312
2313 { 0x1f, 0x0000 },
2314 { 0x0b, 0x0000 },
2315 { 0x00, 0x9200 }
2316 };
2317
françois romieu4da19632011-01-03 15:07:55 +00002318 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319}
2320
françois romieu4da19632011-01-03 15:07:55 +00002321static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002322{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002323 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002324 { 0x1f, 0x0002 },
2325 { 0x01, 0x90d0 },
2326 { 0x1f, 0x0000 }
2327 };
2328
françois romieu4da19632011-01-03 15:07:55 +00002329 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002330}
2331
françois romieu4da19632011-01-03 15:07:55 +00002332static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002333{
2334 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002335
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002336 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2337 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002338 return;
2339
françois romieu4da19632011-01-03 15:07:55 +00002340 rtl_writephy(tp, 0x1f, 0x0001);
2341 rtl_writephy(tp, 0x10, 0xf01b);
2342 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002343}
2344
françois romieu4da19632011-01-03 15:07:55 +00002345static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002346{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002347 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002348 { 0x1f, 0x0001 },
2349 { 0x04, 0x0000 },
2350 { 0x03, 0x00a1 },
2351 { 0x02, 0x0008 },
2352 { 0x01, 0x0120 },
2353 { 0x00, 0x1000 },
2354 { 0x04, 0x0800 },
2355 { 0x04, 0x9000 },
2356 { 0x03, 0x802f },
2357 { 0x02, 0x4f02 },
2358 { 0x01, 0x0409 },
2359 { 0x00, 0xf099 },
2360 { 0x04, 0x9800 },
2361 { 0x04, 0xa000 },
2362 { 0x03, 0xdf01 },
2363 { 0x02, 0xdf20 },
2364 { 0x01, 0xff95 },
2365 { 0x00, 0xba00 },
2366 { 0x04, 0xa800 },
2367 { 0x04, 0xf000 },
2368 { 0x03, 0xdf01 },
2369 { 0x02, 0xdf20 },
2370 { 0x01, 0x101a },
2371 { 0x00, 0xa0ff },
2372 { 0x04, 0xf800 },
2373 { 0x04, 0x0000 },
2374 { 0x1f, 0x0000 },
2375
2376 { 0x1f, 0x0001 },
2377 { 0x10, 0xf41b },
2378 { 0x14, 0xfb54 },
2379 { 0x18, 0xf5c7 },
2380 { 0x1f, 0x0000 },
2381
2382 { 0x1f, 0x0001 },
2383 { 0x17, 0x0cc0 },
2384 { 0x1f, 0x0000 }
2385 };
2386
françois romieu4da19632011-01-03 15:07:55 +00002387 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002388
françois romieu4da19632011-01-03 15:07:55 +00002389 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002390}
2391
françois romieu4da19632011-01-03 15:07:55 +00002392static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002393{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002394 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002395 { 0x1f, 0x0001 },
2396 { 0x04, 0x0000 },
2397 { 0x03, 0x00a1 },
2398 { 0x02, 0x0008 },
2399 { 0x01, 0x0120 },
2400 { 0x00, 0x1000 },
2401 { 0x04, 0x0800 },
2402 { 0x04, 0x9000 },
2403 { 0x03, 0x802f },
2404 { 0x02, 0x4f02 },
2405 { 0x01, 0x0409 },
2406 { 0x00, 0xf099 },
2407 { 0x04, 0x9800 },
2408 { 0x04, 0xa000 },
2409 { 0x03, 0xdf01 },
2410 { 0x02, 0xdf20 },
2411 { 0x01, 0xff95 },
2412 { 0x00, 0xba00 },
2413 { 0x04, 0xa800 },
2414 { 0x04, 0xf000 },
2415 { 0x03, 0xdf01 },
2416 { 0x02, 0xdf20 },
2417 { 0x01, 0x101a },
2418 { 0x00, 0xa0ff },
2419 { 0x04, 0xf800 },
2420 { 0x04, 0x0000 },
2421 { 0x1f, 0x0000 },
2422
2423 { 0x1f, 0x0001 },
2424 { 0x0b, 0x8480 },
2425 { 0x1f, 0x0000 },
2426
2427 { 0x1f, 0x0001 },
2428 { 0x18, 0x67c7 },
2429 { 0x04, 0x2000 },
2430 { 0x03, 0x002f },
2431 { 0x02, 0x4360 },
2432 { 0x01, 0x0109 },
2433 { 0x00, 0x3022 },
2434 { 0x04, 0x2800 },
2435 { 0x1f, 0x0000 },
2436
2437 { 0x1f, 0x0001 },
2438 { 0x17, 0x0cc0 },
2439 { 0x1f, 0x0000 }
2440 };
2441
françois romieu4da19632011-01-03 15:07:55 +00002442 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002443}
2444
françois romieu4da19632011-01-03 15:07:55 +00002445static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002446{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002447 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002448 { 0x10, 0xf41b },
2449 { 0x1f, 0x0000 }
2450 };
2451
françois romieu4da19632011-01-03 15:07:55 +00002452 rtl_writephy(tp, 0x1f, 0x0001);
2453 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002454
françois romieu4da19632011-01-03 15:07:55 +00002455 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002456}
2457
françois romieu4da19632011-01-03 15:07:55 +00002458static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002459{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002460 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002461 { 0x1f, 0x0001 },
2462 { 0x10, 0xf41b },
2463 { 0x1f, 0x0000 }
2464 };
2465
françois romieu4da19632011-01-03 15:07:55 +00002466 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002467}
2468
françois romieu4da19632011-01-03 15:07:55 +00002469static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002470{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002471 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002472 { 0x1f, 0x0000 },
2473 { 0x1d, 0x0f00 },
2474 { 0x1f, 0x0002 },
2475 { 0x0c, 0x1ec8 },
2476 { 0x1f, 0x0000 }
2477 };
2478
françois romieu4da19632011-01-03 15:07:55 +00002479 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002480}
2481
françois romieu4da19632011-01-03 15:07:55 +00002482static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002483{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002484 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002485 { 0x1f, 0x0001 },
2486 { 0x1d, 0x3d98 },
2487 { 0x1f, 0x0000 }
2488 };
2489
françois romieu4da19632011-01-03 15:07:55 +00002490 rtl_writephy(tp, 0x1f, 0x0000);
2491 rtl_patchphy(tp, 0x14, 1 << 5);
2492 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002493
françois romieu4da19632011-01-03 15:07:55 +00002494 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002495}
2496
françois romieu4da19632011-01-03 15:07:55 +00002497static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002498{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002499 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002500 { 0x1f, 0x0001 },
2501 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002502 { 0x1f, 0x0002 },
2503 { 0x00, 0x88d4 },
2504 { 0x01, 0x82b1 },
2505 { 0x03, 0x7002 },
2506 { 0x08, 0x9e30 },
2507 { 0x09, 0x01f0 },
2508 { 0x0a, 0x5500 },
2509 { 0x0c, 0x00c8 },
2510 { 0x1f, 0x0003 },
2511 { 0x12, 0xc096 },
2512 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002513 { 0x1f, 0x0000 },
2514 { 0x1f, 0x0000 },
2515 { 0x09, 0x2000 },
2516 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002517 };
2518
françois romieu4da19632011-01-03 15:07:55 +00002519 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002520
françois romieu4da19632011-01-03 15:07:55 +00002521 rtl_patchphy(tp, 0x14, 1 << 5);
2522 rtl_patchphy(tp, 0x0d, 1 << 5);
2523 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002524}
2525
françois romieu4da19632011-01-03 15:07:55 +00002526static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002527{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002528 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002529 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002530 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002531 { 0x03, 0x802f },
2532 { 0x02, 0x4f02 },
2533 { 0x01, 0x0409 },
2534 { 0x00, 0xf099 },
2535 { 0x04, 0x9800 },
2536 { 0x04, 0x9000 },
2537 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002538 { 0x1f, 0x0002 },
2539 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002540 { 0x06, 0x0761 },
2541 { 0x1f, 0x0003 },
2542 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002543 { 0x1f, 0x0000 }
2544 };
2545
françois romieu4da19632011-01-03 15:07:55 +00002546 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002547
françois romieu4da19632011-01-03 15:07:55 +00002548 rtl_patchphy(tp, 0x16, 1 << 0);
2549 rtl_patchphy(tp, 0x14, 1 << 5);
2550 rtl_patchphy(tp, 0x0d, 1 << 5);
2551 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002552}
2553
françois romieu4da19632011-01-03 15:07:55 +00002554static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002555{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002556 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002557 { 0x1f, 0x0001 },
2558 { 0x12, 0x2300 },
2559 { 0x1d, 0x3d98 },
2560 { 0x1f, 0x0002 },
2561 { 0x0c, 0x7eb8 },
2562 { 0x06, 0x5461 },
2563 { 0x1f, 0x0003 },
2564 { 0x16, 0x0f0a },
2565 { 0x1f, 0x0000 }
2566 };
2567
françois romieu4da19632011-01-03 15:07:55 +00002568 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002569
françois romieu4da19632011-01-03 15:07:55 +00002570 rtl_patchphy(tp, 0x16, 1 << 0);
2571 rtl_patchphy(tp, 0x14, 1 << 5);
2572 rtl_patchphy(tp, 0x0d, 1 << 5);
2573 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002574}
2575
françois romieu4da19632011-01-03 15:07:55 +00002576static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002577{
françois romieu4da19632011-01-03 15:07:55 +00002578 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002579}
2580
françois romieubca03d52011-01-03 15:07:31 +00002581static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002582{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002583 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002584 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002585 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002586 { 0x06, 0x4064 },
2587 { 0x07, 0x2863 },
2588 { 0x08, 0x059c },
2589 { 0x09, 0x26b4 },
2590 { 0x0a, 0x6a19 },
2591 { 0x0b, 0xdcc8 },
2592 { 0x10, 0xf06d },
2593 { 0x14, 0x7f68 },
2594 { 0x18, 0x7fd9 },
2595 { 0x1c, 0xf0ff },
2596 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002597 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002598 { 0x12, 0xf49f },
2599 { 0x13, 0x070b },
2600 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002601 { 0x14, 0x94c0 },
2602
2603 /*
2604 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002605 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002606 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002607 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002608 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002609 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002610 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002611 { 0x06, 0x5561 },
2612
2613 /*
2614 * Can not link to 1Gbps with bad cable
2615 * Decrease SNR threshold form 21.07dB to 19.04dB
2616 */
2617 { 0x1f, 0x0001 },
2618 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002619
2620 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002621 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002622 };
françois romieubca03d52011-01-03 15:07:31 +00002623 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002624
françois romieu4da19632011-01-03 15:07:55 +00002625 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002626
françois romieubca03d52011-01-03 15:07:31 +00002627 /*
2628 * Rx Error Issue
2629 * Fine Tune Switching regulator parameter
2630 */
françois romieu4da19632011-01-03 15:07:55 +00002631 rtl_writephy(tp, 0x1f, 0x0002);
2632 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2633 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002634
françois romieudaf9df62009-10-07 12:44:20 +00002635 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002636 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002637 { 0x1f, 0x0002 },
2638 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002639 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002640 { 0x05, 0x8330 },
2641 { 0x06, 0x669a },
2642 { 0x1f, 0x0002 }
2643 };
2644 int val;
2645
françois romieu4da19632011-01-03 15:07:55 +00002646 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002647
françois romieu4da19632011-01-03 15:07:55 +00002648 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002649
2650 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002651 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002652 0x0065, 0x0066, 0x0067, 0x0068,
2653 0x0069, 0x006a, 0x006b, 0x006c
2654 };
2655 int i;
2656
françois romieu4da19632011-01-03 15:07:55 +00002657 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002658
2659 val &= 0xff00;
2660 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002661 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002662 }
2663 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002664 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002665 { 0x1f, 0x0002 },
2666 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002667 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002668 { 0x05, 0x8330 },
2669 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002670 };
2671
françois romieu4da19632011-01-03 15:07:55 +00002672 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002673 }
2674
françois romieubca03d52011-01-03 15:07:31 +00002675 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002676 rtl_writephy(tp, 0x1f, 0x0002);
2677 rtl_patchphy(tp, 0x0d, 0x0300);
2678 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002679
françois romieubca03d52011-01-03 15:07:31 +00002680 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002681 rtl_writephy(tp, 0x1f, 0x0002);
2682 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2683 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002684
françois romieu4da19632011-01-03 15:07:55 +00002685 rtl_writephy(tp, 0x1f, 0x0005);
2686 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002687
2688 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002689
françois romieu4da19632011-01-03 15:07:55 +00002690 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002691}
2692
françois romieubca03d52011-01-03 15:07:31 +00002693static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002694{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002695 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002696 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002697 { 0x1f, 0x0001 },
2698 { 0x06, 0x4064 },
2699 { 0x07, 0x2863 },
2700 { 0x08, 0x059c },
2701 { 0x09, 0x26b4 },
2702 { 0x0a, 0x6a19 },
2703 { 0x0b, 0xdcc8 },
2704 { 0x10, 0xf06d },
2705 { 0x14, 0x7f68 },
2706 { 0x18, 0x7fd9 },
2707 { 0x1c, 0xf0ff },
2708 { 0x1d, 0x3d9c },
2709 { 0x1f, 0x0003 },
2710 { 0x12, 0xf49f },
2711 { 0x13, 0x070b },
2712 { 0x1a, 0x05ad },
2713 { 0x14, 0x94c0 },
2714
françois romieubca03d52011-01-03 15:07:31 +00002715 /*
2716 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002717 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002718 */
françois romieudaf9df62009-10-07 12:44:20 +00002719 { 0x1f, 0x0002 },
2720 { 0x06, 0x5561 },
2721 { 0x1f, 0x0005 },
2722 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002723 { 0x06, 0x5561 },
2724
2725 /*
2726 * Can not link to 1Gbps with bad cable
2727 * Decrease SNR threshold form 21.07dB to 19.04dB
2728 */
2729 { 0x1f, 0x0001 },
2730 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002731
2732 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002733 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002734 };
françois romieubca03d52011-01-03 15:07:31 +00002735 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002736
françois romieu4da19632011-01-03 15:07:55 +00002737 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002738
2739 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002740 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002741 { 0x1f, 0x0002 },
2742 { 0x05, 0x669a },
2743 { 0x1f, 0x0005 },
2744 { 0x05, 0x8330 },
2745 { 0x06, 0x669a },
2746
2747 { 0x1f, 0x0002 }
2748 };
2749 int val;
2750
françois romieu4da19632011-01-03 15:07:55 +00002751 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002752
françois romieu4da19632011-01-03 15:07:55 +00002753 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002754 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002755 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002756 0x0065, 0x0066, 0x0067, 0x0068,
2757 0x0069, 0x006a, 0x006b, 0x006c
2758 };
2759 int i;
2760
françois romieu4da19632011-01-03 15:07:55 +00002761 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002762
2763 val &= 0xff00;
2764 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002765 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002766 }
2767 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002768 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002769 { 0x1f, 0x0002 },
2770 { 0x05, 0x2642 },
2771 { 0x1f, 0x0005 },
2772 { 0x05, 0x8330 },
2773 { 0x06, 0x2642 }
2774 };
2775
françois romieu4da19632011-01-03 15:07:55 +00002776 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002777 }
2778
françois romieubca03d52011-01-03 15:07:31 +00002779 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002780 rtl_writephy(tp, 0x1f, 0x0002);
2781 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2782 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002783
françois romieubca03d52011-01-03 15:07:31 +00002784 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002785 rtl_writephy(tp, 0x1f, 0x0002);
2786 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002787
françois romieu4da19632011-01-03 15:07:55 +00002788 rtl_writephy(tp, 0x1f, 0x0005);
2789 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002790
2791 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002792
françois romieu4da19632011-01-03 15:07:55 +00002793 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002794}
2795
françois romieu4da19632011-01-03 15:07:55 +00002796static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002797{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002798 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002799 { 0x1f, 0x0002 },
2800 { 0x10, 0x0008 },
2801 { 0x0d, 0x006c },
2802
2803 { 0x1f, 0x0000 },
2804 { 0x0d, 0xf880 },
2805
2806 { 0x1f, 0x0001 },
2807 { 0x17, 0x0cc0 },
2808
2809 { 0x1f, 0x0001 },
2810 { 0x0b, 0xa4d8 },
2811 { 0x09, 0x281c },
2812 { 0x07, 0x2883 },
2813 { 0x0a, 0x6b35 },
2814 { 0x1d, 0x3da4 },
2815 { 0x1c, 0xeffd },
2816 { 0x14, 0x7f52 },
2817 { 0x18, 0x7fc6 },
2818 { 0x08, 0x0601 },
2819 { 0x06, 0x4063 },
2820 { 0x10, 0xf074 },
2821 { 0x1f, 0x0003 },
2822 { 0x13, 0x0789 },
2823 { 0x12, 0xf4bd },
2824 { 0x1a, 0x04fd },
2825 { 0x14, 0x84b0 },
2826 { 0x1f, 0x0000 },
2827 { 0x00, 0x9200 },
2828
2829 { 0x1f, 0x0005 },
2830 { 0x01, 0x0340 },
2831 { 0x1f, 0x0001 },
2832 { 0x04, 0x4000 },
2833 { 0x03, 0x1d21 },
2834 { 0x02, 0x0c32 },
2835 { 0x01, 0x0200 },
2836 { 0x00, 0x5554 },
2837 { 0x04, 0x4800 },
2838 { 0x04, 0x4000 },
2839 { 0x04, 0xf000 },
2840 { 0x03, 0xdf01 },
2841 { 0x02, 0xdf20 },
2842 { 0x01, 0x101a },
2843 { 0x00, 0xa0ff },
2844 { 0x04, 0xf800 },
2845 { 0x04, 0xf000 },
2846 { 0x1f, 0x0000 },
2847
2848 { 0x1f, 0x0007 },
2849 { 0x1e, 0x0023 },
2850 { 0x16, 0x0000 },
2851 { 0x1f, 0x0000 }
2852 };
2853
françois romieu4da19632011-01-03 15:07:55 +00002854 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002855}
2856
françois romieue6de30d2011-01-03 15:08:37 +00002857static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2858{
2859 static const struct phy_reg phy_reg_init[] = {
2860 { 0x1f, 0x0001 },
2861 { 0x17, 0x0cc0 },
2862
2863 { 0x1f, 0x0007 },
2864 { 0x1e, 0x002d },
2865 { 0x18, 0x0040 },
2866 { 0x1f, 0x0000 }
2867 };
2868
2869 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2870 rtl_patchphy(tp, 0x0d, 1 << 5);
2871}
2872
Hayes Wang70090422011-07-06 15:58:06 +08002873static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002874{
2875 static const struct phy_reg phy_reg_init[] = {
2876 /* Enable Delay cap */
2877 { 0x1f, 0x0005 },
2878 { 0x05, 0x8b80 },
2879 { 0x06, 0xc896 },
2880 { 0x1f, 0x0000 },
2881
2882 /* Channel estimation fine tune */
2883 { 0x1f, 0x0001 },
2884 { 0x0b, 0x6c20 },
2885 { 0x07, 0x2872 },
2886 { 0x1c, 0xefff },
2887 { 0x1f, 0x0003 },
2888 { 0x14, 0x6420 },
2889 { 0x1f, 0x0000 },
2890
2891 /* Update PFM & 10M TX idle timer */
2892 { 0x1f, 0x0007 },
2893 { 0x1e, 0x002f },
2894 { 0x15, 0x1919 },
2895 { 0x1f, 0x0000 },
2896
2897 { 0x1f, 0x0007 },
2898 { 0x1e, 0x00ac },
2899 { 0x18, 0x0006 },
2900 { 0x1f, 0x0000 }
2901 };
2902
Francois Romieu15ecd032011-04-27 13:52:22 -07002903 rtl_apply_firmware(tp);
2904
hayeswang01dc7fe2011-03-21 01:50:28 +00002905 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2906
2907 /* DCO enable for 10M IDLE Power */
2908 rtl_writephy(tp, 0x1f, 0x0007);
2909 rtl_writephy(tp, 0x1e, 0x0023);
2910 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2911 rtl_writephy(tp, 0x1f, 0x0000);
2912
2913 /* For impedance matching */
2914 rtl_writephy(tp, 0x1f, 0x0002);
2915 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002916 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002917
2918 /* PHY auto speed down */
2919 rtl_writephy(tp, 0x1f, 0x0007);
2920 rtl_writephy(tp, 0x1e, 0x002d);
2921 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2922 rtl_writephy(tp, 0x1f, 0x0000);
2923 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2924
2925 rtl_writephy(tp, 0x1f, 0x0005);
2926 rtl_writephy(tp, 0x05, 0x8b86);
2927 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2928 rtl_writephy(tp, 0x1f, 0x0000);
2929
2930 rtl_writephy(tp, 0x1f, 0x0005);
2931 rtl_writephy(tp, 0x05, 0x8b85);
2932 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2933 rtl_writephy(tp, 0x1f, 0x0007);
2934 rtl_writephy(tp, 0x1e, 0x0020);
2935 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2936 rtl_writephy(tp, 0x1f, 0x0006);
2937 rtl_writephy(tp, 0x00, 0x5a00);
2938 rtl_writephy(tp, 0x1f, 0x0000);
2939 rtl_writephy(tp, 0x0d, 0x0007);
2940 rtl_writephy(tp, 0x0e, 0x003c);
2941 rtl_writephy(tp, 0x0d, 0x4007);
2942 rtl_writephy(tp, 0x0e, 0x0000);
2943 rtl_writephy(tp, 0x0d, 0x0000);
2944}
2945
Hayes Wang70090422011-07-06 15:58:06 +08002946static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2947{
2948 static const struct phy_reg phy_reg_init[] = {
2949 /* Enable Delay cap */
2950 { 0x1f, 0x0004 },
2951 { 0x1f, 0x0007 },
2952 { 0x1e, 0x00ac },
2953 { 0x18, 0x0006 },
2954 { 0x1f, 0x0002 },
2955 { 0x1f, 0x0000 },
2956 { 0x1f, 0x0000 },
2957
2958 /* Channel estimation fine tune */
2959 { 0x1f, 0x0003 },
2960 { 0x09, 0xa20f },
2961 { 0x1f, 0x0000 },
2962 { 0x1f, 0x0000 },
2963
2964 /* Green Setting */
2965 { 0x1f, 0x0005 },
2966 { 0x05, 0x8b5b },
2967 { 0x06, 0x9222 },
2968 { 0x05, 0x8b6d },
2969 { 0x06, 0x8000 },
2970 { 0x05, 0x8b76 },
2971 { 0x06, 0x8000 },
2972 { 0x1f, 0x0000 }
2973 };
2974
2975 rtl_apply_firmware(tp);
2976
2977 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2978
2979 /* For 4-corner performance improve */
2980 rtl_writephy(tp, 0x1f, 0x0005);
2981 rtl_writephy(tp, 0x05, 0x8b80);
2982 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2983 rtl_writephy(tp, 0x1f, 0x0000);
2984
2985 /* PHY auto speed down */
2986 rtl_writephy(tp, 0x1f, 0x0004);
2987 rtl_writephy(tp, 0x1f, 0x0007);
2988 rtl_writephy(tp, 0x1e, 0x002d);
2989 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2990 rtl_writephy(tp, 0x1f, 0x0002);
2991 rtl_writephy(tp, 0x1f, 0x0000);
2992 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2993
2994 /* improve 10M EEE waveform */
2995 rtl_writephy(tp, 0x1f, 0x0005);
2996 rtl_writephy(tp, 0x05, 0x8b86);
2997 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2998 rtl_writephy(tp, 0x1f, 0x0000);
2999
3000 /* Improve 2-pair detection performance */
3001 rtl_writephy(tp, 0x1f, 0x0005);
3002 rtl_writephy(tp, 0x05, 0x8b85);
3003 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3004 rtl_writephy(tp, 0x1f, 0x0000);
3005
3006 /* EEE setting */
3007 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
3008 ERIAR_EXGMAC);
3009 rtl_writephy(tp, 0x1f, 0x0005);
3010 rtl_writephy(tp, 0x05, 0x8b85);
3011 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3012 rtl_writephy(tp, 0x1f, 0x0004);
3013 rtl_writephy(tp, 0x1f, 0x0007);
3014 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04003015 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003016 rtl_writephy(tp, 0x1f, 0x0002);
3017 rtl_writephy(tp, 0x1f, 0x0000);
3018 rtl_writephy(tp, 0x0d, 0x0007);
3019 rtl_writephy(tp, 0x0e, 0x003c);
3020 rtl_writephy(tp, 0x0d, 0x4007);
3021 rtl_writephy(tp, 0x0e, 0x0000);
3022 rtl_writephy(tp, 0x0d, 0x0000);
3023
3024 /* Green feature */
3025 rtl_writephy(tp, 0x1f, 0x0003);
3026 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3027 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3028 rtl_writephy(tp, 0x1f, 0x0000);
3029}
3030
Hayes Wangc2218922011-09-06 16:55:18 +08003031static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3032{
3033 static const struct phy_reg phy_reg_init[] = {
3034 /* Channel estimation fine tune */
3035 { 0x1f, 0x0003 },
3036 { 0x09, 0xa20f },
3037 { 0x1f, 0x0000 },
3038
3039 /* Modify green table for giga & fnet */
3040 { 0x1f, 0x0005 },
3041 { 0x05, 0x8b55 },
3042 { 0x06, 0x0000 },
3043 { 0x05, 0x8b5e },
3044 { 0x06, 0x0000 },
3045 { 0x05, 0x8b67 },
3046 { 0x06, 0x0000 },
3047 { 0x05, 0x8b70 },
3048 { 0x06, 0x0000 },
3049 { 0x1f, 0x0000 },
3050 { 0x1f, 0x0007 },
3051 { 0x1e, 0x0078 },
3052 { 0x17, 0x0000 },
3053 { 0x19, 0x00fb },
3054 { 0x1f, 0x0000 },
3055
3056 /* Modify green table for 10M */
3057 { 0x1f, 0x0005 },
3058 { 0x05, 0x8b79 },
3059 { 0x06, 0xaa00 },
3060 { 0x1f, 0x0000 },
3061
3062 /* Disable hiimpedance detection (RTCT) */
3063 { 0x1f, 0x0003 },
3064 { 0x01, 0x328a },
3065 { 0x1f, 0x0000 }
3066 };
3067
3068 rtl_apply_firmware(tp);
3069
3070 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3071
3072 /* For 4-corner performance improve */
3073 rtl_writephy(tp, 0x1f, 0x0005);
3074 rtl_writephy(tp, 0x05, 0x8b80);
3075 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3076 rtl_writephy(tp, 0x1f, 0x0000);
3077
3078 /* PHY auto speed down */
3079 rtl_writephy(tp, 0x1f, 0x0007);
3080 rtl_writephy(tp, 0x1e, 0x002d);
3081 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3082 rtl_writephy(tp, 0x1f, 0x0000);
3083 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3084
3085 /* Improve 10M EEE waveform */
3086 rtl_writephy(tp, 0x1f, 0x0005);
3087 rtl_writephy(tp, 0x05, 0x8b86);
3088 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3089 rtl_writephy(tp, 0x1f, 0x0000);
3090
3091 /* Improve 2-pair detection performance */
3092 rtl_writephy(tp, 0x1f, 0x0005);
3093 rtl_writephy(tp, 0x05, 0x8b85);
3094 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3095 rtl_writephy(tp, 0x1f, 0x0000);
3096}
3097
3098static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3099{
3100 rtl_apply_firmware(tp);
3101
3102 /* For 4-corner performance improve */
3103 rtl_writephy(tp, 0x1f, 0x0005);
3104 rtl_writephy(tp, 0x05, 0x8b80);
3105 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3106 rtl_writephy(tp, 0x1f, 0x0000);
3107
3108 /* PHY auto speed down */
3109 rtl_writephy(tp, 0x1f, 0x0007);
3110 rtl_writephy(tp, 0x1e, 0x002d);
3111 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3112 rtl_writephy(tp, 0x1f, 0x0000);
3113 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3114
3115 /* Improve 10M EEE waveform */
3116 rtl_writephy(tp, 0x1f, 0x0005);
3117 rtl_writephy(tp, 0x05, 0x8b86);
3118 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3119 rtl_writephy(tp, 0x1f, 0x0000);
3120}
3121
françois romieu4da19632011-01-03 15:07:55 +00003122static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003123{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003124 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003125 { 0x1f, 0x0003 },
3126 { 0x08, 0x441d },
3127 { 0x01, 0x9100 },
3128 { 0x1f, 0x0000 }
3129 };
3130
françois romieu4da19632011-01-03 15:07:55 +00003131 rtl_writephy(tp, 0x1f, 0x0000);
3132 rtl_patchphy(tp, 0x11, 1 << 12);
3133 rtl_patchphy(tp, 0x19, 1 << 13);
3134 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003135
françois romieu4da19632011-01-03 15:07:55 +00003136 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003137}
3138
Hayes Wang5a5e4442011-02-22 17:26:21 +08003139static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3140{
3141 static const struct phy_reg phy_reg_init[] = {
3142 { 0x1f, 0x0005 },
3143 { 0x1a, 0x0000 },
3144 { 0x1f, 0x0000 },
3145
3146 { 0x1f, 0x0004 },
3147 { 0x1c, 0x0000 },
3148 { 0x1f, 0x0000 },
3149
3150 { 0x1f, 0x0001 },
3151 { 0x15, 0x7701 },
3152 { 0x1f, 0x0000 }
3153 };
3154
3155 /* Disable ALDPS before ram code */
3156 rtl_writephy(tp, 0x1f, 0x0000);
3157 rtl_writephy(tp, 0x18, 0x0310);
3158 msleep(100);
3159
François Romieu953a12c2011-04-24 17:38:48 +02003160 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003161
3162 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3163}
3164
Francois Romieu5615d9f2007-08-17 17:50:46 +02003165static void rtl_hw_phy_config(struct net_device *dev)
3166{
3167 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003168
3169 rtl8169_print_mac_version(tp);
3170
3171 switch (tp->mac_version) {
3172 case RTL_GIGA_MAC_VER_01:
3173 break;
3174 case RTL_GIGA_MAC_VER_02:
3175 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003176 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003177 break;
3178 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003179 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003180 break;
françois romieu2e9558562009-08-10 19:44:19 +00003181 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003182 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003183 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003184 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003185 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003186 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003187 case RTL_GIGA_MAC_VER_07:
3188 case RTL_GIGA_MAC_VER_08:
3189 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003190 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003191 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003192 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003193 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003194 break;
3195 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003196 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003197 break;
3198 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003199 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003200 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003201 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003202 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003203 break;
3204 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003205 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003206 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003207 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003208 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003209 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003210 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003211 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003212 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003213 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003214 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003215 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003216 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003217 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003218 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003219 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003220 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003221 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003222 break;
3223 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003224 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003225 break;
3226 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003227 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003228 break;
françois romieue6de30d2011-01-03 15:08:37 +00003229 case RTL_GIGA_MAC_VER_28:
3230 rtl8168d_4_hw_phy_config(tp);
3231 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003232 case RTL_GIGA_MAC_VER_29:
3233 case RTL_GIGA_MAC_VER_30:
3234 rtl8105e_hw_phy_config(tp);
3235 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003236 case RTL_GIGA_MAC_VER_31:
3237 /* None. */
3238 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003239 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003240 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003241 rtl8168e_1_hw_phy_config(tp);
3242 break;
3243 case RTL_GIGA_MAC_VER_34:
3244 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003245 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003246 case RTL_GIGA_MAC_VER_35:
3247 rtl8168f_1_hw_phy_config(tp);
3248 break;
3249 case RTL_GIGA_MAC_VER_36:
3250 rtl8168f_2_hw_phy_config(tp);
3251 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003252
Francois Romieu5615d9f2007-08-17 17:50:46 +02003253 default:
3254 break;
3255 }
3256}
3257
Francois Romieuda78dbf2012-01-26 14:18:23 +01003258static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 struct timer_list *timer = &tp->timer;
3261 void __iomem *ioaddr = tp->mmio_addr;
3262 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3263
Francois Romieubcf0bf92006-07-26 23:14:13 +02003264 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
françois romieu4da19632011-01-03 15:07:55 +00003266 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003267 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 * A busy loop could burn quite a few cycles on nowadays CPU.
3269 * Let's delay the execution of the timer for a few ticks.
3270 */
3271 timeout = HZ/10;
3272 goto out_mod_timer;
3273 }
3274
3275 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01003276 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
Francois Romieuda78dbf2012-01-26 14:18:23 +01003278 netif_warn(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
françois romieu4da19632011-01-03 15:07:55 +00003280 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
3282out_mod_timer:
3283 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003284}
3285
3286static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3287{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003288 if (!test_and_set_bit(flag, tp->wk.flags))
3289 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003290}
3291
3292static void rtl8169_phy_timer(unsigned long __opaque)
3293{
3294 struct net_device *dev = (struct net_device *)__opaque;
3295 struct rtl8169_private *tp = netdev_priv(dev);
3296
Francois Romieu98ddf982012-01-31 10:47:34 +01003297 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298}
3299
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3301 void __iomem *ioaddr)
3302{
3303 iounmap(ioaddr);
3304 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003305 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 pci_disable_device(pdev);
3307 free_netdev(dev);
3308}
3309
Francois Romieubf793292006-11-01 00:53:05 +01003310static void rtl8169_phy_reset(struct net_device *dev,
3311 struct rtl8169_private *tp)
3312{
Francois Romieu07d3f512007-02-21 22:40:46 +01003313 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003314
françois romieu4da19632011-01-03 15:07:55 +00003315 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003316 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003317 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003318 return;
3319 msleep(1);
3320 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003321 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003322}
3323
David S. Miller8decf862011-09-22 03:23:13 -04003324static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3325{
3326 void __iomem *ioaddr = tp->mmio_addr;
3327
3328 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3329 (RTL_R8(PHYstatus) & TBI_Enable);
3330}
3331
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003332static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003334 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003335
Francois Romieu5615d9f2007-08-17 17:50:46 +02003336 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003337
Marcus Sundberg773328942008-07-10 21:28:08 +02003338 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3339 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3340 RTL_W8(0x82, 0x01);
3341 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003342
Francois Romieu6dccd162007-02-13 23:38:05 +01003343 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3344
3345 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3346 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003347
Francois Romieubcf0bf92006-07-26 23:14:13 +02003348 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003349 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3350 RTL_W8(0x82, 0x01);
3351 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003352 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003353 }
3354
Francois Romieubf793292006-11-01 00:53:05 +01003355 rtl8169_phy_reset(dev, tp);
3356
Oliver Neukum54405cd2011-01-06 21:55:13 +01003357 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003358 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3359 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3360 (tp->mii.supports_gmii ?
3361 ADVERTISED_1000baseT_Half |
3362 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003363
David S. Miller8decf862011-09-22 03:23:13 -04003364 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003365 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003366}
3367
Francois Romieu773d2022007-01-31 23:47:43 +01003368static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3369{
3370 void __iomem *ioaddr = tp->mmio_addr;
3371 u32 high;
3372 u32 low;
3373
3374 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3375 high = addr[4] | (addr[5] << 8);
3376
Francois Romieuda78dbf2012-01-26 14:18:23 +01003377 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003378
3379 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003380
Francois Romieu773d2022007-01-31 23:47:43 +01003381 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00003382 RTL_R32(MAC4);
3383
Francois Romieu78f1cd02010-03-27 19:35:46 -07003384 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00003385 RTL_R32(MAC0);
3386
françois romieuc28aa382011-08-02 03:53:43 +00003387 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3388 const struct exgmac_reg e[] = {
3389 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3390 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3391 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3392 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3393 low >> 16 },
3394 };
3395
3396 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3397 }
3398
Francois Romieu773d2022007-01-31 23:47:43 +01003399 RTL_W8(Cfg9346, Cfg9346_Lock);
3400
Francois Romieuda78dbf2012-01-26 14:18:23 +01003401 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003402}
3403
3404static int rtl_set_mac_address(struct net_device *dev, void *p)
3405{
3406 struct rtl8169_private *tp = netdev_priv(dev);
3407 struct sockaddr *addr = p;
3408
3409 if (!is_valid_ether_addr(addr->sa_data))
3410 return -EADDRNOTAVAIL;
3411
3412 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3413
3414 rtl_rar_set(tp, dev->dev_addr);
3415
3416 return 0;
3417}
3418
Francois Romieu5f787a12006-08-17 13:02:36 +02003419static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3420{
3421 struct rtl8169_private *tp = netdev_priv(dev);
3422 struct mii_ioctl_data *data = if_mii(ifr);
3423
Francois Romieu8b4ab282008-11-19 22:05:25 -08003424 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3425}
Francois Romieu5f787a12006-08-17 13:02:36 +02003426
Francois Romieucecb5fd2011-04-01 10:21:07 +02003427static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3428 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003429{
Francois Romieu5f787a12006-08-17 13:02:36 +02003430 switch (cmd) {
3431 case SIOCGMIIPHY:
3432 data->phy_id = 32; /* Internal PHY */
3433 return 0;
3434
3435 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003436 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003437 return 0;
3438
3439 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003440 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003441 return 0;
3442 }
3443 return -EOPNOTSUPP;
3444}
3445
Francois Romieu8b4ab282008-11-19 22:05:25 -08003446static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3447{
3448 return -EOPNOTSUPP;
3449}
3450
Francois Romieufbac58f2007-10-04 22:51:38 +02003451static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3452{
3453 if (tp->features & RTL_FEATURE_MSI) {
3454 pci_disable_msi(pdev);
3455 tp->features &= ~RTL_FEATURE_MSI;
3456 }
3457}
3458
françois romieuc0e45c12011-01-03 15:08:04 +00003459static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3460{
3461 struct mdio_ops *ops = &tp->mdio_ops;
3462
3463 switch (tp->mac_version) {
3464 case RTL_GIGA_MAC_VER_27:
3465 ops->write = r8168dp_1_mdio_write;
3466 ops->read = r8168dp_1_mdio_read;
3467 break;
françois romieue6de30d2011-01-03 15:08:37 +00003468 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003469 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003470 ops->write = r8168dp_2_mdio_write;
3471 ops->read = r8168dp_2_mdio_read;
3472 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003473 default:
3474 ops->write = r8169_mdio_write;
3475 ops->read = r8169_mdio_read;
3476 break;
3477 }
3478}
3479
David S. Miller1805b2f2011-10-24 18:18:09 -04003480static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3481{
3482 void __iomem *ioaddr = tp->mmio_addr;
3483
3484 switch (tp->mac_version) {
3485 case RTL_GIGA_MAC_VER_29:
3486 case RTL_GIGA_MAC_VER_30:
3487 case RTL_GIGA_MAC_VER_32:
3488 case RTL_GIGA_MAC_VER_33:
3489 case RTL_GIGA_MAC_VER_34:
3490 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3491 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3492 break;
3493 default:
3494 break;
3495 }
3496}
3497
3498static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3499{
3500 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3501 return false;
3502
3503 rtl_writephy(tp, 0x1f, 0x0000);
3504 rtl_writephy(tp, MII_BMCR, 0x0000);
3505
3506 rtl_wol_suspend_quirk(tp);
3507
3508 return true;
3509}
3510
françois romieu065c27c2011-01-03 15:08:12 +00003511static void r810x_phy_power_down(struct rtl8169_private *tp)
3512{
3513 rtl_writephy(tp, 0x1f, 0x0000);
3514 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3515}
3516
3517static void r810x_phy_power_up(struct rtl8169_private *tp)
3518{
3519 rtl_writephy(tp, 0x1f, 0x0000);
3520 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3521}
3522
3523static void r810x_pll_power_down(struct rtl8169_private *tp)
3524{
David S. Miller1805b2f2011-10-24 18:18:09 -04003525 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003526 return;
françois romieu065c27c2011-01-03 15:08:12 +00003527
3528 r810x_phy_power_down(tp);
3529}
3530
3531static void r810x_pll_power_up(struct rtl8169_private *tp)
3532{
3533 r810x_phy_power_up(tp);
3534}
3535
3536static void r8168_phy_power_up(struct rtl8169_private *tp)
3537{
3538 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003539 switch (tp->mac_version) {
3540 case RTL_GIGA_MAC_VER_11:
3541 case RTL_GIGA_MAC_VER_12:
3542 case RTL_GIGA_MAC_VER_17:
3543 case RTL_GIGA_MAC_VER_18:
3544 case RTL_GIGA_MAC_VER_19:
3545 case RTL_GIGA_MAC_VER_20:
3546 case RTL_GIGA_MAC_VER_21:
3547 case RTL_GIGA_MAC_VER_22:
3548 case RTL_GIGA_MAC_VER_23:
3549 case RTL_GIGA_MAC_VER_24:
3550 case RTL_GIGA_MAC_VER_25:
3551 case RTL_GIGA_MAC_VER_26:
3552 case RTL_GIGA_MAC_VER_27:
3553 case RTL_GIGA_MAC_VER_28:
3554 case RTL_GIGA_MAC_VER_31:
3555 rtl_writephy(tp, 0x0e, 0x0000);
3556 break;
3557 default:
3558 break;
3559 }
françois romieu065c27c2011-01-03 15:08:12 +00003560 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3561}
3562
3563static void r8168_phy_power_down(struct rtl8169_private *tp)
3564{
3565 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003566 switch (tp->mac_version) {
3567 case RTL_GIGA_MAC_VER_32:
3568 case RTL_GIGA_MAC_VER_33:
3569 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3570 break;
3571
3572 case RTL_GIGA_MAC_VER_11:
3573 case RTL_GIGA_MAC_VER_12:
3574 case RTL_GIGA_MAC_VER_17:
3575 case RTL_GIGA_MAC_VER_18:
3576 case RTL_GIGA_MAC_VER_19:
3577 case RTL_GIGA_MAC_VER_20:
3578 case RTL_GIGA_MAC_VER_21:
3579 case RTL_GIGA_MAC_VER_22:
3580 case RTL_GIGA_MAC_VER_23:
3581 case RTL_GIGA_MAC_VER_24:
3582 case RTL_GIGA_MAC_VER_25:
3583 case RTL_GIGA_MAC_VER_26:
3584 case RTL_GIGA_MAC_VER_27:
3585 case RTL_GIGA_MAC_VER_28:
3586 case RTL_GIGA_MAC_VER_31:
3587 rtl_writephy(tp, 0x0e, 0x0200);
3588 default:
3589 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3590 break;
3591 }
françois romieu065c27c2011-01-03 15:08:12 +00003592}
3593
3594static void r8168_pll_power_down(struct rtl8169_private *tp)
3595{
3596 void __iomem *ioaddr = tp->mmio_addr;
3597
Francois Romieucecb5fd2011-04-01 10:21:07 +02003598 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3599 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3600 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003601 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003602 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003603 }
françois romieu065c27c2011-01-03 15:08:12 +00003604
Francois Romieucecb5fd2011-04-01 10:21:07 +02003605 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3606 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003607 (RTL_R16(CPlusCmd) & ASF)) {
3608 return;
3609 }
3610
hayeswang01dc7fe2011-03-21 01:50:28 +00003611 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3612 tp->mac_version == RTL_GIGA_MAC_VER_33)
3613 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3614
David S. Miller1805b2f2011-10-24 18:18:09 -04003615 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003616 return;
françois romieu065c27c2011-01-03 15:08:12 +00003617
3618 r8168_phy_power_down(tp);
3619
3620 switch (tp->mac_version) {
3621 case RTL_GIGA_MAC_VER_25:
3622 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003623 case RTL_GIGA_MAC_VER_27:
3624 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003625 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003626 case RTL_GIGA_MAC_VER_32:
3627 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003628 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3629 break;
3630 }
3631}
3632
3633static void r8168_pll_power_up(struct rtl8169_private *tp)
3634{
3635 void __iomem *ioaddr = tp->mmio_addr;
3636
Francois Romieucecb5fd2011-04-01 10:21:07 +02003637 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3638 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3639 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003640 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003641 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003642 }
françois romieu065c27c2011-01-03 15:08:12 +00003643
3644 switch (tp->mac_version) {
3645 case RTL_GIGA_MAC_VER_25:
3646 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003647 case RTL_GIGA_MAC_VER_27:
3648 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003649 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003650 case RTL_GIGA_MAC_VER_32:
3651 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003652 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3653 break;
3654 }
3655
3656 r8168_phy_power_up(tp);
3657}
3658
Francois Romieud58d46b2011-05-03 16:38:29 +02003659static void rtl_generic_op(struct rtl8169_private *tp,
3660 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00003661{
3662 if (op)
3663 op(tp);
3664}
3665
3666static void rtl_pll_power_down(struct rtl8169_private *tp)
3667{
Francois Romieud58d46b2011-05-03 16:38:29 +02003668 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00003669}
3670
3671static void rtl_pll_power_up(struct rtl8169_private *tp)
3672{
Francois Romieud58d46b2011-05-03 16:38:29 +02003673 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00003674}
3675
3676static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3677{
3678 struct pll_power_ops *ops = &tp->pll_power_ops;
3679
3680 switch (tp->mac_version) {
3681 case RTL_GIGA_MAC_VER_07:
3682 case RTL_GIGA_MAC_VER_08:
3683 case RTL_GIGA_MAC_VER_09:
3684 case RTL_GIGA_MAC_VER_10:
3685 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003686 case RTL_GIGA_MAC_VER_29:
3687 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003688 ops->down = r810x_pll_power_down;
3689 ops->up = r810x_pll_power_up;
3690 break;
3691
3692 case RTL_GIGA_MAC_VER_11:
3693 case RTL_GIGA_MAC_VER_12:
3694 case RTL_GIGA_MAC_VER_17:
3695 case RTL_GIGA_MAC_VER_18:
3696 case RTL_GIGA_MAC_VER_19:
3697 case RTL_GIGA_MAC_VER_20:
3698 case RTL_GIGA_MAC_VER_21:
3699 case RTL_GIGA_MAC_VER_22:
3700 case RTL_GIGA_MAC_VER_23:
3701 case RTL_GIGA_MAC_VER_24:
3702 case RTL_GIGA_MAC_VER_25:
3703 case RTL_GIGA_MAC_VER_26:
3704 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003705 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003706 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003707 case RTL_GIGA_MAC_VER_32:
3708 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003709 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08003710 case RTL_GIGA_MAC_VER_35:
3711 case RTL_GIGA_MAC_VER_36:
françois romieu065c27c2011-01-03 15:08:12 +00003712 ops->down = r8168_pll_power_down;
3713 ops->up = r8168_pll_power_up;
3714 break;
3715
3716 default:
3717 ops->down = NULL;
3718 ops->up = NULL;
3719 break;
3720 }
3721}
3722
Hayes Wange542a222011-07-06 15:58:04 +08003723static void rtl_init_rxcfg(struct rtl8169_private *tp)
3724{
3725 void __iomem *ioaddr = tp->mmio_addr;
3726
3727 switch (tp->mac_version) {
3728 case RTL_GIGA_MAC_VER_01:
3729 case RTL_GIGA_MAC_VER_02:
3730 case RTL_GIGA_MAC_VER_03:
3731 case RTL_GIGA_MAC_VER_04:
3732 case RTL_GIGA_MAC_VER_05:
3733 case RTL_GIGA_MAC_VER_06:
3734 case RTL_GIGA_MAC_VER_10:
3735 case RTL_GIGA_MAC_VER_11:
3736 case RTL_GIGA_MAC_VER_12:
3737 case RTL_GIGA_MAC_VER_13:
3738 case RTL_GIGA_MAC_VER_14:
3739 case RTL_GIGA_MAC_VER_15:
3740 case RTL_GIGA_MAC_VER_16:
3741 case RTL_GIGA_MAC_VER_17:
3742 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3743 break;
3744 case RTL_GIGA_MAC_VER_18:
3745 case RTL_GIGA_MAC_VER_19:
3746 case RTL_GIGA_MAC_VER_20:
3747 case RTL_GIGA_MAC_VER_21:
3748 case RTL_GIGA_MAC_VER_22:
3749 case RTL_GIGA_MAC_VER_23:
3750 case RTL_GIGA_MAC_VER_24:
Francois Romieu6418cc42012-06-20 12:09:18 +00003751 case RTL_GIGA_MAC_VER_34:
Hayes Wange542a222011-07-06 15:58:04 +08003752 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3753 break;
3754 default:
3755 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3756 break;
3757 }
3758}
3759
Hayes Wang92fc43b2011-07-06 15:58:03 +08003760static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3761{
3762 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3763}
3764
Francois Romieud58d46b2011-05-03 16:38:29 +02003765static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3766{
françois romieu9c5028e2012-03-02 04:43:14 +00003767 void __iomem *ioaddr = tp->mmio_addr;
3768
3769 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003770 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00003771 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003772}
3773
3774static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3775{
françois romieu9c5028e2012-03-02 04:43:14 +00003776 void __iomem *ioaddr = tp->mmio_addr;
3777
3778 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003779 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00003780 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003781}
3782
3783static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3784{
3785 void __iomem *ioaddr = tp->mmio_addr;
3786
3787 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3788 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3789 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3790}
3791
3792static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3793{
3794 void __iomem *ioaddr = tp->mmio_addr;
3795
3796 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3797 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3798 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3799}
3800
3801static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3802{
3803 void __iomem *ioaddr = tp->mmio_addr;
3804
3805 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3806}
3807
3808static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3809{
3810 void __iomem *ioaddr = tp->mmio_addr;
3811
3812 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3813}
3814
3815static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3816{
3817 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003818
3819 RTL_W8(MaxTxPacketSize, 0x3f);
3820 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3821 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003822 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003823}
3824
3825static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3826{
3827 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003828
3829 RTL_W8(MaxTxPacketSize, 0x0c);
3830 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3831 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003832 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003833}
3834
3835static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3836{
3837 rtl_tx_performance_tweak(tp->pci_dev,
3838 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3839}
3840
3841static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3842{
3843 rtl_tx_performance_tweak(tp->pci_dev,
3844 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3845}
3846
3847static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3848{
3849 void __iomem *ioaddr = tp->mmio_addr;
3850
3851 r8168b_0_hw_jumbo_enable(tp);
3852
3853 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
3854}
3855
3856static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3857{
3858 void __iomem *ioaddr = tp->mmio_addr;
3859
3860 r8168b_0_hw_jumbo_disable(tp);
3861
3862 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3863}
3864
3865static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
3866{
3867 struct jumbo_ops *ops = &tp->jumbo_ops;
3868
3869 switch (tp->mac_version) {
3870 case RTL_GIGA_MAC_VER_11:
3871 ops->disable = r8168b_0_hw_jumbo_disable;
3872 ops->enable = r8168b_0_hw_jumbo_enable;
3873 break;
3874 case RTL_GIGA_MAC_VER_12:
3875 case RTL_GIGA_MAC_VER_17:
3876 ops->disable = r8168b_1_hw_jumbo_disable;
3877 ops->enable = r8168b_1_hw_jumbo_enable;
3878 break;
3879 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
3880 case RTL_GIGA_MAC_VER_19:
3881 case RTL_GIGA_MAC_VER_20:
3882 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
3883 case RTL_GIGA_MAC_VER_22:
3884 case RTL_GIGA_MAC_VER_23:
3885 case RTL_GIGA_MAC_VER_24:
3886 case RTL_GIGA_MAC_VER_25:
3887 case RTL_GIGA_MAC_VER_26:
3888 ops->disable = r8168c_hw_jumbo_disable;
3889 ops->enable = r8168c_hw_jumbo_enable;
3890 break;
3891 case RTL_GIGA_MAC_VER_27:
3892 case RTL_GIGA_MAC_VER_28:
3893 ops->disable = r8168dp_hw_jumbo_disable;
3894 ops->enable = r8168dp_hw_jumbo_enable;
3895 break;
3896 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
3897 case RTL_GIGA_MAC_VER_32:
3898 case RTL_GIGA_MAC_VER_33:
3899 case RTL_GIGA_MAC_VER_34:
3900 ops->disable = r8168e_hw_jumbo_disable;
3901 ops->enable = r8168e_hw_jumbo_enable;
3902 break;
3903
3904 /*
3905 * No action needed for jumbo frames with 8169.
3906 * No jumbo for 810x at all.
3907 */
3908 default:
3909 ops->disable = NULL;
3910 ops->enable = NULL;
3911 break;
3912 }
3913}
3914
Francois Romieu6f43adc2011-04-29 15:05:51 +02003915static void rtl_hw_reset(struct rtl8169_private *tp)
3916{
3917 void __iomem *ioaddr = tp->mmio_addr;
3918 int i;
3919
3920 /* Soft reset the chip. */
3921 RTL_W8(ChipCmd, CmdReset);
3922
3923 /* Check that the chip has finished the reset. */
3924 for (i = 0; i < 100; i++) {
3925 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3926 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003927 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003928 }
3929}
3930
Francois Romieub6ffd972011-06-17 17:00:05 +02003931static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
3932{
3933 struct rtl_fw *rtl_fw;
3934 const char *name;
3935 int rc = -ENOMEM;
3936
3937 name = rtl_lookup_firmware_name(tp);
3938 if (!name)
3939 goto out_no_firmware;
3940
3941 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
3942 if (!rtl_fw)
3943 goto err_warn;
3944
3945 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
3946 if (rc < 0)
3947 goto err_free;
3948
Francois Romieufd112f22011-06-18 00:10:29 +02003949 rc = rtl_check_firmware(tp, rtl_fw);
3950 if (rc < 0)
3951 goto err_release_firmware;
3952
Francois Romieub6ffd972011-06-17 17:00:05 +02003953 tp->rtl_fw = rtl_fw;
3954out:
3955 return;
3956
Francois Romieufd112f22011-06-18 00:10:29 +02003957err_release_firmware:
3958 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02003959err_free:
3960 kfree(rtl_fw);
3961err_warn:
3962 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
3963 name, rc);
3964out_no_firmware:
3965 tp->rtl_fw = NULL;
3966 goto out;
3967}
3968
François Romieu953a12c2011-04-24 17:38:48 +02003969static void rtl_request_firmware(struct rtl8169_private *tp)
3970{
Francois Romieub6ffd972011-06-17 17:00:05 +02003971 if (IS_ERR(tp->rtl_fw))
3972 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02003973}
3974
Hayes Wang92fc43b2011-07-06 15:58:03 +08003975static void rtl_rx_close(struct rtl8169_private *tp)
3976{
3977 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003978
Francois Romieu1687b562011-07-19 17:21:29 +02003979 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08003980}
3981
françois romieue6de30d2011-01-03 15:08:37 +00003982static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983{
françois romieue6de30d2011-01-03 15:08:37 +00003984 void __iomem *ioaddr = tp->mmio_addr;
3985
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00003987 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988
Hayes Wang92fc43b2011-07-06 15:58:03 +08003989 rtl_rx_close(tp);
3990
Hayes Wang5d2e1952011-02-22 17:26:22 +08003991 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00003992 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3993 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00003994 while (RTL_R8(TxPoll) & NPQ)
3995 udelay(20);
Hayes Wangc2218922011-09-06 16:55:18 +08003996 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
3997 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
3998 tp->mac_version == RTL_GIGA_MAC_VER_36) {
David S. Miller8decf862011-09-22 03:23:13 -04003999 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08004000 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4001 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004002 } else {
4003 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4004 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004005 }
4006
Hayes Wang92fc43b2011-07-06 15:58:03 +08004007 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008}
4009
Francois Romieu7f796d82007-06-11 23:04:41 +02004010static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004011{
4012 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004013
4014 /* Set DMA burst size and Interframe Gap Time */
4015 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4016 (InterFrameGap << TxInterFrameGapShift));
4017}
4018
Francois Romieu07ce4062007-02-23 23:36:39 +01004019static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020{
4021 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
Francois Romieu07ce4062007-02-23 23:36:39 +01004023 tp->hw_start(dev);
4024
Francois Romieuda78dbf2012-01-26 14:18:23 +01004025 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004026}
4027
Francois Romieu7f796d82007-06-11 23:04:41 +02004028static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4029 void __iomem *ioaddr)
4030{
4031 /*
4032 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4033 * register to be written before TxDescAddrLow to work.
4034 * Switching from MMIO to I/O access fixes the issue as well.
4035 */
4036 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004037 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02004038 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004039 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02004040}
4041
4042static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4043{
4044 u16 cmd;
4045
4046 cmd = RTL_R16(CPlusCmd);
4047 RTL_W16(CPlusCmd, cmd);
4048 return cmd;
4049}
4050
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004051static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d82007-06-11 23:04:41 +02004052{
4053 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00004054 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d82007-06-11 23:04:41 +02004055}
4056
Francois Romieu6dccd162007-02-13 23:38:05 +01004057static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4058{
Francois Romieu37441002011-06-17 22:58:54 +02004059 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004060 u32 mac_version;
4061 u32 clk;
4062 u32 val;
4063 } cfg2_info [] = {
4064 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4065 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4066 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4067 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004068 };
4069 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004070 unsigned int i;
4071 u32 clk;
4072
4073 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004074 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004075 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4076 RTL_W32(0x7c, p->val);
4077 break;
4078 }
4079 }
4080}
4081
Francois Romieue6b763e2012-03-08 09:35:39 +01004082static void rtl_set_rx_mode(struct net_device *dev)
4083{
4084 struct rtl8169_private *tp = netdev_priv(dev);
4085 void __iomem *ioaddr = tp->mmio_addr;
4086 u32 mc_filter[2]; /* Multicast hash filter */
4087 int rx_mode;
4088 u32 tmp = 0;
4089
4090 if (dev->flags & IFF_PROMISC) {
4091 /* Unconditionally log net taps. */
4092 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4093 rx_mode =
4094 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4095 AcceptAllPhys;
4096 mc_filter[1] = mc_filter[0] = 0xffffffff;
4097 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4098 (dev->flags & IFF_ALLMULTI)) {
4099 /* Too many to filter perfectly -- accept all multicasts. */
4100 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4101 mc_filter[1] = mc_filter[0] = 0xffffffff;
4102 } else {
4103 struct netdev_hw_addr *ha;
4104
4105 rx_mode = AcceptBroadcast | AcceptMyPhys;
4106 mc_filter[1] = mc_filter[0] = 0;
4107 netdev_for_each_mc_addr(ha, dev) {
4108 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4109 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4110 rx_mode |= AcceptMulticast;
4111 }
4112 }
4113
4114 if (dev->features & NETIF_F_RXALL)
4115 rx_mode |= (AcceptErr | AcceptRunt);
4116
4117 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4118
4119 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4120 u32 data = mc_filter[0];
4121
4122 mc_filter[0] = swab32(mc_filter[1]);
4123 mc_filter[1] = swab32(data);
4124 }
4125
4126 RTL_W32(MAR0 + 4, mc_filter[1]);
4127 RTL_W32(MAR0 + 0, mc_filter[0]);
4128
4129 RTL_W32(RxConfig, tmp);
4130}
4131
Francois Romieu07ce4062007-02-23 23:36:39 +01004132static void rtl_hw_start_8169(struct net_device *dev)
4133{
4134 struct rtl8169_private *tp = netdev_priv(dev);
4135 void __iomem *ioaddr = tp->mmio_addr;
4136 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004137
Francois Romieu9cb427b2006-11-02 00:10:16 +01004138 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4139 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4140 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4141 }
4142
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004144 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4145 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4146 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4147 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004148 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4149
Hayes Wange542a222011-07-06 15:58:04 +08004150 rtl_init_rxcfg(tp);
4151
françois romieuf0298f82011-01-03 15:07:42 +00004152 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004154 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155
Francois Romieucecb5fd2011-04-01 10:21:07 +02004156 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4157 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4158 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4159 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004160 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
Francois Romieu7f796d82007-06-11 23:04:41 +02004162 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004163
Francois Romieucecb5fd2011-04-01 10:21:07 +02004164 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4165 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004166 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004168 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 }
4170
Francois Romieubcf0bf92006-07-26 23:14:13 +02004171 RTL_W16(CPlusCmd, tp->cp_cmd);
4172
Francois Romieu6dccd162007-02-13 23:38:05 +01004173 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4174
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 /*
4176 * Undocumented corner. Supposedly:
4177 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4178 */
4179 RTL_W16(IntrMitigate, 0x0000);
4180
Francois Romieu7f796d82007-06-11 23:04:41 +02004181 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004182
Francois Romieucecb5fd2011-04-01 10:21:07 +02004183 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4184 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4185 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4186 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004187 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4188 rtl_set_rx_tx_config_registers(tp);
4189 }
4190
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004192
4193 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4194 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
4196 RTL_W32(RxMissed, 0);
4197
Francois Romieu07ce4062007-02-23 23:36:39 +01004198 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199
4200 /* no early-rx interrupts */
4201 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004202}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
françois romieu650e8d52011-01-03 15:08:29 +00004204static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004205{
4206 u32 csi;
4207
4208 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00004209 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4210}
4211
françois romieue6de30d2011-01-03 15:08:37 +00004212static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4213{
4214 rtl_csi_access_enable(ioaddr, 0x17000000);
4215}
4216
françois romieu650e8d52011-01-03 15:08:29 +00004217static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4218{
4219 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02004220}
4221
4222struct ephy_info {
4223 unsigned int offset;
4224 u16 mask;
4225 u16 bits;
4226};
4227
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004228static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004229{
4230 u16 w;
4231
4232 while (len-- > 0) {
4233 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4234 rtl_ephy_write(ioaddr, e->offset, w);
4235 e++;
4236 }
4237}
4238
Francois Romieub726e492008-06-28 12:22:59 +02004239static void rtl_disable_clock_request(struct pci_dev *pdev)
4240{
Jon Masone44daad2011-06-27 07:46:31 +00004241 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004242
4243 if (cap) {
4244 u16 ctl;
4245
4246 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4247 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4248 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4249 }
4250}
4251
françois romieue6de30d2011-01-03 15:08:37 +00004252static void rtl_enable_clock_request(struct pci_dev *pdev)
4253{
Jon Masone44daad2011-06-27 07:46:31 +00004254 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004255
4256 if (cap) {
4257 u16 ctl;
4258
4259 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4260 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4261 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4262 }
4263}
4264
Francois Romieub726e492008-06-28 12:22:59 +02004265#define R8168_CPCMD_QUIRK_MASK (\
4266 EnableBist | \
4267 Mac_dbgo_oe | \
4268 Force_half_dup | \
4269 Force_rxflow_en | \
4270 Force_txflow_en | \
4271 Cxpl_dbg_sel | \
4272 ASF | \
4273 PktCntrDisable | \
4274 Mac_dbgo_sel)
4275
Francois Romieu219a1e92008-06-28 11:58:39 +02004276static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4277{
Francois Romieub726e492008-06-28 12:22:59 +02004278 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4279
4280 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4281
Francois Romieu2e68ae42008-06-28 12:00:55 +02004282 rtl_tx_performance_tweak(pdev,
4283 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004284}
4285
4286static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4287{
4288 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004289
françois romieuf0298f82011-01-03 15:07:42 +00004290 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004291
4292 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004293}
4294
4295static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4296{
Francois Romieub726e492008-06-28 12:22:59 +02004297 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4298
4299 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4300
Francois Romieu219a1e92008-06-28 11:58:39 +02004301 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004302
4303 rtl_disable_clock_request(pdev);
4304
4305 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004306}
4307
Francois Romieuef3386f2008-06-29 12:24:30 +02004308static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004309{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004310 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004311 { 0x01, 0, 0x0001 },
4312 { 0x02, 0x0800, 0x1000 },
4313 { 0x03, 0, 0x0042 },
4314 { 0x06, 0x0080, 0x0000 },
4315 { 0x07, 0, 0x2000 }
4316 };
4317
françois romieu650e8d52011-01-03 15:08:29 +00004318 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004319
4320 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4321
Francois Romieu219a1e92008-06-28 11:58:39 +02004322 __rtl_hw_start_8168cp(ioaddr, pdev);
4323}
4324
Francois Romieuef3386f2008-06-29 12:24:30 +02004325static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4326{
françois romieu650e8d52011-01-03 15:08:29 +00004327 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004328
4329 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4330
4331 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4332
4333 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4334}
4335
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004336static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4337{
françois romieu650e8d52011-01-03 15:08:29 +00004338 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004339
4340 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4341
4342 /* Magic. */
4343 RTL_W8(DBG_REG, 0x20);
4344
françois romieuf0298f82011-01-03 15:07:42 +00004345 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004346
4347 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4348
4349 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4350}
4351
Francois Romieu219a1e92008-06-28 11:58:39 +02004352static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4353{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004354 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004355 { 0x02, 0x0800, 0x1000 },
4356 { 0x03, 0, 0x0002 },
4357 { 0x06, 0x0080, 0x0000 }
4358 };
4359
françois romieu650e8d52011-01-03 15:08:29 +00004360 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004361
4362 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4363
4364 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4365
Francois Romieu219a1e92008-06-28 11:58:39 +02004366 __rtl_hw_start_8168cp(ioaddr, pdev);
4367}
4368
4369static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4370{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004371 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004372 { 0x01, 0, 0x0001 },
4373 { 0x03, 0x0400, 0x0220 }
4374 };
4375
françois romieu650e8d52011-01-03 15:08:29 +00004376 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004377
4378 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4379
Francois Romieu219a1e92008-06-28 11:58:39 +02004380 __rtl_hw_start_8168cp(ioaddr, pdev);
4381}
4382
Francois Romieu197ff762008-06-28 13:16:02 +02004383static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4384{
4385 rtl_hw_start_8168c_2(ioaddr, pdev);
4386}
4387
Francois Romieu6fb07052008-06-29 11:54:28 +02004388static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4389{
françois romieu650e8d52011-01-03 15:08:29 +00004390 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004391
4392 __rtl_hw_start_8168cp(ioaddr, pdev);
4393}
4394
Francois Romieu5b538df2008-07-20 16:22:45 +02004395static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4396{
françois romieu650e8d52011-01-03 15:08:29 +00004397 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004398
4399 rtl_disable_clock_request(pdev);
4400
françois romieuf0298f82011-01-03 15:07:42 +00004401 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004402
4403 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4404
4405 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4406}
4407
hayeswang4804b3b2011-03-21 01:50:29 +00004408static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4409{
4410 rtl_csi_access_enable_1(ioaddr);
4411
4412 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4413
4414 RTL_W8(MaxTxPacketSize, TxPacketMax);
4415
4416 rtl_disable_clock_request(pdev);
4417}
4418
françois romieue6de30d2011-01-03 15:08:37 +00004419static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4420{
4421 static const struct ephy_info e_info_8168d_4[] = {
4422 { 0x0b, ~0, 0x48 },
4423 { 0x19, 0x20, 0x50 },
4424 { 0x0c, ~0, 0x20 }
4425 };
4426 int i;
4427
4428 rtl_csi_access_enable_1(ioaddr);
4429
4430 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4431
4432 RTL_W8(MaxTxPacketSize, TxPacketMax);
4433
4434 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4435 const struct ephy_info *e = e_info_8168d_4 + i;
4436 u16 w;
4437
4438 w = rtl_ephy_read(ioaddr, e->offset);
4439 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4440 }
4441
4442 rtl_enable_clock_request(pdev);
4443}
4444
Hayes Wang70090422011-07-06 15:58:06 +08004445static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
hayeswang01dc7fe2011-03-21 01:50:28 +00004446{
Hayes Wang70090422011-07-06 15:58:06 +08004447 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004448 { 0x00, 0x0200, 0x0100 },
4449 { 0x00, 0x0000, 0x0004 },
4450 { 0x06, 0x0002, 0x0001 },
4451 { 0x06, 0x0000, 0x0030 },
4452 { 0x07, 0x0000, 0x2000 },
4453 { 0x00, 0x0000, 0x0020 },
4454 { 0x03, 0x5800, 0x2000 },
4455 { 0x03, 0x0000, 0x0001 },
4456 { 0x01, 0x0800, 0x1000 },
4457 { 0x07, 0x0000, 0x4000 },
4458 { 0x1e, 0x0000, 0x2000 },
4459 { 0x19, 0xffff, 0xfe6c },
4460 { 0x0a, 0x0000, 0x0040 }
4461 };
4462
4463 rtl_csi_access_enable_2(ioaddr);
4464
Hayes Wang70090422011-07-06 15:58:06 +08004465 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004466
4467 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4468
4469 RTL_W8(MaxTxPacketSize, TxPacketMax);
4470
4471 rtl_disable_clock_request(pdev);
4472
4473 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004474 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4475 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004476
Francois Romieucecb5fd2011-04-01 10:21:07 +02004477 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004478}
4479
Hayes Wang70090422011-07-06 15:58:06 +08004480static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4481{
4482 static const struct ephy_info e_info_8168e_2[] = {
4483 { 0x09, 0x0000, 0x0080 },
4484 { 0x19, 0x0000, 0x0224 }
4485 };
4486
4487 rtl_csi_access_enable_1(ioaddr);
4488
4489 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4490
4491 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4492
4493 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4494 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4495 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4496 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4497 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4498 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4499 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4500 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4501 ERIAR_EXGMAC);
4502
Hayes Wang3090bd92011-09-06 16:55:15 +08004503 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004504
4505 rtl_disable_clock_request(pdev);
4506
4507 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4508 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4509
4510 /* Adjust EEE LED frequency */
4511 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4512
4513 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4514 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4515 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4516}
4517
Hayes Wangc2218922011-09-06 16:55:18 +08004518static void rtl_hw_start_8168f_1(void __iomem *ioaddr, struct pci_dev *pdev)
4519{
4520 static const struct ephy_info e_info_8168f_1[] = {
4521 { 0x06, 0x00c0, 0x0020 },
4522 { 0x08, 0x0001, 0x0002 },
4523 { 0x09, 0x0000, 0x0080 },
4524 { 0x19, 0x0000, 0x0224 }
4525 };
4526
4527 rtl_csi_access_enable_1(ioaddr);
4528
4529 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4530
4531 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4532
4533 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4534 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4535 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4536 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4537 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4538 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4539 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4540 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4541 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4542 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
4543 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4544 ERIAR_EXGMAC);
4545
4546 RTL_W8(MaxTxPacketSize, EarlySize);
4547
4548 rtl_disable_clock_request(pdev);
4549
4550 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4551 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4552
4553 /* Adjust EEE LED frequency */
4554 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4555
4556 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4557 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4558 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4559}
4560
Francois Romieu07ce4062007-02-23 23:36:39 +01004561static void rtl_hw_start_8168(struct net_device *dev)
4562{
Francois Romieu2dd99532007-06-11 23:22:52 +02004563 struct rtl8169_private *tp = netdev_priv(dev);
4564 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004565 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004566
4567 RTL_W8(Cfg9346, Cfg9346_Unlock);
4568
françois romieuf0298f82011-01-03 15:07:42 +00004569 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004570
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004571 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004572
Francois Romieu0e485152007-02-20 00:00:26 +01004573 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004574
4575 RTL_W16(CPlusCmd, tp->cp_cmd);
4576
Francois Romieu0e485152007-02-20 00:00:26 +01004577 RTL_W16(IntrMitigate, 0x5151);
4578
4579 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00004580 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01004581 tp->event_slow |= RxFIFOOver | PCSTimeout;
4582 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01004583 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004584
4585 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4586
Francois Romieub8363902008-06-01 12:31:57 +02004587 rtl_set_rx_mode(dev);
4588
4589 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4590 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004591
4592 RTL_R8(IntrMask);
4593
Francois Romieu219a1e92008-06-28 11:58:39 +02004594 switch (tp->mac_version) {
4595 case RTL_GIGA_MAC_VER_11:
4596 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004597 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004598
4599 case RTL_GIGA_MAC_VER_12:
4600 case RTL_GIGA_MAC_VER_17:
4601 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004602 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004603
4604 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004605 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004606 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004607
4608 case RTL_GIGA_MAC_VER_19:
4609 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004610 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004611
4612 case RTL_GIGA_MAC_VER_20:
4613 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004614 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004615
Francois Romieu197ff762008-06-28 13:16:02 +02004616 case RTL_GIGA_MAC_VER_21:
4617 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004618 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004619
Francois Romieu6fb07052008-06-29 11:54:28 +02004620 case RTL_GIGA_MAC_VER_22:
4621 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004622 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004623
Francois Romieuef3386f2008-06-29 12:24:30 +02004624 case RTL_GIGA_MAC_VER_23:
4625 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004626 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004627
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004628 case RTL_GIGA_MAC_VER_24:
4629 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004630 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004631
Francois Romieu5b538df2008-07-20 16:22:45 +02004632 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004633 case RTL_GIGA_MAC_VER_26:
4634 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004635 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004636 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004637
françois romieue6de30d2011-01-03 15:08:37 +00004638 case RTL_GIGA_MAC_VER_28:
4639 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004640 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004641
hayeswang4804b3b2011-03-21 01:50:29 +00004642 case RTL_GIGA_MAC_VER_31:
4643 rtl_hw_start_8168dp(ioaddr, pdev);
4644 break;
4645
hayeswang01dc7fe2011-03-21 01:50:28 +00004646 case RTL_GIGA_MAC_VER_32:
4647 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004648 rtl_hw_start_8168e_1(ioaddr, pdev);
4649 break;
4650 case RTL_GIGA_MAC_VER_34:
4651 rtl_hw_start_8168e_2(ioaddr, pdev);
hayeswang01dc7fe2011-03-21 01:50:28 +00004652 break;
françois romieue6de30d2011-01-03 15:08:37 +00004653
Hayes Wangc2218922011-09-06 16:55:18 +08004654 case RTL_GIGA_MAC_VER_35:
4655 case RTL_GIGA_MAC_VER_36:
4656 rtl_hw_start_8168f_1(ioaddr, pdev);
4657 break;
4658
Francois Romieu219a1e92008-06-28 11:58:39 +02004659 default:
4660 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4661 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004662 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004663 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004664
Francois Romieu0e485152007-02-20 00:00:26 +01004665 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4666
Francois Romieub8363902008-06-01 12:31:57 +02004667 RTL_W8(Cfg9346, Cfg9346_Lock);
4668
Francois Romieu2dd99532007-06-11 23:22:52 +02004669 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004670}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671
Francois Romieu2857ffb2008-08-02 21:08:49 +02004672#define R810X_CPCMD_QUIRK_MASK (\
4673 EnableBist | \
4674 Mac_dbgo_oe | \
4675 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004676 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004677 Force_txflow_en | \
4678 Cxpl_dbg_sel | \
4679 ASF | \
4680 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004681 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004682
4683static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4684{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004685 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004686 { 0x01, 0, 0x6e65 },
4687 { 0x02, 0, 0x091f },
4688 { 0x03, 0, 0xc2f9 },
4689 { 0x06, 0, 0xafb5 },
4690 { 0x07, 0, 0x0e00 },
4691 { 0x19, 0, 0xec80 },
4692 { 0x01, 0, 0x2e65 },
4693 { 0x01, 0, 0x6e65 }
4694 };
4695 u8 cfg1;
4696
françois romieu650e8d52011-01-03 15:08:29 +00004697 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004698
4699 RTL_W8(DBG_REG, FIX_NAK_1);
4700
4701 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4702
4703 RTL_W8(Config1,
4704 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4705 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4706
4707 cfg1 = RTL_R8(Config1);
4708 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4709 RTL_W8(Config1, cfg1 & ~LEDS0);
4710
Francois Romieu2857ffb2008-08-02 21:08:49 +02004711 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4712}
4713
4714static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4715{
françois romieu650e8d52011-01-03 15:08:29 +00004716 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004717
4718 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4719
4720 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4721 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004722}
4723
4724static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4725{
4726 rtl_hw_start_8102e_2(ioaddr, pdev);
4727
4728 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4729}
4730
Hayes Wang5a5e4442011-02-22 17:26:21 +08004731static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4732{
4733 static const struct ephy_info e_info_8105e_1[] = {
4734 { 0x07, 0, 0x4000 },
4735 { 0x19, 0, 0x0200 },
4736 { 0x19, 0, 0x0020 },
4737 { 0x1e, 0, 0x2000 },
4738 { 0x03, 0, 0x0001 },
4739 { 0x19, 0, 0x0100 },
4740 { 0x19, 0, 0x0004 },
4741 { 0x0a, 0, 0x0020 }
4742 };
4743
Francois Romieucecb5fd2011-04-01 10:21:07 +02004744 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004745 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4746
Francois Romieucecb5fd2011-04-01 10:21:07 +02004747 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004748 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4749
4750 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e2011-07-06 15:58:02 +08004751 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004752
4753 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4754}
4755
4756static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4757{
4758 rtl_hw_start_8105e_1(ioaddr, pdev);
4759 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4760}
4761
Francois Romieu07ce4062007-02-23 23:36:39 +01004762static void rtl_hw_start_8101(struct net_device *dev)
4763{
Francois Romieucdf1a602007-06-11 23:29:50 +02004764 struct rtl8169_private *tp = netdev_priv(dev);
4765 void __iomem *ioaddr = tp->mmio_addr;
4766 struct pci_dev *pdev = tp->pci_dev;
4767
Francois Romieuda78dbf2012-01-26 14:18:23 +01004768 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
4769 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00004770
Francois Romieucecb5fd2011-04-01 10:21:07 +02004771 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4772 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00004773 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02004774
4775 if (cap) {
4776 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4777 PCI_EXP_DEVCTL_NOSNOOP_EN);
4778 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004779 }
4780
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004781 RTL_W8(Cfg9346, Cfg9346_Unlock);
4782
Francois Romieu2857ffb2008-08-02 21:08:49 +02004783 switch (tp->mac_version) {
4784 case RTL_GIGA_MAC_VER_07:
4785 rtl_hw_start_8102e_1(ioaddr, pdev);
4786 break;
4787
4788 case RTL_GIGA_MAC_VER_08:
4789 rtl_hw_start_8102e_3(ioaddr, pdev);
4790 break;
4791
4792 case RTL_GIGA_MAC_VER_09:
4793 rtl_hw_start_8102e_2(ioaddr, pdev);
4794 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004795
4796 case RTL_GIGA_MAC_VER_29:
4797 rtl_hw_start_8105e_1(ioaddr, pdev);
4798 break;
4799 case RTL_GIGA_MAC_VER_30:
4800 rtl_hw_start_8105e_2(ioaddr, pdev);
4801 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004802 }
4803
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004804 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004805
françois romieuf0298f82011-01-03 15:07:42 +00004806 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004807
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004808 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004809
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004810 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004811 RTL_W16(CPlusCmd, tp->cp_cmd);
4812
4813 RTL_W16(IntrMitigate, 0x0000);
4814
4815 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4816
4817 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4818 rtl_set_rx_tx_config_registers(tp);
4819
Francois Romieucdf1a602007-06-11 23:29:50 +02004820 RTL_R8(IntrMask);
4821
Francois Romieucdf1a602007-06-11 23:29:50 +02004822 rtl_set_rx_mode(dev);
4823
4824 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825}
4826
4827static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4828{
Francois Romieud58d46b2011-05-03 16:38:29 +02004829 struct rtl8169_private *tp = netdev_priv(dev);
4830
4831 if (new_mtu < ETH_ZLEN ||
4832 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 return -EINVAL;
4834
Francois Romieud58d46b2011-05-03 16:38:29 +02004835 if (new_mtu > ETH_DATA_LEN)
4836 rtl_hw_jumbo_enable(tp);
4837 else
4838 rtl_hw_jumbo_disable(tp);
4839
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004841 netdev_update_features(dev);
4842
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004843 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844}
4845
4846static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4847{
Al Viro95e09182007-12-22 18:55:39 +00004848 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4850}
4851
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004852static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4853 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004855 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004856 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004857
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004858 kfree(*data_buff);
4859 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 rtl8169_make_unusable_by_asic(desc);
4861}
4862
4863static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4864{
4865 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4866
4867 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4868}
4869
4870static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4871 u32 rx_buf_sz)
4872{
4873 desc->addr = cpu_to_le64(mapping);
4874 wmb();
4875 rtl8169_mark_to_asic(desc, rx_buf_sz);
4876}
4877
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004878static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004880 return (void *)ALIGN((long)data, 16);
4881}
4882
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004883static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4884 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004885{
4886 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004888 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004889 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004890 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004892 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4893 if (!data)
4894 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004895
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004896 if (rtl8169_align(data) != data) {
4897 kfree(data);
4898 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4899 if (!data)
4900 return NULL;
4901 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004902
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004903 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004904 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004905 if (unlikely(dma_mapping_error(d, mapping))) {
4906 if (net_ratelimit())
4907 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004908 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910
4911 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004912 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004913
4914err_out:
4915 kfree(data);
4916 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917}
4918
4919static void rtl8169_rx_clear(struct rtl8169_private *tp)
4920{
Francois Romieu07d3f512007-02-21 22:40:46 +01004921 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
4923 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004924 if (tp->Rx_databuff[i]) {
4925 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 tp->RxDescArray + i);
4927 }
4928 }
4929}
4930
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004931static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004933 desc->opts1 |= cpu_to_le32(RingEnd);
4934}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004935
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004936static int rtl8169_rx_fill(struct rtl8169_private *tp)
4937{
4938 unsigned int i;
4939
4940 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004941 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004942
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004943 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004945
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004946 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004947 if (!data) {
4948 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004949 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004950 }
4951 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004954 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4955 return 0;
4956
4957err_out:
4958 rtl8169_rx_clear(tp);
4959 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960}
4961
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962static int rtl8169_init_ring(struct net_device *dev)
4963{
4964 struct rtl8169_private *tp = netdev_priv(dev);
4965
4966 rtl8169_init_ring_indexes(tp);
4967
4968 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004969 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004971 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972}
4973
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004974static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 struct TxDesc *desc)
4976{
4977 unsigned int len = tx_skb->len;
4978
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004979 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4980
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 desc->opts1 = 0x00;
4982 desc->opts2 = 0x00;
4983 desc->addr = 0x00;
4984 tx_skb->len = 0;
4985}
4986
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004987static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4988 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989{
4990 unsigned int i;
4991
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004992 for (i = 0; i < n; i++) {
4993 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 struct ring_info *tx_skb = tp->tx_skb + entry;
4995 unsigned int len = tx_skb->len;
4996
4997 if (len) {
4998 struct sk_buff *skb = tx_skb->skb;
4999
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005000 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 tp->TxDescArray + entry);
5002 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005003 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 dev_kfree_skb(skb);
5005 tx_skb->skb = NULL;
5006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 }
5008 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005009}
5010
5011static void rtl8169_tx_clear(struct rtl8169_private *tp)
5012{
5013 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 tp->cur_tx = tp->dirty_tx = 0;
5015}
5016
Francois Romieu4422bcd2012-01-26 11:23:32 +01005017static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018{
David Howellsc4028952006-11-22 14:57:56 +00005019 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005020 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021
Francois Romieuda78dbf2012-01-26 14:18:23 +01005022 napi_disable(&tp->napi);
5023 netif_stop_queue(dev);
5024 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025
françois romieuc7c2c392011-12-04 20:30:52 +00005026 rtl8169_hw_reset(tp);
5027
Francois Romieu56de4142011-03-15 17:29:31 +01005028 for (i = 0; i < NUM_RX_DESC; i++)
5029 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5030
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005032 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033
Francois Romieuda78dbf2012-01-26 14:18:23 +01005034 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01005035 rtl_hw_start(dev);
5036 netif_wake_queue(dev);
5037 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038}
5039
5040static void rtl8169_tx_timeout(struct net_device *dev)
5041{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005042 struct rtl8169_private *tp = netdev_priv(dev);
5043
5044 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045}
5046
5047static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005048 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049{
5050 struct skb_shared_info *info = skb_shinfo(skb);
5051 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005052 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005053 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054
5055 entry = tp->cur_tx;
5056 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005057 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 dma_addr_t mapping;
5059 u32 status, len;
5060 void *addr;
5061
5062 entry = (entry + 1) % NUM_TX_DESC;
5063
5064 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005065 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005066 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005067 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005068 if (unlikely(dma_mapping_error(d, mapping))) {
5069 if (net_ratelimit())
5070 netif_err(tp, drv, tp->dev,
5071 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005072 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074
Francois Romieucecb5fd2011-04-01 10:21:07 +02005075 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005076 status = opts[0] | len |
5077 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078
5079 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005080 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 txd->addr = cpu_to_le64(mapping);
5082
5083 tp->tx_skb[entry].len = len;
5084 }
5085
5086 if (cur_frag) {
5087 tp->tx_skb[entry].skb = skb;
5088 txd->opts1 |= cpu_to_le32(LastFrag);
5089 }
5090
5091 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005092
5093err_out:
5094 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5095 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096}
5097
Francois Romieu2b7b4312011-04-18 22:53:24 -07005098static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5099 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005101 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005102 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005103 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104
Francois Romieu2b7b4312011-04-18 22:53:24 -07005105 if (mss) {
5106 opts[0] |= TD_LSO;
5107 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5108 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005109 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110
5111 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005112 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005114 opts[offset] |= info->checksum.udp;
5115 else
5116 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118}
5119
Stephen Hemminger613573252009-08-31 19:50:58 +00005120static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5121 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122{
5123 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005124 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 struct TxDesc *txd = tp->TxDescArray + entry;
5126 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005127 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 dma_addr_t mapping;
5129 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005130 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005131 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005132
Julien Ducourthial477206a2012-05-09 00:00:06 +02005133 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005134 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005135 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 }
5137
5138 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005139 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005141 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005142 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005143 if (unlikely(dma_mapping_error(d, mapping))) {
5144 if (net_ratelimit())
5145 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005146 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
5149 tp->tx_skb[entry].len = len;
5150 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151
Francois Romieu2b7b4312011-04-18 22:53:24 -07005152 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5153 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005154
Francois Romieu2b7b4312011-04-18 22:53:24 -07005155 rtl8169_tso_csum(tp, skb, opts);
5156
5157 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005158 if (frags < 0)
5159 goto err_dma_1;
5160 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005161 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005162 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005163 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005164 tp->tx_skb[entry].skb = skb;
5165 }
5166
Francois Romieu2b7b4312011-04-18 22:53:24 -07005167 txd->opts2 = cpu_to_le32(opts[1]);
5168
Richard Cochran5047fb52012-03-10 07:29:42 +00005169 skb_tx_timestamp(skb);
5170
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 wmb();
5172
Francois Romieucecb5fd2011-04-01 10:21:07 +02005173 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005174 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 txd->opts1 = cpu_to_le32(status);
5176
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 tp->cur_tx += frags + 1;
5178
David Dillow4c020a92010-03-03 16:33:10 +00005179 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180
Francois Romieucecb5fd2011-04-01 10:21:07 +02005181 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182
Francois Romieuda78dbf2012-01-26 14:18:23 +01005183 mmiowb();
5184
Julien Ducourthial477206a2012-05-09 00:00:06 +02005185 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01005186 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5187 * not miss a ring update when it notices a stopped queue.
5188 */
5189 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01005191 /* Sync with rtl_tx:
5192 * - publish queue status and cur_tx ring index (write barrier)
5193 * - refresh dirty_tx ring index (read barrier).
5194 * May the current thread have a pessimistic view of the ring
5195 * status and forget to wake up queue, a racing rtl_tx thread
5196 * can't.
5197 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005198 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02005199 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 netif_wake_queue(dev);
5201 }
5202
Stephen Hemminger613573252009-08-31 19:50:58 +00005203 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005205err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005206 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005207err_dma_0:
5208 dev_kfree_skb(skb);
5209 dev->stats.tx_dropped++;
5210 return NETDEV_TX_OK;
5211
5212err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005214 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005215 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216}
5217
5218static void rtl8169_pcierr_interrupt(struct net_device *dev)
5219{
5220 struct rtl8169_private *tp = netdev_priv(dev);
5221 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 u16 pci_status, pci_cmd;
5223
5224 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5225 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5226
Joe Perchesbf82c182010-02-09 11:49:50 +00005227 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5228 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229
5230 /*
5231 * The recovery sequence below admits a very elaborated explanation:
5232 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005233 * - I did not see what else could be done;
5234 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 *
5236 * Feel free to adjust to your needs.
5237 */
Francois Romieua27993f2006-12-18 00:04:19 +01005238 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005239 pci_cmd &= ~PCI_COMMAND_PARITY;
5240 else
5241 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5242
5243 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244
5245 pci_write_config_word(pdev, PCI_STATUS,
5246 pci_status & (PCI_STATUS_DETECTED_PARITY |
5247 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5248 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5249
5250 /* The infamous DAC f*ckup only happens at boot time */
5251 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005252 void __iomem *ioaddr = tp->mmio_addr;
5253
Joe Perchesbf82c182010-02-09 11:49:50 +00005254 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 tp->cp_cmd &= ~PCIDAC;
5256 RTL_W16(CPlusCmd, tp->cp_cmd);
5257 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 }
5259
françois romieue6de30d2011-01-03 15:08:37 +00005260 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005261
Francois Romieu98ddf982012-01-31 10:47:34 +01005262 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263}
5264
Francois Romieuda78dbf2012-01-26 14:18:23 +01005265static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266{
5267 unsigned int dirty_tx, tx_left;
5268
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 dirty_tx = tp->dirty_tx;
5270 smp_rmb();
5271 tx_left = tp->cur_tx - dirty_tx;
5272
5273 while (tx_left > 0) {
5274 unsigned int entry = dirty_tx % NUM_TX_DESC;
5275 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 u32 status;
5277
5278 rmb();
5279 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5280 if (status & DescOwn)
5281 break;
5282
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005283 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5284 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 if (status & LastFrag) {
Francois Romieuf63b1d92012-07-23 22:55:55 +02005286 u64_stats_update_begin(&tp->tx_stats.syncp);
5287 tp->tx_stats.packets++;
5288 tp->tx_stats.bytes += tx_skb->skb->len;
5289 u64_stats_update_end(&tp->tx_stats.syncp);
5290 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 tx_skb->skb = NULL;
5292 }
5293 dirty_tx++;
5294 tx_left--;
5295 }
5296
5297 if (tp->dirty_tx != dirty_tx) {
5298 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005299 /* Sync with rtl8169_start_xmit:
5300 * - publish dirty_tx ring index (write barrier)
5301 * - refresh cur_tx ring index and queue status (read barrier)
5302 * May the current thread miss the stopped queue condition,
5303 * a racing xmit thread can only have a right view of the
5304 * ring status.
5305 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005306 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02005308 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 netif_wake_queue(dev);
5310 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005311 /*
5312 * 8168 hack: TxPoll requests are lost when the Tx packets are
5313 * too close. Let's kick an extra TxPoll request when a burst
5314 * of start_xmit activity is detected (if it is not detected,
5315 * it is slow enough). -- FR
5316 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005317 if (tp->cur_tx != dirty_tx) {
5318 void __iomem *ioaddr = tp->mmio_addr;
5319
Francois Romieud78ae2d2007-08-26 20:08:19 +02005320 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 }
5323}
5324
Francois Romieu126fa4b2005-05-12 20:09:17 -04005325static inline int rtl8169_fragmented_frame(u32 status)
5326{
5327 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5328}
5329
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005330static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 u32 status = opts1 & RxProtoMask;
5333
5334 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005335 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 skb->ip_summed = CHECKSUM_UNNECESSARY;
5337 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005338 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339}
5340
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005341static struct sk_buff *rtl8169_try_rx_copy(void *data,
5342 struct rtl8169_private *tp,
5343 int pkt_size,
5344 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005346 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005347 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005349 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005350 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005351 prefetch(data);
5352 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5353 if (skb)
5354 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005355 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5356
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005357 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358}
5359
Francois Romieuda78dbf2012-01-26 14:18:23 +01005360static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361{
5362 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005363 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 cur_rx = tp->cur_rx;
5366 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005367 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005369 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005371 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 u32 status;
5373
5374 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005375 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376
5377 if (status & DescOwn)
5378 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005379 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005380 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5381 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005382 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005384 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005386 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005387 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005388 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005389 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005390 }
Ben Greear6bbe0212012-02-10 15:04:33 +00005391 if ((status & (RxRUNT | RxCRC)) &&
5392 !(status & (RxRWT | RxFOVF)) &&
5393 (dev->features & NETIF_F_RXALL))
5394 goto process_pkt;
5395
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005396 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005398 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00005399 dma_addr_t addr;
5400 int pkt_size;
5401
5402process_pkt:
5403 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00005404 if (likely(!(dev->features & NETIF_F_RXFCS)))
5405 pkt_size = (status & 0x00003fff) - 4;
5406 else
5407 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408
Francois Romieu126fa4b2005-05-12 20:09:17 -04005409 /*
5410 * The driver does not support incoming fragmented
5411 * frames. They are seen as a symptom of over-mtu
5412 * sized frames.
5413 */
5414 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005415 dev->stats.rx_dropped++;
5416 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005417 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005418 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005419 }
5420
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005421 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5422 tp, pkt_size, addr);
5423 rtl8169_mark_to_asic(desc, rx_buf_sz);
5424 if (!skb) {
5425 dev->stats.rx_dropped++;
5426 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 }
5428
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005429 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 skb_put(skb, pkt_size);
5431 skb->protocol = eth_type_trans(skb, dev);
5432
Francois Romieu7a8fc772011-03-01 17:18:33 +01005433 rtl8169_rx_vlan_tag(desc, skb);
5434
Francois Romieu56de4142011-03-15 17:29:31 +01005435 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436
Junchang Wang8027aa22012-03-04 23:30:32 +01005437 u64_stats_update_begin(&tp->rx_stats.syncp);
5438 tp->rx_stats.packets++;
5439 tp->rx_stats.bytes += pkt_size;
5440 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005442
5443 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005444 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005445 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5446 desc->opts2 = 0;
5447 cur_rx++;
5448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 }
5450
5451 count = cur_rx - tp->cur_rx;
5452 tp->cur_rx = cur_rx;
5453
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005454 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455
5456 return count;
5457}
5458
Francois Romieu07d3f512007-02-21 22:40:46 +01005459static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460{
Francois Romieu07d3f512007-02-21 22:40:46 +01005461 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 int handled = 0;
Francois Romieu9085cdf2012-01-26 12:59:08 +01005464 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465
Francois Romieu9085cdf2012-01-26 12:59:08 +01005466 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005467 if (status && status != 0xffff) {
5468 status &= RTL_EVENT_NAPI | tp->event_slow;
5469 if (status) {
5470 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00005471
Francois Romieuda78dbf2012-01-26 14:18:23 +01005472 rtl_irq_disable(tp);
5473 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 return IRQ_RETVAL(handled);
5477}
5478
Francois Romieuda78dbf2012-01-26 14:18:23 +01005479/*
5480 * Workqueue context.
5481 */
5482static void rtl_slow_event_work(struct rtl8169_private *tp)
5483{
5484 struct net_device *dev = tp->dev;
5485 u16 status;
5486
5487 status = rtl_get_events(tp) & tp->event_slow;
5488 rtl_ack_events(tp, status);
5489
5490 if (unlikely(status & RxFIFOOver)) {
5491 switch (tp->mac_version) {
5492 /* Work around for rx fifo overflow */
5493 case RTL_GIGA_MAC_VER_11:
5494 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01005495 /* XXX - Hack alert. See rtl_task(). */
5496 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005497 default:
5498 break;
5499 }
5500 }
5501
5502 if (unlikely(status & SYSErr))
5503 rtl8169_pcierr_interrupt(dev);
5504
5505 if (status & LinkChg)
5506 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
5507
5508 napi_disable(&tp->napi);
5509 rtl_irq_disable(tp);
5510
5511 napi_enable(&tp->napi);
5512 napi_schedule(&tp->napi);
5513}
5514
Francois Romieu4422bcd2012-01-26 11:23:32 +01005515static void rtl_task(struct work_struct *work)
5516{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005517 static const struct {
5518 int bitnr;
5519 void (*action)(struct rtl8169_private *);
5520 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01005521 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005522 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
5523 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
5524 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
5525 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01005526 struct rtl8169_private *tp =
5527 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005528 struct net_device *dev = tp->dev;
5529 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01005530
Francois Romieuda78dbf2012-01-26 14:18:23 +01005531 rtl_lock_work(tp);
5532
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005533 if (!netif_running(dev) ||
5534 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01005535 goto out_unlock;
5536
5537 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
5538 bool pending;
5539
Francois Romieuda78dbf2012-01-26 14:18:23 +01005540 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005541 if (pending)
5542 rtl_work[i].action(tp);
5543 }
5544
5545out_unlock:
5546 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01005547}
5548
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005549static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005551 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5552 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005553 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
5554 int work_done= 0;
5555 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556
Francois Romieuda78dbf2012-01-26 14:18:23 +01005557 status = rtl_get_events(tp);
5558 rtl_ack_events(tp, status & ~tp->event_slow);
5559
5560 if (status & RTL_EVENT_NAPI_RX)
5561 work_done = rtl_rx(dev, tp, (u32) budget);
5562
5563 if (status & RTL_EVENT_NAPI_TX)
5564 rtl_tx(dev, tp);
5565
5566 if (status & tp->event_slow) {
5567 enable_mask &= ~tp->event_slow;
5568
5569 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
5570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005572 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005573 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005574
Francois Romieuda78dbf2012-01-26 14:18:23 +01005575 rtl_irq_enable(tp, enable_mask);
5576 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 }
5578
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005579 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581
Francois Romieu523a6092008-09-10 22:28:56 +02005582static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5583{
5584 struct rtl8169_private *tp = netdev_priv(dev);
5585
5586 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5587 return;
5588
5589 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5590 RTL_W32(RxMissed, 0);
5591}
5592
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593static void rtl8169_down(struct net_device *dev)
5594{
5595 struct rtl8169_private *tp = netdev_priv(dev);
5596 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597
Francois Romieu4876cc12011-03-11 21:07:11 +01005598 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005600 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005601 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602
Hayes Wang92fc43b2011-07-06 15:58:03 +08005603 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005604 /*
5605 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01005606 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
5607 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005608 */
Francois Romieu523a6092008-09-10 22:28:56 +02005609 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005612 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 rtl8169_tx_clear(tp);
5615
5616 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005617
5618 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619}
5620
5621static int rtl8169_close(struct net_device *dev)
5622{
5623 struct rtl8169_private *tp = netdev_priv(dev);
5624 struct pci_dev *pdev = tp->pci_dev;
5625
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005626 pm_runtime_get_sync(&pdev->dev);
5627
Francois Romieucecb5fd2011-04-01 10:21:07 +02005628 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005629 rtl8169_update_counters(dev);
5630
Francois Romieuda78dbf2012-01-26 14:18:23 +01005631 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005632 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005633
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005635 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636
Francois Romieu92a7c4e2012-03-10 10:42:12 +01005637 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005639 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5640 tp->RxPhyAddr);
5641 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5642 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 tp->TxDescArray = NULL;
5644 tp->RxDescArray = NULL;
5645
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005646 pm_runtime_put_sync(&pdev->dev);
5647
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 return 0;
5649}
5650
Francois Romieudc1c00c2012-03-08 10:06:18 +01005651#ifdef CONFIG_NET_POLL_CONTROLLER
5652static void rtl8169_netpoll(struct net_device *dev)
5653{
5654 struct rtl8169_private *tp = netdev_priv(dev);
5655
5656 rtl8169_interrupt(tp->pci_dev->irq, dev);
5657}
5658#endif
5659
Francois Romieudf43ac72012-03-08 09:48:40 +01005660static int rtl_open(struct net_device *dev)
5661{
5662 struct rtl8169_private *tp = netdev_priv(dev);
5663 void __iomem *ioaddr = tp->mmio_addr;
5664 struct pci_dev *pdev = tp->pci_dev;
5665 int retval = -ENOMEM;
5666
5667 pm_runtime_get_sync(&pdev->dev);
5668
5669 /*
5670 * Rx and Tx desscriptors needs 256 bytes alignment.
5671 * dma_alloc_coherent provides more.
5672 */
5673 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
5674 &tp->TxPhyAddr, GFP_KERNEL);
5675 if (!tp->TxDescArray)
5676 goto err_pm_runtime_put;
5677
5678 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
5679 &tp->RxPhyAddr, GFP_KERNEL);
5680 if (!tp->RxDescArray)
5681 goto err_free_tx_0;
5682
5683 retval = rtl8169_init_ring(dev);
5684 if (retval < 0)
5685 goto err_free_rx_1;
5686
5687 INIT_WORK(&tp->wk.work, rtl_task);
5688
5689 smp_mb();
5690
5691 rtl_request_firmware(tp);
5692
Francois Romieu92a7c4e2012-03-10 10:42:12 +01005693 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01005694 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
5695 dev->name, dev);
5696 if (retval < 0)
5697 goto err_release_fw_2;
5698
5699 rtl_lock_work(tp);
5700
5701 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
5702
5703 napi_enable(&tp->napi);
5704
5705 rtl8169_init_phy(dev, tp);
5706
5707 __rtl8169_set_features(dev, dev->features);
5708
5709 rtl_pll_power_up(tp);
5710
5711 rtl_hw_start(dev);
5712
5713 netif_start_queue(dev);
5714
5715 rtl_unlock_work(tp);
5716
5717 tp->saved_wolopts = 0;
5718 pm_runtime_put_noidle(&pdev->dev);
5719
5720 rtl8169_check_link_status(dev, tp, ioaddr);
5721out:
5722 return retval;
5723
5724err_release_fw_2:
5725 rtl_release_firmware(tp);
5726 rtl8169_rx_clear(tp);
5727err_free_rx_1:
5728 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5729 tp->RxPhyAddr);
5730 tp->RxDescArray = NULL;
5731err_free_tx_0:
5732 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5733 tp->TxPhyAddr);
5734 tp->TxDescArray = NULL;
5735err_pm_runtime_put:
5736 pm_runtime_put_noidle(&pdev->dev);
5737 goto out;
5738}
5739
Junchang Wang8027aa22012-03-04 23:30:32 +01005740static struct rtnl_link_stats64 *
5741rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742{
5743 struct rtl8169_private *tp = netdev_priv(dev);
5744 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01005745 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746
Francois Romieuda78dbf2012-01-26 14:18:23 +01005747 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02005748 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02005749
Junchang Wang8027aa22012-03-04 23:30:32 +01005750 do {
5751 start = u64_stats_fetch_begin_bh(&tp->rx_stats.syncp);
5752 stats->rx_packets = tp->rx_stats.packets;
5753 stats->rx_bytes = tp->rx_stats.bytes;
5754 } while (u64_stats_fetch_retry_bh(&tp->rx_stats.syncp, start));
5755
5756
5757 do {
5758 start = u64_stats_fetch_begin_bh(&tp->tx_stats.syncp);
5759 stats->tx_packets = tp->tx_stats.packets;
5760 stats->tx_bytes = tp->tx_stats.bytes;
5761 } while (u64_stats_fetch_retry_bh(&tp->tx_stats.syncp, start));
5762
5763 stats->rx_dropped = dev->stats.rx_dropped;
5764 stats->tx_dropped = dev->stats.tx_dropped;
5765 stats->rx_length_errors = dev->stats.rx_length_errors;
5766 stats->rx_errors = dev->stats.rx_errors;
5767 stats->rx_crc_errors = dev->stats.rx_crc_errors;
5768 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
5769 stats->rx_missed_errors = dev->stats.rx_missed_errors;
5770
5771 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772}
5773
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005774static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005775{
françois romieu065c27c2011-01-03 15:08:12 +00005776 struct rtl8169_private *tp = netdev_priv(dev);
5777
Francois Romieu5d06a992006-02-23 00:47:58 +01005778 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005779 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005780
5781 netif_device_detach(dev);
5782 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005783
5784 rtl_lock_work(tp);
5785 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005786 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005787 rtl_unlock_work(tp);
5788
5789 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005790}
Francois Romieu5d06a992006-02-23 00:47:58 +01005791
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005792#ifdef CONFIG_PM
5793
5794static int rtl8169_suspend(struct device *device)
5795{
5796 struct pci_dev *pdev = to_pci_dev(device);
5797 struct net_device *dev = pci_get_drvdata(pdev);
5798
5799 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005800
Francois Romieu5d06a992006-02-23 00:47:58 +01005801 return 0;
5802}
5803
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005804static void __rtl8169_resume(struct net_device *dev)
5805{
françois romieu065c27c2011-01-03 15:08:12 +00005806 struct rtl8169_private *tp = netdev_priv(dev);
5807
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005808 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005809
5810 rtl_pll_power_up(tp);
5811
Artem Savkovcff4c162012-04-03 10:29:11 +00005812 rtl_lock_work(tp);
5813 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005814 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00005815 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005816
Francois Romieu98ddf982012-01-31 10:47:34 +01005817 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005818}
5819
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005820static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005821{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005822 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005823 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005824 struct rtl8169_private *tp = netdev_priv(dev);
5825
5826 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005827
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005828 if (netif_running(dev))
5829 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005830
Francois Romieu5d06a992006-02-23 00:47:58 +01005831 return 0;
5832}
5833
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005834static int rtl8169_runtime_suspend(struct device *device)
5835{
5836 struct pci_dev *pdev = to_pci_dev(device);
5837 struct net_device *dev = pci_get_drvdata(pdev);
5838 struct rtl8169_private *tp = netdev_priv(dev);
5839
5840 if (!tp->TxDescArray)
5841 return 0;
5842
Francois Romieuda78dbf2012-01-26 14:18:23 +01005843 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005844 tp->saved_wolopts = __rtl8169_get_wol(tp);
5845 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005846 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005847
5848 rtl8169_net_suspend(dev);
5849
5850 return 0;
5851}
5852
5853static int rtl8169_runtime_resume(struct device *device)
5854{
5855 struct pci_dev *pdev = to_pci_dev(device);
5856 struct net_device *dev = pci_get_drvdata(pdev);
5857 struct rtl8169_private *tp = netdev_priv(dev);
5858
5859 if (!tp->TxDescArray)
5860 return 0;
5861
Francois Romieuda78dbf2012-01-26 14:18:23 +01005862 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005863 __rtl8169_set_wol(tp, tp->saved_wolopts);
5864 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005865 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005866
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005867 rtl8169_init_phy(dev, tp);
5868
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005869 __rtl8169_resume(dev);
5870
5871 return 0;
5872}
5873
5874static int rtl8169_runtime_idle(struct device *device)
5875{
5876 struct pci_dev *pdev = to_pci_dev(device);
5877 struct net_device *dev = pci_get_drvdata(pdev);
5878 struct rtl8169_private *tp = netdev_priv(dev);
5879
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005880 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005881}
5882
Alexey Dobriyan47145212009-12-14 18:00:08 -08005883static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005884 .suspend = rtl8169_suspend,
5885 .resume = rtl8169_resume,
5886 .freeze = rtl8169_suspend,
5887 .thaw = rtl8169_resume,
5888 .poweroff = rtl8169_suspend,
5889 .restore = rtl8169_resume,
5890 .runtime_suspend = rtl8169_runtime_suspend,
5891 .runtime_resume = rtl8169_runtime_resume,
5892 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005893};
5894
5895#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5896
5897#else /* !CONFIG_PM */
5898
5899#define RTL8169_PM_OPS NULL
5900
5901#endif /* !CONFIG_PM */
5902
David S. Miller1805b2f2011-10-24 18:18:09 -04005903static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
5904{
5905 void __iomem *ioaddr = tp->mmio_addr;
5906
5907 /* WoL fails with 8168b when the receiver is disabled. */
5908 switch (tp->mac_version) {
5909 case RTL_GIGA_MAC_VER_11:
5910 case RTL_GIGA_MAC_VER_12:
5911 case RTL_GIGA_MAC_VER_17:
5912 pci_clear_master(tp->pci_dev);
5913
5914 RTL_W8(ChipCmd, CmdRxEnb);
5915 /* PCI commit */
5916 RTL_R8(ChipCmd);
5917 break;
5918 default:
5919 break;
5920 }
5921}
5922
Francois Romieu1765f952008-09-13 17:21:40 +02005923static void rtl_shutdown(struct pci_dev *pdev)
5924{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005925 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005926 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00005927 struct device *d = &pdev->dev;
5928
5929 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02005930
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005931 rtl8169_net_suspend(dev);
5932
Francois Romieucecb5fd2011-04-01 10:21:07 +02005933 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08005934 rtl_rar_set(tp, dev->perm_addr);
5935
Hayes Wang92fc43b2011-07-06 15:58:03 +08005936 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00005937
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005938 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04005939 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
5940 rtl_wol_suspend_quirk(tp);
5941 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00005942 }
5943
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005944 pci_wake_from_d3(pdev, true);
5945 pci_set_power_state(pdev, PCI_D3hot);
5946 }
françois romieu2a15cd22012-03-06 01:14:12 +00005947
5948 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005949}
Francois Romieu5d06a992006-02-23 00:47:58 +01005950
Francois Romieue27566e2012-03-08 09:54:01 +01005951static void __devexit rtl_remove_one(struct pci_dev *pdev)
5952{
5953 struct net_device *dev = pci_get_drvdata(pdev);
5954 struct rtl8169_private *tp = netdev_priv(dev);
5955
5956 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
5957 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5958 tp->mac_version == RTL_GIGA_MAC_VER_31) {
5959 rtl8168_driver_stop(tp);
5960 }
5961
5962 cancel_work_sync(&tp->wk.work);
5963
Devendra Naga0440cf62012-05-31 01:51:20 +00005964 netif_napi_del(&tp->napi);
5965
Francois Romieue27566e2012-03-08 09:54:01 +01005966 unregister_netdev(dev);
5967
5968 rtl_release_firmware(tp);
5969
5970 if (pci_dev_run_wake(pdev))
5971 pm_runtime_get_noresume(&pdev->dev);
5972
5973 /* restore original MAC address */
5974 rtl_rar_set(tp, dev->perm_addr);
5975
5976 rtl_disable_msi(pdev, tp);
5977 rtl8169_release_board(pdev, dev, tp->mmio_addr);
5978 pci_set_drvdata(pdev, NULL);
5979}
5980
Francois Romieufa9c3852012-03-08 10:01:50 +01005981static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01005982 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01005983 .ndo_stop = rtl8169_close,
5984 .ndo_get_stats64 = rtl8169_get_stats64,
5985 .ndo_start_xmit = rtl8169_start_xmit,
5986 .ndo_tx_timeout = rtl8169_tx_timeout,
5987 .ndo_validate_addr = eth_validate_addr,
5988 .ndo_change_mtu = rtl8169_change_mtu,
5989 .ndo_fix_features = rtl8169_fix_features,
5990 .ndo_set_features = rtl8169_set_features,
5991 .ndo_set_mac_address = rtl_set_mac_address,
5992 .ndo_do_ioctl = rtl8169_ioctl,
5993 .ndo_set_rx_mode = rtl_set_rx_mode,
5994#ifdef CONFIG_NET_POLL_CONTROLLER
5995 .ndo_poll_controller = rtl8169_netpoll,
5996#endif
5997
5998};
5999
Francois Romieu31fa8b12012-03-08 10:09:40 +01006000static const struct rtl_cfg_info {
6001 void (*hw_start)(struct net_device *);
6002 unsigned int region;
6003 unsigned int align;
6004 u16 event_slow;
6005 unsigned features;
6006 u8 default_ver;
6007} rtl_cfg_infos [] = {
6008 [RTL_CFG_0] = {
6009 .hw_start = rtl_hw_start_8169,
6010 .region = 1,
6011 .align = 0,
6012 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
6013 .features = RTL_FEATURE_GMII,
6014 .default_ver = RTL_GIGA_MAC_VER_01,
6015 },
6016 [RTL_CFG_1] = {
6017 .hw_start = rtl_hw_start_8168,
6018 .region = 2,
6019 .align = 8,
6020 .event_slow = SYSErr | LinkChg | RxOverflow,
6021 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
6022 .default_ver = RTL_GIGA_MAC_VER_11,
6023 },
6024 [RTL_CFG_2] = {
6025 .hw_start = rtl_hw_start_8101,
6026 .region = 2,
6027 .align = 8,
6028 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
6029 PCSTimeout,
6030 .features = RTL_FEATURE_MSI,
6031 .default_ver = RTL_GIGA_MAC_VER_13,
6032 }
6033};
6034
6035/* Cfg9346_Unlock assumed. */
6036static unsigned rtl_try_msi(struct rtl8169_private *tp,
6037 const struct rtl_cfg_info *cfg)
6038{
6039 void __iomem *ioaddr = tp->mmio_addr;
6040 unsigned msi = 0;
6041 u8 cfg2;
6042
6043 cfg2 = RTL_R8(Config2) & ~MSIEnable;
6044 if (cfg->features & RTL_FEATURE_MSI) {
6045 if (pci_enable_msi(tp->pci_dev)) {
6046 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
6047 } else {
6048 cfg2 |= MSIEnable;
6049 msi = RTL_FEATURE_MSI;
6050 }
6051 }
6052 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6053 RTL_W8(Config2, cfg2);
6054 return msi;
6055}
6056
Francois Romieu3b6cf252012-03-08 09:59:04 +01006057static int __devinit
6058rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6059{
6060 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
6061 const unsigned int region = cfg->region;
6062 struct rtl8169_private *tp;
6063 struct mii_if_info *mii;
6064 struct net_device *dev;
6065 void __iomem *ioaddr;
6066 int chipset, i;
6067 int rc;
6068
6069 if (netif_msg_drv(&debug)) {
6070 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
6071 MODULENAME, RTL8169_VERSION);
6072 }
6073
6074 dev = alloc_etherdev(sizeof (*tp));
6075 if (!dev) {
6076 rc = -ENOMEM;
6077 goto out;
6078 }
6079
6080 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01006081 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006082 tp = netdev_priv(dev);
6083 tp->dev = dev;
6084 tp->pci_dev = pdev;
6085 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
6086
6087 mii = &tp->mii;
6088 mii->dev = dev;
6089 mii->mdio_read = rtl_mdio_read;
6090 mii->mdio_write = rtl_mdio_write;
6091 mii->phy_id_mask = 0x1f;
6092 mii->reg_num_mask = 0x1f;
6093 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
6094
6095 /* disable ASPM completely as that cause random device stop working
6096 * problems as well as full system hangs for some PCIe devices users */
6097 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6098 PCIE_LINK_STATE_CLKPM);
6099
6100 /* enable device (incl. PCI PM wakeup and hotplug setup) */
6101 rc = pci_enable_device(pdev);
6102 if (rc < 0) {
6103 netif_err(tp, probe, dev, "enable failure\n");
6104 goto err_out_free_dev_1;
6105 }
6106
6107 if (pci_set_mwi(pdev) < 0)
6108 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
6109
6110 /* make sure PCI base addr 1 is MMIO */
6111 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
6112 netif_err(tp, probe, dev,
6113 "region #%d not an MMIO resource, aborting\n",
6114 region);
6115 rc = -ENODEV;
6116 goto err_out_mwi_2;
6117 }
6118
6119 /* check for weird/broken PCI region reporting */
6120 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
6121 netif_err(tp, probe, dev,
6122 "Invalid PCI region size(s), aborting\n");
6123 rc = -ENODEV;
6124 goto err_out_mwi_2;
6125 }
6126
6127 rc = pci_request_regions(pdev, MODULENAME);
6128 if (rc < 0) {
6129 netif_err(tp, probe, dev, "could not request regions\n");
6130 goto err_out_mwi_2;
6131 }
6132
6133 tp->cp_cmd = RxChkSum;
6134
6135 if ((sizeof(dma_addr_t) > 4) &&
6136 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
6137 tp->cp_cmd |= PCIDAC;
6138 dev->features |= NETIF_F_HIGHDMA;
6139 } else {
6140 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6141 if (rc < 0) {
6142 netif_err(tp, probe, dev, "DMA configuration failed\n");
6143 goto err_out_free_res_3;
6144 }
6145 }
6146
6147 /* ioremap MMIO region */
6148 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
6149 if (!ioaddr) {
6150 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
6151 rc = -EIO;
6152 goto err_out_free_res_3;
6153 }
6154 tp->mmio_addr = ioaddr;
6155
6156 if (!pci_is_pcie(pdev))
6157 netif_info(tp, probe, dev, "not PCI Express\n");
6158
6159 /* Identify chip attached to board */
6160 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
6161
6162 rtl_init_rxcfg(tp);
6163
6164 rtl_irq_disable(tp);
6165
6166 rtl_hw_reset(tp);
6167
6168 rtl_ack_events(tp, 0xffff);
6169
6170 pci_set_master(pdev);
6171
6172 /*
6173 * Pretend we are using VLANs; This bypasses a nasty bug where
6174 * Interrupts stop flowing on high load on 8110SCd controllers.
6175 */
6176 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6177 tp->cp_cmd |= RxVlan;
6178
6179 rtl_init_mdio_ops(tp);
6180 rtl_init_pll_power_ops(tp);
6181 rtl_init_jumbo_ops(tp);
6182
6183 rtl8169_print_mac_version(tp);
6184
6185 chipset = tp->mac_version;
6186 tp->txd_version = rtl_chip_infos[chipset].txd_version;
6187
6188 RTL_W8(Cfg9346, Cfg9346_Unlock);
6189 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
6190 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
6191 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
6192 tp->features |= RTL_FEATURE_WOL;
6193 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
6194 tp->features |= RTL_FEATURE_WOL;
6195 tp->features |= rtl_try_msi(tp, cfg);
6196 RTL_W8(Cfg9346, Cfg9346_Lock);
6197
6198 if (rtl_tbi_enabled(tp)) {
6199 tp->set_speed = rtl8169_set_speed_tbi;
6200 tp->get_settings = rtl8169_gset_tbi;
6201 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
6202 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
6203 tp->link_ok = rtl8169_tbi_link_ok;
6204 tp->do_ioctl = rtl_tbi_ioctl;
6205 } else {
6206 tp->set_speed = rtl8169_set_speed_xmii;
6207 tp->get_settings = rtl8169_gset_xmii;
6208 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
6209 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
6210 tp->link_ok = rtl8169_xmii_link_ok;
6211 tp->do_ioctl = rtl_xmii_ioctl;
6212 }
6213
6214 mutex_init(&tp->wk.mutex);
6215
6216 /* Get MAC address */
6217 for (i = 0; i < ETH_ALEN; i++)
6218 dev->dev_addr[i] = RTL_R8(MAC0 + i);
6219 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
6220
6221 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
6222 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006223
6224 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
6225
6226 /* don't enable SG, IP_CSUM and TSO by default - it might not work
6227 * properly for all devices */
6228 dev->features |= NETIF_F_RXCSUM |
6229 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6230
6231 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6232 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6233 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6234 NETIF_F_HIGHDMA;
6235
6236 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6237 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
6238 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
6239
6240 dev->hw_features |= NETIF_F_RXALL;
6241 dev->hw_features |= NETIF_F_RXFCS;
6242
6243 tp->hw_start = cfg->hw_start;
6244 tp->event_slow = cfg->event_slow;
6245
6246 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
6247 ~(RxBOVF | RxFOVF) : ~0;
6248
6249 init_timer(&tp->timer);
6250 tp->timer.data = (unsigned long) dev;
6251 tp->timer.function = rtl8169_phy_timer;
6252
6253 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
6254
6255 rc = register_netdev(dev);
6256 if (rc < 0)
6257 goto err_out_msi_4;
6258
6259 pci_set_drvdata(pdev, dev);
6260
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006261 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
6262 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
6263 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006264 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
6265 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
6266 "tx checksumming: %s]\n",
6267 rtl_chip_infos[chipset].jumbo_max,
6268 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
6269 }
6270
6271 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6272 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6273 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6274 rtl8168_driver_start(tp);
6275 }
6276
6277 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
6278
6279 if (pci_dev_run_wake(pdev))
6280 pm_runtime_put_noidle(&pdev->dev);
6281
6282 netif_carrier_off(dev);
6283
6284out:
6285 return rc;
6286
6287err_out_msi_4:
Devendra Naga0440cf62012-05-31 01:51:20 +00006288 netif_napi_del(&tp->napi);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006289 rtl_disable_msi(pdev, tp);
6290 iounmap(ioaddr);
6291err_out_free_res_3:
6292 pci_release_regions(pdev);
6293err_out_mwi_2:
6294 pci_clear_mwi(pdev);
6295 pci_disable_device(pdev);
6296err_out_free_dev_1:
6297 free_netdev(dev);
6298 goto out;
6299}
6300
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301static struct pci_driver rtl8169_pci_driver = {
6302 .name = MODULENAME,
6303 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01006304 .probe = rtl_init_one,
Francois Romieue27566e2012-03-08 09:54:01 +01006305 .remove = __devexit_p(rtl_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02006306 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006307 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308};
6309
Francois Romieu07d3f512007-02-21 22:40:46 +01006310static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311{
Jeff Garzik29917622006-08-19 17:48:59 -04006312 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313}
6314
Francois Romieu07d3f512007-02-21 22:40:46 +01006315static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316{
6317 pci_unregister_driver(&rtl8169_pci_driver);
6318}
6319
6320module_init(rtl8169_init_module);
6321module_exit(rtl8169_cleanup_module);