blob: 978af217ef0b03d9638d3594cd61d2607fb2eed2 [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
Michal Schmidt56cb8f72012-09-09 13:55:26 +000076#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
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,
Francois Romieu160f00f2012-10-06 11:19:53 +0200322#define PME_SIGNAL (1 << 5) /* 8168c and later */
323
Francois Romieu07d3f512007-02-21 22:40:46 +0100324 Config3 = 0x54,
325 Config4 = 0x55,
326 Config5 = 0x56,
327 MultiIntr = 0x5c,
328 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100329 PHYstatus = 0x6c,
330 RxMaxSize = 0xda,
331 CPlusCmd = 0xe0,
332 IntrMitigate = 0xe2,
333 RxDescAddrLow = 0xe4,
334 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000335 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
336
337#define NoEarlyTx 0x3f /* Max value : no early transmit. */
338
339 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
340
341#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800342#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000343
Francois Romieu07d3f512007-02-21 22:40:46 +0100344 FuncEvent = 0xf0,
345 FuncEventMask = 0xf4,
346 FuncPresetState = 0xf8,
347 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348};
349
Francois Romieuf162a5d2008-06-01 22:37:49 +0200350enum rtl8110_registers {
351 TBICSR = 0x64,
352 TBI_ANAR = 0x68,
353 TBI_LPAR = 0x6a,
354};
355
356enum rtl8168_8101_registers {
357 CSIDR = 0x64,
358 CSIAR = 0x68,
359#define CSIAR_FLAG 0x80000000
360#define CSIAR_WRITE_CMD 0x80000000
361#define CSIAR_BYTE_ENABLE 0x0f
362#define CSIAR_BYTE_ENABLE_SHIFT 12
363#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000364 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200365 EPHYAR = 0x80,
366#define EPHYAR_FLAG 0x80000000
367#define EPHYAR_WRITE_CMD 0x80000000
368#define EPHYAR_REG_MASK 0x1f
369#define EPHYAR_REG_SHIFT 16
370#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800371 DLLPR = 0xd0,
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800372#define PFM_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200373 DBG_REG = 0xd1,
374#define FIX_NAK_1 (1 << 4)
375#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800376 TWSI = 0xd2,
377 MCU = 0xd3,
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800378#define NOW_IS_OOB (1 << 7)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800379#define EN_NDP (1 << 3)
380#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000381 EFUSEAR = 0xdc,
382#define EFUSEAR_FLAG 0x80000000
383#define EFUSEAR_WRITE_CMD 0x80000000
384#define EFUSEAR_READ_CMD 0x00000000
385#define EFUSEAR_REG_MASK 0x03ff
386#define EFUSEAR_REG_SHIFT 8
387#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200388};
389
françois romieuc0e45c12011-01-03 15:08:04 +0000390enum rtl8168_registers {
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800391 LED_FREQ = 0x1a,
392 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000393 ERIDR = 0x70,
394 ERIAR = 0x74,
395#define ERIAR_FLAG 0x80000000
396#define ERIAR_WRITE_CMD 0x80000000
397#define ERIAR_READ_CMD 0x00000000
398#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000399#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800400#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
401#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
402#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
403#define ERIAR_MASK_SHIFT 12
404#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
405#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
406#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000407 EPHY_RXER_NUM = 0x7c,
408 OCPDR = 0xb0, /* OCP GPHY access */
409#define OCPDR_WRITE_CMD 0x80000000
410#define OCPDR_READ_CMD 0x00000000
411#define OCPDR_REG_MASK 0x7f
412#define OCPDR_GPHY_REG_SHIFT 16
413#define OCPDR_DATA_MASK 0xffff
414 OCPAR = 0xb4,
415#define OCPAR_FLAG 0x80000000
416#define OCPAR_GPHY_WRITE_CMD 0x8000f060
417#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000418 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
419 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200420#define TXPLA_RST (1 << 29)
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800421#define PWM_EN (1 << 22)
françois romieuc0e45c12011-01-03 15:08:04 +0000422};
423
Francois Romieu07d3f512007-02-21 22:40:46 +0100424enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100426 SYSErr = 0x8000,
427 PCSTimeout = 0x4000,
428 SWInt = 0x0100,
429 TxDescUnavail = 0x0080,
430 RxFIFOOver = 0x0040,
431 LinkChg = 0x0020,
432 RxOverflow = 0x0010,
433 TxErr = 0x0008,
434 TxOK = 0x0004,
435 RxErr = 0x0002,
436 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400439 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200440 RxFOVF = (1 << 23),
441 RxRWT = (1 << 22),
442 RxRES = (1 << 21),
443 RxRUNT = (1 << 20),
444 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 /* ChipCmdBits */
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800447 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100448 CmdReset = 0x10,
449 CmdRxEnb = 0x08,
450 CmdTxEnb = 0x04,
451 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Francois Romieu275391a2007-02-23 23:50:28 +0100453 /* TXPoll register p.5 */
454 HPQ = 0x80, /* Poll cmd on the high prio queue */
455 NPQ = 0x40, /* Poll cmd on the low prio queue */
456 FSWInt = 0x01, /* Forced software interrupt */
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100459 Cfg9346_Lock = 0x00,
460 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100463 AcceptErr = 0x20,
464 AcceptRunt = 0x10,
465 AcceptBroadcast = 0x08,
466 AcceptMulticast = 0x04,
467 AcceptMyPhys = 0x02,
468 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200469#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /* TxConfigBits */
472 TxInterFrameGapShift = 24,
473 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
474
Francois Romieu5d06a992006-02-23 00:47:58 +0100475 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200476 LEDS1 = (1 << 7),
477 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200478 Speed_down = (1 << 4),
479 MEMMAP = (1 << 3),
480 IOMAP = (1 << 2),
481 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100482 PMEnable = (1 << 0), /* Power Management Enable */
483
Francois Romieu6dccd162007-02-13 23:38:05 +0100484 /* Config2 register p. 25 */
françois romieu2ca6cf02011-12-15 08:37:43 +0000485 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100486 PCI_Clock_66MHz = 0x01,
487 PCI_Clock_33MHz = 0x00,
488
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100489 /* Config3 register p.25 */
490 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
491 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200492 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200493 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100494
Francois Romieud58d46b2011-05-03 16:38:29 +0200495 /* Config4 register */
496 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
497
Francois Romieu5d06a992006-02-23 00:47:58 +0100498 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100499 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
500 MWF = (1 << 5), /* Accept Multicast wakeup frame */
501 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200502 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100503 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100504 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /* TBICSR p.28 */
507 TBIReset = 0x80000000,
508 TBILoopback = 0x40000000,
509 TBINwEnable = 0x20000000,
510 TBINwRestart = 0x10000000,
511 TBILinkOk = 0x02000000,
512 TBINwComplete = 0x01000000,
513
514 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200515 EnableBist = (1 << 15), // 8168 8101
516 Mac_dbgo_oe = (1 << 14), // 8168 8101
517 Normal_mode = (1 << 13), // unused
518 Force_half_dup = (1 << 12), // 8168 8101
519 Force_rxflow_en = (1 << 11), // 8168 8101
520 Force_txflow_en = (1 << 10), // 8168 8101
521 Cxpl_dbg_sel = (1 << 9), // 8168 8101
522 ASF = (1 << 8), // 8168 8101
523 PktCntrDisable = (1 << 7), // 8168 8101
524 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 RxVlan = (1 << 6),
526 RxChkSum = (1 << 5),
527 PCIDAC = (1 << 4),
528 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100529 INTT_0 = 0x0000, // 8168
530 INTT_1 = 0x0001, // 8168
531 INTT_2 = 0x0002, // 8168
532 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100535 TBI_Enable = 0x80,
536 TxFlowCtrl = 0x40,
537 RxFlowCtrl = 0x20,
538 _1000bpsF = 0x10,
539 _100bps = 0x08,
540 _10bps = 0x04,
541 LinkStatus = 0x02,
542 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100545 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200546
547 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100548 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549};
550
Francois Romieu2b7b4312011-04-18 22:53:24 -0700551enum rtl_desc_bit {
552 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
554 RingEnd = (1 << 30), /* End of descriptor ring */
555 FirstFrag = (1 << 29), /* First segment of a packet */
556 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700557};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Francois Romieu2b7b4312011-04-18 22:53:24 -0700559/* Generic case. */
560enum rtl_tx_desc_bit {
561 /* First doubleword. */
562 TD_LSO = (1 << 27), /* Large Send Offload */
563#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Francois Romieu2b7b4312011-04-18 22:53:24 -0700565 /* Second doubleword. */
566 TxVlanTag = (1 << 17), /* Add VLAN tag */
567};
568
569/* 8169, 8168b and 810x except 8102e. */
570enum rtl_tx_desc_bit_0 {
571 /* First doubleword. */
572#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
573 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
574 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
575 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
576};
577
578/* 8102e, 8168c and beyond. */
579enum rtl_tx_desc_bit_1 {
580 /* Second doubleword. */
581#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
582 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
583 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
584 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
585};
586
587static const struct rtl_tx_desc_info {
588 struct {
589 u32 udp;
590 u32 tcp;
591 } checksum;
592 u16 mss_shift;
593 u16 opts_offset;
594} tx_desc_info [] = {
595 [RTL_TD_0] = {
596 .checksum = {
597 .udp = TD0_IP_CS | TD0_UDP_CS,
598 .tcp = TD0_IP_CS | TD0_TCP_CS
599 },
600 .mss_shift = TD0_MSS_SHIFT,
601 .opts_offset = 0
602 },
603 [RTL_TD_1] = {
604 .checksum = {
605 .udp = TD1_IP_CS | TD1_UDP_CS,
606 .tcp = TD1_IP_CS | TD1_TCP_CS
607 },
608 .mss_shift = TD1_MSS_SHIFT,
609 .opts_offset = 1
610 }
611};
612
613enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /* Rx private */
615 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
616 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
617
618#define RxProtoUDP (PID1)
619#define RxProtoTCP (PID0)
620#define RxProtoIP (PID1 | PID0)
621#define RxProtoMask RxProtoIP
622
623 IPFail = (1 << 16), /* IP checksum failed */
624 UDPFail = (1 << 15), /* UDP/IP checksum failed */
625 TCPFail = (1 << 14), /* TCP/IP checksum failed */
626 RxVlanTag = (1 << 16), /* VLAN tag available */
627};
628
629#define RsvdMask 0x3fffc000
630
631struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200632 __le32 opts1;
633 __le32 opts2;
634 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635};
636
637struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200638 __le32 opts1;
639 __le32 opts2;
640 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641};
642
643struct ring_info {
644 struct sk_buff *skb;
645 u32 len;
646 u8 __pad[sizeof(void *) - sizeof(u32)];
647};
648
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200649enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200650 RTL_FEATURE_WOL = (1 << 0),
651 RTL_FEATURE_MSI = (1 << 1),
652 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200653};
654
Ivan Vecera355423d2009-02-06 21:49:57 -0800655struct rtl8169_counters {
656 __le64 tx_packets;
657 __le64 rx_packets;
658 __le64 tx_errors;
659 __le32 rx_errors;
660 __le16 rx_missed;
661 __le16 align_errors;
662 __le32 tx_one_collision;
663 __le32 tx_multi_collision;
664 __le64 rx_unicast;
665 __le64 rx_broadcast;
666 __le32 rx_multicast;
667 __le16 tx_aborted;
668 __le16 tx_underun;
669};
670
Francois Romieuda78dbf2012-01-26 14:18:23 +0100671enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100672 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100673 RTL_FLAG_TASK_SLOW_PENDING,
674 RTL_FLAG_TASK_RESET_PENDING,
675 RTL_FLAG_TASK_PHY_PENDING,
676 RTL_FLAG_MAX
677};
678
Junchang Wang8027aa22012-03-04 23:30:32 +0100679struct rtl8169_stats {
680 u64 packets;
681 u64 bytes;
682 struct u64_stats_sync syncp;
683};
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685struct rtl8169_private {
686 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200687 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000688 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700689 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200690 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700691 u16 txd_version;
692 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
694 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
695 u32 dirty_rx;
696 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100697 struct rtl8169_stats rx_stats;
698 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
700 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
701 dma_addr_t TxPhyAddr;
702 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000703 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 struct timer_list timer;
706 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100707
708 u16 event_slow;
françois romieuc0e45c12011-01-03 15:08:04 +0000709
710 struct mdio_ops {
711 void (*write)(void __iomem *, int, int);
712 int (*read)(void __iomem *, int);
713 } mdio_ops;
714
françois romieu065c27c2011-01-03 15:08:12 +0000715 struct pll_power_ops {
716 void (*down)(struct rtl8169_private *);
717 void (*up)(struct rtl8169_private *);
718 } pll_power_ops;
719
Francois Romieud58d46b2011-05-03 16:38:29 +0200720 struct jumbo_ops {
721 void (*enable)(struct rtl8169_private *);
722 void (*disable)(struct rtl8169_private *);
723 } jumbo_ops;
724
Oliver Neukum54405cd2011-01-06 21:55:13 +0100725 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200726 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000727 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100728 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000729 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800731 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100732
733 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100734 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
735 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100736 struct work_struct work;
737 } wk;
738
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200739 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200740
741 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800742 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000743 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400744 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000745
Francois Romieub6ffd972011-06-17 17:00:05 +0200746 struct rtl_fw {
747 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200748
749#define RTL_VER_SIZE 32
750
751 char version[RTL_VER_SIZE];
752
753 struct rtl_fw_phy_action {
754 __le32 *code;
755 size_t size;
756 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200757 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300758#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759};
760
Ralf Baechle979b6c12005-06-13 14:30:40 -0700761MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700764MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200765module_param_named(debug, debug.msg_enable, int, 0);
766MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767MODULE_LICENSE("GPL");
768MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000769MODULE_FIRMWARE(FIRMWARE_8168D_1);
770MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000771MODULE_FIRMWARE(FIRMWARE_8168E_1);
772MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400773MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800774MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800775MODULE_FIRMWARE(FIRMWARE_8168F_1);
776MODULE_FIRMWARE(FIRMWARE_8168F_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Francois Romieuda78dbf2012-01-26 14:18:23 +0100778static void rtl_lock_work(struct rtl8169_private *tp)
779{
780 mutex_lock(&tp->wk.mutex);
781}
782
783static void rtl_unlock_work(struct rtl8169_private *tp)
784{
785 mutex_unlock(&tp->wk.mutex);
786}
787
Francois Romieud58d46b2011-05-03 16:38:29 +0200788static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
789{
790 int cap = pci_pcie_cap(pdev);
791
792 if (cap) {
793 u16 ctl;
794
795 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
796 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
797 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
798 }
799}
800
françois romieub646d902011-01-03 15:08:21 +0000801static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
802{
803 void __iomem *ioaddr = tp->mmio_addr;
804 int i;
805
806 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
807 for (i = 0; i < 20; i++) {
808 udelay(100);
809 if (RTL_R32(OCPAR) & OCPAR_FLAG)
810 break;
811 }
812 return RTL_R32(OCPDR);
813}
814
815static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
816{
817 void __iomem *ioaddr = tp->mmio_addr;
818 int i;
819
820 RTL_W32(OCPDR, data);
821 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
822 for (i = 0; i < 20; i++) {
823 udelay(100);
824 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
825 break;
826 }
827}
828
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800829static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000830{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800831 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000832 int i;
833
834 RTL_W8(ERIDR, cmd);
835 RTL_W32(ERIAR, 0x800010e8);
836 msleep(2);
837 for (i = 0; i < 5; i++) {
838 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200839 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000840 break;
841 }
842
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800843 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000844}
845
846#define OOB_CMD_RESET 0x00
847#define OOB_CMD_DRIVER_START 0x05
848#define OOB_CMD_DRIVER_STOP 0x06
849
Francois Romieucecb5fd2011-04-01 10:21:07 +0200850static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
851{
852 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
853}
854
françois romieub646d902011-01-03 15:08:21 +0000855static void rtl8168_driver_start(struct rtl8169_private *tp)
856{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200857 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000858 int i;
859
860 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
861
Francois Romieucecb5fd2011-04-01 10:21:07 +0200862 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000863
françois romieub646d902011-01-03 15:08:21 +0000864 for (i = 0; i < 10; i++) {
865 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000866 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000867 break;
868 }
869}
870
871static void rtl8168_driver_stop(struct rtl8169_private *tp)
872{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200873 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000874 int i;
875
876 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
877
Francois Romieucecb5fd2011-04-01 10:21:07 +0200878 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000879
françois romieub646d902011-01-03 15:08:21 +0000880 for (i = 0; i < 10; i++) {
881 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000882 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000883 break;
884 }
885}
886
hayeswang4804b3b2011-03-21 01:50:29 +0000887static int r8168dp_check_dash(struct rtl8169_private *tp)
888{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200889 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000890
Francois Romieucecb5fd2011-04-01 10:21:07 +0200891 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000892}
françois romieub646d902011-01-03 15:08:21 +0000893
françois romieu4da19632011-01-03 15:07:55 +0000894static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896 int i;
897
Francois Romieua6baf3a2007-11-08 23:23:21 +0100898 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Francois Romieu23714082006-01-29 00:49:09 +0100900 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100901 /*
902 * Check if the RTL8169 has completed writing to the specified
903 * MII register.
904 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200905 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 break;
Francois Romieu23714082006-01-29 00:49:09 +0100907 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700909 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700910 * According to hardware specs a 20us delay is required after write
911 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700912 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700913 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
françois romieu4da19632011-01-03 15:07:55 +0000916static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 int i, value = -1;
919
Francois Romieua6baf3a2007-11-08 23:23:21 +0100920 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Francois Romieu23714082006-01-29 00:49:09 +0100922 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100923 /*
924 * Check if the RTL8169 has completed retrieving data from
925 * the specified MII register.
926 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100928 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 break;
930 }
Francois Romieu23714082006-01-29 00:49:09 +0100931 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700933 /*
934 * According to hardware specs a 20us delay is required after read
935 * complete indication, but before sending next command.
936 */
937 udelay(20);
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return value;
940}
941
françois romieuc0e45c12011-01-03 15:08:04 +0000942static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
943{
944 int i;
945
946 RTL_W32(OCPDR, data |
947 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
948 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
949 RTL_W32(EPHY_RXER_NUM, 0);
950
951 for (i = 0; i < 100; i++) {
952 mdelay(1);
953 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
954 break;
955 }
956}
957
958static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
959{
960 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
961 (value & OCPDR_DATA_MASK));
962}
963
964static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
965{
966 int i;
967
968 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
969
970 mdelay(1);
971 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
972 RTL_W32(EPHY_RXER_NUM, 0);
973
974 for (i = 0; i < 100; i++) {
975 mdelay(1);
976 if (RTL_R32(OCPAR) & OCPAR_FLAG)
977 break;
978 }
979
980 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
981}
982
françois romieue6de30d2011-01-03 15:08:37 +0000983#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
984
985static void r8168dp_2_mdio_start(void __iomem *ioaddr)
986{
987 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
988}
989
990static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
991{
992 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
993}
994
995static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
996{
997 r8168dp_2_mdio_start(ioaddr);
998
999 r8169_mdio_write(ioaddr, reg_addr, value);
1000
1001 r8168dp_2_mdio_stop(ioaddr);
1002}
1003
1004static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
1005{
1006 int value;
1007
1008 r8168dp_2_mdio_start(ioaddr);
1009
1010 value = r8169_mdio_read(ioaddr, reg_addr);
1011
1012 r8168dp_2_mdio_stop(ioaddr);
1013
1014 return value;
1015}
1016
françois romieu4da19632011-01-03 15:07:55 +00001017static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001018{
françois romieuc0e45c12011-01-03 15:08:04 +00001019 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001020}
1021
françois romieu4da19632011-01-03 15:07:55 +00001022static int rtl_readphy(struct rtl8169_private *tp, int location)
1023{
françois romieuc0e45c12011-01-03 15:08:04 +00001024 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +00001025}
1026
1027static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1028{
1029 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1030}
1031
1032static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001033{
1034 int val;
1035
françois romieu4da19632011-01-03 15:07:55 +00001036 val = rtl_readphy(tp, reg_addr);
1037 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001038}
1039
Francois Romieuccdffb92008-07-26 14:26:06 +02001040static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1041 int val)
1042{
1043 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001044
françois romieu4da19632011-01-03 15:07:55 +00001045 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001046}
1047
1048static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1049{
1050 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001051
françois romieu4da19632011-01-03 15:07:55 +00001052 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001053}
1054
Francois Romieudacf8152008-08-02 20:44:13 +02001055static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
1056{
1057 unsigned int i;
1058
1059 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1060 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1061
1062 for (i = 0; i < 100; i++) {
1063 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1064 break;
1065 udelay(10);
1066 }
1067}
1068
1069static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1070{
1071 u16 value = 0xffff;
1072 unsigned int i;
1073
1074 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1075
1076 for (i = 0; i < 100; i++) {
1077 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1078 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1079 break;
1080 }
1081 udelay(10);
1082 }
1083
1084 return value;
1085}
1086
1087static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1088{
1089 unsigned int i;
1090
1091 RTL_W32(CSIDR, value);
1092 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1093 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1094
1095 for (i = 0; i < 100; i++) {
1096 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1097 break;
1098 udelay(10);
1099 }
1100}
1101
1102static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1103{
1104 u32 value = ~0x00;
1105 unsigned int i;
1106
1107 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1108 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1109
1110 for (i = 0; i < 100; i++) {
1111 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1112 value = RTL_R32(CSIDR);
1113 break;
1114 }
1115 udelay(10);
1116 }
1117
1118 return value;
1119}
1120
Hayes Wang133ac402011-07-06 15:58:05 +08001121static
1122void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1123{
1124 unsigned int i;
1125
1126 BUG_ON((addr & 3) || (mask == 0));
1127 RTL_W32(ERIDR, val);
1128 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1129
1130 for (i = 0; i < 100; i++) {
1131 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1132 break;
1133 udelay(100);
1134 }
1135}
1136
1137static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1138{
1139 u32 value = ~0x00;
1140 unsigned int i;
1141
1142 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1143
1144 for (i = 0; i < 100; i++) {
1145 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1146 value = RTL_R32(ERIDR);
1147 break;
1148 }
1149 udelay(100);
1150 }
1151
1152 return value;
1153}
1154
1155static void
1156rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1157{
1158 u32 val;
1159
1160 val = rtl_eri_read(ioaddr, addr, type);
1161 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1162}
1163
françois romieuc28aa382011-08-02 03:53:43 +00001164struct exgmac_reg {
1165 u16 addr;
1166 u16 mask;
1167 u32 val;
1168};
1169
1170static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1171 const struct exgmac_reg *r, int len)
1172{
1173 while (len-- > 0) {
1174 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1175 r++;
1176 }
1177}
1178
françois romieudaf9df62009-10-07 12:44:20 +00001179static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1180{
1181 u8 value = 0xff;
1182 unsigned int i;
1183
1184 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1185
1186 for (i = 0; i < 300; i++) {
1187 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1188 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1189 break;
1190 }
1191 udelay(100);
1192 }
1193
1194 return value;
1195}
1196
Francois Romieu9085cdf2012-01-26 12:59:08 +01001197static u16 rtl_get_events(struct rtl8169_private *tp)
1198{
1199 void __iomem *ioaddr = tp->mmio_addr;
1200
1201 return RTL_R16(IntrStatus);
1202}
1203
1204static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1205{
1206 void __iomem *ioaddr = tp->mmio_addr;
1207
1208 RTL_W16(IntrStatus, bits);
1209 mmiowb();
1210}
1211
1212static void rtl_irq_disable(struct rtl8169_private *tp)
1213{
1214 void __iomem *ioaddr = tp->mmio_addr;
1215
1216 RTL_W16(IntrMask, 0);
1217 mmiowb();
1218}
1219
Francois Romieu3e990ff2012-01-26 12:50:01 +01001220static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1221{
1222 void __iomem *ioaddr = tp->mmio_addr;
1223
1224 RTL_W16(IntrMask, bits);
1225}
1226
Francois Romieuda78dbf2012-01-26 14:18:23 +01001227#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1228#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1229#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1230
1231static void rtl_irq_enable_all(struct rtl8169_private *tp)
1232{
1233 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1234}
1235
françois romieu811fd302011-12-04 20:30:45 +00001236static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
françois romieu811fd302011-12-04 20:30:45 +00001238 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Francois Romieu9085cdf2012-01-26 12:59:08 +01001240 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001241 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001242 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
françois romieu4da19632011-01-03 15:07:55 +00001245static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
françois romieu4da19632011-01-03 15:07:55 +00001247 void __iomem *ioaddr = tp->mmio_addr;
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return RTL_R32(TBICSR) & TBIReset;
1250}
1251
françois romieu4da19632011-01-03 15:07:55 +00001252static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
françois romieu4da19632011-01-03 15:07:55 +00001254 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
1257static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1258{
1259 return RTL_R32(TBICSR) & TBILinkOk;
1260}
1261
1262static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1263{
1264 return RTL_R8(PHYstatus) & LinkStatus;
1265}
1266
françois romieu4da19632011-01-03 15:07:55 +00001267static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
françois romieu4da19632011-01-03 15:07:55 +00001269 void __iomem *ioaddr = tp->mmio_addr;
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1272}
1273
françois romieu4da19632011-01-03 15:07:55 +00001274static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
1276 unsigned int val;
1277
françois romieu4da19632011-01-03 15:07:55 +00001278 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1279 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
Hayes Wang70090422011-07-06 15:58:06 +08001282static void rtl_link_chg_patch(struct rtl8169_private *tp)
1283{
1284 void __iomem *ioaddr = tp->mmio_addr;
1285 struct net_device *dev = tp->dev;
1286
1287 if (!netif_running(dev))
1288 return;
1289
1290 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1291 if (RTL_R8(PHYstatus) & _1000bpsF) {
1292 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1293 0x00000011, ERIAR_EXGMAC);
1294 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1295 0x00000005, ERIAR_EXGMAC);
1296 } else if (RTL_R8(PHYstatus) & _100bps) {
1297 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1298 0x0000001f, ERIAR_EXGMAC);
1299 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1300 0x00000005, ERIAR_EXGMAC);
1301 } else {
1302 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1303 0x0000001f, ERIAR_EXGMAC);
1304 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1305 0x0000003f, ERIAR_EXGMAC);
1306 }
1307 /* Reset packet filter */
1308 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1309 ERIAR_EXGMAC);
1310 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1311 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001312 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1313 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1314 if (RTL_R8(PHYstatus) & _1000bpsF) {
1315 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1316 0x00000011, ERIAR_EXGMAC);
1317 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1318 0x00000005, ERIAR_EXGMAC);
1319 } else {
1320 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1321 0x0000001f, ERIAR_EXGMAC);
1322 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1323 0x0000003f, ERIAR_EXGMAC);
1324 }
Hayes Wang70090422011-07-06 15:58:06 +08001325 }
1326}
1327
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001328static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001329 struct rtl8169_private *tp,
1330 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001333 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001334 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001335 if (pm)
1336 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001338 if (net_ratelimit())
1339 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001340 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001342 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001343 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001344 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001348static void rtl8169_check_link_status(struct net_device *dev,
1349 struct rtl8169_private *tp,
1350 void __iomem *ioaddr)
1351{
1352 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1353}
1354
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001355#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1356
1357static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1358{
1359 void __iomem *ioaddr = tp->mmio_addr;
1360 u8 options;
1361 u32 wolopts = 0;
1362
1363 options = RTL_R8(Config1);
1364 if (!(options & PMEnable))
1365 return 0;
1366
1367 options = RTL_R8(Config3);
1368 if (options & LinkUp)
1369 wolopts |= WAKE_PHY;
1370 if (options & MagicPacket)
1371 wolopts |= WAKE_MAGIC;
1372
1373 options = RTL_R8(Config5);
1374 if (options & UWF)
1375 wolopts |= WAKE_UCAST;
1376 if (options & BWF)
1377 wolopts |= WAKE_BCAST;
1378 if (options & MWF)
1379 wolopts |= WAKE_MCAST;
1380
1381 return wolopts;
1382}
1383
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001384static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1385{
1386 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001387
Francois Romieuda78dbf2012-01-26 14:18:23 +01001388 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001389
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001390 wol->supported = WAKE_ANY;
1391 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001392
Francois Romieuda78dbf2012-01-26 14:18:23 +01001393 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001394}
1395
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001396static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001397{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001398 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001399 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001400 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001401 u32 opt;
1402 u16 reg;
1403 u8 mask;
1404 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001405 { WAKE_PHY, Config3, LinkUp },
1406 { WAKE_MAGIC, Config3, MagicPacket },
1407 { WAKE_UCAST, Config5, UWF },
1408 { WAKE_BCAST, Config5, BWF },
1409 { WAKE_MCAST, Config5, MWF },
1410 { WAKE_ANY, Config5, LanWake }
1411 };
Francois Romieu34ffca42012-10-06 11:19:52 +02001412 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001413
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001414 RTL_W8(Cfg9346, Cfg9346_Unlock);
1415
1416 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
Francois Romieu34ffca42012-10-06 11:19:52 +02001417 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001418 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001419 options |= cfg[i].mask;
1420 RTL_W8(cfg[i].reg, options);
1421 }
1422
Francois Romieu34ffca42012-10-06 11:19:52 +02001423 switch (tp->mac_version) {
1424 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1425 options = RTL_R8(Config1) & ~PMEnable;
1426 if (wolopts)
1427 options |= PMEnable;
1428 RTL_W8(Config1, options);
1429 break;
1430 default:
Francois Romieu160f00f2012-10-06 11:19:53 +02001431 options = RTL_R8(Config2) & ~PME_SIGNAL;
1432 if (wolopts)
1433 options |= PME_SIGNAL;
1434 RTL_W8(Config2, options);
Francois Romieu34ffca42012-10-06 11:19:52 +02001435 break;
1436 }
1437
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001438 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001439}
1440
1441static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1442{
1443 struct rtl8169_private *tp = netdev_priv(dev);
1444
Francois Romieuda78dbf2012-01-26 14:18:23 +01001445 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001446
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001447 if (wol->wolopts)
1448 tp->features |= RTL_FEATURE_WOL;
1449 else
1450 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001451 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001452
1453 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001454
françois romieuea809072010-11-08 13:23:58 +00001455 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1456
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001457 return 0;
1458}
1459
Francois Romieu31bd2042011-04-26 18:58:59 +02001460static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1461{
Francois Romieu85bffe62011-04-27 08:22:39 +02001462 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001463}
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465static void rtl8169_get_drvinfo(struct net_device *dev,
1466 struct ethtool_drvinfo *info)
1467{
1468 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001469 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Rick Jones68aad782011-11-07 13:29:27 +00001471 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1472 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1473 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001474 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001475 if (!IS_ERR_OR_NULL(rtl_fw))
1476 strlcpy(info->fw_version, rtl_fw->version,
1477 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
1480static int rtl8169_get_regs_len(struct net_device *dev)
1481{
1482 return R8169_REGS_SIZE;
1483}
1484
1485static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001486 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
1488 struct rtl8169_private *tp = netdev_priv(dev);
1489 void __iomem *ioaddr = tp->mmio_addr;
1490 int ret = 0;
1491 u32 reg;
1492
1493 reg = RTL_R32(TBICSR);
1494 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1495 (duplex == DUPLEX_FULL)) {
1496 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1497 } else if (autoneg == AUTONEG_ENABLE)
1498 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1499 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001500 netif_warn(tp, link, dev,
1501 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 ret = -EOPNOTSUPP;
1503 }
1504
1505 return ret;
1506}
1507
1508static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001509 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
1511 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001512 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001513 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Hayes Wang716b50a2011-02-22 17:26:18 +08001515 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001518 int auto_nego;
1519
françois romieu4da19632011-01-03 15:07:55 +00001520 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001521 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1522 ADVERTISE_100HALF | ADVERTISE_100FULL);
1523
1524 if (adv & ADVERTISED_10baseT_Half)
1525 auto_nego |= ADVERTISE_10HALF;
1526 if (adv & ADVERTISED_10baseT_Full)
1527 auto_nego |= ADVERTISE_10FULL;
1528 if (adv & ADVERTISED_100baseT_Half)
1529 auto_nego |= ADVERTISE_100HALF;
1530 if (adv & ADVERTISED_100baseT_Full)
1531 auto_nego |= ADVERTISE_100FULL;
1532
françois romieu3577aa12009-05-19 10:46:48 +00001533 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1534
françois romieu4da19632011-01-03 15:07:55 +00001535 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001536 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1537
1538 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001539 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001540 if (adv & ADVERTISED_1000baseT_Half)
1541 giga_ctrl |= ADVERTISE_1000HALF;
1542 if (adv & ADVERTISED_1000baseT_Full)
1543 giga_ctrl |= ADVERTISE_1000FULL;
1544 } else if (adv & (ADVERTISED_1000baseT_Half |
1545 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001546 netif_info(tp, link, dev,
1547 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001548 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
françois romieu3577aa12009-05-19 10:46:48 +00001551 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001552
françois romieu4da19632011-01-03 15:07:55 +00001553 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1554 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001555 } else {
1556 giga_ctrl = 0;
1557
1558 if (speed == SPEED_10)
1559 bmcr = 0;
1560 else if (speed == SPEED_100)
1561 bmcr = BMCR_SPEED100;
1562 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001563 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001564
1565 if (duplex == DUPLEX_FULL)
1566 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001567 }
1568
françois romieu4da19632011-01-03 15:07:55 +00001569 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001570
Francois Romieucecb5fd2011-04-01 10:21:07 +02001571 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1572 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001573 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001574 rtl_writephy(tp, 0x17, 0x2138);
1575 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001576 } else {
françois romieu4da19632011-01-03 15:07:55 +00001577 rtl_writephy(tp, 0x17, 0x2108);
1578 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001579 }
1580 }
1581
Oliver Neukum54405cd2011-01-06 21:55:13 +01001582 rc = 0;
1583out:
1584 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585}
1586
1587static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001588 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
1590 struct rtl8169_private *tp = netdev_priv(dev);
1591 int ret;
1592
Oliver Neukum54405cd2011-01-06 21:55:13 +01001593 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001594 if (ret < 0)
1595 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Francois Romieu4876cc12011-03-11 21:07:11 +01001597 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1598 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001600 }
1601out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return ret;
1603}
1604
1605static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1606{
1607 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 int ret;
1609
Francois Romieu4876cc12011-03-11 21:07:11 +01001610 del_timer_sync(&tp->timer);
1611
Francois Romieuda78dbf2012-01-26 14:18:23 +01001612 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001613 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001614 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001615 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return ret;
1618}
1619
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001620static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1621 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
Francois Romieud58d46b2011-05-03 16:38:29 +02001623 struct rtl8169_private *tp = netdev_priv(dev);
1624
Francois Romieu2b7b4312011-04-18 22:53:24 -07001625 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001626 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Francois Romieud58d46b2011-05-03 16:38:29 +02001628 if (dev->mtu > JUMBO_1K &&
1629 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1630 features &= ~NETIF_F_IP_CSUM;
1631
Michał Mirosław350fb322011-04-08 06:35:56 +00001632 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
1634
Francois Romieuda78dbf2012-01-26 14:18:23 +01001635static void __rtl8169_set_features(struct net_device *dev,
1636 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
1638 struct rtl8169_private *tp = netdev_priv(dev);
Ben Greear6bbe0212012-02-10 15:04:33 +00001639 netdev_features_t changed = features ^ dev->features;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001640 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Ben Greear6bbe0212012-02-10 15:04:33 +00001642 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)))
1643 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Ben Greear6bbe0212012-02-10 15:04:33 +00001645 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)) {
1646 if (features & NETIF_F_RXCSUM)
1647 tp->cp_cmd |= RxChkSum;
1648 else
1649 tp->cp_cmd &= ~RxChkSum;
Michał Mirosław350fb322011-04-08 06:35:56 +00001650
Ben Greear6bbe0212012-02-10 15:04:33 +00001651 if (dev->features & NETIF_F_HW_VLAN_RX)
1652 tp->cp_cmd |= RxVlan;
1653 else
1654 tp->cp_cmd &= ~RxVlan;
1655
1656 RTL_W16(CPlusCmd, tp->cp_cmd);
1657 RTL_R16(CPlusCmd);
1658 }
1659 if (changed & NETIF_F_RXALL) {
1660 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1661 if (features & NETIF_F_RXALL)
1662 tmp |= (AcceptErr | AcceptRunt);
1663 RTL_W32(RxConfig, tmp);
1664 }
Francois Romieuda78dbf2012-01-26 14:18:23 +01001665}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Francois Romieuda78dbf2012-01-26 14:18:23 +01001667static int rtl8169_set_features(struct net_device *dev,
1668 netdev_features_t features)
1669{
1670 struct rtl8169_private *tp = netdev_priv(dev);
1671
1672 rtl_lock_work(tp);
1673 __rtl8169_set_features(dev, features);
1674 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 return 0;
1677}
1678
Francois Romieuda78dbf2012-01-26 14:18:23 +01001679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1681 struct sk_buff *skb)
1682{
Jesse Grosseab6d182010-10-20 13:56:03 +00001683 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1685}
1686
Francois Romieu7a8fc772011-03-01 17:18:33 +01001687static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
1689 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Francois Romieu7a8fc772011-03-01 17:18:33 +01001691 if (opts2 & RxVlanTag)
1692 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695}
1696
Francois Romieuccdffb92008-07-26 14:26:06 +02001697static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
1699 struct rtl8169_private *tp = netdev_priv(dev);
1700 void __iomem *ioaddr = tp->mmio_addr;
1701 u32 status;
1702
1703 cmd->supported =
1704 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1705 cmd->port = PORT_FIBRE;
1706 cmd->transceiver = XCVR_INTERNAL;
1707
1708 status = RTL_R32(TBICSR);
1709 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1710 cmd->autoneg = !!(status & TBINwEnable);
1711
David Decotigny70739492011-04-27 18:32:40 +00001712 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001714
1715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
1717
Francois Romieuccdffb92008-07-26 14:26:06 +02001718static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
1720 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Francois Romieuccdffb92008-07-26 14:26:06 +02001722 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
1724
1725static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1726{
1727 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001728 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Francois Romieuda78dbf2012-01-26 14:18:23 +01001730 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02001731 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001732 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Francois Romieuccdffb92008-07-26 14:26:06 +02001734 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735}
1736
1737static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1738 void *p)
1739{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001740 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Francois Romieu5b0384f2006-08-16 16:00:01 +02001742 if (regs->len > R8169_REGS_SIZE)
1743 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Francois Romieuda78dbf2012-01-26 14:18:23 +01001745 rtl_lock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001746 memcpy_fromio(p, tp->mmio_addr, regs->len);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001747 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748}
1749
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001750static u32 rtl8169_get_msglevel(struct net_device *dev)
1751{
1752 struct rtl8169_private *tp = netdev_priv(dev);
1753
1754 return tp->msg_enable;
1755}
1756
1757static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1758{
1759 struct rtl8169_private *tp = netdev_priv(dev);
1760
1761 tp->msg_enable = value;
1762}
1763
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001764static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1765 "tx_packets",
1766 "rx_packets",
1767 "tx_errors",
1768 "rx_errors",
1769 "rx_missed",
1770 "align_errors",
1771 "tx_single_collisions",
1772 "tx_multi_collisions",
1773 "unicast",
1774 "broadcast",
1775 "multicast",
1776 "tx_aborted",
1777 "tx_underrun",
1778};
1779
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001780static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001781{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001782 switch (sset) {
1783 case ETH_SS_STATS:
1784 return ARRAY_SIZE(rtl8169_gstrings);
1785 default:
1786 return -EOPNOTSUPP;
1787 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001788}
1789
Ivan Vecera355423d2009-02-06 21:49:57 -08001790static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001791{
1792 struct rtl8169_private *tp = netdev_priv(dev);
1793 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001794 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001795 struct rtl8169_counters *counters;
1796 dma_addr_t paddr;
1797 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001798 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001799
Ivan Vecera355423d2009-02-06 21:49:57 -08001800 /*
1801 * Some chips are unable to dump tally counters when the receiver
1802 * is disabled.
1803 */
1804 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1805 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001806
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001807 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001808 if (!counters)
1809 return;
1810
1811 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001812 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001813 RTL_W32(CounterAddrLow, cmd);
1814 RTL_W32(CounterAddrLow, cmd | CounterDump);
1815
Ivan Vecera355423d2009-02-06 21:49:57 -08001816 while (wait--) {
1817 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001818 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001819 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001820 }
1821 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001822 }
1823
1824 RTL_W32(CounterAddrLow, 0);
1825 RTL_W32(CounterAddrHigh, 0);
1826
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001827 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001828}
1829
Ivan Vecera355423d2009-02-06 21:49:57 -08001830static void rtl8169_get_ethtool_stats(struct net_device *dev,
1831 struct ethtool_stats *stats, u64 *data)
1832{
1833 struct rtl8169_private *tp = netdev_priv(dev);
1834
1835 ASSERT_RTNL();
1836
1837 rtl8169_update_counters(dev);
1838
1839 data[0] = le64_to_cpu(tp->counters.tx_packets);
1840 data[1] = le64_to_cpu(tp->counters.rx_packets);
1841 data[2] = le64_to_cpu(tp->counters.tx_errors);
1842 data[3] = le32_to_cpu(tp->counters.rx_errors);
1843 data[4] = le16_to_cpu(tp->counters.rx_missed);
1844 data[5] = le16_to_cpu(tp->counters.align_errors);
1845 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1846 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1847 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1848 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1849 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1850 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1851 data[12] = le16_to_cpu(tp->counters.tx_underun);
1852}
1853
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001854static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1855{
1856 switch(stringset) {
1857 case ETH_SS_STATS:
1858 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1859 break;
1860 }
1861}
1862
Jeff Garzik7282d492006-09-13 14:30:00 -04001863static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 .get_drvinfo = rtl8169_get_drvinfo,
1865 .get_regs_len = rtl8169_get_regs_len,
1866 .get_link = ethtool_op_get_link,
1867 .get_settings = rtl8169_get_settings,
1868 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001869 .get_msglevel = rtl8169_get_msglevel,
1870 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001872 .get_wol = rtl8169_get_wol,
1873 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001874 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001875 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001876 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877};
1878
Francois Romieu07d3f512007-02-21 22:40:46 +01001879static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001880 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
Francois Romieu5d320a22011-05-08 17:47:36 +02001882 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001883 /*
1884 * The driver currently handles the 8168Bf and the 8168Be identically
1885 * but they can be identified more specifically through the test below
1886 * if needed:
1887 *
1888 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001889 *
1890 * Same thing for the 8101Eb and the 8101Ec:
1891 *
1892 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001893 */
Francois Romieu37441002011-06-17 22:58:54 +02001894 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001896 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 int mac_version;
1898 } mac_info[] = {
Hayes Wangc2218922011-09-06 16:55:18 +08001899 /* 8168F family. */
1900 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1901 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1902
hayeswang01dc7fe2011-03-21 01:50:28 +00001903 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001904 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001905 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1906 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1907 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1908
Francois Romieu5b538df2008-07-20 16:22:45 +02001909 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001910 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1911 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001912 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001913
françois romieue6de30d2011-01-03 15:08:37 +00001914 /* 8168DP family. */
1915 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1916 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001917 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001918
Francois Romieuef808d52008-06-29 13:10:54 +02001919 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001920 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001921 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001922 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001923 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001924 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1925 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001926 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001927 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001928 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001929
1930 /* 8168B family. */
1931 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1932 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1933 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1934 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1935
1936 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001937 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001938 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1939 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1940 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001941 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1942 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1943 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1944 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1945 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1946 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001947 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001948 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001949 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001950 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1951 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001952 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1953 /* FIXME: where did these entries come from ? -- FR */
1954 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1955 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1956
1957 /* 8110 family. */
1958 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1959 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1960 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1961 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1962 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1963 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1964
Jean Delvaref21b75e2009-05-26 20:54:48 -07001965 /* Catch-all */
1966 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001967 };
1968 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 u32 reg;
1970
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001971 reg = RTL_R32(TxConfig);
1972 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 p++;
1974 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001975
1976 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1977 netif_notice(tp, probe, dev,
1978 "unknown MAC, using family default\n");
1979 tp->mac_version = default_version;
1980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
1983static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1984{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001985 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986}
1987
Francois Romieu867763c2007-08-17 18:21:58 +02001988struct phy_reg {
1989 u16 reg;
1990 u16 val;
1991};
1992
françois romieu4da19632011-01-03 15:07:55 +00001993static void rtl_writephy_batch(struct rtl8169_private *tp,
1994 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001995{
1996 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001997 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001998 regs++;
1999 }
2000}
2001
françois romieubca03d52011-01-03 15:07:31 +00002002#define PHY_READ 0x00000000
2003#define PHY_DATA_OR 0x10000000
2004#define PHY_DATA_AND 0x20000000
2005#define PHY_BJMPN 0x30000000
2006#define PHY_READ_EFUSE 0x40000000
2007#define PHY_READ_MAC_BYTE 0x50000000
2008#define PHY_WRITE_MAC_BYTE 0x60000000
2009#define PHY_CLEAR_READCOUNT 0x70000000
2010#define PHY_WRITE 0x80000000
2011#define PHY_READCOUNT_EQ_SKIP 0x90000000
2012#define PHY_COMP_EQ_SKIPN 0xa0000000
2013#define PHY_COMP_NEQ_SKIPN 0xb0000000
2014#define PHY_WRITE_PREVIOUS 0xc0000000
2015#define PHY_SKIPN 0xd0000000
2016#define PHY_DELAY_MS 0xe0000000
2017#define PHY_WRITE_ERI_WORD 0xf0000000
2018
Hayes Wang960aee62011-06-18 11:37:48 +02002019struct fw_info {
2020 u32 magic;
2021 char version[RTL_VER_SIZE];
2022 __le32 fw_start;
2023 __le32 fw_len;
2024 u8 chksum;
2025} __packed;
2026
Francois Romieu1c361ef2011-06-17 17:16:24 +02002027#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2028
2029static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002030{
Francois Romieub6ffd972011-06-17 17:00:05 +02002031 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002032 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002033 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2034 char *version = rtl_fw->version;
2035 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002036
Francois Romieu1c361ef2011-06-17 17:16:24 +02002037 if (fw->size < FW_OPCODE_SIZE)
2038 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002039
2040 if (!fw_info->magic) {
2041 size_t i, size, start;
2042 u8 checksum = 0;
2043
2044 if (fw->size < sizeof(*fw_info))
2045 goto out;
2046
2047 for (i = 0; i < fw->size; i++)
2048 checksum += fw->data[i];
2049 if (checksum != 0)
2050 goto out;
2051
2052 start = le32_to_cpu(fw_info->fw_start);
2053 if (start > fw->size)
2054 goto out;
2055
2056 size = le32_to_cpu(fw_info->fw_len);
2057 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2058 goto out;
2059
2060 memcpy(version, fw_info->version, RTL_VER_SIZE);
2061
2062 pa->code = (__le32 *)(fw->data + start);
2063 pa->size = size;
2064 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002065 if (fw->size % FW_OPCODE_SIZE)
2066 goto out;
2067
2068 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2069
2070 pa->code = (__le32 *)fw->data;
2071 pa->size = fw->size / FW_OPCODE_SIZE;
2072 }
2073 version[RTL_VER_SIZE - 1] = 0;
2074
2075 rc = true;
2076out:
2077 return rc;
2078}
2079
Francois Romieufd112f22011-06-18 00:10:29 +02002080static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2081 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002082{
Francois Romieufd112f22011-06-18 00:10:29 +02002083 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002084 size_t index;
2085
Francois Romieu1c361ef2011-06-17 17:16:24 +02002086 for (index = 0; index < pa->size; index++) {
2087 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002088 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002089
hayeswang42b82dc2011-01-10 02:07:25 +00002090 switch(action & 0xf0000000) {
2091 case PHY_READ:
2092 case PHY_DATA_OR:
2093 case PHY_DATA_AND:
2094 case PHY_READ_EFUSE:
2095 case PHY_CLEAR_READCOUNT:
2096 case PHY_WRITE:
2097 case PHY_WRITE_PREVIOUS:
2098 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002099 break;
2100
hayeswang42b82dc2011-01-10 02:07:25 +00002101 case PHY_BJMPN:
2102 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002103 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002104 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002105 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002106 }
2107 break;
2108 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002109 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002110 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002111 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002112 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002113 }
2114 break;
2115 case PHY_COMP_EQ_SKIPN:
2116 case PHY_COMP_NEQ_SKIPN:
2117 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002118 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002119 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002120 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002121 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002122 }
2123 break;
2124
2125 case PHY_READ_MAC_BYTE:
2126 case PHY_WRITE_MAC_BYTE:
2127 case PHY_WRITE_ERI_WORD:
2128 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002129 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002130 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002131 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002132 }
2133 }
Francois Romieufd112f22011-06-18 00:10:29 +02002134 rc = true;
2135out:
2136 return rc;
2137}
françois romieubca03d52011-01-03 15:07:31 +00002138
Francois Romieufd112f22011-06-18 00:10:29 +02002139static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2140{
2141 struct net_device *dev = tp->dev;
2142 int rc = -EINVAL;
2143
2144 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2145 netif_err(tp, ifup, dev, "invalid firwmare\n");
2146 goto out;
2147 }
2148
2149 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2150 rc = 0;
2151out:
2152 return rc;
2153}
2154
2155static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2156{
2157 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2158 u32 predata, count;
2159 size_t index;
2160
2161 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002162
Francois Romieu1c361ef2011-06-17 17:16:24 +02002163 for (index = 0; index < pa->size; ) {
2164 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002165 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002166 u32 regno = (action & 0x0fff0000) >> 16;
2167
2168 if (!action)
2169 break;
françois romieubca03d52011-01-03 15:07:31 +00002170
2171 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002172 case PHY_READ:
2173 predata = rtl_readphy(tp, regno);
2174 count++;
2175 index++;
françois romieubca03d52011-01-03 15:07:31 +00002176 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002177 case PHY_DATA_OR:
2178 predata |= data;
2179 index++;
2180 break;
2181 case PHY_DATA_AND:
2182 predata &= data;
2183 index++;
2184 break;
2185 case PHY_BJMPN:
2186 index -= regno;
2187 break;
2188 case PHY_READ_EFUSE:
2189 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2190 index++;
2191 break;
2192 case PHY_CLEAR_READCOUNT:
2193 count = 0;
2194 index++;
2195 break;
2196 case PHY_WRITE:
2197 rtl_writephy(tp, regno, data);
2198 index++;
2199 break;
2200 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002201 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002202 break;
2203 case PHY_COMP_EQ_SKIPN:
2204 if (predata == data)
2205 index += regno;
2206 index++;
2207 break;
2208 case PHY_COMP_NEQ_SKIPN:
2209 if (predata != data)
2210 index += regno;
2211 index++;
2212 break;
2213 case PHY_WRITE_PREVIOUS:
2214 rtl_writephy(tp, regno, predata);
2215 index++;
2216 break;
2217 case PHY_SKIPN:
2218 index += regno + 1;
2219 break;
2220 case PHY_DELAY_MS:
2221 mdelay(data);
2222 index++;
2223 break;
2224
2225 case PHY_READ_MAC_BYTE:
2226 case PHY_WRITE_MAC_BYTE:
2227 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002228 default:
2229 BUG();
2230 }
2231 }
2232}
2233
françois romieuf1e02ed2011-01-13 13:07:53 +00002234static void rtl_release_firmware(struct rtl8169_private *tp)
2235{
Francois Romieub6ffd972011-06-17 17:00:05 +02002236 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2237 release_firmware(tp->rtl_fw->fw);
2238 kfree(tp->rtl_fw);
2239 }
2240 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002241}
2242
François Romieu953a12c2011-04-24 17:38:48 +02002243static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002244{
Francois Romieub6ffd972011-06-17 17:00:05 +02002245 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002246
2247 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002248 if (!IS_ERR_OR_NULL(rtl_fw))
2249 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002250}
2251
2252static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2253{
2254 if (rtl_readphy(tp, reg) != val)
2255 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2256 else
2257 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002258}
2259
françois romieu4da19632011-01-03 15:07:55 +00002260static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002262 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002263 { 0x1f, 0x0001 },
2264 { 0x06, 0x006e },
2265 { 0x08, 0x0708 },
2266 { 0x15, 0x4000 },
2267 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
françois romieu0b9b5712009-08-10 19:44:56 +00002269 { 0x1f, 0x0001 },
2270 { 0x03, 0x00a1 },
2271 { 0x02, 0x0008 },
2272 { 0x01, 0x0120 },
2273 { 0x00, 0x1000 },
2274 { 0x04, 0x0800 },
2275 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
françois romieu0b9b5712009-08-10 19:44:56 +00002277 { 0x03, 0xff41 },
2278 { 0x02, 0xdf60 },
2279 { 0x01, 0x0140 },
2280 { 0x00, 0x0077 },
2281 { 0x04, 0x7800 },
2282 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
françois romieu0b9b5712009-08-10 19:44:56 +00002284 { 0x03, 0x802f },
2285 { 0x02, 0x4f02 },
2286 { 0x01, 0x0409 },
2287 { 0x00, 0xf0f9 },
2288 { 0x04, 0x9800 },
2289 { 0x04, 0x9000 },
2290
2291 { 0x03, 0xdf01 },
2292 { 0x02, 0xdf20 },
2293 { 0x01, 0xff95 },
2294 { 0x00, 0xba00 },
2295 { 0x04, 0xa800 },
2296 { 0x04, 0xa000 },
2297
2298 { 0x03, 0xff41 },
2299 { 0x02, 0xdf20 },
2300 { 0x01, 0x0140 },
2301 { 0x00, 0x00bb },
2302 { 0x04, 0xb800 },
2303 { 0x04, 0xb000 },
2304
2305 { 0x03, 0xdf41 },
2306 { 0x02, 0xdc60 },
2307 { 0x01, 0x6340 },
2308 { 0x00, 0x007d },
2309 { 0x04, 0xd800 },
2310 { 0x04, 0xd000 },
2311
2312 { 0x03, 0xdf01 },
2313 { 0x02, 0xdf20 },
2314 { 0x01, 0x100a },
2315 { 0x00, 0xa0ff },
2316 { 0x04, 0xf800 },
2317 { 0x04, 0xf000 },
2318
2319 { 0x1f, 0x0000 },
2320 { 0x0b, 0x0000 },
2321 { 0x00, 0x9200 }
2322 };
2323
françois romieu4da19632011-01-03 15:07:55 +00002324 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}
2326
françois romieu4da19632011-01-03 15:07:55 +00002327static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002328{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002329 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002330 { 0x1f, 0x0002 },
2331 { 0x01, 0x90d0 },
2332 { 0x1f, 0x0000 }
2333 };
2334
françois romieu4da19632011-01-03 15:07:55 +00002335 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002336}
2337
françois romieu4da19632011-01-03 15:07:55 +00002338static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002339{
2340 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002341
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002342 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2343 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002344 return;
2345
françois romieu4da19632011-01-03 15:07:55 +00002346 rtl_writephy(tp, 0x1f, 0x0001);
2347 rtl_writephy(tp, 0x10, 0xf01b);
2348 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002349}
2350
françois romieu4da19632011-01-03 15:07:55 +00002351static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002352{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002353 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002354 { 0x1f, 0x0001 },
2355 { 0x04, 0x0000 },
2356 { 0x03, 0x00a1 },
2357 { 0x02, 0x0008 },
2358 { 0x01, 0x0120 },
2359 { 0x00, 0x1000 },
2360 { 0x04, 0x0800 },
2361 { 0x04, 0x9000 },
2362 { 0x03, 0x802f },
2363 { 0x02, 0x4f02 },
2364 { 0x01, 0x0409 },
2365 { 0x00, 0xf099 },
2366 { 0x04, 0x9800 },
2367 { 0x04, 0xa000 },
2368 { 0x03, 0xdf01 },
2369 { 0x02, 0xdf20 },
2370 { 0x01, 0xff95 },
2371 { 0x00, 0xba00 },
2372 { 0x04, 0xa800 },
2373 { 0x04, 0xf000 },
2374 { 0x03, 0xdf01 },
2375 { 0x02, 0xdf20 },
2376 { 0x01, 0x101a },
2377 { 0x00, 0xa0ff },
2378 { 0x04, 0xf800 },
2379 { 0x04, 0x0000 },
2380 { 0x1f, 0x0000 },
2381
2382 { 0x1f, 0x0001 },
2383 { 0x10, 0xf41b },
2384 { 0x14, 0xfb54 },
2385 { 0x18, 0xf5c7 },
2386 { 0x1f, 0x0000 },
2387
2388 { 0x1f, 0x0001 },
2389 { 0x17, 0x0cc0 },
2390 { 0x1f, 0x0000 }
2391 };
2392
françois romieu4da19632011-01-03 15:07:55 +00002393 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002394
françois romieu4da19632011-01-03 15:07:55 +00002395 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002396}
2397
françois romieu4da19632011-01-03 15:07:55 +00002398static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002399{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002400 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002401 { 0x1f, 0x0001 },
2402 { 0x04, 0x0000 },
2403 { 0x03, 0x00a1 },
2404 { 0x02, 0x0008 },
2405 { 0x01, 0x0120 },
2406 { 0x00, 0x1000 },
2407 { 0x04, 0x0800 },
2408 { 0x04, 0x9000 },
2409 { 0x03, 0x802f },
2410 { 0x02, 0x4f02 },
2411 { 0x01, 0x0409 },
2412 { 0x00, 0xf099 },
2413 { 0x04, 0x9800 },
2414 { 0x04, 0xa000 },
2415 { 0x03, 0xdf01 },
2416 { 0x02, 0xdf20 },
2417 { 0x01, 0xff95 },
2418 { 0x00, 0xba00 },
2419 { 0x04, 0xa800 },
2420 { 0x04, 0xf000 },
2421 { 0x03, 0xdf01 },
2422 { 0x02, 0xdf20 },
2423 { 0x01, 0x101a },
2424 { 0x00, 0xa0ff },
2425 { 0x04, 0xf800 },
2426 { 0x04, 0x0000 },
2427 { 0x1f, 0x0000 },
2428
2429 { 0x1f, 0x0001 },
2430 { 0x0b, 0x8480 },
2431 { 0x1f, 0x0000 },
2432
2433 { 0x1f, 0x0001 },
2434 { 0x18, 0x67c7 },
2435 { 0x04, 0x2000 },
2436 { 0x03, 0x002f },
2437 { 0x02, 0x4360 },
2438 { 0x01, 0x0109 },
2439 { 0x00, 0x3022 },
2440 { 0x04, 0x2800 },
2441 { 0x1f, 0x0000 },
2442
2443 { 0x1f, 0x0001 },
2444 { 0x17, 0x0cc0 },
2445 { 0x1f, 0x0000 }
2446 };
2447
françois romieu4da19632011-01-03 15:07:55 +00002448 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002449}
2450
françois romieu4da19632011-01-03 15:07:55 +00002451static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002452{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002453 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002454 { 0x10, 0xf41b },
2455 { 0x1f, 0x0000 }
2456 };
2457
françois romieu4da19632011-01-03 15:07:55 +00002458 rtl_writephy(tp, 0x1f, 0x0001);
2459 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002460
françois romieu4da19632011-01-03 15:07:55 +00002461 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002462}
2463
françois romieu4da19632011-01-03 15:07:55 +00002464static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002465{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002466 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002467 { 0x1f, 0x0001 },
2468 { 0x10, 0xf41b },
2469 { 0x1f, 0x0000 }
2470 };
2471
françois romieu4da19632011-01-03 15:07:55 +00002472 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002473}
2474
françois romieu4da19632011-01-03 15:07:55 +00002475static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002476{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002477 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002478 { 0x1f, 0x0000 },
2479 { 0x1d, 0x0f00 },
2480 { 0x1f, 0x0002 },
2481 { 0x0c, 0x1ec8 },
2482 { 0x1f, 0x0000 }
2483 };
2484
françois romieu4da19632011-01-03 15:07:55 +00002485 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002486}
2487
françois romieu4da19632011-01-03 15:07:55 +00002488static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002489{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002490 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002491 { 0x1f, 0x0001 },
2492 { 0x1d, 0x3d98 },
2493 { 0x1f, 0x0000 }
2494 };
2495
françois romieu4da19632011-01-03 15:07:55 +00002496 rtl_writephy(tp, 0x1f, 0x0000);
2497 rtl_patchphy(tp, 0x14, 1 << 5);
2498 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002499
françois romieu4da19632011-01-03 15:07:55 +00002500 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002501}
2502
françois romieu4da19632011-01-03 15:07:55 +00002503static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002504{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002505 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002506 { 0x1f, 0x0001 },
2507 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002508 { 0x1f, 0x0002 },
2509 { 0x00, 0x88d4 },
2510 { 0x01, 0x82b1 },
2511 { 0x03, 0x7002 },
2512 { 0x08, 0x9e30 },
2513 { 0x09, 0x01f0 },
2514 { 0x0a, 0x5500 },
2515 { 0x0c, 0x00c8 },
2516 { 0x1f, 0x0003 },
2517 { 0x12, 0xc096 },
2518 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002519 { 0x1f, 0x0000 },
2520 { 0x1f, 0x0000 },
2521 { 0x09, 0x2000 },
2522 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002523 };
2524
françois romieu4da19632011-01-03 15:07:55 +00002525 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002526
françois romieu4da19632011-01-03 15:07:55 +00002527 rtl_patchphy(tp, 0x14, 1 << 5);
2528 rtl_patchphy(tp, 0x0d, 1 << 5);
2529 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002530}
2531
françois romieu4da19632011-01-03 15:07:55 +00002532static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002533{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002534 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002535 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002536 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002537 { 0x03, 0x802f },
2538 { 0x02, 0x4f02 },
2539 { 0x01, 0x0409 },
2540 { 0x00, 0xf099 },
2541 { 0x04, 0x9800 },
2542 { 0x04, 0x9000 },
2543 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002544 { 0x1f, 0x0002 },
2545 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002546 { 0x06, 0x0761 },
2547 { 0x1f, 0x0003 },
2548 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002549 { 0x1f, 0x0000 }
2550 };
2551
françois romieu4da19632011-01-03 15:07:55 +00002552 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002553
françois romieu4da19632011-01-03 15:07:55 +00002554 rtl_patchphy(tp, 0x16, 1 << 0);
2555 rtl_patchphy(tp, 0x14, 1 << 5);
2556 rtl_patchphy(tp, 0x0d, 1 << 5);
2557 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002558}
2559
françois romieu4da19632011-01-03 15:07:55 +00002560static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002561{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002562 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002563 { 0x1f, 0x0001 },
2564 { 0x12, 0x2300 },
2565 { 0x1d, 0x3d98 },
2566 { 0x1f, 0x0002 },
2567 { 0x0c, 0x7eb8 },
2568 { 0x06, 0x5461 },
2569 { 0x1f, 0x0003 },
2570 { 0x16, 0x0f0a },
2571 { 0x1f, 0x0000 }
2572 };
2573
françois romieu4da19632011-01-03 15:07:55 +00002574 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002575
françois romieu4da19632011-01-03 15:07:55 +00002576 rtl_patchphy(tp, 0x16, 1 << 0);
2577 rtl_patchphy(tp, 0x14, 1 << 5);
2578 rtl_patchphy(tp, 0x0d, 1 << 5);
2579 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002580}
2581
françois romieu4da19632011-01-03 15:07:55 +00002582static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002583{
françois romieu4da19632011-01-03 15:07:55 +00002584 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002585}
2586
françois romieubca03d52011-01-03 15:07:31 +00002587static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002588{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002589 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002590 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002591 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002592 { 0x06, 0x4064 },
2593 { 0x07, 0x2863 },
2594 { 0x08, 0x059c },
2595 { 0x09, 0x26b4 },
2596 { 0x0a, 0x6a19 },
2597 { 0x0b, 0xdcc8 },
2598 { 0x10, 0xf06d },
2599 { 0x14, 0x7f68 },
2600 { 0x18, 0x7fd9 },
2601 { 0x1c, 0xf0ff },
2602 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002603 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002604 { 0x12, 0xf49f },
2605 { 0x13, 0x070b },
2606 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002607 { 0x14, 0x94c0 },
2608
2609 /*
2610 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002611 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002612 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002613 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002614 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002615 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002616 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002617 { 0x06, 0x5561 },
2618
2619 /*
2620 * Can not link to 1Gbps with bad cable
2621 * Decrease SNR threshold form 21.07dB to 19.04dB
2622 */
2623 { 0x1f, 0x0001 },
2624 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002625
2626 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002627 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002628 };
françois romieubca03d52011-01-03 15:07:31 +00002629 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002630
françois romieu4da19632011-01-03 15:07:55 +00002631 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002632
françois romieubca03d52011-01-03 15:07:31 +00002633 /*
2634 * Rx Error Issue
2635 * Fine Tune Switching regulator parameter
2636 */
françois romieu4da19632011-01-03 15:07:55 +00002637 rtl_writephy(tp, 0x1f, 0x0002);
2638 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2639 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002640
françois romieudaf9df62009-10-07 12:44:20 +00002641 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002642 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002643 { 0x1f, 0x0002 },
2644 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002645 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002646 { 0x05, 0x8330 },
2647 { 0x06, 0x669a },
2648 { 0x1f, 0x0002 }
2649 };
2650 int val;
2651
françois romieu4da19632011-01-03 15:07:55 +00002652 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002653
françois romieu4da19632011-01-03 15:07:55 +00002654 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002655
2656 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002657 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002658 0x0065, 0x0066, 0x0067, 0x0068,
2659 0x0069, 0x006a, 0x006b, 0x006c
2660 };
2661 int i;
2662
françois romieu4da19632011-01-03 15:07:55 +00002663 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002664
2665 val &= 0xff00;
2666 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002667 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002668 }
2669 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002670 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002671 { 0x1f, 0x0002 },
2672 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002673 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002674 { 0x05, 0x8330 },
2675 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002676 };
2677
françois romieu4da19632011-01-03 15:07:55 +00002678 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002679 }
2680
françois romieubca03d52011-01-03 15:07:31 +00002681 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002682 rtl_writephy(tp, 0x1f, 0x0002);
2683 rtl_patchphy(tp, 0x0d, 0x0300);
2684 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002685
françois romieubca03d52011-01-03 15:07:31 +00002686 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002687 rtl_writephy(tp, 0x1f, 0x0002);
2688 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2689 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002690
françois romieu4da19632011-01-03 15:07:55 +00002691 rtl_writephy(tp, 0x1f, 0x0005);
2692 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002693
2694 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002695
françois romieu4da19632011-01-03 15:07:55 +00002696 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002697}
2698
françois romieubca03d52011-01-03 15:07:31 +00002699static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002700{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002701 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002702 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002703 { 0x1f, 0x0001 },
2704 { 0x06, 0x4064 },
2705 { 0x07, 0x2863 },
2706 { 0x08, 0x059c },
2707 { 0x09, 0x26b4 },
2708 { 0x0a, 0x6a19 },
2709 { 0x0b, 0xdcc8 },
2710 { 0x10, 0xf06d },
2711 { 0x14, 0x7f68 },
2712 { 0x18, 0x7fd9 },
2713 { 0x1c, 0xf0ff },
2714 { 0x1d, 0x3d9c },
2715 { 0x1f, 0x0003 },
2716 { 0x12, 0xf49f },
2717 { 0x13, 0x070b },
2718 { 0x1a, 0x05ad },
2719 { 0x14, 0x94c0 },
2720
françois romieubca03d52011-01-03 15:07:31 +00002721 /*
2722 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002723 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002724 */
françois romieudaf9df62009-10-07 12:44:20 +00002725 { 0x1f, 0x0002 },
2726 { 0x06, 0x5561 },
2727 { 0x1f, 0x0005 },
2728 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002729 { 0x06, 0x5561 },
2730
2731 /*
2732 * Can not link to 1Gbps with bad cable
2733 * Decrease SNR threshold form 21.07dB to 19.04dB
2734 */
2735 { 0x1f, 0x0001 },
2736 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002737
2738 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002739 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002740 };
françois romieubca03d52011-01-03 15:07:31 +00002741 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002742
françois romieu4da19632011-01-03 15:07:55 +00002743 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002744
2745 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002746 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002747 { 0x1f, 0x0002 },
2748 { 0x05, 0x669a },
2749 { 0x1f, 0x0005 },
2750 { 0x05, 0x8330 },
2751 { 0x06, 0x669a },
2752
2753 { 0x1f, 0x0002 }
2754 };
2755 int val;
2756
françois romieu4da19632011-01-03 15:07:55 +00002757 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002758
françois romieu4da19632011-01-03 15:07:55 +00002759 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002760 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002761 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002762 0x0065, 0x0066, 0x0067, 0x0068,
2763 0x0069, 0x006a, 0x006b, 0x006c
2764 };
2765 int i;
2766
françois romieu4da19632011-01-03 15:07:55 +00002767 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002768
2769 val &= 0xff00;
2770 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002771 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002772 }
2773 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002774 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002775 { 0x1f, 0x0002 },
2776 { 0x05, 0x2642 },
2777 { 0x1f, 0x0005 },
2778 { 0x05, 0x8330 },
2779 { 0x06, 0x2642 }
2780 };
2781
françois romieu4da19632011-01-03 15:07:55 +00002782 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002783 }
2784
françois romieubca03d52011-01-03 15:07:31 +00002785 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002786 rtl_writephy(tp, 0x1f, 0x0002);
2787 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2788 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002789
françois romieubca03d52011-01-03 15:07:31 +00002790 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002791 rtl_writephy(tp, 0x1f, 0x0002);
2792 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002793
françois romieu4da19632011-01-03 15:07:55 +00002794 rtl_writephy(tp, 0x1f, 0x0005);
2795 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002796
2797 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002798
françois romieu4da19632011-01-03 15:07:55 +00002799 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002800}
2801
françois romieu4da19632011-01-03 15:07:55 +00002802static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002803{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002804 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002805 { 0x1f, 0x0002 },
2806 { 0x10, 0x0008 },
2807 { 0x0d, 0x006c },
2808
2809 { 0x1f, 0x0000 },
2810 { 0x0d, 0xf880 },
2811
2812 { 0x1f, 0x0001 },
2813 { 0x17, 0x0cc0 },
2814
2815 { 0x1f, 0x0001 },
2816 { 0x0b, 0xa4d8 },
2817 { 0x09, 0x281c },
2818 { 0x07, 0x2883 },
2819 { 0x0a, 0x6b35 },
2820 { 0x1d, 0x3da4 },
2821 { 0x1c, 0xeffd },
2822 { 0x14, 0x7f52 },
2823 { 0x18, 0x7fc6 },
2824 { 0x08, 0x0601 },
2825 { 0x06, 0x4063 },
2826 { 0x10, 0xf074 },
2827 { 0x1f, 0x0003 },
2828 { 0x13, 0x0789 },
2829 { 0x12, 0xf4bd },
2830 { 0x1a, 0x04fd },
2831 { 0x14, 0x84b0 },
2832 { 0x1f, 0x0000 },
2833 { 0x00, 0x9200 },
2834
2835 { 0x1f, 0x0005 },
2836 { 0x01, 0x0340 },
2837 { 0x1f, 0x0001 },
2838 { 0x04, 0x4000 },
2839 { 0x03, 0x1d21 },
2840 { 0x02, 0x0c32 },
2841 { 0x01, 0x0200 },
2842 { 0x00, 0x5554 },
2843 { 0x04, 0x4800 },
2844 { 0x04, 0x4000 },
2845 { 0x04, 0xf000 },
2846 { 0x03, 0xdf01 },
2847 { 0x02, 0xdf20 },
2848 { 0x01, 0x101a },
2849 { 0x00, 0xa0ff },
2850 { 0x04, 0xf800 },
2851 { 0x04, 0xf000 },
2852 { 0x1f, 0x0000 },
2853
2854 { 0x1f, 0x0007 },
2855 { 0x1e, 0x0023 },
2856 { 0x16, 0x0000 },
2857 { 0x1f, 0x0000 }
2858 };
2859
françois romieu4da19632011-01-03 15:07:55 +00002860 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002861}
2862
françois romieue6de30d2011-01-03 15:08:37 +00002863static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2864{
2865 static const struct phy_reg phy_reg_init[] = {
2866 { 0x1f, 0x0001 },
2867 { 0x17, 0x0cc0 },
2868
2869 { 0x1f, 0x0007 },
2870 { 0x1e, 0x002d },
2871 { 0x18, 0x0040 },
2872 { 0x1f, 0x0000 }
2873 };
2874
2875 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2876 rtl_patchphy(tp, 0x0d, 1 << 5);
2877}
2878
Hayes Wang70090422011-07-06 15:58:06 +08002879static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002880{
2881 static const struct phy_reg phy_reg_init[] = {
2882 /* Enable Delay cap */
2883 { 0x1f, 0x0005 },
2884 { 0x05, 0x8b80 },
2885 { 0x06, 0xc896 },
2886 { 0x1f, 0x0000 },
2887
2888 /* Channel estimation fine tune */
2889 { 0x1f, 0x0001 },
2890 { 0x0b, 0x6c20 },
2891 { 0x07, 0x2872 },
2892 { 0x1c, 0xefff },
2893 { 0x1f, 0x0003 },
2894 { 0x14, 0x6420 },
2895 { 0x1f, 0x0000 },
2896
2897 /* Update PFM & 10M TX idle timer */
2898 { 0x1f, 0x0007 },
2899 { 0x1e, 0x002f },
2900 { 0x15, 0x1919 },
2901 { 0x1f, 0x0000 },
2902
2903 { 0x1f, 0x0007 },
2904 { 0x1e, 0x00ac },
2905 { 0x18, 0x0006 },
2906 { 0x1f, 0x0000 }
2907 };
2908
Francois Romieu15ecd032011-04-27 13:52:22 -07002909 rtl_apply_firmware(tp);
2910
hayeswang01dc7fe2011-03-21 01:50:28 +00002911 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2912
2913 /* DCO enable for 10M IDLE Power */
2914 rtl_writephy(tp, 0x1f, 0x0007);
2915 rtl_writephy(tp, 0x1e, 0x0023);
2916 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2917 rtl_writephy(tp, 0x1f, 0x0000);
2918
2919 /* For impedance matching */
2920 rtl_writephy(tp, 0x1f, 0x0002);
2921 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002922 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002923
2924 /* PHY auto speed down */
2925 rtl_writephy(tp, 0x1f, 0x0007);
2926 rtl_writephy(tp, 0x1e, 0x002d);
2927 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2928 rtl_writephy(tp, 0x1f, 0x0000);
2929 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2930
2931 rtl_writephy(tp, 0x1f, 0x0005);
2932 rtl_writephy(tp, 0x05, 0x8b86);
2933 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2934 rtl_writephy(tp, 0x1f, 0x0000);
2935
2936 rtl_writephy(tp, 0x1f, 0x0005);
2937 rtl_writephy(tp, 0x05, 0x8b85);
2938 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2939 rtl_writephy(tp, 0x1f, 0x0007);
2940 rtl_writephy(tp, 0x1e, 0x0020);
2941 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2942 rtl_writephy(tp, 0x1f, 0x0006);
2943 rtl_writephy(tp, 0x00, 0x5a00);
2944 rtl_writephy(tp, 0x1f, 0x0000);
2945 rtl_writephy(tp, 0x0d, 0x0007);
2946 rtl_writephy(tp, 0x0e, 0x003c);
2947 rtl_writephy(tp, 0x0d, 0x4007);
2948 rtl_writephy(tp, 0x0e, 0x0000);
2949 rtl_writephy(tp, 0x0d, 0x0000);
2950}
2951
Hayes Wang70090422011-07-06 15:58:06 +08002952static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2953{
2954 static const struct phy_reg phy_reg_init[] = {
2955 /* Enable Delay cap */
2956 { 0x1f, 0x0004 },
2957 { 0x1f, 0x0007 },
2958 { 0x1e, 0x00ac },
2959 { 0x18, 0x0006 },
2960 { 0x1f, 0x0002 },
2961 { 0x1f, 0x0000 },
2962 { 0x1f, 0x0000 },
2963
2964 /* Channel estimation fine tune */
2965 { 0x1f, 0x0003 },
2966 { 0x09, 0xa20f },
2967 { 0x1f, 0x0000 },
2968 { 0x1f, 0x0000 },
2969
2970 /* Green Setting */
2971 { 0x1f, 0x0005 },
2972 { 0x05, 0x8b5b },
2973 { 0x06, 0x9222 },
2974 { 0x05, 0x8b6d },
2975 { 0x06, 0x8000 },
2976 { 0x05, 0x8b76 },
2977 { 0x06, 0x8000 },
2978 { 0x1f, 0x0000 }
2979 };
2980
2981 rtl_apply_firmware(tp);
2982
2983 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2984
2985 /* For 4-corner performance improve */
2986 rtl_writephy(tp, 0x1f, 0x0005);
2987 rtl_writephy(tp, 0x05, 0x8b80);
2988 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2989 rtl_writephy(tp, 0x1f, 0x0000);
2990
2991 /* PHY auto speed down */
2992 rtl_writephy(tp, 0x1f, 0x0004);
2993 rtl_writephy(tp, 0x1f, 0x0007);
2994 rtl_writephy(tp, 0x1e, 0x002d);
2995 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2996 rtl_writephy(tp, 0x1f, 0x0002);
2997 rtl_writephy(tp, 0x1f, 0x0000);
2998 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2999
3000 /* improve 10M EEE waveform */
3001 rtl_writephy(tp, 0x1f, 0x0005);
3002 rtl_writephy(tp, 0x05, 0x8b86);
3003 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3004 rtl_writephy(tp, 0x1f, 0x0000);
3005
3006 /* Improve 2-pair detection performance */
3007 rtl_writephy(tp, 0x1f, 0x0005);
3008 rtl_writephy(tp, 0x05, 0x8b85);
3009 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3010 rtl_writephy(tp, 0x1f, 0x0000);
3011
3012 /* EEE setting */
3013 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
3014 ERIAR_EXGMAC);
3015 rtl_writephy(tp, 0x1f, 0x0005);
3016 rtl_writephy(tp, 0x05, 0x8b85);
3017 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3018 rtl_writephy(tp, 0x1f, 0x0004);
3019 rtl_writephy(tp, 0x1f, 0x0007);
3020 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04003021 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003022 rtl_writephy(tp, 0x1f, 0x0002);
3023 rtl_writephy(tp, 0x1f, 0x0000);
3024 rtl_writephy(tp, 0x0d, 0x0007);
3025 rtl_writephy(tp, 0x0e, 0x003c);
3026 rtl_writephy(tp, 0x0d, 0x4007);
3027 rtl_writephy(tp, 0x0e, 0x0000);
3028 rtl_writephy(tp, 0x0d, 0x0000);
3029
3030 /* Green feature */
3031 rtl_writephy(tp, 0x1f, 0x0003);
3032 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3033 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3034 rtl_writephy(tp, 0x1f, 0x0000);
3035}
3036
Hayes Wangc2218922011-09-06 16:55:18 +08003037static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3038{
3039 static const struct phy_reg phy_reg_init[] = {
3040 /* Channel estimation fine tune */
3041 { 0x1f, 0x0003 },
3042 { 0x09, 0xa20f },
3043 { 0x1f, 0x0000 },
3044
3045 /* Modify green table for giga & fnet */
3046 { 0x1f, 0x0005 },
3047 { 0x05, 0x8b55 },
3048 { 0x06, 0x0000 },
3049 { 0x05, 0x8b5e },
3050 { 0x06, 0x0000 },
3051 { 0x05, 0x8b67 },
3052 { 0x06, 0x0000 },
3053 { 0x05, 0x8b70 },
3054 { 0x06, 0x0000 },
3055 { 0x1f, 0x0000 },
3056 { 0x1f, 0x0007 },
3057 { 0x1e, 0x0078 },
3058 { 0x17, 0x0000 },
3059 { 0x19, 0x00fb },
3060 { 0x1f, 0x0000 },
3061
3062 /* Modify green table for 10M */
3063 { 0x1f, 0x0005 },
3064 { 0x05, 0x8b79 },
3065 { 0x06, 0xaa00 },
3066 { 0x1f, 0x0000 },
3067
3068 /* Disable hiimpedance detection (RTCT) */
3069 { 0x1f, 0x0003 },
3070 { 0x01, 0x328a },
3071 { 0x1f, 0x0000 }
3072 };
3073
3074 rtl_apply_firmware(tp);
3075
3076 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3077
3078 /* For 4-corner performance improve */
3079 rtl_writephy(tp, 0x1f, 0x0005);
3080 rtl_writephy(tp, 0x05, 0x8b80);
3081 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3082 rtl_writephy(tp, 0x1f, 0x0000);
3083
3084 /* PHY auto speed down */
3085 rtl_writephy(tp, 0x1f, 0x0007);
3086 rtl_writephy(tp, 0x1e, 0x002d);
3087 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3088 rtl_writephy(tp, 0x1f, 0x0000);
3089 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3090
3091 /* Improve 10M EEE waveform */
3092 rtl_writephy(tp, 0x1f, 0x0005);
3093 rtl_writephy(tp, 0x05, 0x8b86);
3094 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3095 rtl_writephy(tp, 0x1f, 0x0000);
3096
3097 /* Improve 2-pair detection performance */
3098 rtl_writephy(tp, 0x1f, 0x0005);
3099 rtl_writephy(tp, 0x05, 0x8b85);
3100 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3101 rtl_writephy(tp, 0x1f, 0x0000);
3102}
3103
3104static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3105{
3106 rtl_apply_firmware(tp);
3107
3108 /* For 4-corner performance improve */
3109 rtl_writephy(tp, 0x1f, 0x0005);
3110 rtl_writephy(tp, 0x05, 0x8b80);
3111 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3112 rtl_writephy(tp, 0x1f, 0x0000);
3113
3114 /* PHY auto speed down */
3115 rtl_writephy(tp, 0x1f, 0x0007);
3116 rtl_writephy(tp, 0x1e, 0x002d);
3117 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3118 rtl_writephy(tp, 0x1f, 0x0000);
3119 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3120
3121 /* Improve 10M EEE waveform */
3122 rtl_writephy(tp, 0x1f, 0x0005);
3123 rtl_writephy(tp, 0x05, 0x8b86);
3124 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3125 rtl_writephy(tp, 0x1f, 0x0000);
3126}
3127
françois romieu4da19632011-01-03 15:07:55 +00003128static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003129{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003130 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003131 { 0x1f, 0x0003 },
3132 { 0x08, 0x441d },
3133 { 0x01, 0x9100 },
3134 { 0x1f, 0x0000 }
3135 };
3136
françois romieu4da19632011-01-03 15:07:55 +00003137 rtl_writephy(tp, 0x1f, 0x0000);
3138 rtl_patchphy(tp, 0x11, 1 << 12);
3139 rtl_patchphy(tp, 0x19, 1 << 13);
3140 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003141
françois romieu4da19632011-01-03 15:07:55 +00003142 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003143}
3144
Hayes Wang5a5e4442011-02-22 17:26:21 +08003145static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3146{
3147 static const struct phy_reg phy_reg_init[] = {
3148 { 0x1f, 0x0005 },
3149 { 0x1a, 0x0000 },
3150 { 0x1f, 0x0000 },
3151
3152 { 0x1f, 0x0004 },
3153 { 0x1c, 0x0000 },
3154 { 0x1f, 0x0000 },
3155
3156 { 0x1f, 0x0001 },
3157 { 0x15, 0x7701 },
3158 { 0x1f, 0x0000 }
3159 };
3160
3161 /* Disable ALDPS before ram code */
3162 rtl_writephy(tp, 0x1f, 0x0000);
3163 rtl_writephy(tp, 0x18, 0x0310);
3164 msleep(100);
3165
François Romieu953a12c2011-04-24 17:38:48 +02003166 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003167
3168 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3169}
3170
Francois Romieu5615d9f2007-08-17 17:50:46 +02003171static void rtl_hw_phy_config(struct net_device *dev)
3172{
3173 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003174
3175 rtl8169_print_mac_version(tp);
3176
3177 switch (tp->mac_version) {
3178 case RTL_GIGA_MAC_VER_01:
3179 break;
3180 case RTL_GIGA_MAC_VER_02:
3181 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003182 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003183 break;
3184 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003185 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003186 break;
françois romieu2e9558562009-08-10 19:44:19 +00003187 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003188 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003189 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003190 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003191 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003192 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003193 case RTL_GIGA_MAC_VER_07:
3194 case RTL_GIGA_MAC_VER_08:
3195 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003196 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003197 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003198 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003199 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003200 break;
3201 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003202 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003203 break;
3204 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003205 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003206 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003207 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003208 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003209 break;
3210 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003211 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003212 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003213 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003214 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003215 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003216 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003217 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003218 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003219 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003220 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003221 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003222 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003223 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003224 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003225 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003226 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003227 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003228 break;
3229 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003230 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003231 break;
3232 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003233 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003234 break;
françois romieue6de30d2011-01-03 15:08:37 +00003235 case RTL_GIGA_MAC_VER_28:
3236 rtl8168d_4_hw_phy_config(tp);
3237 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003238 case RTL_GIGA_MAC_VER_29:
3239 case RTL_GIGA_MAC_VER_30:
3240 rtl8105e_hw_phy_config(tp);
3241 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003242 case RTL_GIGA_MAC_VER_31:
3243 /* None. */
3244 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003245 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003246 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003247 rtl8168e_1_hw_phy_config(tp);
3248 break;
3249 case RTL_GIGA_MAC_VER_34:
3250 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003251 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003252 case RTL_GIGA_MAC_VER_35:
3253 rtl8168f_1_hw_phy_config(tp);
3254 break;
3255 case RTL_GIGA_MAC_VER_36:
3256 rtl8168f_2_hw_phy_config(tp);
3257 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003258
Francois Romieu5615d9f2007-08-17 17:50:46 +02003259 default:
3260 break;
3261 }
3262}
3263
Francois Romieuda78dbf2012-01-26 14:18:23 +01003264static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 struct timer_list *timer = &tp->timer;
3267 void __iomem *ioaddr = tp->mmio_addr;
3268 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3269
Francois Romieubcf0bf92006-07-26 23:14:13 +02003270 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
françois romieu4da19632011-01-03 15:07:55 +00003272 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003273 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 * A busy loop could burn quite a few cycles on nowadays CPU.
3275 * Let's delay the execution of the timer for a few ticks.
3276 */
3277 timeout = HZ/10;
3278 goto out_mod_timer;
3279 }
3280
3281 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01003282 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Francois Romieuda78dbf2012-01-26 14:18:23 +01003284 netif_warn(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
françois romieu4da19632011-01-03 15:07:55 +00003286 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
3288out_mod_timer:
3289 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003290}
3291
3292static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3293{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003294 if (!test_and_set_bit(flag, tp->wk.flags))
3295 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003296}
3297
3298static void rtl8169_phy_timer(unsigned long __opaque)
3299{
3300 struct net_device *dev = (struct net_device *)__opaque;
3301 struct rtl8169_private *tp = netdev_priv(dev);
3302
Francois Romieu98ddf982012-01-31 10:47:34 +01003303 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304}
3305
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3307 void __iomem *ioaddr)
3308{
3309 iounmap(ioaddr);
3310 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003311 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 pci_disable_device(pdev);
3313 free_netdev(dev);
3314}
3315
Francois Romieubf793292006-11-01 00:53:05 +01003316static void rtl8169_phy_reset(struct net_device *dev,
3317 struct rtl8169_private *tp)
3318{
Francois Romieu07d3f512007-02-21 22:40:46 +01003319 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003320
françois romieu4da19632011-01-03 15:07:55 +00003321 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003322 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003323 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003324 return;
3325 msleep(1);
3326 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003327 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003328}
3329
David S. Miller8decf862011-09-22 03:23:13 -04003330static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3331{
3332 void __iomem *ioaddr = tp->mmio_addr;
3333
3334 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3335 (RTL_R8(PHYstatus) & TBI_Enable);
3336}
3337
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003338static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003340 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003341
Francois Romieu5615d9f2007-08-17 17:50:46 +02003342 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003343
Marcus Sundberg773328942008-07-10 21:28:08 +02003344 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3345 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3346 RTL_W8(0x82, 0x01);
3347 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003348
Francois Romieu6dccd162007-02-13 23:38:05 +01003349 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3350
3351 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3352 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003353
Francois Romieubcf0bf92006-07-26 23:14:13 +02003354 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003355 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3356 RTL_W8(0x82, 0x01);
3357 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003358 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003359 }
3360
Francois Romieubf793292006-11-01 00:53:05 +01003361 rtl8169_phy_reset(dev, tp);
3362
Oliver Neukum54405cd2011-01-06 21:55:13 +01003363 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003364 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3365 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3366 (tp->mii.supports_gmii ?
3367 ADVERTISED_1000baseT_Half |
3368 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003369
David S. Miller8decf862011-09-22 03:23:13 -04003370 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003371 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003372}
3373
Francois Romieu773d2022007-01-31 23:47:43 +01003374static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3375{
3376 void __iomem *ioaddr = tp->mmio_addr;
3377 u32 high;
3378 u32 low;
3379
3380 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3381 high = addr[4] | (addr[5] << 8);
3382
Francois Romieuda78dbf2012-01-26 14:18:23 +01003383 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003384
3385 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003386
Francois Romieu773d2022007-01-31 23:47:43 +01003387 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00003388 RTL_R32(MAC4);
3389
Francois Romieu78f1cd02010-03-27 19:35:46 -07003390 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00003391 RTL_R32(MAC0);
3392
françois romieuc28aa382011-08-02 03:53:43 +00003393 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3394 const struct exgmac_reg e[] = {
3395 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3396 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3397 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3398 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3399 low >> 16 },
3400 };
3401
3402 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3403 }
3404
Francois Romieu773d2022007-01-31 23:47:43 +01003405 RTL_W8(Cfg9346, Cfg9346_Lock);
3406
Francois Romieuda78dbf2012-01-26 14:18:23 +01003407 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003408}
3409
3410static int rtl_set_mac_address(struct net_device *dev, void *p)
3411{
3412 struct rtl8169_private *tp = netdev_priv(dev);
3413 struct sockaddr *addr = p;
3414
3415 if (!is_valid_ether_addr(addr->sa_data))
3416 return -EADDRNOTAVAIL;
3417
3418 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3419
3420 rtl_rar_set(tp, dev->dev_addr);
3421
3422 return 0;
3423}
3424
Francois Romieu5f787a12006-08-17 13:02:36 +02003425static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3426{
3427 struct rtl8169_private *tp = netdev_priv(dev);
3428 struct mii_ioctl_data *data = if_mii(ifr);
3429
Francois Romieu8b4ab282008-11-19 22:05:25 -08003430 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3431}
Francois Romieu5f787a12006-08-17 13:02:36 +02003432
Francois Romieucecb5fd2011-04-01 10:21:07 +02003433static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3434 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003435{
Francois Romieu5f787a12006-08-17 13:02:36 +02003436 switch (cmd) {
3437 case SIOCGMIIPHY:
3438 data->phy_id = 32; /* Internal PHY */
3439 return 0;
3440
3441 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003442 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003443 return 0;
3444
3445 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003446 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003447 return 0;
3448 }
3449 return -EOPNOTSUPP;
3450}
3451
Francois Romieu8b4ab282008-11-19 22:05:25 -08003452static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3453{
3454 return -EOPNOTSUPP;
3455}
3456
Francois Romieufbac58f2007-10-04 22:51:38 +02003457static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3458{
3459 if (tp->features & RTL_FEATURE_MSI) {
3460 pci_disable_msi(pdev);
3461 tp->features &= ~RTL_FEATURE_MSI;
3462 }
3463}
3464
françois romieuc0e45c12011-01-03 15:08:04 +00003465static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3466{
3467 struct mdio_ops *ops = &tp->mdio_ops;
3468
3469 switch (tp->mac_version) {
3470 case RTL_GIGA_MAC_VER_27:
3471 ops->write = r8168dp_1_mdio_write;
3472 ops->read = r8168dp_1_mdio_read;
3473 break;
françois romieue6de30d2011-01-03 15:08:37 +00003474 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003475 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003476 ops->write = r8168dp_2_mdio_write;
3477 ops->read = r8168dp_2_mdio_read;
3478 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003479 default:
3480 ops->write = r8169_mdio_write;
3481 ops->read = r8169_mdio_read;
3482 break;
3483 }
3484}
3485
Hayes Wang880f56d2013-04-13 12:26:32 +02003486static void rtl_speed_down(struct rtl8169_private *tp)
3487{
3488 u32 adv;
3489 int lpa;
3490
3491 rtl_writephy(tp, 0x1f, 0x0000);
3492 lpa = rtl_readphy(tp, MII_LPA);
3493
3494 if (lpa & (LPA_10HALF | LPA_10FULL))
3495 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
3496 else if (lpa & (LPA_100HALF | LPA_100FULL))
3497 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3498 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
3499 else
3500 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3501 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3502 (tp->mii.supports_gmii ?
3503 ADVERTISED_1000baseT_Half |
3504 ADVERTISED_1000baseT_Full : 0);
3505
3506 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
3507 adv);
3508}
3509
David S. Miller1805b2f2011-10-24 18:18:09 -04003510static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3511{
3512 void __iomem *ioaddr = tp->mmio_addr;
3513
3514 switch (tp->mac_version) {
Cyril Bruleboiscfc2c992012-10-31 14:00:46 +00003515 case RTL_GIGA_MAC_VER_25:
3516 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04003517 case RTL_GIGA_MAC_VER_29:
3518 case RTL_GIGA_MAC_VER_30:
3519 case RTL_GIGA_MAC_VER_32:
3520 case RTL_GIGA_MAC_VER_33:
3521 case RTL_GIGA_MAC_VER_34:
3522 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3523 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3524 break;
3525 default:
3526 break;
3527 }
3528}
3529
3530static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3531{
3532 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3533 return false;
3534
Hayes Wang880f56d2013-04-13 12:26:32 +02003535 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04003536 rtl_wol_suspend_quirk(tp);
3537
3538 return true;
3539}
3540
françois romieu065c27c2011-01-03 15:08:12 +00003541static void r810x_phy_power_down(struct rtl8169_private *tp)
3542{
3543 rtl_writephy(tp, 0x1f, 0x0000);
3544 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3545}
3546
3547static void r810x_phy_power_up(struct rtl8169_private *tp)
3548{
3549 rtl_writephy(tp, 0x1f, 0x0000);
3550 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3551}
3552
3553static void r810x_pll_power_down(struct rtl8169_private *tp)
3554{
David S. Miller1805b2f2011-10-24 18:18:09 -04003555 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003556 return;
françois romieu065c27c2011-01-03 15:08:12 +00003557
3558 r810x_phy_power_down(tp);
3559}
3560
3561static void r810x_pll_power_up(struct rtl8169_private *tp)
3562{
3563 r810x_phy_power_up(tp);
3564}
3565
3566static void r8168_phy_power_up(struct rtl8169_private *tp)
3567{
3568 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003569 switch (tp->mac_version) {
3570 case RTL_GIGA_MAC_VER_11:
3571 case RTL_GIGA_MAC_VER_12:
3572 case RTL_GIGA_MAC_VER_17:
3573 case RTL_GIGA_MAC_VER_18:
3574 case RTL_GIGA_MAC_VER_19:
3575 case RTL_GIGA_MAC_VER_20:
3576 case RTL_GIGA_MAC_VER_21:
3577 case RTL_GIGA_MAC_VER_22:
3578 case RTL_GIGA_MAC_VER_23:
3579 case RTL_GIGA_MAC_VER_24:
3580 case RTL_GIGA_MAC_VER_25:
3581 case RTL_GIGA_MAC_VER_26:
3582 case RTL_GIGA_MAC_VER_27:
3583 case RTL_GIGA_MAC_VER_28:
3584 case RTL_GIGA_MAC_VER_31:
3585 rtl_writephy(tp, 0x0e, 0x0000);
3586 break;
3587 default:
3588 break;
3589 }
françois romieu065c27c2011-01-03 15:08:12 +00003590 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3591}
3592
3593static void r8168_phy_power_down(struct rtl8169_private *tp)
3594{
3595 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003596 switch (tp->mac_version) {
3597 case RTL_GIGA_MAC_VER_32:
3598 case RTL_GIGA_MAC_VER_33:
3599 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3600 break;
3601
3602 case RTL_GIGA_MAC_VER_11:
3603 case RTL_GIGA_MAC_VER_12:
3604 case RTL_GIGA_MAC_VER_17:
3605 case RTL_GIGA_MAC_VER_18:
3606 case RTL_GIGA_MAC_VER_19:
3607 case RTL_GIGA_MAC_VER_20:
3608 case RTL_GIGA_MAC_VER_21:
3609 case RTL_GIGA_MAC_VER_22:
3610 case RTL_GIGA_MAC_VER_23:
3611 case RTL_GIGA_MAC_VER_24:
3612 case RTL_GIGA_MAC_VER_25:
3613 case RTL_GIGA_MAC_VER_26:
3614 case RTL_GIGA_MAC_VER_27:
3615 case RTL_GIGA_MAC_VER_28:
3616 case RTL_GIGA_MAC_VER_31:
3617 rtl_writephy(tp, 0x0e, 0x0200);
3618 default:
3619 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3620 break;
3621 }
françois romieu065c27c2011-01-03 15:08:12 +00003622}
3623
3624static void r8168_pll_power_down(struct rtl8169_private *tp)
3625{
3626 void __iomem *ioaddr = tp->mmio_addr;
3627
Francois Romieucecb5fd2011-04-01 10:21:07 +02003628 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3629 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3630 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003631 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003632 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003633 }
françois romieu065c27c2011-01-03 15:08:12 +00003634
Francois Romieucecb5fd2011-04-01 10:21:07 +02003635 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3636 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003637 (RTL_R16(CPlusCmd) & ASF)) {
3638 return;
3639 }
3640
hayeswang01dc7fe2011-03-21 01:50:28 +00003641 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3642 tp->mac_version == RTL_GIGA_MAC_VER_33)
3643 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3644
David S. Miller1805b2f2011-10-24 18:18:09 -04003645 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003646 return;
françois romieu065c27c2011-01-03 15:08:12 +00003647
3648 r8168_phy_power_down(tp);
3649
3650 switch (tp->mac_version) {
3651 case RTL_GIGA_MAC_VER_25:
3652 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003653 case RTL_GIGA_MAC_VER_27:
3654 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003655 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003656 case RTL_GIGA_MAC_VER_32:
3657 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003658 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3659 break;
3660 }
3661}
3662
3663static void r8168_pll_power_up(struct rtl8169_private *tp)
3664{
3665 void __iomem *ioaddr = tp->mmio_addr;
3666
Francois Romieucecb5fd2011-04-01 10:21:07 +02003667 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3668 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3669 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003670 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003671 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003672 }
françois romieu065c27c2011-01-03 15:08:12 +00003673
3674 switch (tp->mac_version) {
3675 case RTL_GIGA_MAC_VER_25:
3676 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003677 case RTL_GIGA_MAC_VER_27:
3678 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003679 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003680 case RTL_GIGA_MAC_VER_32:
3681 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003682 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3683 break;
3684 }
3685
3686 r8168_phy_power_up(tp);
3687}
3688
Francois Romieud58d46b2011-05-03 16:38:29 +02003689static void rtl_generic_op(struct rtl8169_private *tp,
3690 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00003691{
3692 if (op)
3693 op(tp);
3694}
3695
3696static void rtl_pll_power_down(struct rtl8169_private *tp)
3697{
Francois Romieud58d46b2011-05-03 16:38:29 +02003698 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00003699}
3700
3701static void rtl_pll_power_up(struct rtl8169_private *tp)
3702{
Francois Romieud58d46b2011-05-03 16:38:29 +02003703 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00003704}
3705
3706static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3707{
3708 struct pll_power_ops *ops = &tp->pll_power_ops;
3709
3710 switch (tp->mac_version) {
3711 case RTL_GIGA_MAC_VER_07:
3712 case RTL_GIGA_MAC_VER_08:
3713 case RTL_GIGA_MAC_VER_09:
3714 case RTL_GIGA_MAC_VER_10:
3715 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003716 case RTL_GIGA_MAC_VER_29:
3717 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003718 ops->down = r810x_pll_power_down;
3719 ops->up = r810x_pll_power_up;
3720 break;
3721
3722 case RTL_GIGA_MAC_VER_11:
3723 case RTL_GIGA_MAC_VER_12:
3724 case RTL_GIGA_MAC_VER_17:
3725 case RTL_GIGA_MAC_VER_18:
3726 case RTL_GIGA_MAC_VER_19:
3727 case RTL_GIGA_MAC_VER_20:
3728 case RTL_GIGA_MAC_VER_21:
3729 case RTL_GIGA_MAC_VER_22:
3730 case RTL_GIGA_MAC_VER_23:
3731 case RTL_GIGA_MAC_VER_24:
3732 case RTL_GIGA_MAC_VER_25:
3733 case RTL_GIGA_MAC_VER_26:
3734 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003735 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003736 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003737 case RTL_GIGA_MAC_VER_32:
3738 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003739 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08003740 case RTL_GIGA_MAC_VER_35:
3741 case RTL_GIGA_MAC_VER_36:
françois romieu065c27c2011-01-03 15:08:12 +00003742 ops->down = r8168_pll_power_down;
3743 ops->up = r8168_pll_power_up;
3744 break;
3745
3746 default:
3747 ops->down = NULL;
3748 ops->up = NULL;
3749 break;
3750 }
3751}
3752
Hayes Wange542a222011-07-06 15:58:04 +08003753static void rtl_init_rxcfg(struct rtl8169_private *tp)
3754{
3755 void __iomem *ioaddr = tp->mmio_addr;
3756
3757 switch (tp->mac_version) {
3758 case RTL_GIGA_MAC_VER_01:
3759 case RTL_GIGA_MAC_VER_02:
3760 case RTL_GIGA_MAC_VER_03:
3761 case RTL_GIGA_MAC_VER_04:
3762 case RTL_GIGA_MAC_VER_05:
3763 case RTL_GIGA_MAC_VER_06:
3764 case RTL_GIGA_MAC_VER_10:
3765 case RTL_GIGA_MAC_VER_11:
3766 case RTL_GIGA_MAC_VER_12:
3767 case RTL_GIGA_MAC_VER_13:
3768 case RTL_GIGA_MAC_VER_14:
3769 case RTL_GIGA_MAC_VER_15:
3770 case RTL_GIGA_MAC_VER_16:
3771 case RTL_GIGA_MAC_VER_17:
3772 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3773 break;
3774 case RTL_GIGA_MAC_VER_18:
3775 case RTL_GIGA_MAC_VER_19:
3776 case RTL_GIGA_MAC_VER_20:
3777 case RTL_GIGA_MAC_VER_21:
3778 case RTL_GIGA_MAC_VER_22:
3779 case RTL_GIGA_MAC_VER_23:
3780 case RTL_GIGA_MAC_VER_24:
Francois Romieu6418cc42012-06-20 12:09:18 +00003781 case RTL_GIGA_MAC_VER_34:
Hayes Wange542a222011-07-06 15:58:04 +08003782 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3783 break;
3784 default:
3785 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3786 break;
3787 }
3788}
3789
Hayes Wang92fc43b2011-07-06 15:58:03 +08003790static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3791{
3792 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3793}
3794
Francois Romieud58d46b2011-05-03 16:38:29 +02003795static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3796{
françois romieu9c5028e2012-03-02 04:43:14 +00003797 void __iomem *ioaddr = tp->mmio_addr;
3798
3799 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003800 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00003801 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003802}
3803
3804static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3805{
françois romieu9c5028e2012-03-02 04:43:14 +00003806 void __iomem *ioaddr = tp->mmio_addr;
3807
3808 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003809 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00003810 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003811}
3812
3813static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3814{
3815 void __iomem *ioaddr = tp->mmio_addr;
3816
3817 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3818 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3819 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3820}
3821
3822static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3823{
3824 void __iomem *ioaddr = tp->mmio_addr;
3825
3826 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3827 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3828 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3829}
3830
3831static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3832{
3833 void __iomem *ioaddr = tp->mmio_addr;
3834
3835 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3836}
3837
3838static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3839{
3840 void __iomem *ioaddr = tp->mmio_addr;
3841
3842 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3843}
3844
3845static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3846{
3847 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003848
3849 RTL_W8(MaxTxPacketSize, 0x3f);
3850 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3851 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003852 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003853}
3854
3855static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3856{
3857 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003858
3859 RTL_W8(MaxTxPacketSize, 0x0c);
3860 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3861 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003862 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003863}
3864
3865static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3866{
3867 rtl_tx_performance_tweak(tp->pci_dev,
3868 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3869}
3870
3871static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3872{
3873 rtl_tx_performance_tweak(tp->pci_dev,
3874 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3875}
3876
3877static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3878{
3879 void __iomem *ioaddr = tp->mmio_addr;
3880
3881 r8168b_0_hw_jumbo_enable(tp);
3882
3883 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
3884}
3885
3886static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3887{
3888 void __iomem *ioaddr = tp->mmio_addr;
3889
3890 r8168b_0_hw_jumbo_disable(tp);
3891
3892 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3893}
3894
3895static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
3896{
3897 struct jumbo_ops *ops = &tp->jumbo_ops;
3898
3899 switch (tp->mac_version) {
3900 case RTL_GIGA_MAC_VER_11:
3901 ops->disable = r8168b_0_hw_jumbo_disable;
3902 ops->enable = r8168b_0_hw_jumbo_enable;
3903 break;
3904 case RTL_GIGA_MAC_VER_12:
3905 case RTL_GIGA_MAC_VER_17:
3906 ops->disable = r8168b_1_hw_jumbo_disable;
3907 ops->enable = r8168b_1_hw_jumbo_enable;
3908 break;
3909 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
3910 case RTL_GIGA_MAC_VER_19:
3911 case RTL_GIGA_MAC_VER_20:
3912 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
3913 case RTL_GIGA_MAC_VER_22:
3914 case RTL_GIGA_MAC_VER_23:
3915 case RTL_GIGA_MAC_VER_24:
3916 case RTL_GIGA_MAC_VER_25:
3917 case RTL_GIGA_MAC_VER_26:
3918 ops->disable = r8168c_hw_jumbo_disable;
3919 ops->enable = r8168c_hw_jumbo_enable;
3920 break;
3921 case RTL_GIGA_MAC_VER_27:
3922 case RTL_GIGA_MAC_VER_28:
3923 ops->disable = r8168dp_hw_jumbo_disable;
3924 ops->enable = r8168dp_hw_jumbo_enable;
3925 break;
3926 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
3927 case RTL_GIGA_MAC_VER_32:
3928 case RTL_GIGA_MAC_VER_33:
3929 case RTL_GIGA_MAC_VER_34:
3930 ops->disable = r8168e_hw_jumbo_disable;
3931 ops->enable = r8168e_hw_jumbo_enable;
3932 break;
3933
3934 /*
3935 * No action needed for jumbo frames with 8169.
3936 * No jumbo for 810x at all.
3937 */
3938 default:
3939 ops->disable = NULL;
3940 ops->enable = NULL;
3941 break;
3942 }
3943}
3944
Francois Romieu6f43adc2011-04-29 15:05:51 +02003945static void rtl_hw_reset(struct rtl8169_private *tp)
3946{
3947 void __iomem *ioaddr = tp->mmio_addr;
3948 int i;
3949
3950 /* Soft reset the chip. */
3951 RTL_W8(ChipCmd, CmdReset);
3952
3953 /* Check that the chip has finished the reset. */
3954 for (i = 0; i < 100; i++) {
3955 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3956 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003957 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003958 }
3959}
3960
Francois Romieub6ffd972011-06-17 17:00:05 +02003961static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
3962{
3963 struct rtl_fw *rtl_fw;
3964 const char *name;
3965 int rc = -ENOMEM;
3966
3967 name = rtl_lookup_firmware_name(tp);
3968 if (!name)
3969 goto out_no_firmware;
3970
3971 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
3972 if (!rtl_fw)
3973 goto err_warn;
3974
3975 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
3976 if (rc < 0)
3977 goto err_free;
3978
Francois Romieufd112f22011-06-18 00:10:29 +02003979 rc = rtl_check_firmware(tp, rtl_fw);
3980 if (rc < 0)
3981 goto err_release_firmware;
3982
Francois Romieub6ffd972011-06-17 17:00:05 +02003983 tp->rtl_fw = rtl_fw;
3984out:
3985 return;
3986
Francois Romieufd112f22011-06-18 00:10:29 +02003987err_release_firmware:
3988 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02003989err_free:
3990 kfree(rtl_fw);
3991err_warn:
3992 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
3993 name, rc);
3994out_no_firmware:
3995 tp->rtl_fw = NULL;
3996 goto out;
3997}
3998
François Romieu953a12c2011-04-24 17:38:48 +02003999static void rtl_request_firmware(struct rtl8169_private *tp)
4000{
Francois Romieub6ffd972011-06-17 17:00:05 +02004001 if (IS_ERR(tp->rtl_fw))
4002 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004003}
4004
Hayes Wang92fc43b2011-07-06 15:58:03 +08004005static void rtl_rx_close(struct rtl8169_private *tp)
4006{
4007 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004008
Francois Romieu1687b562011-07-19 17:21:29 +02004009 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004010}
4011
françois romieue6de30d2011-01-03 15:08:37 +00004012static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013{
françois romieue6de30d2011-01-03 15:08:37 +00004014 void __iomem *ioaddr = tp->mmio_addr;
4015
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004017 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018
Hayes Wang92fc43b2011-07-06 15:58:03 +08004019 rtl_rx_close(tp);
4020
Hayes Wang5d2e1952011-02-22 17:26:22 +08004021 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004022 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4023 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00004024 while (RTL_R8(TxPoll) & NPQ)
4025 udelay(20);
Hayes Wangc2218922011-09-06 16:55:18 +08004026 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4027 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
4028 tp->mac_version == RTL_GIGA_MAC_VER_36) {
David S. Miller8decf862011-09-22 03:23:13 -04004029 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08004030 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4031 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004032 } else {
4033 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4034 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004035 }
4036
Hayes Wang92fc43b2011-07-06 15:58:03 +08004037 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038}
4039
Francois Romieu7f796d82007-06-11 23:04:41 +02004040static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004041{
4042 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004043
4044 /* Set DMA burst size and Interframe Gap Time */
4045 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4046 (InterFrameGap << TxInterFrameGapShift));
4047}
4048
Francois Romieu07ce4062007-02-23 23:36:39 +01004049static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050{
4051 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
Francois Romieu07ce4062007-02-23 23:36:39 +01004053 tp->hw_start(dev);
4054
Francois Romieuda78dbf2012-01-26 14:18:23 +01004055 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004056}
4057
Francois Romieu7f796d82007-06-11 23:04:41 +02004058static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4059 void __iomem *ioaddr)
4060{
4061 /*
4062 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4063 * register to be written before TxDescAddrLow to work.
4064 * Switching from MMIO to I/O access fixes the issue as well.
4065 */
4066 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004067 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02004068 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004069 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02004070}
4071
4072static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4073{
4074 u16 cmd;
4075
4076 cmd = RTL_R16(CPlusCmd);
4077 RTL_W16(CPlusCmd, cmd);
4078 return cmd;
4079}
4080
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004081static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d82007-06-11 23:04:41 +02004082{
4083 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00004084 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d82007-06-11 23:04:41 +02004085}
4086
Francois Romieu6dccd162007-02-13 23:38:05 +01004087static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4088{
Francois Romieu37441002011-06-17 22:58:54 +02004089 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004090 u32 mac_version;
4091 u32 clk;
4092 u32 val;
4093 } cfg2_info [] = {
4094 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4095 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4096 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4097 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004098 };
4099 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004100 unsigned int i;
4101 u32 clk;
4102
4103 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004104 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004105 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4106 RTL_W32(0x7c, p->val);
4107 break;
4108 }
4109 }
4110}
4111
Francois Romieue6b763e2012-03-08 09:35:39 +01004112static void rtl_set_rx_mode(struct net_device *dev)
4113{
4114 struct rtl8169_private *tp = netdev_priv(dev);
4115 void __iomem *ioaddr = tp->mmio_addr;
4116 u32 mc_filter[2]; /* Multicast hash filter */
4117 int rx_mode;
4118 u32 tmp = 0;
4119
4120 if (dev->flags & IFF_PROMISC) {
4121 /* Unconditionally log net taps. */
4122 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4123 rx_mode =
4124 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4125 AcceptAllPhys;
4126 mc_filter[1] = mc_filter[0] = 0xffffffff;
4127 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4128 (dev->flags & IFF_ALLMULTI)) {
4129 /* Too many to filter perfectly -- accept all multicasts. */
4130 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4131 mc_filter[1] = mc_filter[0] = 0xffffffff;
4132 } else {
4133 struct netdev_hw_addr *ha;
4134
4135 rx_mode = AcceptBroadcast | AcceptMyPhys;
4136 mc_filter[1] = mc_filter[0] = 0;
4137 netdev_for_each_mc_addr(ha, dev) {
4138 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4139 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4140 rx_mode |= AcceptMulticast;
4141 }
4142 }
4143
4144 if (dev->features & NETIF_F_RXALL)
4145 rx_mode |= (AcceptErr | AcceptRunt);
4146
4147 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4148
4149 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4150 u32 data = mc_filter[0];
4151
4152 mc_filter[0] = swab32(mc_filter[1]);
4153 mc_filter[1] = swab32(data);
4154 }
4155
Nathan Walp1b10e0b2012-11-01 12:08:47 +00004156 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
4157 mc_filter[1] = mc_filter[0] = 0xffffffff;
4158
Francois Romieue6b763e2012-03-08 09:35:39 +01004159 RTL_W32(MAR0 + 4, mc_filter[1]);
4160 RTL_W32(MAR0 + 0, mc_filter[0]);
4161
4162 RTL_W32(RxConfig, tmp);
4163}
4164
Francois Romieu07ce4062007-02-23 23:36:39 +01004165static void rtl_hw_start_8169(struct net_device *dev)
4166{
4167 struct rtl8169_private *tp = netdev_priv(dev);
4168 void __iomem *ioaddr = tp->mmio_addr;
4169 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004170
Francois Romieu9cb427b2006-11-02 00:10:16 +01004171 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4172 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4173 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4174 }
4175
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004177 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4178 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4179 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4180 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004181 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4182
Hayes Wange542a222011-07-06 15:58:04 +08004183 rtl_init_rxcfg(tp);
4184
françois romieuf0298f82011-01-03 15:07:42 +00004185 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004187 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
Francois Romieucecb5fd2011-04-01 10:21:07 +02004189 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4190 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4191 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4192 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004193 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194
Francois Romieu7f796d82007-06-11 23:04:41 +02004195 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004196
Francois Romieucecb5fd2011-04-01 10:21:07 +02004197 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4198 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004199 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004201 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 }
4203
Francois Romieubcf0bf92006-07-26 23:14:13 +02004204 RTL_W16(CPlusCmd, tp->cp_cmd);
4205
Francois Romieu6dccd162007-02-13 23:38:05 +01004206 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4207
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 /*
4209 * Undocumented corner. Supposedly:
4210 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4211 */
4212 RTL_W16(IntrMitigate, 0x0000);
4213
Francois Romieu7f796d82007-06-11 23:04:41 +02004214 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004215
Francois Romieucecb5fd2011-04-01 10:21:07 +02004216 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4217 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4218 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4219 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004220 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4221 rtl_set_rx_tx_config_registers(tp);
4222 }
4223
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004225
4226 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4227 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228
4229 RTL_W32(RxMissed, 0);
4230
Francois Romieu07ce4062007-02-23 23:36:39 +01004231 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232
4233 /* no early-rx interrupts */
4234 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004235}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
françois romieu650e8d52011-01-03 15:08:29 +00004237static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004238{
4239 u32 csi;
4240
4241 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00004242 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4243}
4244
françois romieue6de30d2011-01-03 15:08:37 +00004245static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4246{
4247 rtl_csi_access_enable(ioaddr, 0x17000000);
4248}
4249
françois romieu650e8d52011-01-03 15:08:29 +00004250static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4251{
4252 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02004253}
4254
4255struct ephy_info {
4256 unsigned int offset;
4257 u16 mask;
4258 u16 bits;
4259};
4260
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004261static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004262{
4263 u16 w;
4264
4265 while (len-- > 0) {
4266 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4267 rtl_ephy_write(ioaddr, e->offset, w);
4268 e++;
4269 }
4270}
4271
Francois Romieub726e492008-06-28 12:22:59 +02004272static void rtl_disable_clock_request(struct pci_dev *pdev)
4273{
Jon Masone44daad2011-06-27 07:46:31 +00004274 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004275
4276 if (cap) {
4277 u16 ctl;
4278
4279 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4280 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4281 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4282 }
4283}
4284
françois romieue6de30d2011-01-03 15:08:37 +00004285static void rtl_enable_clock_request(struct pci_dev *pdev)
4286{
Jon Masone44daad2011-06-27 07:46:31 +00004287 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004288
4289 if (cap) {
4290 u16 ctl;
4291
4292 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4293 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4294 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4295 }
4296}
4297
Francois Romieub726e492008-06-28 12:22:59 +02004298#define R8168_CPCMD_QUIRK_MASK (\
4299 EnableBist | \
4300 Mac_dbgo_oe | \
4301 Force_half_dup | \
4302 Force_rxflow_en | \
4303 Force_txflow_en | \
4304 Cxpl_dbg_sel | \
4305 ASF | \
4306 PktCntrDisable | \
4307 Mac_dbgo_sel)
4308
Francois Romieu219a1e92008-06-28 11:58:39 +02004309static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4310{
Francois Romieub726e492008-06-28 12:22:59 +02004311 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4312
4313 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4314
Francois Romieu2e68ae42008-06-28 12:00:55 +02004315 rtl_tx_performance_tweak(pdev,
4316 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004317}
4318
4319static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4320{
4321 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004322
françois romieuf0298f82011-01-03 15:07:42 +00004323 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004324
4325 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004326}
4327
4328static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4329{
Francois Romieub726e492008-06-28 12:22:59 +02004330 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4331
4332 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4333
Francois Romieu219a1e92008-06-28 11:58:39 +02004334 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004335
4336 rtl_disable_clock_request(pdev);
4337
4338 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004339}
4340
Francois Romieuef3386f2008-06-29 12:24:30 +02004341static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004342{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004343 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004344 { 0x01, 0, 0x0001 },
4345 { 0x02, 0x0800, 0x1000 },
4346 { 0x03, 0, 0x0042 },
4347 { 0x06, 0x0080, 0x0000 },
4348 { 0x07, 0, 0x2000 }
4349 };
4350
françois romieu650e8d52011-01-03 15:08:29 +00004351 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004352
4353 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4354
Francois Romieu219a1e92008-06-28 11:58:39 +02004355 __rtl_hw_start_8168cp(ioaddr, pdev);
4356}
4357
Francois Romieuef3386f2008-06-29 12:24:30 +02004358static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4359{
françois romieu650e8d52011-01-03 15:08:29 +00004360 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004361
4362 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4363
4364 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4365
4366 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4367}
4368
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004369static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4370{
françois romieu650e8d52011-01-03 15:08:29 +00004371 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004372
4373 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4374
4375 /* Magic. */
4376 RTL_W8(DBG_REG, 0x20);
4377
françois romieuf0298f82011-01-03 15:07:42 +00004378 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004379
4380 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4381
4382 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4383}
4384
Francois Romieu219a1e92008-06-28 11:58:39 +02004385static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4386{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004387 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004388 { 0x02, 0x0800, 0x1000 },
4389 { 0x03, 0, 0x0002 },
4390 { 0x06, 0x0080, 0x0000 }
4391 };
4392
françois romieu650e8d52011-01-03 15:08:29 +00004393 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004394
4395 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4396
4397 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4398
Francois Romieu219a1e92008-06-28 11:58:39 +02004399 __rtl_hw_start_8168cp(ioaddr, pdev);
4400}
4401
4402static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4403{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004404 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004405 { 0x01, 0, 0x0001 },
4406 { 0x03, 0x0400, 0x0220 }
4407 };
4408
françois romieu650e8d52011-01-03 15:08:29 +00004409 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004410
4411 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4412
Francois Romieu219a1e92008-06-28 11:58:39 +02004413 __rtl_hw_start_8168cp(ioaddr, pdev);
4414}
4415
Francois Romieu197ff762008-06-28 13:16:02 +02004416static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4417{
4418 rtl_hw_start_8168c_2(ioaddr, pdev);
4419}
4420
Francois Romieu6fb07052008-06-29 11:54:28 +02004421static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4422{
françois romieu650e8d52011-01-03 15:08:29 +00004423 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004424
4425 __rtl_hw_start_8168cp(ioaddr, pdev);
4426}
4427
Francois Romieu5b538df2008-07-20 16:22:45 +02004428static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4429{
françois romieu650e8d52011-01-03 15:08:29 +00004430 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004431
4432 rtl_disable_clock_request(pdev);
4433
françois romieuf0298f82011-01-03 15:07:42 +00004434 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004435
4436 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4437
4438 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4439}
4440
hayeswang4804b3b2011-03-21 01:50:29 +00004441static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4442{
4443 rtl_csi_access_enable_1(ioaddr);
4444
4445 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4446
4447 RTL_W8(MaxTxPacketSize, TxPacketMax);
4448
4449 rtl_disable_clock_request(pdev);
4450}
4451
françois romieue6de30d2011-01-03 15:08:37 +00004452static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4453{
4454 static const struct ephy_info e_info_8168d_4[] = {
4455 { 0x0b, ~0, 0x48 },
4456 { 0x19, 0x20, 0x50 },
4457 { 0x0c, ~0, 0x20 }
4458 };
4459 int i;
4460
4461 rtl_csi_access_enable_1(ioaddr);
4462
4463 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4464
4465 RTL_W8(MaxTxPacketSize, TxPacketMax);
4466
4467 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4468 const struct ephy_info *e = e_info_8168d_4 + i;
4469 u16 w;
4470
4471 w = rtl_ephy_read(ioaddr, e->offset);
4472 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4473 }
4474
4475 rtl_enable_clock_request(pdev);
4476}
4477
Hayes Wang70090422011-07-06 15:58:06 +08004478static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
hayeswang01dc7fe2011-03-21 01:50:28 +00004479{
Hayes Wang70090422011-07-06 15:58:06 +08004480 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004481 { 0x00, 0x0200, 0x0100 },
4482 { 0x00, 0x0000, 0x0004 },
4483 { 0x06, 0x0002, 0x0001 },
4484 { 0x06, 0x0000, 0x0030 },
4485 { 0x07, 0x0000, 0x2000 },
4486 { 0x00, 0x0000, 0x0020 },
4487 { 0x03, 0x5800, 0x2000 },
4488 { 0x03, 0x0000, 0x0001 },
4489 { 0x01, 0x0800, 0x1000 },
4490 { 0x07, 0x0000, 0x4000 },
4491 { 0x1e, 0x0000, 0x2000 },
4492 { 0x19, 0xffff, 0xfe6c },
4493 { 0x0a, 0x0000, 0x0040 }
4494 };
4495
4496 rtl_csi_access_enable_2(ioaddr);
4497
Hayes Wang70090422011-07-06 15:58:06 +08004498 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004499
4500 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4501
4502 RTL_W8(MaxTxPacketSize, TxPacketMax);
4503
4504 rtl_disable_clock_request(pdev);
4505
4506 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004507 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4508 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004509
Francois Romieucecb5fd2011-04-01 10:21:07 +02004510 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004511}
4512
Hayes Wang70090422011-07-06 15:58:06 +08004513static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4514{
4515 static const struct ephy_info e_info_8168e_2[] = {
4516 { 0x09, 0x0000, 0x0080 },
4517 { 0x19, 0x0000, 0x0224 }
4518 };
4519
4520 rtl_csi_access_enable_1(ioaddr);
4521
4522 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4523
4524 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4525
4526 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4527 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4528 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4529 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4530 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4531 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4532 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4533 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4534 ERIAR_EXGMAC);
4535
Hayes Wang3090bd92011-09-06 16:55:15 +08004536 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004537
4538 rtl_disable_clock_request(pdev);
4539
4540 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4541 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4542
4543 /* Adjust EEE LED frequency */
4544 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4545
4546 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4547 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4548 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4549}
4550
Hayes Wangc2218922011-09-06 16:55:18 +08004551static void rtl_hw_start_8168f_1(void __iomem *ioaddr, struct pci_dev *pdev)
4552{
4553 static const struct ephy_info e_info_8168f_1[] = {
4554 { 0x06, 0x00c0, 0x0020 },
4555 { 0x08, 0x0001, 0x0002 },
4556 { 0x09, 0x0000, 0x0080 },
4557 { 0x19, 0x0000, 0x0224 }
4558 };
4559
4560 rtl_csi_access_enable_1(ioaddr);
4561
4562 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4563
4564 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4565
4566 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4567 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4568 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4569 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4570 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4571 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4572 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4573 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4574 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4575 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
4576 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4577 ERIAR_EXGMAC);
4578
4579 RTL_W8(MaxTxPacketSize, EarlySize);
4580
4581 rtl_disable_clock_request(pdev);
4582
4583 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4584 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4585
4586 /* Adjust EEE LED frequency */
4587 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4588
4589 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4590 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4591 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4592}
4593
Francois Romieu07ce4062007-02-23 23:36:39 +01004594static void rtl_hw_start_8168(struct net_device *dev)
4595{
Francois Romieu2dd99532007-06-11 23:22:52 +02004596 struct rtl8169_private *tp = netdev_priv(dev);
4597 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004598 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004599
4600 RTL_W8(Cfg9346, Cfg9346_Unlock);
4601
françois romieuf0298f82011-01-03 15:07:42 +00004602 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004603
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004604 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004605
Francois Romieu0e485152007-02-20 00:00:26 +01004606 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004607
4608 RTL_W16(CPlusCmd, tp->cp_cmd);
4609
Francois Romieu0e485152007-02-20 00:00:26 +01004610 RTL_W16(IntrMitigate, 0x5151);
4611
4612 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00004613 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01004614 tp->event_slow |= RxFIFOOver | PCSTimeout;
4615 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01004616 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004617
4618 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4619
Francois Romieub8363902008-06-01 12:31:57 +02004620 rtl_set_rx_mode(dev);
4621
4622 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4623 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004624
4625 RTL_R8(IntrMask);
4626
Francois Romieu219a1e92008-06-28 11:58:39 +02004627 switch (tp->mac_version) {
4628 case RTL_GIGA_MAC_VER_11:
4629 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004630 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004631
4632 case RTL_GIGA_MAC_VER_12:
4633 case RTL_GIGA_MAC_VER_17:
4634 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004635 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004636
4637 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004638 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004639 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004640
4641 case RTL_GIGA_MAC_VER_19:
4642 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004643 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004644
4645 case RTL_GIGA_MAC_VER_20:
4646 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004647 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004648
Francois Romieu197ff762008-06-28 13:16:02 +02004649 case RTL_GIGA_MAC_VER_21:
4650 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004651 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004652
Francois Romieu6fb07052008-06-29 11:54:28 +02004653 case RTL_GIGA_MAC_VER_22:
4654 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004655 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004656
Francois Romieuef3386f2008-06-29 12:24:30 +02004657 case RTL_GIGA_MAC_VER_23:
4658 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004659 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004660
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004661 case RTL_GIGA_MAC_VER_24:
4662 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004663 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004664
Francois Romieu5b538df2008-07-20 16:22:45 +02004665 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004666 case RTL_GIGA_MAC_VER_26:
4667 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004668 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004669 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004670
françois romieue6de30d2011-01-03 15:08:37 +00004671 case RTL_GIGA_MAC_VER_28:
4672 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004673 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004674
hayeswang4804b3b2011-03-21 01:50:29 +00004675 case RTL_GIGA_MAC_VER_31:
4676 rtl_hw_start_8168dp(ioaddr, pdev);
4677 break;
4678
hayeswang01dc7fe2011-03-21 01:50:28 +00004679 case RTL_GIGA_MAC_VER_32:
4680 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004681 rtl_hw_start_8168e_1(ioaddr, pdev);
4682 break;
4683 case RTL_GIGA_MAC_VER_34:
4684 rtl_hw_start_8168e_2(ioaddr, pdev);
hayeswang01dc7fe2011-03-21 01:50:28 +00004685 break;
françois romieue6de30d2011-01-03 15:08:37 +00004686
Hayes Wangc2218922011-09-06 16:55:18 +08004687 case RTL_GIGA_MAC_VER_35:
4688 case RTL_GIGA_MAC_VER_36:
4689 rtl_hw_start_8168f_1(ioaddr, pdev);
4690 break;
4691
Francois Romieu219a1e92008-06-28 11:58:39 +02004692 default:
4693 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4694 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004695 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004696 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004697
Francois Romieu0e485152007-02-20 00:00:26 +01004698 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4699
Francois Romieub8363902008-06-01 12:31:57 +02004700 RTL_W8(Cfg9346, Cfg9346_Lock);
4701
Francois Romieu2dd99532007-06-11 23:22:52 +02004702 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004703}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704
Francois Romieu2857ffb2008-08-02 21:08:49 +02004705#define R810X_CPCMD_QUIRK_MASK (\
4706 EnableBist | \
4707 Mac_dbgo_oe | \
4708 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004709 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004710 Force_txflow_en | \
4711 Cxpl_dbg_sel | \
4712 ASF | \
4713 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004714 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004715
4716static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4717{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004718 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004719 { 0x01, 0, 0x6e65 },
4720 { 0x02, 0, 0x091f },
4721 { 0x03, 0, 0xc2f9 },
4722 { 0x06, 0, 0xafb5 },
4723 { 0x07, 0, 0x0e00 },
4724 { 0x19, 0, 0xec80 },
4725 { 0x01, 0, 0x2e65 },
4726 { 0x01, 0, 0x6e65 }
4727 };
4728 u8 cfg1;
4729
françois romieu650e8d52011-01-03 15:08:29 +00004730 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004731
4732 RTL_W8(DBG_REG, FIX_NAK_1);
4733
4734 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4735
4736 RTL_W8(Config1,
4737 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4738 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4739
4740 cfg1 = RTL_R8(Config1);
4741 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4742 RTL_W8(Config1, cfg1 & ~LEDS0);
4743
Francois Romieu2857ffb2008-08-02 21:08:49 +02004744 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4745}
4746
4747static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4748{
françois romieu650e8d52011-01-03 15:08:29 +00004749 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004750
4751 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4752
4753 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4754 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004755}
4756
4757static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4758{
4759 rtl_hw_start_8102e_2(ioaddr, pdev);
4760
4761 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4762}
4763
Hayes Wang5a5e4442011-02-22 17:26:21 +08004764static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4765{
4766 static const struct ephy_info e_info_8105e_1[] = {
4767 { 0x07, 0, 0x4000 },
4768 { 0x19, 0, 0x0200 },
4769 { 0x19, 0, 0x0020 },
4770 { 0x1e, 0, 0x2000 },
4771 { 0x03, 0, 0x0001 },
4772 { 0x19, 0, 0x0100 },
4773 { 0x19, 0, 0x0004 },
4774 { 0x0a, 0, 0x0020 }
4775 };
4776
Francois Romieucecb5fd2011-04-01 10:21:07 +02004777 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004778 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4779
Francois Romieucecb5fd2011-04-01 10:21:07 +02004780 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004781 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4782
4783 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e2011-07-06 15:58:02 +08004784 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004785
4786 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4787}
4788
4789static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4790{
4791 rtl_hw_start_8105e_1(ioaddr, pdev);
4792 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4793}
4794
Francois Romieu07ce4062007-02-23 23:36:39 +01004795static void rtl_hw_start_8101(struct net_device *dev)
4796{
Francois Romieucdf1a602007-06-11 23:29:50 +02004797 struct rtl8169_private *tp = netdev_priv(dev);
4798 void __iomem *ioaddr = tp->mmio_addr;
4799 struct pci_dev *pdev = tp->pci_dev;
4800
Francois Romieuda78dbf2012-01-26 14:18:23 +01004801 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
4802 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00004803
Francois Romieucecb5fd2011-04-01 10:21:07 +02004804 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4805 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00004806 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02004807
4808 if (cap) {
4809 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4810 PCI_EXP_DEVCTL_NOSNOOP_EN);
4811 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004812 }
4813
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004814 RTL_W8(Cfg9346, Cfg9346_Unlock);
4815
Francois Romieu2857ffb2008-08-02 21:08:49 +02004816 switch (tp->mac_version) {
4817 case RTL_GIGA_MAC_VER_07:
4818 rtl_hw_start_8102e_1(ioaddr, pdev);
4819 break;
4820
4821 case RTL_GIGA_MAC_VER_08:
4822 rtl_hw_start_8102e_3(ioaddr, pdev);
4823 break;
4824
4825 case RTL_GIGA_MAC_VER_09:
4826 rtl_hw_start_8102e_2(ioaddr, pdev);
4827 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004828
4829 case RTL_GIGA_MAC_VER_29:
4830 rtl_hw_start_8105e_1(ioaddr, pdev);
4831 break;
4832 case RTL_GIGA_MAC_VER_30:
4833 rtl_hw_start_8105e_2(ioaddr, pdev);
4834 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004835 }
4836
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004837 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004838
françois romieuf0298f82011-01-03 15:07:42 +00004839 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004840
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004841 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004842
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004843 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004844 RTL_W16(CPlusCmd, tp->cp_cmd);
4845
4846 RTL_W16(IntrMitigate, 0x0000);
4847
4848 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4849
4850 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4851 rtl_set_rx_tx_config_registers(tp);
4852
Francois Romieucdf1a602007-06-11 23:29:50 +02004853 RTL_R8(IntrMask);
4854
Francois Romieucdf1a602007-06-11 23:29:50 +02004855 rtl_set_rx_mode(dev);
4856
4857 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858}
4859
4860static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4861{
Francois Romieud58d46b2011-05-03 16:38:29 +02004862 struct rtl8169_private *tp = netdev_priv(dev);
4863
4864 if (new_mtu < ETH_ZLEN ||
4865 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 return -EINVAL;
4867
Francois Romieud58d46b2011-05-03 16:38:29 +02004868 if (new_mtu > ETH_DATA_LEN)
4869 rtl_hw_jumbo_enable(tp);
4870 else
4871 rtl_hw_jumbo_disable(tp);
4872
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004874 netdev_update_features(dev);
4875
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004876 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877}
4878
4879static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4880{
Al Viro95e09182007-12-22 18:55:39 +00004881 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4883}
4884
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004885static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4886 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004888 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004889 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004890
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004891 kfree(*data_buff);
4892 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 rtl8169_make_unusable_by_asic(desc);
4894}
4895
4896static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4897{
4898 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4899
4900 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4901}
4902
4903static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4904 u32 rx_buf_sz)
4905{
4906 desc->addr = cpu_to_le64(mapping);
4907 wmb();
4908 rtl8169_mark_to_asic(desc, rx_buf_sz);
4909}
4910
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004911static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004913 return (void *)ALIGN((long)data, 16);
4914}
4915
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004916static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4917 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004918{
4919 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004921 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004922 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004923 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004925 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4926 if (!data)
4927 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004928
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004929 if (rtl8169_align(data) != data) {
4930 kfree(data);
4931 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4932 if (!data)
4933 return NULL;
4934 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004935
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004936 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004937 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004938 if (unlikely(dma_mapping_error(d, mapping))) {
4939 if (net_ratelimit())
4940 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004941 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943
4944 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004945 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004946
4947err_out:
4948 kfree(data);
4949 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950}
4951
4952static void rtl8169_rx_clear(struct rtl8169_private *tp)
4953{
Francois Romieu07d3f512007-02-21 22:40:46 +01004954 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955
4956 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004957 if (tp->Rx_databuff[i]) {
4958 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 tp->RxDescArray + i);
4960 }
4961 }
4962}
4963
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004964static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004966 desc->opts1 |= cpu_to_le32(RingEnd);
4967}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004968
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004969static int rtl8169_rx_fill(struct rtl8169_private *tp)
4970{
4971 unsigned int i;
4972
4973 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004974 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004975
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004976 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004978
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004979 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004980 if (!data) {
4981 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004982 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004983 }
4984 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004987 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4988 return 0;
4989
4990err_out:
4991 rtl8169_rx_clear(tp);
4992 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993}
4994
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995static int rtl8169_init_ring(struct net_device *dev)
4996{
4997 struct rtl8169_private *tp = netdev_priv(dev);
4998
4999 rtl8169_init_ring_indexes(tp);
5000
5001 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005002 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005004 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005}
5006
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005007static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 struct TxDesc *desc)
5009{
5010 unsigned int len = tx_skb->len;
5011
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005012 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5013
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 desc->opts1 = 0x00;
5015 desc->opts2 = 0x00;
5016 desc->addr = 0x00;
5017 tx_skb->len = 0;
5018}
5019
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005020static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5021 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022{
5023 unsigned int i;
5024
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005025 for (i = 0; i < n; i++) {
5026 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 struct ring_info *tx_skb = tp->tx_skb + entry;
5028 unsigned int len = tx_skb->len;
5029
5030 if (len) {
5031 struct sk_buff *skb = tx_skb->skb;
5032
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005033 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 tp->TxDescArray + entry);
5035 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005036 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 dev_kfree_skb(skb);
5038 tx_skb->skb = NULL;
5039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 }
5041 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005042}
5043
5044static void rtl8169_tx_clear(struct rtl8169_private *tp)
5045{
5046 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 tp->cur_tx = tp->dirty_tx = 0;
5048}
5049
Francois Romieu4422bcd2012-01-26 11:23:32 +01005050static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051{
David Howellsc4028952006-11-22 14:57:56 +00005052 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005053 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054
Francois Romieuda78dbf2012-01-26 14:18:23 +01005055 napi_disable(&tp->napi);
5056 netif_stop_queue(dev);
5057 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058
françois romieuc7c2c392011-12-04 20:30:52 +00005059 rtl8169_hw_reset(tp);
5060
Francois Romieu56de4142011-03-15 17:29:31 +01005061 for (i = 0; i < NUM_RX_DESC; i++)
5062 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5063
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005065 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
Francois Romieuda78dbf2012-01-26 14:18:23 +01005067 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01005068 rtl_hw_start(dev);
5069 netif_wake_queue(dev);
5070 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071}
5072
5073static void rtl8169_tx_timeout(struct net_device *dev)
5074{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005075 struct rtl8169_private *tp = netdev_priv(dev);
5076
5077 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078}
5079
5080static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005081 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082{
5083 struct skb_shared_info *info = skb_shinfo(skb);
5084 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005085 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005086 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087
5088 entry = tp->cur_tx;
5089 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005090 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 dma_addr_t mapping;
5092 u32 status, len;
5093 void *addr;
5094
5095 entry = (entry + 1) % NUM_TX_DESC;
5096
5097 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005098 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005099 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005100 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005101 if (unlikely(dma_mapping_error(d, mapping))) {
5102 if (net_ratelimit())
5103 netif_err(tp, drv, tp->dev,
5104 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005105 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
Francois Romieucecb5fd2011-04-01 10:21:07 +02005108 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005109 status = opts[0] | len |
5110 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111
5112 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005113 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 txd->addr = cpu_to_le64(mapping);
5115
5116 tp->tx_skb[entry].len = len;
5117 }
5118
5119 if (cur_frag) {
5120 tp->tx_skb[entry].skb = skb;
5121 txd->opts1 |= cpu_to_le32(LastFrag);
5122 }
5123
5124 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005125
5126err_out:
5127 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5128 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129}
5130
Francois Romieu2b7b4312011-04-18 22:53:24 -07005131static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5132 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005134 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005135 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005136 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137
Francois Romieu2b7b4312011-04-18 22:53:24 -07005138 if (mss) {
5139 opts[0] |= TD_LSO;
5140 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5141 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005142 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143
5144 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005145 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005147 opts[offset] |= info->checksum.udp;
5148 else
5149 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151}
5152
Stephen Hemminger613573252009-08-31 19:50:58 +00005153static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5154 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155{
5156 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005157 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 struct TxDesc *txd = tp->TxDescArray + entry;
5159 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005160 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 dma_addr_t mapping;
5162 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005163 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005164 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005165
Julien Ducourthial477206a2012-05-09 00:00:06 +02005166 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005167 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005168 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 }
5170
5171 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005172 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005174 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005175 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005176 if (unlikely(dma_mapping_error(d, mapping))) {
5177 if (net_ratelimit())
5178 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005179 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181
5182 tp->tx_skb[entry].len = len;
5183 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184
Francois Romieu2b7b4312011-04-18 22:53:24 -07005185 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5186 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005187
Francois Romieu2b7b4312011-04-18 22:53:24 -07005188 rtl8169_tso_csum(tp, skb, opts);
5189
5190 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005191 if (frags < 0)
5192 goto err_dma_1;
5193 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005194 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005195 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005196 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005197 tp->tx_skb[entry].skb = skb;
5198 }
5199
Francois Romieu2b7b4312011-04-18 22:53:24 -07005200 txd->opts2 = cpu_to_le32(opts[1]);
5201
Richard Cochran5047fb52012-03-10 07:29:42 +00005202 skb_tx_timestamp(skb);
5203
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 wmb();
5205
Francois Romieucecb5fd2011-04-01 10:21:07 +02005206 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005207 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 txd->opts1 = cpu_to_le32(status);
5209
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 tp->cur_tx += frags + 1;
5211
David Dillow4c020a92010-03-03 16:33:10 +00005212 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213
Francois Romieucecb5fd2011-04-01 10:21:07 +02005214 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215
Francois Romieuda78dbf2012-01-26 14:18:23 +01005216 mmiowb();
5217
Julien Ducourthial477206a2012-05-09 00:00:06 +02005218 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01005219 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5220 * not miss a ring update when it notices a stopped queue.
5221 */
5222 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01005224 /* Sync with rtl_tx:
5225 * - publish queue status and cur_tx ring index (write barrier)
5226 * - refresh dirty_tx ring index (read barrier).
5227 * May the current thread have a pessimistic view of the ring
5228 * status and forget to wake up queue, a racing rtl_tx thread
5229 * can't.
5230 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005231 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02005232 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 netif_wake_queue(dev);
5234 }
5235
Stephen Hemminger613573252009-08-31 19:50:58 +00005236 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005238err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005239 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005240err_dma_0:
5241 dev_kfree_skb(skb);
5242 dev->stats.tx_dropped++;
5243 return NETDEV_TX_OK;
5244
5245err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005247 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005248 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249}
5250
5251static void rtl8169_pcierr_interrupt(struct net_device *dev)
5252{
5253 struct rtl8169_private *tp = netdev_priv(dev);
5254 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 u16 pci_status, pci_cmd;
5256
5257 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5258 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5259
Joe Perchesbf82c182010-02-09 11:49:50 +00005260 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5261 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262
5263 /*
5264 * The recovery sequence below admits a very elaborated explanation:
5265 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005266 * - I did not see what else could be done;
5267 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 *
5269 * Feel free to adjust to your needs.
5270 */
Francois Romieua27993f2006-12-18 00:04:19 +01005271 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005272 pci_cmd &= ~PCI_COMMAND_PARITY;
5273 else
5274 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5275
5276 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277
5278 pci_write_config_word(pdev, PCI_STATUS,
5279 pci_status & (PCI_STATUS_DETECTED_PARITY |
5280 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5281 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5282
5283 /* The infamous DAC f*ckup only happens at boot time */
5284 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005285 void __iomem *ioaddr = tp->mmio_addr;
5286
Joe Perchesbf82c182010-02-09 11:49:50 +00005287 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 tp->cp_cmd &= ~PCIDAC;
5289 RTL_W16(CPlusCmd, tp->cp_cmd);
5290 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 }
5292
françois romieue6de30d2011-01-03 15:08:37 +00005293 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005294
Francois Romieu98ddf982012-01-31 10:47:34 +01005295 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296}
5297
Francois Romieuda78dbf2012-01-26 14:18:23 +01005298static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299{
5300 unsigned int dirty_tx, tx_left;
5301
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 dirty_tx = tp->dirty_tx;
5303 smp_rmb();
5304 tx_left = tp->cur_tx - dirty_tx;
5305
5306 while (tx_left > 0) {
5307 unsigned int entry = dirty_tx % NUM_TX_DESC;
5308 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 u32 status;
5310
5311 rmb();
5312 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5313 if (status & DescOwn)
5314 break;
5315
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005316 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5317 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 if (status & LastFrag) {
Francois Romieuf63b1d92012-07-23 22:55:55 +02005319 u64_stats_update_begin(&tp->tx_stats.syncp);
5320 tp->tx_stats.packets++;
5321 tp->tx_stats.bytes += tx_skb->skb->len;
5322 u64_stats_update_end(&tp->tx_stats.syncp);
5323 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 tx_skb->skb = NULL;
5325 }
5326 dirty_tx++;
5327 tx_left--;
5328 }
5329
5330 if (tp->dirty_tx != dirty_tx) {
5331 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005332 /* Sync with rtl8169_start_xmit:
5333 * - publish dirty_tx ring index (write barrier)
5334 * - refresh cur_tx ring index and queue status (read barrier)
5335 * May the current thread miss the stopped queue condition,
5336 * a racing xmit thread can only have a right view of the
5337 * ring status.
5338 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005339 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02005341 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 netif_wake_queue(dev);
5343 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005344 /*
5345 * 8168 hack: TxPoll requests are lost when the Tx packets are
5346 * too close. Let's kick an extra TxPoll request when a burst
5347 * of start_xmit activity is detected (if it is not detected,
5348 * it is slow enough). -- FR
5349 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005350 if (tp->cur_tx != dirty_tx) {
5351 void __iomem *ioaddr = tp->mmio_addr;
5352
Francois Romieud78ae2d2007-08-26 20:08:19 +02005353 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 }
5356}
5357
Francois Romieu126fa4b2005-05-12 20:09:17 -04005358static inline int rtl8169_fragmented_frame(u32 status)
5359{
5360 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5361}
5362
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005363static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 u32 status = opts1 & RxProtoMask;
5366
5367 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005368 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 skb->ip_summed = CHECKSUM_UNNECESSARY;
5370 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005371 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372}
5373
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005374static struct sk_buff *rtl8169_try_rx_copy(void *data,
5375 struct rtl8169_private *tp,
5376 int pkt_size,
5377 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005379 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005380 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005382 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005383 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005384 prefetch(data);
5385 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5386 if (skb)
5387 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005388 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5389
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005390 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391}
5392
Francois Romieuda78dbf2012-01-26 14:18:23 +01005393static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394{
5395 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005396 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 cur_rx = tp->cur_rx;
5399 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005400 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005402 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005404 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 u32 status;
5406
5407 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005408 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409
5410 if (status & DescOwn)
5411 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005412 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005413 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5414 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005415 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005417 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005419 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005420 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005421 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005422 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005423 }
Ben Greear6bbe0212012-02-10 15:04:33 +00005424 if ((status & (RxRUNT | RxCRC)) &&
5425 !(status & (RxRWT | RxFOVF)) &&
5426 (dev->features & NETIF_F_RXALL))
5427 goto process_pkt;
5428
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005429 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005431 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00005432 dma_addr_t addr;
5433 int pkt_size;
5434
5435process_pkt:
5436 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00005437 if (likely(!(dev->features & NETIF_F_RXFCS)))
5438 pkt_size = (status & 0x00003fff) - 4;
5439 else
5440 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441
Francois Romieu126fa4b2005-05-12 20:09:17 -04005442 /*
5443 * The driver does not support incoming fragmented
5444 * frames. They are seen as a symptom of over-mtu
5445 * sized frames.
5446 */
5447 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005448 dev->stats.rx_dropped++;
5449 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005450 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005451 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005452 }
5453
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005454 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5455 tp, pkt_size, addr);
5456 rtl8169_mark_to_asic(desc, rx_buf_sz);
5457 if (!skb) {
5458 dev->stats.rx_dropped++;
5459 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 }
5461
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005462 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 skb_put(skb, pkt_size);
5464 skb->protocol = eth_type_trans(skb, dev);
5465
Francois Romieu7a8fc772011-03-01 17:18:33 +01005466 rtl8169_rx_vlan_tag(desc, skb);
5467
Francois Romieu56de4142011-03-15 17:29:31 +01005468 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469
Junchang Wang8027aa22012-03-04 23:30:32 +01005470 u64_stats_update_begin(&tp->rx_stats.syncp);
5471 tp->rx_stats.packets++;
5472 tp->rx_stats.bytes += pkt_size;
5473 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 }
5476
5477 count = cur_rx - tp->cur_rx;
5478 tp->cur_rx = cur_rx;
5479
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005480 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481
5482 return count;
5483}
5484
Francois Romieu07d3f512007-02-21 22:40:46 +01005485static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486{
Francois Romieu07d3f512007-02-21 22:40:46 +01005487 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 int handled = 0;
Francois Romieu9085cdf2012-01-26 12:59:08 +01005490 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491
Francois Romieu9085cdf2012-01-26 12:59:08 +01005492 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005493 if (status && status != 0xffff) {
5494 status &= RTL_EVENT_NAPI | tp->event_slow;
5495 if (status) {
5496 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00005497
Francois Romieuda78dbf2012-01-26 14:18:23 +01005498 rtl_irq_disable(tp);
5499 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 return IRQ_RETVAL(handled);
5503}
5504
Francois Romieuda78dbf2012-01-26 14:18:23 +01005505/*
5506 * Workqueue context.
5507 */
5508static void rtl_slow_event_work(struct rtl8169_private *tp)
5509{
5510 struct net_device *dev = tp->dev;
5511 u16 status;
5512
5513 status = rtl_get_events(tp) & tp->event_slow;
5514 rtl_ack_events(tp, status);
5515
5516 if (unlikely(status & RxFIFOOver)) {
5517 switch (tp->mac_version) {
5518 /* Work around for rx fifo overflow */
5519 case RTL_GIGA_MAC_VER_11:
5520 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01005521 /* XXX - Hack alert. See rtl_task(). */
5522 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005523 default:
5524 break;
5525 }
5526 }
5527
5528 if (unlikely(status & SYSErr))
5529 rtl8169_pcierr_interrupt(dev);
5530
5531 if (status & LinkChg)
5532 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
5533
Francois Romieuced8dfb2012-06-09 10:53:16 +00005534 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005535}
5536
Francois Romieu4422bcd2012-01-26 11:23:32 +01005537static void rtl_task(struct work_struct *work)
5538{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005539 static const struct {
5540 int bitnr;
5541 void (*action)(struct rtl8169_private *);
5542 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01005543 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005544 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
5545 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
5546 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
5547 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01005548 struct rtl8169_private *tp =
5549 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005550 struct net_device *dev = tp->dev;
5551 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01005552
Francois Romieuda78dbf2012-01-26 14:18:23 +01005553 rtl_lock_work(tp);
5554
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005555 if (!netif_running(dev) ||
5556 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01005557 goto out_unlock;
5558
5559 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
5560 bool pending;
5561
Francois Romieuda78dbf2012-01-26 14:18:23 +01005562 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005563 if (pending)
5564 rtl_work[i].action(tp);
5565 }
5566
5567out_unlock:
5568 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01005569}
5570
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005571static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005573 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5574 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005575 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
5576 int work_done= 0;
5577 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578
Francois Romieuda78dbf2012-01-26 14:18:23 +01005579 status = rtl_get_events(tp);
5580 rtl_ack_events(tp, status & ~tp->event_slow);
5581
5582 if (status & RTL_EVENT_NAPI_RX)
5583 work_done = rtl_rx(dev, tp, (u32) budget);
5584
5585 if (status & RTL_EVENT_NAPI_TX)
5586 rtl_tx(dev, tp);
5587
5588 if (status & tp->event_slow) {
5589 enable_mask &= ~tp->event_slow;
5590
5591 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
5592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005594 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005595 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005596
Francois Romieuda78dbf2012-01-26 14:18:23 +01005597 rtl_irq_enable(tp, enable_mask);
5598 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 }
5600
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005601 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603
Francois Romieu523a6092008-09-10 22:28:56 +02005604static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5605{
5606 struct rtl8169_private *tp = netdev_priv(dev);
5607
5608 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5609 return;
5610
5611 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5612 RTL_W32(RxMissed, 0);
5613}
5614
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615static void rtl8169_down(struct net_device *dev)
5616{
5617 struct rtl8169_private *tp = netdev_priv(dev);
5618 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619
Francois Romieu4876cc12011-03-11 21:07:11 +01005620 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005622 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005623 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624
Hayes Wang92fc43b2011-07-06 15:58:03 +08005625 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005626 /*
5627 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01005628 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
5629 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005630 */
Francois Romieu523a6092008-09-10 22:28:56 +02005631 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005634 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 rtl8169_tx_clear(tp);
5637
5638 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005639
5640 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641}
5642
5643static int rtl8169_close(struct net_device *dev)
5644{
5645 struct rtl8169_private *tp = netdev_priv(dev);
5646 struct pci_dev *pdev = tp->pci_dev;
5647
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005648 pm_runtime_get_sync(&pdev->dev);
5649
Francois Romieucecb5fd2011-04-01 10:21:07 +02005650 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005651 rtl8169_update_counters(dev);
5652
Francois Romieuda78dbf2012-01-26 14:18:23 +01005653 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005654 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005655
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005657 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658
Francois Romieu92a7c4e2012-03-10 10:42:12 +01005659 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005661 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5662 tp->RxPhyAddr);
5663 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5664 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 tp->TxDescArray = NULL;
5666 tp->RxDescArray = NULL;
5667
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005668 pm_runtime_put_sync(&pdev->dev);
5669
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 return 0;
5671}
5672
Francois Romieudc1c00c2012-03-08 10:06:18 +01005673#ifdef CONFIG_NET_POLL_CONTROLLER
5674static void rtl8169_netpoll(struct net_device *dev)
5675{
5676 struct rtl8169_private *tp = netdev_priv(dev);
5677
5678 rtl8169_interrupt(tp->pci_dev->irq, dev);
5679}
5680#endif
5681
Francois Romieudf43ac72012-03-08 09:48:40 +01005682static int rtl_open(struct net_device *dev)
5683{
5684 struct rtl8169_private *tp = netdev_priv(dev);
5685 void __iomem *ioaddr = tp->mmio_addr;
5686 struct pci_dev *pdev = tp->pci_dev;
5687 int retval = -ENOMEM;
5688
5689 pm_runtime_get_sync(&pdev->dev);
5690
5691 /*
5692 * Rx and Tx desscriptors needs 256 bytes alignment.
5693 * dma_alloc_coherent provides more.
5694 */
5695 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
5696 &tp->TxPhyAddr, GFP_KERNEL);
5697 if (!tp->TxDescArray)
5698 goto err_pm_runtime_put;
5699
5700 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
5701 &tp->RxPhyAddr, GFP_KERNEL);
5702 if (!tp->RxDescArray)
5703 goto err_free_tx_0;
5704
5705 retval = rtl8169_init_ring(dev);
5706 if (retval < 0)
5707 goto err_free_rx_1;
5708
5709 INIT_WORK(&tp->wk.work, rtl_task);
5710
5711 smp_mb();
5712
5713 rtl_request_firmware(tp);
5714
Francois Romieu92a7c4e2012-03-10 10:42:12 +01005715 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01005716 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
5717 dev->name, dev);
5718 if (retval < 0)
5719 goto err_release_fw_2;
5720
5721 rtl_lock_work(tp);
5722
5723 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
5724
5725 napi_enable(&tp->napi);
5726
5727 rtl8169_init_phy(dev, tp);
5728
5729 __rtl8169_set_features(dev, dev->features);
5730
5731 rtl_pll_power_up(tp);
5732
5733 rtl_hw_start(dev);
5734
5735 netif_start_queue(dev);
5736
5737 rtl_unlock_work(tp);
5738
5739 tp->saved_wolopts = 0;
5740 pm_runtime_put_noidle(&pdev->dev);
5741
5742 rtl8169_check_link_status(dev, tp, ioaddr);
5743out:
5744 return retval;
5745
5746err_release_fw_2:
5747 rtl_release_firmware(tp);
5748 rtl8169_rx_clear(tp);
5749err_free_rx_1:
5750 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5751 tp->RxPhyAddr);
5752 tp->RxDescArray = NULL;
5753err_free_tx_0:
5754 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5755 tp->TxPhyAddr);
5756 tp->TxDescArray = NULL;
5757err_pm_runtime_put:
5758 pm_runtime_put_noidle(&pdev->dev);
5759 goto out;
5760}
5761
Junchang Wang8027aa22012-03-04 23:30:32 +01005762static struct rtnl_link_stats64 *
5763rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764{
5765 struct rtl8169_private *tp = netdev_priv(dev);
5766 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01005767 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768
Francois Romieuda78dbf2012-01-26 14:18:23 +01005769 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02005770 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02005771
Junchang Wang8027aa22012-03-04 23:30:32 +01005772 do {
5773 start = u64_stats_fetch_begin_bh(&tp->rx_stats.syncp);
5774 stats->rx_packets = tp->rx_stats.packets;
5775 stats->rx_bytes = tp->rx_stats.bytes;
5776 } while (u64_stats_fetch_retry_bh(&tp->rx_stats.syncp, start));
5777
5778
5779 do {
5780 start = u64_stats_fetch_begin_bh(&tp->tx_stats.syncp);
5781 stats->tx_packets = tp->tx_stats.packets;
5782 stats->tx_bytes = tp->tx_stats.bytes;
5783 } while (u64_stats_fetch_retry_bh(&tp->tx_stats.syncp, start));
5784
5785 stats->rx_dropped = dev->stats.rx_dropped;
5786 stats->tx_dropped = dev->stats.tx_dropped;
5787 stats->rx_length_errors = dev->stats.rx_length_errors;
5788 stats->rx_errors = dev->stats.rx_errors;
5789 stats->rx_crc_errors = dev->stats.rx_crc_errors;
5790 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
5791 stats->rx_missed_errors = dev->stats.rx_missed_errors;
5792
5793 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794}
5795
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005796static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005797{
françois romieu065c27c2011-01-03 15:08:12 +00005798 struct rtl8169_private *tp = netdev_priv(dev);
5799
Francois Romieu5d06a992006-02-23 00:47:58 +01005800 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005801 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005802
5803 netif_device_detach(dev);
5804 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005805
5806 rtl_lock_work(tp);
5807 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005808 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005809 rtl_unlock_work(tp);
5810
5811 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005812}
Francois Romieu5d06a992006-02-23 00:47:58 +01005813
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005814#ifdef CONFIG_PM
5815
5816static int rtl8169_suspend(struct device *device)
5817{
5818 struct pci_dev *pdev = to_pci_dev(device);
5819 struct net_device *dev = pci_get_drvdata(pdev);
5820
5821 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005822
Francois Romieu5d06a992006-02-23 00:47:58 +01005823 return 0;
5824}
5825
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005826static void __rtl8169_resume(struct net_device *dev)
5827{
françois romieu065c27c2011-01-03 15:08:12 +00005828 struct rtl8169_private *tp = netdev_priv(dev);
5829
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005830 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005831
5832 rtl_pll_power_up(tp);
5833
Artem Savkovcff4c162012-04-03 10:29:11 +00005834 rtl_lock_work(tp);
5835 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005836 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00005837 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005838
Francois Romieu98ddf982012-01-31 10:47:34 +01005839 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005840}
5841
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005842static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005843{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005844 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005845 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005846 struct rtl8169_private *tp = netdev_priv(dev);
5847
5848 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005849
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005850 if (netif_running(dev))
5851 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005852
Francois Romieu5d06a992006-02-23 00:47:58 +01005853 return 0;
5854}
5855
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005856static int rtl8169_runtime_suspend(struct device *device)
5857{
5858 struct pci_dev *pdev = to_pci_dev(device);
5859 struct net_device *dev = pci_get_drvdata(pdev);
5860 struct rtl8169_private *tp = netdev_priv(dev);
5861
5862 if (!tp->TxDescArray)
5863 return 0;
5864
Francois Romieuda78dbf2012-01-26 14:18:23 +01005865 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005866 tp->saved_wolopts = __rtl8169_get_wol(tp);
5867 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005868 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005869
5870 rtl8169_net_suspend(dev);
5871
5872 return 0;
5873}
5874
5875static int rtl8169_runtime_resume(struct device *device)
5876{
5877 struct pci_dev *pdev = to_pci_dev(device);
5878 struct net_device *dev = pci_get_drvdata(pdev);
5879 struct rtl8169_private *tp = netdev_priv(dev);
5880
5881 if (!tp->TxDescArray)
5882 return 0;
5883
Francois Romieuda78dbf2012-01-26 14:18:23 +01005884 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005885 __rtl8169_set_wol(tp, tp->saved_wolopts);
5886 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005887 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005888
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005889 rtl8169_init_phy(dev, tp);
5890
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005891 __rtl8169_resume(dev);
5892
5893 return 0;
5894}
5895
5896static int rtl8169_runtime_idle(struct device *device)
5897{
5898 struct pci_dev *pdev = to_pci_dev(device);
5899 struct net_device *dev = pci_get_drvdata(pdev);
5900 struct rtl8169_private *tp = netdev_priv(dev);
5901
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005902 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005903}
5904
Alexey Dobriyan47145212009-12-14 18:00:08 -08005905static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005906 .suspend = rtl8169_suspend,
5907 .resume = rtl8169_resume,
5908 .freeze = rtl8169_suspend,
5909 .thaw = rtl8169_resume,
5910 .poweroff = rtl8169_suspend,
5911 .restore = rtl8169_resume,
5912 .runtime_suspend = rtl8169_runtime_suspend,
5913 .runtime_resume = rtl8169_runtime_resume,
5914 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005915};
5916
5917#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5918
5919#else /* !CONFIG_PM */
5920
5921#define RTL8169_PM_OPS NULL
5922
5923#endif /* !CONFIG_PM */
5924
David S. Miller1805b2f2011-10-24 18:18:09 -04005925static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
5926{
5927 void __iomem *ioaddr = tp->mmio_addr;
5928
5929 /* WoL fails with 8168b when the receiver is disabled. */
5930 switch (tp->mac_version) {
5931 case RTL_GIGA_MAC_VER_11:
5932 case RTL_GIGA_MAC_VER_12:
5933 case RTL_GIGA_MAC_VER_17:
5934 pci_clear_master(tp->pci_dev);
5935
5936 RTL_W8(ChipCmd, CmdRxEnb);
5937 /* PCI commit */
5938 RTL_R8(ChipCmd);
5939 break;
5940 default:
5941 break;
5942 }
5943}
5944
Francois Romieu1765f952008-09-13 17:21:40 +02005945static void rtl_shutdown(struct pci_dev *pdev)
5946{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005947 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005948 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00005949 struct device *d = &pdev->dev;
5950
5951 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02005952
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005953 rtl8169_net_suspend(dev);
5954
Francois Romieucecb5fd2011-04-01 10:21:07 +02005955 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08005956 rtl_rar_set(tp, dev->perm_addr);
5957
Hayes Wang92fc43b2011-07-06 15:58:03 +08005958 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00005959
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005960 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04005961 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
5962 rtl_wol_suspend_quirk(tp);
5963 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00005964 }
5965
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005966 pci_wake_from_d3(pdev, true);
5967 pci_set_power_state(pdev, PCI_D3hot);
5968 }
françois romieu2a15cd22012-03-06 01:14:12 +00005969
5970 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005971}
Francois Romieu5d06a992006-02-23 00:47:58 +01005972
Francois Romieue27566e2012-03-08 09:54:01 +01005973static void __devexit rtl_remove_one(struct pci_dev *pdev)
5974{
5975 struct net_device *dev = pci_get_drvdata(pdev);
5976 struct rtl8169_private *tp = netdev_priv(dev);
5977
5978 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
5979 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5980 tp->mac_version == RTL_GIGA_MAC_VER_31) {
5981 rtl8168_driver_stop(tp);
5982 }
5983
5984 cancel_work_sync(&tp->wk.work);
5985
Devendra Naga0440cf62012-05-31 01:51:20 +00005986 netif_napi_del(&tp->napi);
5987
Francois Romieue27566e2012-03-08 09:54:01 +01005988 unregister_netdev(dev);
5989
5990 rtl_release_firmware(tp);
5991
5992 if (pci_dev_run_wake(pdev))
5993 pm_runtime_get_noresume(&pdev->dev);
5994
5995 /* restore original MAC address */
5996 rtl_rar_set(tp, dev->perm_addr);
5997
5998 rtl_disable_msi(pdev, tp);
5999 rtl8169_release_board(pdev, dev, tp->mmio_addr);
6000 pci_set_drvdata(pdev, NULL);
6001}
6002
Francois Romieufa9c3852012-03-08 10:01:50 +01006003static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01006004 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01006005 .ndo_stop = rtl8169_close,
6006 .ndo_get_stats64 = rtl8169_get_stats64,
6007 .ndo_start_xmit = rtl8169_start_xmit,
6008 .ndo_tx_timeout = rtl8169_tx_timeout,
6009 .ndo_validate_addr = eth_validate_addr,
6010 .ndo_change_mtu = rtl8169_change_mtu,
6011 .ndo_fix_features = rtl8169_fix_features,
6012 .ndo_set_features = rtl8169_set_features,
6013 .ndo_set_mac_address = rtl_set_mac_address,
6014 .ndo_do_ioctl = rtl8169_ioctl,
6015 .ndo_set_rx_mode = rtl_set_rx_mode,
6016#ifdef CONFIG_NET_POLL_CONTROLLER
6017 .ndo_poll_controller = rtl8169_netpoll,
6018#endif
6019
6020};
6021
Francois Romieu31fa8b12012-03-08 10:09:40 +01006022static const struct rtl_cfg_info {
6023 void (*hw_start)(struct net_device *);
6024 unsigned int region;
6025 unsigned int align;
6026 u16 event_slow;
6027 unsigned features;
6028 u8 default_ver;
6029} rtl_cfg_infos [] = {
6030 [RTL_CFG_0] = {
6031 .hw_start = rtl_hw_start_8169,
6032 .region = 1,
6033 .align = 0,
6034 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
6035 .features = RTL_FEATURE_GMII,
6036 .default_ver = RTL_GIGA_MAC_VER_01,
6037 },
6038 [RTL_CFG_1] = {
6039 .hw_start = rtl_hw_start_8168,
6040 .region = 2,
6041 .align = 8,
6042 .event_slow = SYSErr | LinkChg | RxOverflow,
6043 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
6044 .default_ver = RTL_GIGA_MAC_VER_11,
6045 },
6046 [RTL_CFG_2] = {
6047 .hw_start = rtl_hw_start_8101,
6048 .region = 2,
6049 .align = 8,
6050 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
6051 PCSTimeout,
6052 .features = RTL_FEATURE_MSI,
6053 .default_ver = RTL_GIGA_MAC_VER_13,
6054 }
6055};
6056
6057/* Cfg9346_Unlock assumed. */
6058static unsigned rtl_try_msi(struct rtl8169_private *tp,
6059 const struct rtl_cfg_info *cfg)
6060{
6061 void __iomem *ioaddr = tp->mmio_addr;
6062 unsigned msi = 0;
6063 u8 cfg2;
6064
6065 cfg2 = RTL_R8(Config2) & ~MSIEnable;
6066 if (cfg->features & RTL_FEATURE_MSI) {
6067 if (pci_enable_msi(tp->pci_dev)) {
6068 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
6069 } else {
6070 cfg2 |= MSIEnable;
6071 msi = RTL_FEATURE_MSI;
6072 }
6073 }
6074 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6075 RTL_W8(Config2, cfg2);
6076 return msi;
6077}
6078
Francois Romieu3b6cf252012-03-08 09:59:04 +01006079static int __devinit
6080rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6081{
6082 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
6083 const unsigned int region = cfg->region;
6084 struct rtl8169_private *tp;
6085 struct mii_if_info *mii;
6086 struct net_device *dev;
6087 void __iomem *ioaddr;
6088 int chipset, i;
6089 int rc;
6090
6091 if (netif_msg_drv(&debug)) {
6092 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
6093 MODULENAME, RTL8169_VERSION);
6094 }
6095
6096 dev = alloc_etherdev(sizeof (*tp));
6097 if (!dev) {
6098 rc = -ENOMEM;
6099 goto out;
6100 }
6101
6102 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01006103 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006104 tp = netdev_priv(dev);
6105 tp->dev = dev;
6106 tp->pci_dev = pdev;
6107 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
6108
6109 mii = &tp->mii;
6110 mii->dev = dev;
6111 mii->mdio_read = rtl_mdio_read;
6112 mii->mdio_write = rtl_mdio_write;
6113 mii->phy_id_mask = 0x1f;
6114 mii->reg_num_mask = 0x1f;
6115 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
6116
6117 /* disable ASPM completely as that cause random device stop working
6118 * problems as well as full system hangs for some PCIe devices users */
6119 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6120 PCIE_LINK_STATE_CLKPM);
6121
6122 /* enable device (incl. PCI PM wakeup and hotplug setup) */
6123 rc = pci_enable_device(pdev);
6124 if (rc < 0) {
6125 netif_err(tp, probe, dev, "enable failure\n");
6126 goto err_out_free_dev_1;
6127 }
6128
6129 if (pci_set_mwi(pdev) < 0)
6130 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
6131
6132 /* make sure PCI base addr 1 is MMIO */
6133 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
6134 netif_err(tp, probe, dev,
6135 "region #%d not an MMIO resource, aborting\n",
6136 region);
6137 rc = -ENODEV;
6138 goto err_out_mwi_2;
6139 }
6140
6141 /* check for weird/broken PCI region reporting */
6142 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
6143 netif_err(tp, probe, dev,
6144 "Invalid PCI region size(s), aborting\n");
6145 rc = -ENODEV;
6146 goto err_out_mwi_2;
6147 }
6148
6149 rc = pci_request_regions(pdev, MODULENAME);
6150 if (rc < 0) {
6151 netif_err(tp, probe, dev, "could not request regions\n");
6152 goto err_out_mwi_2;
6153 }
6154
6155 tp->cp_cmd = RxChkSum;
6156
6157 if ((sizeof(dma_addr_t) > 4) &&
6158 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
6159 tp->cp_cmd |= PCIDAC;
6160 dev->features |= NETIF_F_HIGHDMA;
6161 } else {
6162 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6163 if (rc < 0) {
6164 netif_err(tp, probe, dev, "DMA configuration failed\n");
6165 goto err_out_free_res_3;
6166 }
6167 }
6168
6169 /* ioremap MMIO region */
6170 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
6171 if (!ioaddr) {
6172 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
6173 rc = -EIO;
6174 goto err_out_free_res_3;
6175 }
6176 tp->mmio_addr = ioaddr;
6177
6178 if (!pci_is_pcie(pdev))
6179 netif_info(tp, probe, dev, "not PCI Express\n");
6180
6181 /* Identify chip attached to board */
6182 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
6183
6184 rtl_init_rxcfg(tp);
6185
6186 rtl_irq_disable(tp);
6187
6188 rtl_hw_reset(tp);
6189
6190 rtl_ack_events(tp, 0xffff);
6191
6192 pci_set_master(pdev);
6193
6194 /*
6195 * Pretend we are using VLANs; This bypasses a nasty bug where
6196 * Interrupts stop flowing on high load on 8110SCd controllers.
6197 */
6198 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6199 tp->cp_cmd |= RxVlan;
6200
6201 rtl_init_mdio_ops(tp);
6202 rtl_init_pll_power_ops(tp);
6203 rtl_init_jumbo_ops(tp);
6204
6205 rtl8169_print_mac_version(tp);
6206
6207 chipset = tp->mac_version;
6208 tp->txd_version = rtl_chip_infos[chipset].txd_version;
6209
6210 RTL_W8(Cfg9346, Cfg9346_Unlock);
6211 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
6212 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
6213 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
6214 tp->features |= RTL_FEATURE_WOL;
6215 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
6216 tp->features |= RTL_FEATURE_WOL;
6217 tp->features |= rtl_try_msi(tp, cfg);
6218 RTL_W8(Cfg9346, Cfg9346_Lock);
6219
6220 if (rtl_tbi_enabled(tp)) {
6221 tp->set_speed = rtl8169_set_speed_tbi;
6222 tp->get_settings = rtl8169_gset_tbi;
6223 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
6224 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
6225 tp->link_ok = rtl8169_tbi_link_ok;
6226 tp->do_ioctl = rtl_tbi_ioctl;
6227 } else {
6228 tp->set_speed = rtl8169_set_speed_xmii;
6229 tp->get_settings = rtl8169_gset_xmii;
6230 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
6231 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
6232 tp->link_ok = rtl8169_xmii_link_ok;
6233 tp->do_ioctl = rtl_xmii_ioctl;
6234 }
6235
6236 mutex_init(&tp->wk.mutex);
6237
6238 /* Get MAC address */
6239 for (i = 0; i < ETH_ALEN; i++)
6240 dev->dev_addr[i] = RTL_R8(MAC0 + i);
6241 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
6242
6243 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
6244 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006245
6246 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
6247
6248 /* don't enable SG, IP_CSUM and TSO by default - it might not work
6249 * properly for all devices */
6250 dev->features |= NETIF_F_RXCSUM |
6251 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6252
6253 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6254 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6255 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6256 NETIF_F_HIGHDMA;
6257
6258 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6259 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
6260 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
6261
6262 dev->hw_features |= NETIF_F_RXALL;
6263 dev->hw_features |= NETIF_F_RXFCS;
6264
6265 tp->hw_start = cfg->hw_start;
6266 tp->event_slow = cfg->event_slow;
6267
6268 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
6269 ~(RxBOVF | RxFOVF) : ~0;
6270
6271 init_timer(&tp->timer);
6272 tp->timer.data = (unsigned long) dev;
6273 tp->timer.function = rtl8169_phy_timer;
6274
6275 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
6276
6277 rc = register_netdev(dev);
6278 if (rc < 0)
6279 goto err_out_msi_4;
6280
6281 pci_set_drvdata(pdev, dev);
6282
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006283 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
6284 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
6285 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006286 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
6287 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
6288 "tx checksumming: %s]\n",
6289 rtl_chip_infos[chipset].jumbo_max,
6290 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
6291 }
6292
6293 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6294 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6295 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6296 rtl8168_driver_start(tp);
6297 }
6298
6299 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
6300
6301 if (pci_dev_run_wake(pdev))
6302 pm_runtime_put_noidle(&pdev->dev);
6303
6304 netif_carrier_off(dev);
6305
6306out:
6307 return rc;
6308
6309err_out_msi_4:
Devendra Naga0440cf62012-05-31 01:51:20 +00006310 netif_napi_del(&tp->napi);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006311 rtl_disable_msi(pdev, tp);
6312 iounmap(ioaddr);
6313err_out_free_res_3:
6314 pci_release_regions(pdev);
6315err_out_mwi_2:
6316 pci_clear_mwi(pdev);
6317 pci_disable_device(pdev);
6318err_out_free_dev_1:
6319 free_netdev(dev);
6320 goto out;
6321}
6322
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323static struct pci_driver rtl8169_pci_driver = {
6324 .name = MODULENAME,
6325 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01006326 .probe = rtl_init_one,
Francois Romieue27566e2012-03-08 09:54:01 +01006327 .remove = __devexit_p(rtl_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02006328 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006329 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330};
6331
Francois Romieu07d3f512007-02-21 22:40:46 +01006332static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333{
Jeff Garzik29917622006-08-19 17:48:59 -04006334 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335}
6336
Francois Romieu07d3f512007-02-21 22:40:46 +01006337static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338{
6339 pci_unregister_driver(&rtl8169_pci_driver);
6340}
6341
6342module_init(rtl8169_init_module);
6343module_exit(rtl8169_cleanup_module);