blob: 2ce60709a455d8f833aa528af4160572301ef799 [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
Francois Romieu99f252b2007-04-02 22:59:59 +020032#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/io.h>
34#include <asm/irq.h>
35
Francois Romieu865c6522008-05-11 14:51:00 +020036#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MODULENAME "r8169"
38#define PFX MODULENAME ": "
39
françois romieubca03d52011-01-03 15:07:31 +000040#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
41#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000042#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
43#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080044#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080045#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
46#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080047#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#ifdef RTL8169_DEBUG
50#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020051 if (!(expr)) { \
52 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070053 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020054 }
Joe Perches06fa7352007-10-18 21:15:00 +020055#define dprintk(fmt, args...) \
56 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#else
58#define assert(expr) do {} while (0)
59#define dprintk(fmt, args...) do {} while (0)
60#endif /* RTL8169_DEBUG */
61
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020062#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d92005-09-30 16:54:02 -070063 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define TX_BUFFS_AVAIL(tp) \
66 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
69 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050070static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/* MAC address length */
73#define MAC_ADDR_LEN 6
74
Francois Romieu9c14cea2008-07-05 00:21:15 +020075#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
78#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
79
80#define R8169_REGS_SIZE 256
81#define R8169_NAPI_WEIGHT 64
82#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
83#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
84#define RX_BUF_SIZE 1536 /* Rx Buffer size */
85#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
86#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
87
88#define RTL8169_TX_TIMEOUT (6*HZ)
89#define RTL8169_PHY_TIMEOUT (10*HZ)
90
françois romieuea8dbdd2009-03-15 01:10:50 +000091#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
92#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020093#define RTL_EEPROM_SIG_ADDR 0x0000
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/* write/read MMIO register */
96#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
97#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
98#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
99#define RTL_R8(reg) readb (ioaddr + (reg))
100#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +0000101#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200104 RTL_GIGA_MAC_VER_01 = 0,
105 RTL_GIGA_MAC_VER_02,
106 RTL_GIGA_MAC_VER_03,
107 RTL_GIGA_MAC_VER_04,
108 RTL_GIGA_MAC_VER_05,
109 RTL_GIGA_MAC_VER_06,
110 RTL_GIGA_MAC_VER_07,
111 RTL_GIGA_MAC_VER_08,
112 RTL_GIGA_MAC_VER_09,
113 RTL_GIGA_MAC_VER_10,
114 RTL_GIGA_MAC_VER_11,
115 RTL_GIGA_MAC_VER_12,
116 RTL_GIGA_MAC_VER_13,
117 RTL_GIGA_MAC_VER_14,
118 RTL_GIGA_MAC_VER_15,
119 RTL_GIGA_MAC_VER_16,
120 RTL_GIGA_MAC_VER_17,
121 RTL_GIGA_MAC_VER_18,
122 RTL_GIGA_MAC_VER_19,
123 RTL_GIGA_MAC_VER_20,
124 RTL_GIGA_MAC_VER_21,
125 RTL_GIGA_MAC_VER_22,
126 RTL_GIGA_MAC_VER_23,
127 RTL_GIGA_MAC_VER_24,
128 RTL_GIGA_MAC_VER_25,
129 RTL_GIGA_MAC_VER_26,
130 RTL_GIGA_MAC_VER_27,
131 RTL_GIGA_MAC_VER_28,
132 RTL_GIGA_MAC_VER_29,
133 RTL_GIGA_MAC_VER_30,
134 RTL_GIGA_MAC_VER_31,
135 RTL_GIGA_MAC_VER_32,
136 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800137 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800138 RTL_GIGA_MAC_VER_35,
139 RTL_GIGA_MAC_VER_36,
Francois Romieu85bffe62011-04-27 08:22:39 +0200140 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
Francois Romieu2b7b4312011-04-18 22:53:24 -0700143enum rtl_tx_desc_version {
144 RTL_TD_0 = 0,
145 RTL_TD_1 = 1,
146};
147
Francois Romieud58d46b2011-05-03 16:38:29 +0200148#define JUMBO_1K ETH_DATA_LEN
149#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
150#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
151#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
152#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
153
154#define _R(NAME,TD,FW,SZ,B) { \
155 .name = NAME, \
156 .txd_version = TD, \
157 .fw_name = FW, \
158 .jumbo_max = SZ, \
159 .jumbo_tx_csum = B \
160}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800162static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700164 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200165 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200166 u16 jumbo_max;
167 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200168} rtl_chip_infos[] = {
169 /* PCI devices. */
170 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200171 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200172 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200173 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200174 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200175 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200176 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200177 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200178 [RTL_GIGA_MAC_VER_05] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200179 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200180 [RTL_GIGA_MAC_VER_06] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200181 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200182 /* PCI-E devices. */
183 [RTL_GIGA_MAC_VER_07] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200184 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200185 [RTL_GIGA_MAC_VER_08] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200186 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200187 [RTL_GIGA_MAC_VER_09] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200188 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200189 [RTL_GIGA_MAC_VER_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200190 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200191 [RTL_GIGA_MAC_VER_11] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200192 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200193 [RTL_GIGA_MAC_VER_12] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200194 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200195 [RTL_GIGA_MAC_VER_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200196 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200197 [RTL_GIGA_MAC_VER_14] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200198 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200199 [RTL_GIGA_MAC_VER_15] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200200 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200201 [RTL_GIGA_MAC_VER_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200202 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200203 [RTL_GIGA_MAC_VER_17] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200204 _R("RTL8168b/8111b", RTL_TD_1, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200205 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200206 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200207 [RTL_GIGA_MAC_VER_19] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200208 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200209 [RTL_GIGA_MAC_VER_20] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200210 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200211 [RTL_GIGA_MAC_VER_21] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200212 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200213 [RTL_GIGA_MAC_VER_22] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200214 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200215 [RTL_GIGA_MAC_VER_23] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200216 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200217 [RTL_GIGA_MAC_VER_24] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200218 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200219 [RTL_GIGA_MAC_VER_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200220 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
221 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200222 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200223 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
224 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200225 [RTL_GIGA_MAC_VER_27] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200226 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200227 [RTL_GIGA_MAC_VER_28] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200228 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200229 [RTL_GIGA_MAC_VER_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200230 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
231 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200232 [RTL_GIGA_MAC_VER_30] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200233 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
234 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200235 [RTL_GIGA_MAC_VER_31] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200236 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200237 [RTL_GIGA_MAC_VER_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200238 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
239 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200240 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200241 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
242 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800243 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200244 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
245 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800246 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200247 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
248 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800249 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200250 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
251 JUMBO_9K, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252};
253#undef _R
254
Francois Romieubcf0bf92006-07-26 23:14:13 +0200255enum cfg_version {
256 RTL_CFG_0 = 0x00,
257 RTL_CFG_1,
258 RTL_CFG_2
259};
260
Francois Romieu07ce4062007-02-23 23:36:39 +0100261static void rtl_hw_start_8169(struct net_device *);
262static void rtl_hw_start_8168(struct net_device *);
263static void rtl_hw_start_8101(struct net_device *);
264
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000265static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200266 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200267 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200268 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100269 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200270 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
271 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000272 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200273 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200274 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
275 { PCI_VENDOR_ID_LINKSYS, 0x1032,
276 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100277 { 0x0001, 0x8168,
278 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 {0,},
280};
281
282MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
283
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000284static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700285static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200286static struct {
287 u32 msg_enable;
288} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Francois Romieu07d3f512007-02-21 22:40:46 +0100290enum rtl_registers {
291 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100292 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100293 MAR0 = 8, /* Multicast filter. */
294 CounterAddrLow = 0x10,
295 CounterAddrHigh = 0x14,
296 TxDescStartAddrLow = 0x20,
297 TxDescStartAddrHigh = 0x24,
298 TxHDescStartAddrLow = 0x28,
299 TxHDescStartAddrHigh = 0x2c,
300 FLASH = 0x30,
301 ERSR = 0x36,
302 ChipCmd = 0x37,
303 TxPoll = 0x38,
304 IntrMask = 0x3c,
305 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700306
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800307 TxConfig = 0x40,
308#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
309#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
310
311 RxConfig = 0x44,
312#define RX128_INT_EN (1 << 15) /* 8111c and later */
313#define RX_MULTI_EN (1 << 14) /* 8111c only */
314#define RXCFG_FIFO_SHIFT 13
315 /* No threshold before first PCI xfer */
316#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
317#define RXCFG_DMA_SHIFT 8
318 /* Unlimited maximum PCI burst. */
319#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700320
Francois Romieu07d3f512007-02-21 22:40:46 +0100321 RxMissed = 0x4c,
322 Cfg9346 = 0x50,
323 Config0 = 0x51,
324 Config1 = 0x52,
325 Config2 = 0x53,
326 Config3 = 0x54,
327 Config4 = 0x55,
328 Config5 = 0x56,
329 MultiIntr = 0x5c,
330 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100331 PHYstatus = 0x6c,
332 RxMaxSize = 0xda,
333 CPlusCmd = 0xe0,
334 IntrMitigate = 0xe2,
335 RxDescAddrLow = 0xe4,
336 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000337 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
338
339#define NoEarlyTx 0x3f /* Max value : no early transmit. */
340
341 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
342
343#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800344#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000345
Francois Romieu07d3f512007-02-21 22:40:46 +0100346 FuncEvent = 0xf0,
347 FuncEventMask = 0xf4,
348 FuncPresetState = 0xf8,
349 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350};
351
Francois Romieuf162a5d2008-06-01 22:37:49 +0200352enum rtl8110_registers {
353 TBICSR = 0x64,
354 TBI_ANAR = 0x68,
355 TBI_LPAR = 0x6a,
356};
357
358enum rtl8168_8101_registers {
359 CSIDR = 0x64,
360 CSIAR = 0x68,
361#define CSIAR_FLAG 0x80000000
362#define CSIAR_WRITE_CMD 0x80000000
363#define CSIAR_BYTE_ENABLE 0x0f
364#define CSIAR_BYTE_ENABLE_SHIFT 12
365#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000366 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200367 EPHYAR = 0x80,
368#define EPHYAR_FLAG 0x80000000
369#define EPHYAR_WRITE_CMD 0x80000000
370#define EPHYAR_REG_MASK 0x1f
371#define EPHYAR_REG_SHIFT 16
372#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800373 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800374#define PFM_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200375 DBG_REG = 0xd1,
376#define FIX_NAK_1 (1 << 4)
377#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800378 TWSI = 0xd2,
379 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800380#define NOW_IS_OOB (1 << 7)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800381#define EN_NDP (1 << 3)
382#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000383 EFUSEAR = 0xdc,
384#define EFUSEAR_FLAG 0x80000000
385#define EFUSEAR_WRITE_CMD 0x80000000
386#define EFUSEAR_READ_CMD 0x00000000
387#define EFUSEAR_REG_MASK 0x03ff
388#define EFUSEAR_REG_SHIFT 8
389#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200390};
391
françois romieuc0e45c12011-01-03 15:08:04 +0000392enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800393 LED_FREQ = 0x1a,
394 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000395 ERIDR = 0x70,
396 ERIAR = 0x74,
397#define ERIAR_FLAG 0x80000000
398#define ERIAR_WRITE_CMD 0x80000000
399#define ERIAR_READ_CMD 0x00000000
400#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000401#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800402#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
403#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
404#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
405#define ERIAR_MASK_SHIFT 12
406#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
407#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
408#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000409 EPHY_RXER_NUM = 0x7c,
410 OCPDR = 0xb0, /* OCP GPHY access */
411#define OCPDR_WRITE_CMD 0x80000000
412#define OCPDR_READ_CMD 0x00000000
413#define OCPDR_REG_MASK 0x7f
414#define OCPDR_GPHY_REG_SHIFT 16
415#define OCPDR_DATA_MASK 0xffff
416 OCPAR = 0xb4,
417#define OCPAR_FLAG 0x80000000
418#define OCPAR_GPHY_WRITE_CMD 0x8000f060
419#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000420 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
421 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200422#define TXPLA_RST (1 << 29)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800423#define PWM_EN (1 << 22)
françois romieuc0e45c12011-01-03 15:08:04 +0000424};
425
Francois Romieu07d3f512007-02-21 22:40:46 +0100426enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100428 SYSErr = 0x8000,
429 PCSTimeout = 0x4000,
430 SWInt = 0x0100,
431 TxDescUnavail = 0x0080,
432 RxFIFOOver = 0x0040,
433 LinkChg = 0x0020,
434 RxOverflow = 0x0010,
435 TxErr = 0x0008,
436 TxOK = 0x0004,
437 RxErr = 0x0002,
438 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400441 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200442 RxFOVF = (1 << 23),
443 RxRWT = (1 << 22),
444 RxRES = (1 << 21),
445 RxRUNT = (1 << 20),
446 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800449 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100450 CmdReset = 0x10,
451 CmdRxEnb = 0x08,
452 CmdTxEnb = 0x04,
453 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Francois Romieu275391a2007-02-23 23:50:28 +0100455 /* TXPoll register p.5 */
456 HPQ = 0x80, /* Poll cmd on the high prio queue */
457 NPQ = 0x40, /* Poll cmd on the low prio queue */
458 FSWInt = 0x01, /* Forced software interrupt */
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100461 Cfg9346_Lock = 0x00,
462 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100465 AcceptErr = 0x20,
466 AcceptRunt = 0x10,
467 AcceptBroadcast = 0x08,
468 AcceptMulticast = 0x04,
469 AcceptMyPhys = 0x02,
470 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200471#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 /* TxConfigBits */
474 TxInterFrameGapShift = 24,
475 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
476
Francois Romieu5d06a992006-02-23 00:47:58 +0100477 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200478 LEDS1 = (1 << 7),
479 LEDS0 = (1 << 6),
Francois Romieufbac58f2007-10-04 22:51:38 +0200480 MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200481 Speed_down = (1 << 4),
482 MEMMAP = (1 << 3),
483 IOMAP = (1 << 2),
484 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100485 PMEnable = (1 << 0), /* Power Management Enable */
486
Francois Romieu6dccd162007-02-13 23:38:05 +0100487 /* Config2 register p. 25 */
488 PCI_Clock_66MHz = 0x01,
489 PCI_Clock_33MHz = 0x00,
490
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100491 /* Config3 register p.25 */
492 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
493 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200494 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200495 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100496
Francois Romieud58d46b2011-05-03 16:38:29 +0200497 /* Config4 register */
498 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
499
Francois Romieu5d06a992006-02-23 00:47:58 +0100500 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100501 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
502 MWF = (1 << 5), /* Accept Multicast wakeup frame */
503 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200504 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100505 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100506 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 /* TBICSR p.28 */
509 TBIReset = 0x80000000,
510 TBILoopback = 0x40000000,
511 TBINwEnable = 0x20000000,
512 TBINwRestart = 0x10000000,
513 TBILinkOk = 0x02000000,
514 TBINwComplete = 0x01000000,
515
516 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200517 EnableBist = (1 << 15), // 8168 8101
518 Mac_dbgo_oe = (1 << 14), // 8168 8101
519 Normal_mode = (1 << 13), // unused
520 Force_half_dup = (1 << 12), // 8168 8101
521 Force_rxflow_en = (1 << 11), // 8168 8101
522 Force_txflow_en = (1 << 10), // 8168 8101
523 Cxpl_dbg_sel = (1 << 9), // 8168 8101
524 ASF = (1 << 8), // 8168 8101
525 PktCntrDisable = (1 << 7), // 8168 8101
526 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 RxVlan = (1 << 6),
528 RxChkSum = (1 << 5),
529 PCIDAC = (1 << 4),
530 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100531 INTT_0 = 0x0000, // 8168
532 INTT_1 = 0x0001, // 8168
533 INTT_2 = 0x0002, // 8168
534 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100537 TBI_Enable = 0x80,
538 TxFlowCtrl = 0x40,
539 RxFlowCtrl = 0x20,
540 _1000bpsF = 0x10,
541 _100bps = 0x08,
542 _10bps = 0x04,
543 LinkStatus = 0x02,
544 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100547 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200548
549 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100550 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551};
552
Francois Romieu2b7b4312011-04-18 22:53:24 -0700553enum rtl_desc_bit {
554 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
556 RingEnd = (1 << 30), /* End of descriptor ring */
557 FirstFrag = (1 << 29), /* First segment of a packet */
558 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700559};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Francois Romieu2b7b4312011-04-18 22:53:24 -0700561/* Generic case. */
562enum rtl_tx_desc_bit {
563 /* First doubleword. */
564 TD_LSO = (1 << 27), /* Large Send Offload */
565#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Francois Romieu2b7b4312011-04-18 22:53:24 -0700567 /* Second doubleword. */
568 TxVlanTag = (1 << 17), /* Add VLAN tag */
569};
570
571/* 8169, 8168b and 810x except 8102e. */
572enum rtl_tx_desc_bit_0 {
573 /* First doubleword. */
574#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
575 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
576 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
577 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
578};
579
580/* 8102e, 8168c and beyond. */
581enum rtl_tx_desc_bit_1 {
582 /* Second doubleword. */
583#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
584 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
585 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
586 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
587};
588
589static const struct rtl_tx_desc_info {
590 struct {
591 u32 udp;
592 u32 tcp;
593 } checksum;
594 u16 mss_shift;
595 u16 opts_offset;
596} tx_desc_info [] = {
597 [RTL_TD_0] = {
598 .checksum = {
599 .udp = TD0_IP_CS | TD0_UDP_CS,
600 .tcp = TD0_IP_CS | TD0_TCP_CS
601 },
602 .mss_shift = TD0_MSS_SHIFT,
603 .opts_offset = 0
604 },
605 [RTL_TD_1] = {
606 .checksum = {
607 .udp = TD1_IP_CS | TD1_UDP_CS,
608 .tcp = TD1_IP_CS | TD1_TCP_CS
609 },
610 .mss_shift = TD1_MSS_SHIFT,
611 .opts_offset = 1
612 }
613};
614
615enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 /* Rx private */
617 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
618 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
619
620#define RxProtoUDP (PID1)
621#define RxProtoTCP (PID0)
622#define RxProtoIP (PID1 | PID0)
623#define RxProtoMask RxProtoIP
624
625 IPFail = (1 << 16), /* IP checksum failed */
626 UDPFail = (1 << 15), /* UDP/IP checksum failed */
627 TCPFail = (1 << 14), /* TCP/IP checksum failed */
628 RxVlanTag = (1 << 16), /* VLAN tag available */
629};
630
631#define RsvdMask 0x3fffc000
632
633struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200634 __le32 opts1;
635 __le32 opts2;
636 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637};
638
639struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200640 __le32 opts1;
641 __le32 opts2;
642 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643};
644
645struct ring_info {
646 struct sk_buff *skb;
647 u32 len;
648 u8 __pad[sizeof(void *) - sizeof(u32)];
649};
650
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200651enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200652 RTL_FEATURE_WOL = (1 << 0),
653 RTL_FEATURE_MSI = (1 << 1),
654 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200655};
656
Ivan Vecera355423d2009-02-06 21:49:57 -0800657struct rtl8169_counters {
658 __le64 tx_packets;
659 __le64 rx_packets;
660 __le64 tx_errors;
661 __le32 rx_errors;
662 __le16 rx_missed;
663 __le16 align_errors;
664 __le32 tx_one_collision;
665 __le32 tx_multi_collision;
666 __le64 rx_unicast;
667 __le64 rx_broadcast;
668 __le32 rx_multicast;
669 __le16 tx_aborted;
670 __le16 tx_underun;
671};
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673struct rtl8169_private {
674 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200675 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000676 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700677 struct napi_struct napi;
Francois Romieucecb5fd2011-04-01 10:21:07 +0200678 spinlock_t lock;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200679 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700680 u16 txd_version;
681 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
683 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
684 u32 dirty_rx;
685 u32 dirty_tx;
686 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
687 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
688 dma_addr_t TxPhyAddr;
689 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000690 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 struct timer_list timer;
693 u16 cp_cmd;
Francois Romieu0e485152007-02-20 00:00:26 +0100694 u16 intr_event;
695 u16 napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 u16 intr_mask;
françois romieuc0e45c12011-01-03 15:08:04 +0000697
698 struct mdio_ops {
699 void (*write)(void __iomem *, int, int);
700 int (*read)(void __iomem *, int);
701 } mdio_ops;
702
françois romieu065c27c2011-01-03 15:08:12 +0000703 struct pll_power_ops {
704 void (*down)(struct rtl8169_private *);
705 void (*up)(struct rtl8169_private *);
706 } pll_power_ops;
707
Francois Romieud58d46b2011-05-03 16:38:29 +0200708 struct jumbo_ops {
709 void (*enable)(struct rtl8169_private *);
710 void (*disable)(struct rtl8169_private *);
711 } jumbo_ops;
712
Oliver Neukum54405cd2011-01-06 21:55:13 +0100713 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200714 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000715 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100716 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000717 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800719 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
David Howellsc4028952006-11-22 14:57:56 +0000720 struct delayed_work task;
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200721 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200722
723 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800724 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000725 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400726 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000727
Francois Romieub6ffd972011-06-17 17:00:05 +0200728 struct rtl_fw {
729 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200730
731#define RTL_VER_SIZE 32
732
733 char version[RTL_VER_SIZE];
734
735 struct rtl_fw_phy_action {
736 __le32 *code;
737 size_t size;
738 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200739 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300740#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741};
742
Ralf Baechle979b6c12005-06-13 14:30:40 -0700743MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700746MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200747module_param_named(debug, debug.msg_enable, int, 0);
748MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749MODULE_LICENSE("GPL");
750MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000751MODULE_FIRMWARE(FIRMWARE_8168D_1);
752MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000753MODULE_FIRMWARE(FIRMWARE_8168E_1);
754MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400755MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800756MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800757MODULE_FIRMWARE(FIRMWARE_8168F_1);
758MODULE_FIRMWARE(FIRMWARE_8168F_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000761static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
762 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100763static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100765static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100767static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200769static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700771 void __iomem *, u32 budget);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200772static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773static void rtl8169_down(struct net_device *dev);
Francois Romieu99f252b2007-04-02 22:59:59 +0200774static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700775static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Francois Romieud58d46b2011-05-03 16:38:29 +0200777static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
778{
779 int cap = pci_pcie_cap(pdev);
780
781 if (cap) {
782 u16 ctl;
783
784 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
785 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
786 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
787 }
788}
789
françois romieub646d902011-01-03 15:08:21 +0000790static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
791{
792 void __iomem *ioaddr = tp->mmio_addr;
793 int i;
794
795 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
796 for (i = 0; i < 20; i++) {
797 udelay(100);
798 if (RTL_R32(OCPAR) & OCPAR_FLAG)
799 break;
800 }
801 return RTL_R32(OCPDR);
802}
803
804static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
805{
806 void __iomem *ioaddr = tp->mmio_addr;
807 int i;
808
809 RTL_W32(OCPDR, data);
810 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
811 for (i = 0; i < 20; i++) {
812 udelay(100);
813 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
814 break;
815 }
816}
817
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800818static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000819{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800820 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000821 int i;
822
823 RTL_W8(ERIDR, cmd);
824 RTL_W32(ERIAR, 0x800010e8);
825 msleep(2);
826 for (i = 0; i < 5; i++) {
827 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200828 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000829 break;
830 }
831
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800832 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000833}
834
835#define OOB_CMD_RESET 0x00
836#define OOB_CMD_DRIVER_START 0x05
837#define OOB_CMD_DRIVER_STOP 0x06
838
Francois Romieucecb5fd2011-04-01 10:21:07 +0200839static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
840{
841 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
842}
843
françois romieub646d902011-01-03 15:08:21 +0000844static void rtl8168_driver_start(struct rtl8169_private *tp)
845{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200846 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000847 int i;
848
849 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
850
Francois Romieucecb5fd2011-04-01 10:21:07 +0200851 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000852
françois romieub646d902011-01-03 15:08:21 +0000853 for (i = 0; i < 10; i++) {
854 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000855 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000856 break;
857 }
858}
859
860static void rtl8168_driver_stop(struct rtl8169_private *tp)
861{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200862 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000863 int i;
864
865 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
866
Francois Romieucecb5fd2011-04-01 10:21:07 +0200867 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000868
françois romieub646d902011-01-03 15:08:21 +0000869 for (i = 0; i < 10; i++) {
870 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000871 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000872 break;
873 }
874}
875
hayeswang4804b3b2011-03-21 01:50:29 +0000876static int r8168dp_check_dash(struct rtl8169_private *tp)
877{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200878 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000879
Francois Romieucecb5fd2011-04-01 10:21:07 +0200880 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000881}
françois romieub646d902011-01-03 15:08:21 +0000882
françois romieu4da19632011-01-03 15:07:55 +0000883static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
885 int i;
886
Francois Romieua6baf3a2007-11-08 23:23:21 +0100887 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Francois Romieu23714082006-01-29 00:49:09 +0100889 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100890 /*
891 * Check if the RTL8169 has completed writing to the specified
892 * MII register.
893 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200894 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 break;
Francois Romieu23714082006-01-29 00:49:09 +0100896 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700898 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700899 * According to hardware specs a 20us delay is required after write
900 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700901 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700902 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
françois romieu4da19632011-01-03 15:07:55 +0000905static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 int i, value = -1;
908
Francois Romieua6baf3a2007-11-08 23:23:21 +0100909 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Francois Romieu23714082006-01-29 00:49:09 +0100911 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100912 /*
913 * Check if the RTL8169 has completed retrieving data from
914 * the specified MII register.
915 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100917 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 break;
919 }
Francois Romieu23714082006-01-29 00:49:09 +0100920 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700922 /*
923 * According to hardware specs a 20us delay is required after read
924 * complete indication, but before sending next command.
925 */
926 udelay(20);
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return value;
929}
930
françois romieuc0e45c12011-01-03 15:08:04 +0000931static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
932{
933 int i;
934
935 RTL_W32(OCPDR, data |
936 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
937 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
938 RTL_W32(EPHY_RXER_NUM, 0);
939
940 for (i = 0; i < 100; i++) {
941 mdelay(1);
942 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
943 break;
944 }
945}
946
947static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
948{
949 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
950 (value & OCPDR_DATA_MASK));
951}
952
953static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
954{
955 int i;
956
957 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
958
959 mdelay(1);
960 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
961 RTL_W32(EPHY_RXER_NUM, 0);
962
963 for (i = 0; i < 100; i++) {
964 mdelay(1);
965 if (RTL_R32(OCPAR) & OCPAR_FLAG)
966 break;
967 }
968
969 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
970}
971
françois romieue6de30d2011-01-03 15:08:37 +0000972#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
973
974static void r8168dp_2_mdio_start(void __iomem *ioaddr)
975{
976 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
977}
978
979static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
980{
981 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
982}
983
984static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
985{
986 r8168dp_2_mdio_start(ioaddr);
987
988 r8169_mdio_write(ioaddr, reg_addr, value);
989
990 r8168dp_2_mdio_stop(ioaddr);
991}
992
993static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
994{
995 int value;
996
997 r8168dp_2_mdio_start(ioaddr);
998
999 value = r8169_mdio_read(ioaddr, reg_addr);
1000
1001 r8168dp_2_mdio_stop(ioaddr);
1002
1003 return value;
1004}
1005
françois romieu4da19632011-01-03 15:07:55 +00001006static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001007{
françois romieuc0e45c12011-01-03 15:08:04 +00001008 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001009}
1010
françois romieu4da19632011-01-03 15:07:55 +00001011static int rtl_readphy(struct rtl8169_private *tp, int location)
1012{
françois romieuc0e45c12011-01-03 15:08:04 +00001013 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +00001014}
1015
1016static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1017{
1018 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1019}
1020
1021static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001022{
1023 int val;
1024
françois romieu4da19632011-01-03 15:07:55 +00001025 val = rtl_readphy(tp, reg_addr);
1026 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001027}
1028
Francois Romieuccdffb92008-07-26 14:26:06 +02001029static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1030 int val)
1031{
1032 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001033
françois romieu4da19632011-01-03 15:07:55 +00001034 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001035}
1036
1037static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1038{
1039 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001040
françois romieu4da19632011-01-03 15:07:55 +00001041 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001042}
1043
Francois Romieudacf8152008-08-02 20:44:13 +02001044static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
1045{
1046 unsigned int i;
1047
1048 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1049 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1050
1051 for (i = 0; i < 100; i++) {
1052 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1053 break;
1054 udelay(10);
1055 }
1056}
1057
1058static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1059{
1060 u16 value = 0xffff;
1061 unsigned int i;
1062
1063 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1064
1065 for (i = 0; i < 100; i++) {
1066 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1067 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1068 break;
1069 }
1070 udelay(10);
1071 }
1072
1073 return value;
1074}
1075
1076static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1077{
1078 unsigned int i;
1079
1080 RTL_W32(CSIDR, value);
1081 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1082 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1083
1084 for (i = 0; i < 100; i++) {
1085 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1086 break;
1087 udelay(10);
1088 }
1089}
1090
1091static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1092{
1093 u32 value = ~0x00;
1094 unsigned int i;
1095
1096 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1097 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1098
1099 for (i = 0; i < 100; i++) {
1100 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1101 value = RTL_R32(CSIDR);
1102 break;
1103 }
1104 udelay(10);
1105 }
1106
1107 return value;
1108}
1109
Hayes Wang133ac402011-07-06 15:58:05 +08001110static
1111void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1112{
1113 unsigned int i;
1114
1115 BUG_ON((addr & 3) || (mask == 0));
1116 RTL_W32(ERIDR, val);
1117 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1118
1119 for (i = 0; i < 100; i++) {
1120 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1121 break;
1122 udelay(100);
1123 }
1124}
1125
1126static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1127{
1128 u32 value = ~0x00;
1129 unsigned int i;
1130
1131 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1132
1133 for (i = 0; i < 100; i++) {
1134 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1135 value = RTL_R32(ERIDR);
1136 break;
1137 }
1138 udelay(100);
1139 }
1140
1141 return value;
1142}
1143
1144static void
1145rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1146{
1147 u32 val;
1148
1149 val = rtl_eri_read(ioaddr, addr, type);
1150 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1151}
1152
françois romieuc28aa382011-08-02 03:53:43 +00001153struct exgmac_reg {
1154 u16 addr;
1155 u16 mask;
1156 u32 val;
1157};
1158
1159static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1160 const struct exgmac_reg *r, int len)
1161{
1162 while (len-- > 0) {
1163 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1164 r++;
1165 }
1166}
1167
françois romieudaf9df62009-10-07 12:44:20 +00001168static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1169{
1170 u8 value = 0xff;
1171 unsigned int i;
1172
1173 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1174
1175 for (i = 0; i < 300; i++) {
1176 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1177 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1178 break;
1179 }
1180 udelay(100);
1181 }
1182
1183 return value;
1184}
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
1187{
1188 RTL_W16(IntrMask, 0x0000);
1189
1190 RTL_W16(IntrStatus, 0xffff);
1191}
1192
françois romieu4da19632011-01-03 15:07:55 +00001193static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
françois romieu4da19632011-01-03 15:07:55 +00001195 void __iomem *ioaddr = tp->mmio_addr;
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return RTL_R32(TBICSR) & TBIReset;
1198}
1199
françois romieu4da19632011-01-03 15:07:55 +00001200static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
françois romieu4da19632011-01-03 15:07:55 +00001202 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
1205static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1206{
1207 return RTL_R32(TBICSR) & TBILinkOk;
1208}
1209
1210static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1211{
1212 return RTL_R8(PHYstatus) & LinkStatus;
1213}
1214
françois romieu4da19632011-01-03 15:07:55 +00001215static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
françois romieu4da19632011-01-03 15:07:55 +00001217 void __iomem *ioaddr = tp->mmio_addr;
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1220}
1221
françois romieu4da19632011-01-03 15:07:55 +00001222static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 unsigned int val;
1225
françois romieu4da19632011-01-03 15:07:55 +00001226 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1227 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
Hayes Wang70090422011-07-06 15:58:06 +08001230static void rtl_link_chg_patch(struct rtl8169_private *tp)
1231{
1232 void __iomem *ioaddr = tp->mmio_addr;
1233 struct net_device *dev = tp->dev;
1234
1235 if (!netif_running(dev))
1236 return;
1237
1238 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1239 if (RTL_R8(PHYstatus) & _1000bpsF) {
1240 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1241 0x00000011, ERIAR_EXGMAC);
1242 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1243 0x00000005, ERIAR_EXGMAC);
1244 } else if (RTL_R8(PHYstatus) & _100bps) {
1245 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1246 0x0000001f, ERIAR_EXGMAC);
1247 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1248 0x00000005, ERIAR_EXGMAC);
1249 } else {
1250 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1251 0x0000001f, ERIAR_EXGMAC);
1252 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1253 0x0000003f, ERIAR_EXGMAC);
1254 }
1255 /* Reset packet filter */
1256 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1257 ERIAR_EXGMAC);
1258 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1259 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001260 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1261 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1262 if (RTL_R8(PHYstatus) & _1000bpsF) {
1263 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1264 0x00000011, ERIAR_EXGMAC);
1265 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1266 0x00000005, ERIAR_EXGMAC);
1267 } else {
1268 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1269 0x0000001f, ERIAR_EXGMAC);
1270 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1271 0x0000003f, ERIAR_EXGMAC);
1272 }
Hayes Wang70090422011-07-06 15:58:06 +08001273 }
1274}
1275
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001276static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001277 struct rtl8169_private *tp,
1278 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 unsigned long flags;
1281
1282 spin_lock_irqsave(&tp->lock, flags);
1283 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001284 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001285 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001286 if (pm)
1287 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001289 if (net_ratelimit())
1290 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001291 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001293 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001294 if (pm)
1295 pm_schedule_suspend(&tp->pci_dev->dev, 100);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 spin_unlock_irqrestore(&tp->lock, flags);
1298}
1299
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001300static void rtl8169_check_link_status(struct net_device *dev,
1301 struct rtl8169_private *tp,
1302 void __iomem *ioaddr)
1303{
1304 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1305}
1306
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001307#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1308
1309static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1310{
1311 void __iomem *ioaddr = tp->mmio_addr;
1312 u8 options;
1313 u32 wolopts = 0;
1314
1315 options = RTL_R8(Config1);
1316 if (!(options & PMEnable))
1317 return 0;
1318
1319 options = RTL_R8(Config3);
1320 if (options & LinkUp)
1321 wolopts |= WAKE_PHY;
1322 if (options & MagicPacket)
1323 wolopts |= WAKE_MAGIC;
1324
1325 options = RTL_R8(Config5);
1326 if (options & UWF)
1327 wolopts |= WAKE_UCAST;
1328 if (options & BWF)
1329 wolopts |= WAKE_BCAST;
1330 if (options & MWF)
1331 wolopts |= WAKE_MCAST;
1332
1333 return wolopts;
1334}
1335
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001336static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1337{
1338 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001339
1340 spin_lock_irq(&tp->lock);
1341
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001342 wol->supported = WAKE_ANY;
1343 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001344
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001345 spin_unlock_irq(&tp->lock);
1346}
1347
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001348static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001349{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001350 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001351 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001352 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001353 u32 opt;
1354 u16 reg;
1355 u8 mask;
1356 } cfg[] = {
1357 { WAKE_ANY, Config1, PMEnable },
1358 { WAKE_PHY, Config3, LinkUp },
1359 { WAKE_MAGIC, Config3, MagicPacket },
1360 { WAKE_UCAST, Config5, UWF },
1361 { WAKE_BCAST, Config5, BWF },
1362 { WAKE_MCAST, Config5, MWF },
1363 { WAKE_ANY, Config5, LanWake }
1364 };
1365
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001366 RTL_W8(Cfg9346, Cfg9346_Unlock);
1367
1368 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1369 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001370 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001371 options |= cfg[i].mask;
1372 RTL_W8(cfg[i].reg, options);
1373 }
1374
1375 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001376}
1377
1378static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1379{
1380 struct rtl8169_private *tp = netdev_priv(dev);
1381
1382 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001383
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001384 if (wol->wolopts)
1385 tp->features |= RTL_FEATURE_WOL;
1386 else
1387 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001388 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001389 spin_unlock_irq(&tp->lock);
1390
françois romieuea809072010-11-08 13:23:58 +00001391 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1392
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001393 return 0;
1394}
1395
Francois Romieu31bd2042011-04-26 18:58:59 +02001396static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1397{
Francois Romieu85bffe62011-04-27 08:22:39 +02001398 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001399}
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401static void rtl8169_get_drvinfo(struct net_device *dev,
1402 struct ethtool_drvinfo *info)
1403{
1404 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001405 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 strcpy(info->driver, MODULENAME);
1408 strcpy(info->version, RTL8169_VERSION);
1409 strcpy(info->bus_info, pci_name(tp->pci_dev));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001410 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1411 strcpy(info->fw_version, IS_ERR_OR_NULL(rtl_fw) ? "N/A" :
1412 rtl_fw->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
1415static int rtl8169_get_regs_len(struct net_device *dev)
1416{
1417 return R8169_REGS_SIZE;
1418}
1419
1420static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001421 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 struct rtl8169_private *tp = netdev_priv(dev);
1424 void __iomem *ioaddr = tp->mmio_addr;
1425 int ret = 0;
1426 u32 reg;
1427
1428 reg = RTL_R32(TBICSR);
1429 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1430 (duplex == DUPLEX_FULL)) {
1431 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1432 } else if (autoneg == AUTONEG_ENABLE)
1433 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1434 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001435 netif_warn(tp, link, dev,
1436 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 ret = -EOPNOTSUPP;
1438 }
1439
1440 return ret;
1441}
1442
1443static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001444 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001447 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001448 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Hayes Wang716b50a2011-02-22 17:26:18 +08001450 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001453 int auto_nego;
1454
françois romieu4da19632011-01-03 15:07:55 +00001455 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001456 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1457 ADVERTISE_100HALF | ADVERTISE_100FULL);
1458
1459 if (adv & ADVERTISED_10baseT_Half)
1460 auto_nego |= ADVERTISE_10HALF;
1461 if (adv & ADVERTISED_10baseT_Full)
1462 auto_nego |= ADVERTISE_10FULL;
1463 if (adv & ADVERTISED_100baseT_Half)
1464 auto_nego |= ADVERTISE_100HALF;
1465 if (adv & ADVERTISED_100baseT_Full)
1466 auto_nego |= ADVERTISE_100FULL;
1467
françois romieu3577aa12009-05-19 10:46:48 +00001468 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1469
françois romieu4da19632011-01-03 15:07:55 +00001470 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001471 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1472
1473 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001474 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001475 if (adv & ADVERTISED_1000baseT_Half)
1476 giga_ctrl |= ADVERTISE_1000HALF;
1477 if (adv & ADVERTISED_1000baseT_Full)
1478 giga_ctrl |= ADVERTISE_1000FULL;
1479 } else if (adv & (ADVERTISED_1000baseT_Half |
1480 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001481 netif_info(tp, link, dev,
1482 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001483 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
françois romieu3577aa12009-05-19 10:46:48 +00001486 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001487
françois romieu4da19632011-01-03 15:07:55 +00001488 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1489 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001490 } else {
1491 giga_ctrl = 0;
1492
1493 if (speed == SPEED_10)
1494 bmcr = 0;
1495 else if (speed == SPEED_100)
1496 bmcr = BMCR_SPEED100;
1497 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001498 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001499
1500 if (duplex == DUPLEX_FULL)
1501 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001502 }
1503
françois romieu4da19632011-01-03 15:07:55 +00001504 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001505
Francois Romieucecb5fd2011-04-01 10:21:07 +02001506 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1507 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001508 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001509 rtl_writephy(tp, 0x17, 0x2138);
1510 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001511 } else {
françois romieu4da19632011-01-03 15:07:55 +00001512 rtl_writephy(tp, 0x17, 0x2108);
1513 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001514 }
1515 }
1516
Oliver Neukum54405cd2011-01-06 21:55:13 +01001517 rc = 0;
1518out:
1519 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
1522static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001523 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
1525 struct rtl8169_private *tp = netdev_priv(dev);
1526 int ret;
1527
Oliver Neukum54405cd2011-01-06 21:55:13 +01001528 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001529 if (ret < 0)
1530 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Francois Romieu4876cc12011-03-11 21:07:11 +01001532 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1533 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001535 }
1536out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return ret;
1538}
1539
1540static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1541{
1542 struct rtl8169_private *tp = netdev_priv(dev);
1543 unsigned long flags;
1544 int ret;
1545
Francois Romieu4876cc12011-03-11 21:07:11 +01001546 del_timer_sync(&tp->timer);
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 spin_lock_irqsave(&tp->lock, flags);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001549 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001550 cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return ret;
1554}
1555
Michał Mirosław350fb322011-04-08 06:35:56 +00001556static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Francois Romieud58d46b2011-05-03 16:38:29 +02001558 struct rtl8169_private *tp = netdev_priv(dev);
1559
Francois Romieu2b7b4312011-04-18 22:53:24 -07001560 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001561 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Francois Romieud58d46b2011-05-03 16:38:29 +02001563 if (dev->mtu > JUMBO_1K &&
1564 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1565 features &= ~NETIF_F_IP_CSUM;
1566
Michał Mirosław350fb322011-04-08 06:35:56 +00001567 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
Michał Mirosław350fb322011-04-08 06:35:56 +00001570static int rtl8169_set_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
1572 struct rtl8169_private *tp = netdev_priv(dev);
1573 void __iomem *ioaddr = tp->mmio_addr;
1574 unsigned long flags;
1575
1576 spin_lock_irqsave(&tp->lock, flags);
1577
Michał Mirosław350fb322011-04-08 06:35:56 +00001578 if (features & NETIF_F_RXCSUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 tp->cp_cmd |= RxChkSum;
1580 else
1581 tp->cp_cmd &= ~RxChkSum;
1582
Michał Mirosław350fb322011-04-08 06:35:56 +00001583 if (dev->features & NETIF_F_HW_VLAN_RX)
1584 tp->cp_cmd |= RxVlan;
1585 else
1586 tp->cp_cmd &= ~RxVlan;
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 RTL_W16(CPlusCmd, tp->cp_cmd);
1589 RTL_R16(CPlusCmd);
1590
1591 spin_unlock_irqrestore(&tp->lock, flags);
1592
1593 return 0;
1594}
1595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1597 struct sk_buff *skb)
1598{
Jesse Grosseab6d182010-10-20 13:56:03 +00001599 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1601}
1602
Francois Romieu7a8fc772011-03-01 17:18:33 +01001603static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
1605 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Francois Romieu7a8fc772011-03-01 17:18:33 +01001607 if (opts2 & RxVlanTag)
1608 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
1612
Francois Romieuccdffb92008-07-26 14:26:06 +02001613static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
1615 struct rtl8169_private *tp = netdev_priv(dev);
1616 void __iomem *ioaddr = tp->mmio_addr;
1617 u32 status;
1618
1619 cmd->supported =
1620 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1621 cmd->port = PORT_FIBRE;
1622 cmd->transceiver = XCVR_INTERNAL;
1623
1624 status = RTL_R32(TBICSR);
1625 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1626 cmd->autoneg = !!(status & TBINwEnable);
1627
David Decotigny70739492011-04-27 18:32:40 +00001628 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001630
1631 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
1633
Francois Romieuccdffb92008-07-26 14:26:06 +02001634static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
1636 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Francois Romieuccdffb92008-07-26 14:26:06 +02001638 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
1641static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1642{
1643 struct rtl8169_private *tp = netdev_priv(dev);
1644 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001645 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 spin_lock_irqsave(&tp->lock, flags);
1648
Francois Romieuccdffb92008-07-26 14:26:06 +02001649 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001652 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653}
1654
1655static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1656 void *p)
1657{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001658 struct rtl8169_private *tp = netdev_priv(dev);
1659 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Francois Romieu5b0384f2006-08-16 16:00:01 +02001661 if (regs->len > R8169_REGS_SIZE)
1662 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Francois Romieu5b0384f2006-08-16 16:00:01 +02001664 spin_lock_irqsave(&tp->lock, flags);
1665 memcpy_fromio(p, tp->mmio_addr, regs->len);
1666 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667}
1668
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001669static u32 rtl8169_get_msglevel(struct net_device *dev)
1670{
1671 struct rtl8169_private *tp = netdev_priv(dev);
1672
1673 return tp->msg_enable;
1674}
1675
1676static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1677{
1678 struct rtl8169_private *tp = netdev_priv(dev);
1679
1680 tp->msg_enable = value;
1681}
1682
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001683static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1684 "tx_packets",
1685 "rx_packets",
1686 "tx_errors",
1687 "rx_errors",
1688 "rx_missed",
1689 "align_errors",
1690 "tx_single_collisions",
1691 "tx_multi_collisions",
1692 "unicast",
1693 "broadcast",
1694 "multicast",
1695 "tx_aborted",
1696 "tx_underrun",
1697};
1698
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001699static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001700{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001701 switch (sset) {
1702 case ETH_SS_STATS:
1703 return ARRAY_SIZE(rtl8169_gstrings);
1704 default:
1705 return -EOPNOTSUPP;
1706 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001707}
1708
Ivan Vecera355423d2009-02-06 21:49:57 -08001709static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001710{
1711 struct rtl8169_private *tp = netdev_priv(dev);
1712 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001713 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001714 struct rtl8169_counters *counters;
1715 dma_addr_t paddr;
1716 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001717 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001718
Ivan Vecera355423d2009-02-06 21:49:57 -08001719 /*
1720 * Some chips are unable to dump tally counters when the receiver
1721 * is disabled.
1722 */
1723 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1724 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001725
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001726 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001727 if (!counters)
1728 return;
1729
1730 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001731 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001732 RTL_W32(CounterAddrLow, cmd);
1733 RTL_W32(CounterAddrLow, cmd | CounterDump);
1734
Ivan Vecera355423d2009-02-06 21:49:57 -08001735 while (wait--) {
1736 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001737 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001738 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001739 }
1740 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001741 }
1742
1743 RTL_W32(CounterAddrLow, 0);
1744 RTL_W32(CounterAddrHigh, 0);
1745
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001746 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001747}
1748
Ivan Vecera355423d2009-02-06 21:49:57 -08001749static void rtl8169_get_ethtool_stats(struct net_device *dev,
1750 struct ethtool_stats *stats, u64 *data)
1751{
1752 struct rtl8169_private *tp = netdev_priv(dev);
1753
1754 ASSERT_RTNL();
1755
1756 rtl8169_update_counters(dev);
1757
1758 data[0] = le64_to_cpu(tp->counters.tx_packets);
1759 data[1] = le64_to_cpu(tp->counters.rx_packets);
1760 data[2] = le64_to_cpu(tp->counters.tx_errors);
1761 data[3] = le32_to_cpu(tp->counters.rx_errors);
1762 data[4] = le16_to_cpu(tp->counters.rx_missed);
1763 data[5] = le16_to_cpu(tp->counters.align_errors);
1764 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1765 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1766 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1767 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1768 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1769 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1770 data[12] = le16_to_cpu(tp->counters.tx_underun);
1771}
1772
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001773static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1774{
1775 switch(stringset) {
1776 case ETH_SS_STATS:
1777 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1778 break;
1779 }
1780}
1781
Jeff Garzik7282d492006-09-13 14:30:00 -04001782static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 .get_drvinfo = rtl8169_get_drvinfo,
1784 .get_regs_len = rtl8169_get_regs_len,
1785 .get_link = ethtool_op_get_link,
1786 .get_settings = rtl8169_get_settings,
1787 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001788 .get_msglevel = rtl8169_get_msglevel,
1789 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001791 .get_wol = rtl8169_get_wol,
1792 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001793 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001794 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001795 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796};
1797
Francois Romieu07d3f512007-02-21 22:40:46 +01001798static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001799 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
Francois Romieu5d320a22011-05-08 17:47:36 +02001801 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001802 /*
1803 * The driver currently handles the 8168Bf and the 8168Be identically
1804 * but they can be identified more specifically through the test below
1805 * if needed:
1806 *
1807 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001808 *
1809 * Same thing for the 8101Eb and the 8101Ec:
1810 *
1811 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001812 */
Francois Romieu37441002011-06-17 22:58:54 +02001813 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001815 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 int mac_version;
1817 } mac_info[] = {
Hayes Wangc2218922011-09-06 16:55:18 +08001818 /* 8168F family. */
1819 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1820 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1821
hayeswang01dc7fe2011-03-21 01:50:28 +00001822 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001823 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001824 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1825 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1826 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1827
Francois Romieu5b538df2008-07-20 16:22:45 +02001828 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001829 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1830 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001831 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001832
françois romieue6de30d2011-01-03 15:08:37 +00001833 /* 8168DP family. */
1834 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1835 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001836 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001837
Francois Romieuef808d52008-06-29 13:10:54 +02001838 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001839 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001840 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001841 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001842 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001843 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1844 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001845 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001846 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001847 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001848
1849 /* 8168B family. */
1850 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1851 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1852 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1853 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1854
1855 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001856 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001857 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1858 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1859 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001860 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1861 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1862 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1863 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1864 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1865 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001866 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001867 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001868 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001869 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1870 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001871 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1872 /* FIXME: where did these entries come from ? -- FR */
1873 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1874 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1875
1876 /* 8110 family. */
1877 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1878 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1879 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1880 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1881 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1882 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1883
Jean Delvaref21b75e2009-05-26 20:54:48 -07001884 /* Catch-all */
1885 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001886 };
1887 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 u32 reg;
1889
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001890 reg = RTL_R32(TxConfig);
1891 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 p++;
1893 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001894
1895 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1896 netif_notice(tp, probe, dev,
1897 "unknown MAC, using family default\n");
1898 tp->mac_version = default_version;
1899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}
1901
1902static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1903{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001904 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}
1906
Francois Romieu867763c2007-08-17 18:21:58 +02001907struct phy_reg {
1908 u16 reg;
1909 u16 val;
1910};
1911
françois romieu4da19632011-01-03 15:07:55 +00001912static void rtl_writephy_batch(struct rtl8169_private *tp,
1913 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001914{
1915 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001916 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001917 regs++;
1918 }
1919}
1920
françois romieubca03d52011-01-03 15:07:31 +00001921#define PHY_READ 0x00000000
1922#define PHY_DATA_OR 0x10000000
1923#define PHY_DATA_AND 0x20000000
1924#define PHY_BJMPN 0x30000000
1925#define PHY_READ_EFUSE 0x40000000
1926#define PHY_READ_MAC_BYTE 0x50000000
1927#define PHY_WRITE_MAC_BYTE 0x60000000
1928#define PHY_CLEAR_READCOUNT 0x70000000
1929#define PHY_WRITE 0x80000000
1930#define PHY_READCOUNT_EQ_SKIP 0x90000000
1931#define PHY_COMP_EQ_SKIPN 0xa0000000
1932#define PHY_COMP_NEQ_SKIPN 0xb0000000
1933#define PHY_WRITE_PREVIOUS 0xc0000000
1934#define PHY_SKIPN 0xd0000000
1935#define PHY_DELAY_MS 0xe0000000
1936#define PHY_WRITE_ERI_WORD 0xf0000000
1937
Hayes Wang960aee62011-06-18 11:37:48 +02001938struct fw_info {
1939 u32 magic;
1940 char version[RTL_VER_SIZE];
1941 __le32 fw_start;
1942 __le32 fw_len;
1943 u8 chksum;
1944} __packed;
1945
Francois Romieu1c361ef2011-06-17 17:16:24 +02001946#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
1947
1948static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00001949{
Francois Romieub6ffd972011-06-17 17:00:05 +02001950 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02001951 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001952 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1953 char *version = rtl_fw->version;
1954 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00001955
Francois Romieu1c361ef2011-06-17 17:16:24 +02001956 if (fw->size < FW_OPCODE_SIZE)
1957 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02001958
1959 if (!fw_info->magic) {
1960 size_t i, size, start;
1961 u8 checksum = 0;
1962
1963 if (fw->size < sizeof(*fw_info))
1964 goto out;
1965
1966 for (i = 0; i < fw->size; i++)
1967 checksum += fw->data[i];
1968 if (checksum != 0)
1969 goto out;
1970
1971 start = le32_to_cpu(fw_info->fw_start);
1972 if (start > fw->size)
1973 goto out;
1974
1975 size = le32_to_cpu(fw_info->fw_len);
1976 if (size > (fw->size - start) / FW_OPCODE_SIZE)
1977 goto out;
1978
1979 memcpy(version, fw_info->version, RTL_VER_SIZE);
1980
1981 pa->code = (__le32 *)(fw->data + start);
1982 pa->size = size;
1983 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02001984 if (fw->size % FW_OPCODE_SIZE)
1985 goto out;
1986
1987 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
1988
1989 pa->code = (__le32 *)fw->data;
1990 pa->size = fw->size / FW_OPCODE_SIZE;
1991 }
1992 version[RTL_VER_SIZE - 1] = 0;
1993
1994 rc = true;
1995out:
1996 return rc;
1997}
1998
Francois Romieufd112f22011-06-18 00:10:29 +02001999static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2000 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002001{
Francois Romieufd112f22011-06-18 00:10:29 +02002002 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002003 size_t index;
2004
Francois Romieu1c361ef2011-06-17 17:16:24 +02002005 for (index = 0; index < pa->size; index++) {
2006 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002007 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002008
hayeswang42b82dc2011-01-10 02:07:25 +00002009 switch(action & 0xf0000000) {
2010 case PHY_READ:
2011 case PHY_DATA_OR:
2012 case PHY_DATA_AND:
2013 case PHY_READ_EFUSE:
2014 case PHY_CLEAR_READCOUNT:
2015 case PHY_WRITE:
2016 case PHY_WRITE_PREVIOUS:
2017 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002018 break;
2019
hayeswang42b82dc2011-01-10 02:07:25 +00002020 case PHY_BJMPN:
2021 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002022 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002023 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002024 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002025 }
2026 break;
2027 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002028 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002029 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002030 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002031 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002032 }
2033 break;
2034 case PHY_COMP_EQ_SKIPN:
2035 case PHY_COMP_NEQ_SKIPN:
2036 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002037 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002038 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002039 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002040 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002041 }
2042 break;
2043
2044 case PHY_READ_MAC_BYTE:
2045 case PHY_WRITE_MAC_BYTE:
2046 case PHY_WRITE_ERI_WORD:
2047 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002048 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002049 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002050 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002051 }
2052 }
Francois Romieufd112f22011-06-18 00:10:29 +02002053 rc = true;
2054out:
2055 return rc;
2056}
françois romieubca03d52011-01-03 15:07:31 +00002057
Francois Romieufd112f22011-06-18 00:10:29 +02002058static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2059{
2060 struct net_device *dev = tp->dev;
2061 int rc = -EINVAL;
2062
2063 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2064 netif_err(tp, ifup, dev, "invalid firwmare\n");
2065 goto out;
2066 }
2067
2068 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2069 rc = 0;
2070out:
2071 return rc;
2072}
2073
2074static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2075{
2076 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2077 u32 predata, count;
2078 size_t index;
2079
2080 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002081
Francois Romieu1c361ef2011-06-17 17:16:24 +02002082 for (index = 0; index < pa->size; ) {
2083 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002084 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002085 u32 regno = (action & 0x0fff0000) >> 16;
2086
2087 if (!action)
2088 break;
françois romieubca03d52011-01-03 15:07:31 +00002089
2090 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002091 case PHY_READ:
2092 predata = rtl_readphy(tp, regno);
2093 count++;
2094 index++;
françois romieubca03d52011-01-03 15:07:31 +00002095 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002096 case PHY_DATA_OR:
2097 predata |= data;
2098 index++;
2099 break;
2100 case PHY_DATA_AND:
2101 predata &= data;
2102 index++;
2103 break;
2104 case PHY_BJMPN:
2105 index -= regno;
2106 break;
2107 case PHY_READ_EFUSE:
2108 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2109 index++;
2110 break;
2111 case PHY_CLEAR_READCOUNT:
2112 count = 0;
2113 index++;
2114 break;
2115 case PHY_WRITE:
2116 rtl_writephy(tp, regno, data);
2117 index++;
2118 break;
2119 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002120 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002121 break;
2122 case PHY_COMP_EQ_SKIPN:
2123 if (predata == data)
2124 index += regno;
2125 index++;
2126 break;
2127 case PHY_COMP_NEQ_SKIPN:
2128 if (predata != data)
2129 index += regno;
2130 index++;
2131 break;
2132 case PHY_WRITE_PREVIOUS:
2133 rtl_writephy(tp, regno, predata);
2134 index++;
2135 break;
2136 case PHY_SKIPN:
2137 index += regno + 1;
2138 break;
2139 case PHY_DELAY_MS:
2140 mdelay(data);
2141 index++;
2142 break;
2143
2144 case PHY_READ_MAC_BYTE:
2145 case PHY_WRITE_MAC_BYTE:
2146 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002147 default:
2148 BUG();
2149 }
2150 }
2151}
2152
françois romieuf1e02ed2011-01-13 13:07:53 +00002153static void rtl_release_firmware(struct rtl8169_private *tp)
2154{
Francois Romieub6ffd972011-06-17 17:00:05 +02002155 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2156 release_firmware(tp->rtl_fw->fw);
2157 kfree(tp->rtl_fw);
2158 }
2159 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002160}
2161
François Romieu953a12c2011-04-24 17:38:48 +02002162static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002163{
Francois Romieub6ffd972011-06-17 17:00:05 +02002164 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002165
2166 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002167 if (!IS_ERR_OR_NULL(rtl_fw))
2168 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002169}
2170
2171static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2172{
2173 if (rtl_readphy(tp, reg) != val)
2174 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2175 else
2176 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002177}
2178
françois romieu4da19632011-01-03 15:07:55 +00002179static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002181 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002182 { 0x1f, 0x0001 },
2183 { 0x06, 0x006e },
2184 { 0x08, 0x0708 },
2185 { 0x15, 0x4000 },
2186 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
françois romieu0b9b5712009-08-10 19:44:56 +00002188 { 0x1f, 0x0001 },
2189 { 0x03, 0x00a1 },
2190 { 0x02, 0x0008 },
2191 { 0x01, 0x0120 },
2192 { 0x00, 0x1000 },
2193 { 0x04, 0x0800 },
2194 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
françois romieu0b9b5712009-08-10 19:44:56 +00002196 { 0x03, 0xff41 },
2197 { 0x02, 0xdf60 },
2198 { 0x01, 0x0140 },
2199 { 0x00, 0x0077 },
2200 { 0x04, 0x7800 },
2201 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
françois romieu0b9b5712009-08-10 19:44:56 +00002203 { 0x03, 0x802f },
2204 { 0x02, 0x4f02 },
2205 { 0x01, 0x0409 },
2206 { 0x00, 0xf0f9 },
2207 { 0x04, 0x9800 },
2208 { 0x04, 0x9000 },
2209
2210 { 0x03, 0xdf01 },
2211 { 0x02, 0xdf20 },
2212 { 0x01, 0xff95 },
2213 { 0x00, 0xba00 },
2214 { 0x04, 0xa800 },
2215 { 0x04, 0xa000 },
2216
2217 { 0x03, 0xff41 },
2218 { 0x02, 0xdf20 },
2219 { 0x01, 0x0140 },
2220 { 0x00, 0x00bb },
2221 { 0x04, 0xb800 },
2222 { 0x04, 0xb000 },
2223
2224 { 0x03, 0xdf41 },
2225 { 0x02, 0xdc60 },
2226 { 0x01, 0x6340 },
2227 { 0x00, 0x007d },
2228 { 0x04, 0xd800 },
2229 { 0x04, 0xd000 },
2230
2231 { 0x03, 0xdf01 },
2232 { 0x02, 0xdf20 },
2233 { 0x01, 0x100a },
2234 { 0x00, 0xa0ff },
2235 { 0x04, 0xf800 },
2236 { 0x04, 0xf000 },
2237
2238 { 0x1f, 0x0000 },
2239 { 0x0b, 0x0000 },
2240 { 0x00, 0x9200 }
2241 };
2242
françois romieu4da19632011-01-03 15:07:55 +00002243 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244}
2245
françois romieu4da19632011-01-03 15:07:55 +00002246static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002247{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002248 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002249 { 0x1f, 0x0002 },
2250 { 0x01, 0x90d0 },
2251 { 0x1f, 0x0000 }
2252 };
2253
françois romieu4da19632011-01-03 15:07:55 +00002254 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002255}
2256
françois romieu4da19632011-01-03 15:07:55 +00002257static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002258{
2259 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002260
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002261 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2262 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002263 return;
2264
françois romieu4da19632011-01-03 15:07:55 +00002265 rtl_writephy(tp, 0x1f, 0x0001);
2266 rtl_writephy(tp, 0x10, 0xf01b);
2267 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002268}
2269
françois romieu4da19632011-01-03 15:07:55 +00002270static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002271{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002272 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002273 { 0x1f, 0x0001 },
2274 { 0x04, 0x0000 },
2275 { 0x03, 0x00a1 },
2276 { 0x02, 0x0008 },
2277 { 0x01, 0x0120 },
2278 { 0x00, 0x1000 },
2279 { 0x04, 0x0800 },
2280 { 0x04, 0x9000 },
2281 { 0x03, 0x802f },
2282 { 0x02, 0x4f02 },
2283 { 0x01, 0x0409 },
2284 { 0x00, 0xf099 },
2285 { 0x04, 0x9800 },
2286 { 0x04, 0xa000 },
2287 { 0x03, 0xdf01 },
2288 { 0x02, 0xdf20 },
2289 { 0x01, 0xff95 },
2290 { 0x00, 0xba00 },
2291 { 0x04, 0xa800 },
2292 { 0x04, 0xf000 },
2293 { 0x03, 0xdf01 },
2294 { 0x02, 0xdf20 },
2295 { 0x01, 0x101a },
2296 { 0x00, 0xa0ff },
2297 { 0x04, 0xf800 },
2298 { 0x04, 0x0000 },
2299 { 0x1f, 0x0000 },
2300
2301 { 0x1f, 0x0001 },
2302 { 0x10, 0xf41b },
2303 { 0x14, 0xfb54 },
2304 { 0x18, 0xf5c7 },
2305 { 0x1f, 0x0000 },
2306
2307 { 0x1f, 0x0001 },
2308 { 0x17, 0x0cc0 },
2309 { 0x1f, 0x0000 }
2310 };
2311
françois romieu4da19632011-01-03 15:07:55 +00002312 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002313
françois romieu4da19632011-01-03 15:07:55 +00002314 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002315}
2316
françois romieu4da19632011-01-03 15:07:55 +00002317static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002318{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002319 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002320 { 0x1f, 0x0001 },
2321 { 0x04, 0x0000 },
2322 { 0x03, 0x00a1 },
2323 { 0x02, 0x0008 },
2324 { 0x01, 0x0120 },
2325 { 0x00, 0x1000 },
2326 { 0x04, 0x0800 },
2327 { 0x04, 0x9000 },
2328 { 0x03, 0x802f },
2329 { 0x02, 0x4f02 },
2330 { 0x01, 0x0409 },
2331 { 0x00, 0xf099 },
2332 { 0x04, 0x9800 },
2333 { 0x04, 0xa000 },
2334 { 0x03, 0xdf01 },
2335 { 0x02, 0xdf20 },
2336 { 0x01, 0xff95 },
2337 { 0x00, 0xba00 },
2338 { 0x04, 0xa800 },
2339 { 0x04, 0xf000 },
2340 { 0x03, 0xdf01 },
2341 { 0x02, 0xdf20 },
2342 { 0x01, 0x101a },
2343 { 0x00, 0xa0ff },
2344 { 0x04, 0xf800 },
2345 { 0x04, 0x0000 },
2346 { 0x1f, 0x0000 },
2347
2348 { 0x1f, 0x0001 },
2349 { 0x0b, 0x8480 },
2350 { 0x1f, 0x0000 },
2351
2352 { 0x1f, 0x0001 },
2353 { 0x18, 0x67c7 },
2354 { 0x04, 0x2000 },
2355 { 0x03, 0x002f },
2356 { 0x02, 0x4360 },
2357 { 0x01, 0x0109 },
2358 { 0x00, 0x3022 },
2359 { 0x04, 0x2800 },
2360 { 0x1f, 0x0000 },
2361
2362 { 0x1f, 0x0001 },
2363 { 0x17, 0x0cc0 },
2364 { 0x1f, 0x0000 }
2365 };
2366
françois romieu4da19632011-01-03 15:07:55 +00002367 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002368}
2369
françois romieu4da19632011-01-03 15:07:55 +00002370static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002371{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002372 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002373 { 0x10, 0xf41b },
2374 { 0x1f, 0x0000 }
2375 };
2376
françois romieu4da19632011-01-03 15:07:55 +00002377 rtl_writephy(tp, 0x1f, 0x0001);
2378 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002379
françois romieu4da19632011-01-03 15:07:55 +00002380 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002381}
2382
françois romieu4da19632011-01-03 15:07:55 +00002383static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002384{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002385 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002386 { 0x1f, 0x0001 },
2387 { 0x10, 0xf41b },
2388 { 0x1f, 0x0000 }
2389 };
2390
françois romieu4da19632011-01-03 15:07:55 +00002391 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002392}
2393
françois romieu4da19632011-01-03 15:07:55 +00002394static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002395{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002396 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002397 { 0x1f, 0x0000 },
2398 { 0x1d, 0x0f00 },
2399 { 0x1f, 0x0002 },
2400 { 0x0c, 0x1ec8 },
2401 { 0x1f, 0x0000 }
2402 };
2403
françois romieu4da19632011-01-03 15:07:55 +00002404 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002405}
2406
françois romieu4da19632011-01-03 15:07:55 +00002407static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002408{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002409 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002410 { 0x1f, 0x0001 },
2411 { 0x1d, 0x3d98 },
2412 { 0x1f, 0x0000 }
2413 };
2414
françois romieu4da19632011-01-03 15:07:55 +00002415 rtl_writephy(tp, 0x1f, 0x0000);
2416 rtl_patchphy(tp, 0x14, 1 << 5);
2417 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002418
françois romieu4da19632011-01-03 15:07:55 +00002419 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002420}
2421
françois romieu4da19632011-01-03 15:07:55 +00002422static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002423{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002424 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002425 { 0x1f, 0x0001 },
2426 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002427 { 0x1f, 0x0002 },
2428 { 0x00, 0x88d4 },
2429 { 0x01, 0x82b1 },
2430 { 0x03, 0x7002 },
2431 { 0x08, 0x9e30 },
2432 { 0x09, 0x01f0 },
2433 { 0x0a, 0x5500 },
2434 { 0x0c, 0x00c8 },
2435 { 0x1f, 0x0003 },
2436 { 0x12, 0xc096 },
2437 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002438 { 0x1f, 0x0000 },
2439 { 0x1f, 0x0000 },
2440 { 0x09, 0x2000 },
2441 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002442 };
2443
françois romieu4da19632011-01-03 15:07:55 +00002444 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002445
françois romieu4da19632011-01-03 15:07:55 +00002446 rtl_patchphy(tp, 0x14, 1 << 5);
2447 rtl_patchphy(tp, 0x0d, 1 << 5);
2448 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002449}
2450
françois romieu4da19632011-01-03 15:07:55 +00002451static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002452{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002453 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002454 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002455 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002456 { 0x03, 0x802f },
2457 { 0x02, 0x4f02 },
2458 { 0x01, 0x0409 },
2459 { 0x00, 0xf099 },
2460 { 0x04, 0x9800 },
2461 { 0x04, 0x9000 },
2462 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002463 { 0x1f, 0x0002 },
2464 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002465 { 0x06, 0x0761 },
2466 { 0x1f, 0x0003 },
2467 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002468 { 0x1f, 0x0000 }
2469 };
2470
françois romieu4da19632011-01-03 15:07:55 +00002471 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002472
françois romieu4da19632011-01-03 15:07:55 +00002473 rtl_patchphy(tp, 0x16, 1 << 0);
2474 rtl_patchphy(tp, 0x14, 1 << 5);
2475 rtl_patchphy(tp, 0x0d, 1 << 5);
2476 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002477}
2478
françois romieu4da19632011-01-03 15:07:55 +00002479static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002480{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002481 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002482 { 0x1f, 0x0001 },
2483 { 0x12, 0x2300 },
2484 { 0x1d, 0x3d98 },
2485 { 0x1f, 0x0002 },
2486 { 0x0c, 0x7eb8 },
2487 { 0x06, 0x5461 },
2488 { 0x1f, 0x0003 },
2489 { 0x16, 0x0f0a },
2490 { 0x1f, 0x0000 }
2491 };
2492
françois romieu4da19632011-01-03 15:07:55 +00002493 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002494
françois romieu4da19632011-01-03 15:07:55 +00002495 rtl_patchphy(tp, 0x16, 1 << 0);
2496 rtl_patchphy(tp, 0x14, 1 << 5);
2497 rtl_patchphy(tp, 0x0d, 1 << 5);
2498 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002499}
2500
françois romieu4da19632011-01-03 15:07:55 +00002501static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002502{
françois romieu4da19632011-01-03 15:07:55 +00002503 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002504}
2505
françois romieubca03d52011-01-03 15:07:31 +00002506static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002507{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002508 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002509 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002510 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002511 { 0x06, 0x4064 },
2512 { 0x07, 0x2863 },
2513 { 0x08, 0x059c },
2514 { 0x09, 0x26b4 },
2515 { 0x0a, 0x6a19 },
2516 { 0x0b, 0xdcc8 },
2517 { 0x10, 0xf06d },
2518 { 0x14, 0x7f68 },
2519 { 0x18, 0x7fd9 },
2520 { 0x1c, 0xf0ff },
2521 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002522 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002523 { 0x12, 0xf49f },
2524 { 0x13, 0x070b },
2525 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002526 { 0x14, 0x94c0 },
2527
2528 /*
2529 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002530 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002531 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002532 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002533 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002534 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002535 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002536 { 0x06, 0x5561 },
2537
2538 /*
2539 * Can not link to 1Gbps with bad cable
2540 * Decrease SNR threshold form 21.07dB to 19.04dB
2541 */
2542 { 0x1f, 0x0001 },
2543 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002544
2545 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002546 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002547 };
françois romieubca03d52011-01-03 15:07:31 +00002548 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002549
françois romieu4da19632011-01-03 15:07:55 +00002550 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002551
françois romieubca03d52011-01-03 15:07:31 +00002552 /*
2553 * Rx Error Issue
2554 * Fine Tune Switching regulator parameter
2555 */
françois romieu4da19632011-01-03 15:07:55 +00002556 rtl_writephy(tp, 0x1f, 0x0002);
2557 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2558 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002559
françois romieudaf9df62009-10-07 12:44:20 +00002560 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002561 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002562 { 0x1f, 0x0002 },
2563 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002564 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002565 { 0x05, 0x8330 },
2566 { 0x06, 0x669a },
2567 { 0x1f, 0x0002 }
2568 };
2569 int val;
2570
françois romieu4da19632011-01-03 15:07:55 +00002571 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002572
françois romieu4da19632011-01-03 15:07:55 +00002573 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002574
2575 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002576 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002577 0x0065, 0x0066, 0x0067, 0x0068,
2578 0x0069, 0x006a, 0x006b, 0x006c
2579 };
2580 int i;
2581
françois romieu4da19632011-01-03 15:07:55 +00002582 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002583
2584 val &= 0xff00;
2585 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002586 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002587 }
2588 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002589 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002590 { 0x1f, 0x0002 },
2591 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002592 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002593 { 0x05, 0x8330 },
2594 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002595 };
2596
françois romieu4da19632011-01-03 15:07:55 +00002597 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002598 }
2599
françois romieubca03d52011-01-03 15:07:31 +00002600 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002601 rtl_writephy(tp, 0x1f, 0x0002);
2602 rtl_patchphy(tp, 0x0d, 0x0300);
2603 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002604
françois romieubca03d52011-01-03 15:07:31 +00002605 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002606 rtl_writephy(tp, 0x1f, 0x0002);
2607 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2608 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002609
françois romieu4da19632011-01-03 15:07:55 +00002610 rtl_writephy(tp, 0x1f, 0x0005);
2611 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002612
2613 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002614
françois romieu4da19632011-01-03 15:07:55 +00002615 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002616}
2617
françois romieubca03d52011-01-03 15:07:31 +00002618static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002619{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002620 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002621 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002622 { 0x1f, 0x0001 },
2623 { 0x06, 0x4064 },
2624 { 0x07, 0x2863 },
2625 { 0x08, 0x059c },
2626 { 0x09, 0x26b4 },
2627 { 0x0a, 0x6a19 },
2628 { 0x0b, 0xdcc8 },
2629 { 0x10, 0xf06d },
2630 { 0x14, 0x7f68 },
2631 { 0x18, 0x7fd9 },
2632 { 0x1c, 0xf0ff },
2633 { 0x1d, 0x3d9c },
2634 { 0x1f, 0x0003 },
2635 { 0x12, 0xf49f },
2636 { 0x13, 0x070b },
2637 { 0x1a, 0x05ad },
2638 { 0x14, 0x94c0 },
2639
françois romieubca03d52011-01-03 15:07:31 +00002640 /*
2641 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002642 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002643 */
françois romieudaf9df62009-10-07 12:44:20 +00002644 { 0x1f, 0x0002 },
2645 { 0x06, 0x5561 },
2646 { 0x1f, 0x0005 },
2647 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002648 { 0x06, 0x5561 },
2649
2650 /*
2651 * Can not link to 1Gbps with bad cable
2652 * Decrease SNR threshold form 21.07dB to 19.04dB
2653 */
2654 { 0x1f, 0x0001 },
2655 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002656
2657 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002658 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002659 };
françois romieubca03d52011-01-03 15:07:31 +00002660 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002661
françois romieu4da19632011-01-03 15:07:55 +00002662 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002663
2664 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002665 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002666 { 0x1f, 0x0002 },
2667 { 0x05, 0x669a },
2668 { 0x1f, 0x0005 },
2669 { 0x05, 0x8330 },
2670 { 0x06, 0x669a },
2671
2672 { 0x1f, 0x0002 }
2673 };
2674 int val;
2675
françois romieu4da19632011-01-03 15:07:55 +00002676 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002677
françois romieu4da19632011-01-03 15:07:55 +00002678 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002679 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002680 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002681 0x0065, 0x0066, 0x0067, 0x0068,
2682 0x0069, 0x006a, 0x006b, 0x006c
2683 };
2684 int i;
2685
françois romieu4da19632011-01-03 15:07:55 +00002686 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002687
2688 val &= 0xff00;
2689 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002690 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002691 }
2692 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002693 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002694 { 0x1f, 0x0002 },
2695 { 0x05, 0x2642 },
2696 { 0x1f, 0x0005 },
2697 { 0x05, 0x8330 },
2698 { 0x06, 0x2642 }
2699 };
2700
françois romieu4da19632011-01-03 15:07:55 +00002701 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002702 }
2703
françois romieubca03d52011-01-03 15:07:31 +00002704 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002705 rtl_writephy(tp, 0x1f, 0x0002);
2706 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2707 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002708
françois romieubca03d52011-01-03 15:07:31 +00002709 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002710 rtl_writephy(tp, 0x1f, 0x0002);
2711 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002712
françois romieu4da19632011-01-03 15:07:55 +00002713 rtl_writephy(tp, 0x1f, 0x0005);
2714 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002715
2716 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002717
françois romieu4da19632011-01-03 15:07:55 +00002718 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002719}
2720
françois romieu4da19632011-01-03 15:07:55 +00002721static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002722{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002723 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002724 { 0x1f, 0x0002 },
2725 { 0x10, 0x0008 },
2726 { 0x0d, 0x006c },
2727
2728 { 0x1f, 0x0000 },
2729 { 0x0d, 0xf880 },
2730
2731 { 0x1f, 0x0001 },
2732 { 0x17, 0x0cc0 },
2733
2734 { 0x1f, 0x0001 },
2735 { 0x0b, 0xa4d8 },
2736 { 0x09, 0x281c },
2737 { 0x07, 0x2883 },
2738 { 0x0a, 0x6b35 },
2739 { 0x1d, 0x3da4 },
2740 { 0x1c, 0xeffd },
2741 { 0x14, 0x7f52 },
2742 { 0x18, 0x7fc6 },
2743 { 0x08, 0x0601 },
2744 { 0x06, 0x4063 },
2745 { 0x10, 0xf074 },
2746 { 0x1f, 0x0003 },
2747 { 0x13, 0x0789 },
2748 { 0x12, 0xf4bd },
2749 { 0x1a, 0x04fd },
2750 { 0x14, 0x84b0 },
2751 { 0x1f, 0x0000 },
2752 { 0x00, 0x9200 },
2753
2754 { 0x1f, 0x0005 },
2755 { 0x01, 0x0340 },
2756 { 0x1f, 0x0001 },
2757 { 0x04, 0x4000 },
2758 { 0x03, 0x1d21 },
2759 { 0x02, 0x0c32 },
2760 { 0x01, 0x0200 },
2761 { 0x00, 0x5554 },
2762 { 0x04, 0x4800 },
2763 { 0x04, 0x4000 },
2764 { 0x04, 0xf000 },
2765 { 0x03, 0xdf01 },
2766 { 0x02, 0xdf20 },
2767 { 0x01, 0x101a },
2768 { 0x00, 0xa0ff },
2769 { 0x04, 0xf800 },
2770 { 0x04, 0xf000 },
2771 { 0x1f, 0x0000 },
2772
2773 { 0x1f, 0x0007 },
2774 { 0x1e, 0x0023 },
2775 { 0x16, 0x0000 },
2776 { 0x1f, 0x0000 }
2777 };
2778
françois romieu4da19632011-01-03 15:07:55 +00002779 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002780}
2781
françois romieue6de30d2011-01-03 15:08:37 +00002782static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2783{
2784 static const struct phy_reg phy_reg_init[] = {
2785 { 0x1f, 0x0001 },
2786 { 0x17, 0x0cc0 },
2787
2788 { 0x1f, 0x0007 },
2789 { 0x1e, 0x002d },
2790 { 0x18, 0x0040 },
2791 { 0x1f, 0x0000 }
2792 };
2793
2794 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2795 rtl_patchphy(tp, 0x0d, 1 << 5);
2796}
2797
Hayes Wang70090422011-07-06 15:58:06 +08002798static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002799{
2800 static const struct phy_reg phy_reg_init[] = {
2801 /* Enable Delay cap */
2802 { 0x1f, 0x0005 },
2803 { 0x05, 0x8b80 },
2804 { 0x06, 0xc896 },
2805 { 0x1f, 0x0000 },
2806
2807 /* Channel estimation fine tune */
2808 { 0x1f, 0x0001 },
2809 { 0x0b, 0x6c20 },
2810 { 0x07, 0x2872 },
2811 { 0x1c, 0xefff },
2812 { 0x1f, 0x0003 },
2813 { 0x14, 0x6420 },
2814 { 0x1f, 0x0000 },
2815
2816 /* Update PFM & 10M TX idle timer */
2817 { 0x1f, 0x0007 },
2818 { 0x1e, 0x002f },
2819 { 0x15, 0x1919 },
2820 { 0x1f, 0x0000 },
2821
2822 { 0x1f, 0x0007 },
2823 { 0x1e, 0x00ac },
2824 { 0x18, 0x0006 },
2825 { 0x1f, 0x0000 }
2826 };
2827
Francois Romieu15ecd032011-04-27 13:52:22 -07002828 rtl_apply_firmware(tp);
2829
hayeswang01dc7fe2011-03-21 01:50:28 +00002830 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2831
2832 /* DCO enable for 10M IDLE Power */
2833 rtl_writephy(tp, 0x1f, 0x0007);
2834 rtl_writephy(tp, 0x1e, 0x0023);
2835 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2836 rtl_writephy(tp, 0x1f, 0x0000);
2837
2838 /* For impedance matching */
2839 rtl_writephy(tp, 0x1f, 0x0002);
2840 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002841 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002842
2843 /* PHY auto speed down */
2844 rtl_writephy(tp, 0x1f, 0x0007);
2845 rtl_writephy(tp, 0x1e, 0x002d);
2846 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2847 rtl_writephy(tp, 0x1f, 0x0000);
2848 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2849
2850 rtl_writephy(tp, 0x1f, 0x0005);
2851 rtl_writephy(tp, 0x05, 0x8b86);
2852 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2853 rtl_writephy(tp, 0x1f, 0x0000);
2854
2855 rtl_writephy(tp, 0x1f, 0x0005);
2856 rtl_writephy(tp, 0x05, 0x8b85);
2857 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2858 rtl_writephy(tp, 0x1f, 0x0007);
2859 rtl_writephy(tp, 0x1e, 0x0020);
2860 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2861 rtl_writephy(tp, 0x1f, 0x0006);
2862 rtl_writephy(tp, 0x00, 0x5a00);
2863 rtl_writephy(tp, 0x1f, 0x0000);
2864 rtl_writephy(tp, 0x0d, 0x0007);
2865 rtl_writephy(tp, 0x0e, 0x003c);
2866 rtl_writephy(tp, 0x0d, 0x4007);
2867 rtl_writephy(tp, 0x0e, 0x0000);
2868 rtl_writephy(tp, 0x0d, 0x0000);
2869}
2870
Hayes Wang70090422011-07-06 15:58:06 +08002871static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2872{
2873 static const struct phy_reg phy_reg_init[] = {
2874 /* Enable Delay cap */
2875 { 0x1f, 0x0004 },
2876 { 0x1f, 0x0007 },
2877 { 0x1e, 0x00ac },
2878 { 0x18, 0x0006 },
2879 { 0x1f, 0x0002 },
2880 { 0x1f, 0x0000 },
2881 { 0x1f, 0x0000 },
2882
2883 /* Channel estimation fine tune */
2884 { 0x1f, 0x0003 },
2885 { 0x09, 0xa20f },
2886 { 0x1f, 0x0000 },
2887 { 0x1f, 0x0000 },
2888
2889 /* Green Setting */
2890 { 0x1f, 0x0005 },
2891 { 0x05, 0x8b5b },
2892 { 0x06, 0x9222 },
2893 { 0x05, 0x8b6d },
2894 { 0x06, 0x8000 },
2895 { 0x05, 0x8b76 },
2896 { 0x06, 0x8000 },
2897 { 0x1f, 0x0000 }
2898 };
2899
2900 rtl_apply_firmware(tp);
2901
2902 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2903
2904 /* For 4-corner performance improve */
2905 rtl_writephy(tp, 0x1f, 0x0005);
2906 rtl_writephy(tp, 0x05, 0x8b80);
2907 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2908 rtl_writephy(tp, 0x1f, 0x0000);
2909
2910 /* PHY auto speed down */
2911 rtl_writephy(tp, 0x1f, 0x0004);
2912 rtl_writephy(tp, 0x1f, 0x0007);
2913 rtl_writephy(tp, 0x1e, 0x002d);
2914 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2915 rtl_writephy(tp, 0x1f, 0x0002);
2916 rtl_writephy(tp, 0x1f, 0x0000);
2917 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2918
2919 /* improve 10M EEE waveform */
2920 rtl_writephy(tp, 0x1f, 0x0005);
2921 rtl_writephy(tp, 0x05, 0x8b86);
2922 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2923 rtl_writephy(tp, 0x1f, 0x0000);
2924
2925 /* Improve 2-pair detection performance */
2926 rtl_writephy(tp, 0x1f, 0x0005);
2927 rtl_writephy(tp, 0x05, 0x8b85);
2928 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
2929 rtl_writephy(tp, 0x1f, 0x0000);
2930
2931 /* EEE setting */
2932 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
2933 ERIAR_EXGMAC);
2934 rtl_writephy(tp, 0x1f, 0x0005);
2935 rtl_writephy(tp, 0x05, 0x8b85);
2936 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2937 rtl_writephy(tp, 0x1f, 0x0004);
2938 rtl_writephy(tp, 0x1f, 0x0007);
2939 rtl_writephy(tp, 0x1e, 0x0020);
2940 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
2941 rtl_writephy(tp, 0x1f, 0x0002);
2942 rtl_writephy(tp, 0x1f, 0x0000);
2943 rtl_writephy(tp, 0x0d, 0x0007);
2944 rtl_writephy(tp, 0x0e, 0x003c);
2945 rtl_writephy(tp, 0x0d, 0x4007);
2946 rtl_writephy(tp, 0x0e, 0x0000);
2947 rtl_writephy(tp, 0x0d, 0x0000);
2948
2949 /* Green feature */
2950 rtl_writephy(tp, 0x1f, 0x0003);
2951 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
2952 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
2953 rtl_writephy(tp, 0x1f, 0x0000);
2954}
2955
Hayes Wangc2218922011-09-06 16:55:18 +08002956static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
2957{
2958 static const struct phy_reg phy_reg_init[] = {
2959 /* Channel estimation fine tune */
2960 { 0x1f, 0x0003 },
2961 { 0x09, 0xa20f },
2962 { 0x1f, 0x0000 },
2963
2964 /* Modify green table for giga & fnet */
2965 { 0x1f, 0x0005 },
2966 { 0x05, 0x8b55 },
2967 { 0x06, 0x0000 },
2968 { 0x05, 0x8b5e },
2969 { 0x06, 0x0000 },
2970 { 0x05, 0x8b67 },
2971 { 0x06, 0x0000 },
2972 { 0x05, 0x8b70 },
2973 { 0x06, 0x0000 },
2974 { 0x1f, 0x0000 },
2975 { 0x1f, 0x0007 },
2976 { 0x1e, 0x0078 },
2977 { 0x17, 0x0000 },
2978 { 0x19, 0x00fb },
2979 { 0x1f, 0x0000 },
2980
2981 /* Modify green table for 10M */
2982 { 0x1f, 0x0005 },
2983 { 0x05, 0x8b79 },
2984 { 0x06, 0xaa00 },
2985 { 0x1f, 0x0000 },
2986
2987 /* Disable hiimpedance detection (RTCT) */
2988 { 0x1f, 0x0003 },
2989 { 0x01, 0x328a },
2990 { 0x1f, 0x0000 }
2991 };
2992
2993 rtl_apply_firmware(tp);
2994
2995 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2996
2997 /* For 4-corner performance improve */
2998 rtl_writephy(tp, 0x1f, 0x0005);
2999 rtl_writephy(tp, 0x05, 0x8b80);
3000 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3001 rtl_writephy(tp, 0x1f, 0x0000);
3002
3003 /* PHY auto speed down */
3004 rtl_writephy(tp, 0x1f, 0x0007);
3005 rtl_writephy(tp, 0x1e, 0x002d);
3006 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3007 rtl_writephy(tp, 0x1f, 0x0000);
3008 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3009
3010 /* Improve 10M EEE waveform */
3011 rtl_writephy(tp, 0x1f, 0x0005);
3012 rtl_writephy(tp, 0x05, 0x8b86);
3013 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3014 rtl_writephy(tp, 0x1f, 0x0000);
3015
3016 /* Improve 2-pair detection performance */
3017 rtl_writephy(tp, 0x1f, 0x0005);
3018 rtl_writephy(tp, 0x05, 0x8b85);
3019 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3020 rtl_writephy(tp, 0x1f, 0x0000);
3021}
3022
3023static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3024{
3025 rtl_apply_firmware(tp);
3026
3027 /* For 4-corner performance improve */
3028 rtl_writephy(tp, 0x1f, 0x0005);
3029 rtl_writephy(tp, 0x05, 0x8b80);
3030 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3031 rtl_writephy(tp, 0x1f, 0x0000);
3032
3033 /* PHY auto speed down */
3034 rtl_writephy(tp, 0x1f, 0x0007);
3035 rtl_writephy(tp, 0x1e, 0x002d);
3036 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3037 rtl_writephy(tp, 0x1f, 0x0000);
3038 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3039
3040 /* Improve 10M EEE waveform */
3041 rtl_writephy(tp, 0x1f, 0x0005);
3042 rtl_writephy(tp, 0x05, 0x8b86);
3043 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3044 rtl_writephy(tp, 0x1f, 0x0000);
3045}
3046
françois romieu4da19632011-01-03 15:07:55 +00003047static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003048{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003049 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003050 { 0x1f, 0x0003 },
3051 { 0x08, 0x441d },
3052 { 0x01, 0x9100 },
3053 { 0x1f, 0x0000 }
3054 };
3055
françois romieu4da19632011-01-03 15:07:55 +00003056 rtl_writephy(tp, 0x1f, 0x0000);
3057 rtl_patchphy(tp, 0x11, 1 << 12);
3058 rtl_patchphy(tp, 0x19, 1 << 13);
3059 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003060
françois romieu4da19632011-01-03 15:07:55 +00003061 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003062}
3063
Hayes Wang5a5e4442011-02-22 17:26:21 +08003064static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3065{
3066 static const struct phy_reg phy_reg_init[] = {
3067 { 0x1f, 0x0005 },
3068 { 0x1a, 0x0000 },
3069 { 0x1f, 0x0000 },
3070
3071 { 0x1f, 0x0004 },
3072 { 0x1c, 0x0000 },
3073 { 0x1f, 0x0000 },
3074
3075 { 0x1f, 0x0001 },
3076 { 0x15, 0x7701 },
3077 { 0x1f, 0x0000 }
3078 };
3079
3080 /* Disable ALDPS before ram code */
3081 rtl_writephy(tp, 0x1f, 0x0000);
3082 rtl_writephy(tp, 0x18, 0x0310);
3083 msleep(100);
3084
François Romieu953a12c2011-04-24 17:38:48 +02003085 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003086
3087 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3088}
3089
Francois Romieu5615d9f2007-08-17 17:50:46 +02003090static void rtl_hw_phy_config(struct net_device *dev)
3091{
3092 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003093
3094 rtl8169_print_mac_version(tp);
3095
3096 switch (tp->mac_version) {
3097 case RTL_GIGA_MAC_VER_01:
3098 break;
3099 case RTL_GIGA_MAC_VER_02:
3100 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003101 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003102 break;
3103 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003104 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003105 break;
françois romieu2e9558562009-08-10 19:44:19 +00003106 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003107 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003108 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003109 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003110 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003111 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003112 case RTL_GIGA_MAC_VER_07:
3113 case RTL_GIGA_MAC_VER_08:
3114 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003115 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003116 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003117 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003118 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003119 break;
3120 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003121 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003122 break;
3123 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003124 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003125 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003126 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003127 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003128 break;
3129 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003130 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003131 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003132 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003133 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003134 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003135 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003136 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003137 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003138 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003139 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003140 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003141 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003142 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003143 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003144 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003145 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003146 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003147 break;
3148 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003149 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003150 break;
3151 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003152 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003153 break;
françois romieue6de30d2011-01-03 15:08:37 +00003154 case RTL_GIGA_MAC_VER_28:
3155 rtl8168d_4_hw_phy_config(tp);
3156 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003157 case RTL_GIGA_MAC_VER_29:
3158 case RTL_GIGA_MAC_VER_30:
3159 rtl8105e_hw_phy_config(tp);
3160 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003161 case RTL_GIGA_MAC_VER_31:
3162 /* None. */
3163 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003164 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003165 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003166 rtl8168e_1_hw_phy_config(tp);
3167 break;
3168 case RTL_GIGA_MAC_VER_34:
3169 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003170 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003171 case RTL_GIGA_MAC_VER_35:
3172 rtl8168f_1_hw_phy_config(tp);
3173 break;
3174 case RTL_GIGA_MAC_VER_36:
3175 rtl8168f_2_hw_phy_config(tp);
3176 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003177
Francois Romieu5615d9f2007-08-17 17:50:46 +02003178 default:
3179 break;
3180 }
3181}
3182
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183static void rtl8169_phy_timer(unsigned long __opaque)
3184{
3185 struct net_device *dev = (struct net_device *)__opaque;
3186 struct rtl8169_private *tp = netdev_priv(dev);
3187 struct timer_list *timer = &tp->timer;
3188 void __iomem *ioaddr = tp->mmio_addr;
3189 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3190
Francois Romieubcf0bf92006-07-26 23:14:13 +02003191 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 spin_lock_irq(&tp->lock);
3194
françois romieu4da19632011-01-03 15:07:55 +00003195 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003196 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 * A busy loop could burn quite a few cycles on nowadays CPU.
3198 * Let's delay the execution of the timer for a few ticks.
3199 */
3200 timeout = HZ/10;
3201 goto out_mod_timer;
3202 }
3203
3204 if (tp->link_ok(ioaddr))
3205 goto out_unlock;
3206
Joe Perchesbf82c182010-02-09 11:49:50 +00003207 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
françois romieu4da19632011-01-03 15:07:55 +00003209 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
3211out_mod_timer:
3212 mod_timer(timer, jiffies + timeout);
3213out_unlock:
3214 spin_unlock_irq(&tp->lock);
3215}
3216
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217#ifdef CONFIG_NET_POLL_CONTROLLER
3218/*
3219 * Polling 'interrupt' - used by things like netconsole to send skbs
3220 * without having to re-enable interrupts. It's not called while
3221 * the interrupt routine is executing.
3222 */
3223static void rtl8169_netpoll(struct net_device *dev)
3224{
3225 struct rtl8169_private *tp = netdev_priv(dev);
3226 struct pci_dev *pdev = tp->pci_dev;
3227
3228 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01003229 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 enable_irq(pdev->irq);
3231}
3232#endif
3233
3234static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3235 void __iomem *ioaddr)
3236{
3237 iounmap(ioaddr);
3238 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003239 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 pci_disable_device(pdev);
3241 free_netdev(dev);
3242}
3243
Francois Romieubf793292006-11-01 00:53:05 +01003244static void rtl8169_phy_reset(struct net_device *dev,
3245 struct rtl8169_private *tp)
3246{
Francois Romieu07d3f512007-02-21 22:40:46 +01003247 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003248
françois romieu4da19632011-01-03 15:07:55 +00003249 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003250 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003251 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003252 return;
3253 msleep(1);
3254 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003255 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003256}
3257
David S. Miller8decf862011-09-22 03:23:13 -04003258static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3259{
3260 void __iomem *ioaddr = tp->mmio_addr;
3261
3262 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3263 (RTL_R8(PHYstatus) & TBI_Enable);
3264}
3265
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003266static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003268 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003269
Francois Romieu5615d9f2007-08-17 17:50:46 +02003270 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003271
Marcus Sundberg773328942008-07-10 21:28:08 +02003272 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3273 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3274 RTL_W8(0x82, 0x01);
3275 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003276
Francois Romieu6dccd162007-02-13 23:38:05 +01003277 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3278
3279 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3280 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003281
Francois Romieubcf0bf92006-07-26 23:14:13 +02003282 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003283 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3284 RTL_W8(0x82, 0x01);
3285 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003286 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003287 }
3288
Francois Romieubf793292006-11-01 00:53:05 +01003289 rtl8169_phy_reset(dev, tp);
3290
Oliver Neukum54405cd2011-01-06 21:55:13 +01003291 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003292 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3293 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3294 (tp->mii.supports_gmii ?
3295 ADVERTISED_1000baseT_Half |
3296 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003297
David S. Miller8decf862011-09-22 03:23:13 -04003298 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003299 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003300}
3301
Francois Romieu773d2022007-01-31 23:47:43 +01003302static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3303{
3304 void __iomem *ioaddr = tp->mmio_addr;
3305 u32 high;
3306 u32 low;
3307
3308 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3309 high = addr[4] | (addr[5] << 8);
3310
3311 spin_lock_irq(&tp->lock);
3312
3313 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2bf2010-04-26 11:42:58 +00003314
Francois Romieu773d2022007-01-31 23:47:43 +01003315 RTL_W32(MAC4, high);
françois romieu908ba2bf2010-04-26 11:42:58 +00003316 RTL_R32(MAC4);
3317
Francois Romieu78f1cd02010-03-27 19:35:46 -07003318 RTL_W32(MAC0, low);
françois romieu908ba2bf2010-04-26 11:42:58 +00003319 RTL_R32(MAC0);
3320
françois romieuc28aa382011-08-02 03:53:43 +00003321 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3322 const struct exgmac_reg e[] = {
3323 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3324 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3325 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3326 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3327 low >> 16 },
3328 };
3329
3330 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3331 }
3332
Francois Romieu773d2022007-01-31 23:47:43 +01003333 RTL_W8(Cfg9346, Cfg9346_Lock);
3334
3335 spin_unlock_irq(&tp->lock);
3336}
3337
3338static int rtl_set_mac_address(struct net_device *dev, void *p)
3339{
3340 struct rtl8169_private *tp = netdev_priv(dev);
3341 struct sockaddr *addr = p;
3342
3343 if (!is_valid_ether_addr(addr->sa_data))
3344 return -EADDRNOTAVAIL;
3345
3346 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3347
3348 rtl_rar_set(tp, dev->dev_addr);
3349
3350 return 0;
3351}
3352
Francois Romieu5f787a12006-08-17 13:02:36 +02003353static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3354{
3355 struct rtl8169_private *tp = netdev_priv(dev);
3356 struct mii_ioctl_data *data = if_mii(ifr);
3357
Francois Romieu8b4ab282008-11-19 22:05:25 -08003358 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3359}
Francois Romieu5f787a12006-08-17 13:02:36 +02003360
Francois Romieucecb5fd2011-04-01 10:21:07 +02003361static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3362 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003363{
Francois Romieu5f787a12006-08-17 13:02:36 +02003364 switch (cmd) {
3365 case SIOCGMIIPHY:
3366 data->phy_id = 32; /* Internal PHY */
3367 return 0;
3368
3369 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003370 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003371 return 0;
3372
3373 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003374 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003375 return 0;
3376 }
3377 return -EOPNOTSUPP;
3378}
3379
Francois Romieu8b4ab282008-11-19 22:05:25 -08003380static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3381{
3382 return -EOPNOTSUPP;
3383}
3384
Francois Romieu0e485152007-02-20 00:00:26 +01003385static const struct rtl_cfg_info {
3386 void (*hw_start)(struct net_device *);
3387 unsigned int region;
3388 unsigned int align;
3389 u16 intr_event;
3390 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02003391 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07003392 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01003393} rtl_cfg_infos [] = {
3394 [RTL_CFG_0] = {
3395 .hw_start = rtl_hw_start_8169,
3396 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01003397 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01003398 .intr_event = SYSErr | LinkChg | RxOverflow |
3399 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003400 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003401 .features = RTL_FEATURE_GMII,
3402 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01003403 },
3404 [RTL_CFG_1] = {
3405 .hw_start = rtl_hw_start_8168,
3406 .region = 2,
3407 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00003408 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01003409 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003410 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003411 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3412 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01003413 },
3414 [RTL_CFG_2] = {
3415 .hw_start = rtl_hw_start_8101,
3416 .region = 2,
3417 .align = 8,
3418 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
3419 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003420 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003421 .features = RTL_FEATURE_MSI,
3422 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01003423 }
3424};
3425
Francois Romieufbac58f2007-10-04 22:51:38 +02003426/* Cfg9346_Unlock assumed. */
3427static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
3428 const struct rtl_cfg_info *cfg)
3429{
3430 unsigned msi = 0;
3431 u8 cfg2;
3432
3433 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02003434 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02003435 if (pci_enable_msi(pdev)) {
3436 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
3437 } else {
3438 cfg2 |= MSIEnable;
3439 msi = RTL_FEATURE_MSI;
3440 }
3441 }
3442 RTL_W8(Config2, cfg2);
3443 return msi;
3444}
3445
3446static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3447{
3448 if (tp->features & RTL_FEATURE_MSI) {
3449 pci_disable_msi(pdev);
3450 tp->features &= ~RTL_FEATURE_MSI;
3451 }
3452}
3453
Francois Romieu8b4ab282008-11-19 22:05:25 -08003454static const struct net_device_ops rtl8169_netdev_ops = {
3455 .ndo_open = rtl8169_open,
3456 .ndo_stop = rtl8169_close,
3457 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08003458 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003459 .ndo_tx_timeout = rtl8169_tx_timeout,
3460 .ndo_validate_addr = eth_validate_addr,
3461 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00003462 .ndo_fix_features = rtl8169_fix_features,
3463 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003464 .ndo_set_mac_address = rtl_set_mac_address,
3465 .ndo_do_ioctl = rtl8169_ioctl,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003466 .ndo_set_rx_mode = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003467#ifdef CONFIG_NET_POLL_CONTROLLER
3468 .ndo_poll_controller = rtl8169_netpoll,
3469#endif
3470
3471};
3472
françois romieuc0e45c12011-01-03 15:08:04 +00003473static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3474{
3475 struct mdio_ops *ops = &tp->mdio_ops;
3476
3477 switch (tp->mac_version) {
3478 case RTL_GIGA_MAC_VER_27:
3479 ops->write = r8168dp_1_mdio_write;
3480 ops->read = r8168dp_1_mdio_read;
3481 break;
françois romieue6de30d2011-01-03 15:08:37 +00003482 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003483 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003484 ops->write = r8168dp_2_mdio_write;
3485 ops->read = r8168dp_2_mdio_read;
3486 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003487 default:
3488 ops->write = r8169_mdio_write;
3489 ops->read = r8169_mdio_read;
3490 break;
3491 }
3492}
3493
françois romieu065c27c2011-01-03 15:08:12 +00003494static void r810x_phy_power_down(struct rtl8169_private *tp)
3495{
3496 rtl_writephy(tp, 0x1f, 0x0000);
3497 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3498}
3499
3500static void r810x_phy_power_up(struct rtl8169_private *tp)
3501{
3502 rtl_writephy(tp, 0x1f, 0x0000);
3503 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3504}
3505
3506static void r810x_pll_power_down(struct rtl8169_private *tp)
3507{
David S. Miller8decf862011-09-22 03:23:13 -04003508 void __iomem *ioaddr = tp->mmio_addr;
3509
françois romieu065c27c2011-01-03 15:08:12 +00003510 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3511 rtl_writephy(tp, 0x1f, 0x0000);
3512 rtl_writephy(tp, MII_BMCR, 0x0000);
David S. Miller8decf862011-09-22 03:23:13 -04003513
3514 if (tp->mac_version == RTL_GIGA_MAC_VER_29 ||
3515 tp->mac_version == RTL_GIGA_MAC_VER_30)
3516 RTL_W32(RxConfig, RTL_R32(RxConfig) | AcceptBroadcast |
3517 AcceptMulticast | AcceptMyPhys);
françois romieu065c27c2011-01-03 15:08:12 +00003518 return;
3519 }
3520
3521 r810x_phy_power_down(tp);
3522}
3523
3524static void r810x_pll_power_up(struct rtl8169_private *tp)
3525{
3526 r810x_phy_power_up(tp);
3527}
3528
3529static void r8168_phy_power_up(struct rtl8169_private *tp)
3530{
3531 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003532 switch (tp->mac_version) {
3533 case RTL_GIGA_MAC_VER_11:
3534 case RTL_GIGA_MAC_VER_12:
3535 case RTL_GIGA_MAC_VER_17:
3536 case RTL_GIGA_MAC_VER_18:
3537 case RTL_GIGA_MAC_VER_19:
3538 case RTL_GIGA_MAC_VER_20:
3539 case RTL_GIGA_MAC_VER_21:
3540 case RTL_GIGA_MAC_VER_22:
3541 case RTL_GIGA_MAC_VER_23:
3542 case RTL_GIGA_MAC_VER_24:
3543 case RTL_GIGA_MAC_VER_25:
3544 case RTL_GIGA_MAC_VER_26:
3545 case RTL_GIGA_MAC_VER_27:
3546 case RTL_GIGA_MAC_VER_28:
3547 case RTL_GIGA_MAC_VER_31:
3548 rtl_writephy(tp, 0x0e, 0x0000);
3549 break;
3550 default:
3551 break;
3552 }
françois romieu065c27c2011-01-03 15:08:12 +00003553 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3554}
3555
3556static void r8168_phy_power_down(struct rtl8169_private *tp)
3557{
3558 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003559 switch (tp->mac_version) {
3560 case RTL_GIGA_MAC_VER_32:
3561 case RTL_GIGA_MAC_VER_33:
3562 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3563 break;
3564
3565 case RTL_GIGA_MAC_VER_11:
3566 case RTL_GIGA_MAC_VER_12:
3567 case RTL_GIGA_MAC_VER_17:
3568 case RTL_GIGA_MAC_VER_18:
3569 case RTL_GIGA_MAC_VER_19:
3570 case RTL_GIGA_MAC_VER_20:
3571 case RTL_GIGA_MAC_VER_21:
3572 case RTL_GIGA_MAC_VER_22:
3573 case RTL_GIGA_MAC_VER_23:
3574 case RTL_GIGA_MAC_VER_24:
3575 case RTL_GIGA_MAC_VER_25:
3576 case RTL_GIGA_MAC_VER_26:
3577 case RTL_GIGA_MAC_VER_27:
3578 case RTL_GIGA_MAC_VER_28:
3579 case RTL_GIGA_MAC_VER_31:
3580 rtl_writephy(tp, 0x0e, 0x0200);
3581 default:
3582 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3583 break;
3584 }
françois romieu065c27c2011-01-03 15:08:12 +00003585}
3586
3587static void r8168_pll_power_down(struct rtl8169_private *tp)
3588{
3589 void __iomem *ioaddr = tp->mmio_addr;
3590
Francois Romieucecb5fd2011-04-01 10:21:07 +02003591 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3592 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3593 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003594 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003595 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003596 }
françois romieu065c27c2011-01-03 15:08:12 +00003597
Francois Romieucecb5fd2011-04-01 10:21:07 +02003598 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3599 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003600 (RTL_R16(CPlusCmd) & ASF)) {
3601 return;
3602 }
3603
hayeswang01dc7fe2011-03-21 01:50:28 +00003604 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3605 tp->mac_version == RTL_GIGA_MAC_VER_33)
3606 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3607
françois romieu065c27c2011-01-03 15:08:12 +00003608 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3609 rtl_writephy(tp, 0x1f, 0x0000);
3610 rtl_writephy(tp, MII_BMCR, 0x0000);
3611
Hayes Wangd4ed95d2011-07-06 15:58:07 +08003612 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
David S. Miller8decf862011-09-22 03:23:13 -04003613 tp->mac_version == RTL_GIGA_MAC_VER_33 ||
3614 tp->mac_version == RTL_GIGA_MAC_VER_34)
Hayes Wangd4ed95d2011-07-06 15:58:07 +08003615 RTL_W32(RxConfig, RTL_R32(RxConfig) | AcceptBroadcast |
3616 AcceptMulticast | AcceptMyPhys);
françois romieu065c27c2011-01-03 15:08:12 +00003617 return;
3618 }
3619
3620 r8168_phy_power_down(tp);
3621
3622 switch (tp->mac_version) {
3623 case RTL_GIGA_MAC_VER_25:
3624 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003625 case RTL_GIGA_MAC_VER_27:
3626 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003627 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003628 case RTL_GIGA_MAC_VER_32:
3629 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003630 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3631 break;
3632 }
3633}
3634
3635static void r8168_pll_power_up(struct rtl8169_private *tp)
3636{
3637 void __iomem *ioaddr = tp->mmio_addr;
3638
Francois Romieucecb5fd2011-04-01 10:21:07 +02003639 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3640 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3641 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003642 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003643 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003644 }
françois romieu065c27c2011-01-03 15:08:12 +00003645
3646 switch (tp->mac_version) {
3647 case RTL_GIGA_MAC_VER_25:
3648 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003649 case RTL_GIGA_MAC_VER_27:
3650 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003651 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003652 case RTL_GIGA_MAC_VER_32:
3653 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003654 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3655 break;
3656 }
3657
3658 r8168_phy_power_up(tp);
3659}
3660
Francois Romieud58d46b2011-05-03 16:38:29 +02003661static void rtl_generic_op(struct rtl8169_private *tp,
3662 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00003663{
3664 if (op)
3665 op(tp);
3666}
3667
3668static void rtl_pll_power_down(struct rtl8169_private *tp)
3669{
Francois Romieud58d46b2011-05-03 16:38:29 +02003670 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00003671}
3672
3673static void rtl_pll_power_up(struct rtl8169_private *tp)
3674{
Francois Romieud58d46b2011-05-03 16:38:29 +02003675 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00003676}
3677
3678static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3679{
3680 struct pll_power_ops *ops = &tp->pll_power_ops;
3681
3682 switch (tp->mac_version) {
3683 case RTL_GIGA_MAC_VER_07:
3684 case RTL_GIGA_MAC_VER_08:
3685 case RTL_GIGA_MAC_VER_09:
3686 case RTL_GIGA_MAC_VER_10:
3687 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003688 case RTL_GIGA_MAC_VER_29:
3689 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003690 ops->down = r810x_pll_power_down;
3691 ops->up = r810x_pll_power_up;
3692 break;
3693
3694 case RTL_GIGA_MAC_VER_11:
3695 case RTL_GIGA_MAC_VER_12:
3696 case RTL_GIGA_MAC_VER_17:
3697 case RTL_GIGA_MAC_VER_18:
3698 case RTL_GIGA_MAC_VER_19:
3699 case RTL_GIGA_MAC_VER_20:
3700 case RTL_GIGA_MAC_VER_21:
3701 case RTL_GIGA_MAC_VER_22:
3702 case RTL_GIGA_MAC_VER_23:
3703 case RTL_GIGA_MAC_VER_24:
3704 case RTL_GIGA_MAC_VER_25:
3705 case RTL_GIGA_MAC_VER_26:
3706 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003707 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003708 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003709 case RTL_GIGA_MAC_VER_32:
3710 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003711 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08003712 case RTL_GIGA_MAC_VER_35:
3713 case RTL_GIGA_MAC_VER_36:
françois romieu065c27c2011-01-03 15:08:12 +00003714 ops->down = r8168_pll_power_down;
3715 ops->up = r8168_pll_power_up;
3716 break;
3717
3718 default:
3719 ops->down = NULL;
3720 ops->up = NULL;
3721 break;
3722 }
3723}
3724
Hayes Wange542a222011-07-06 15:58:04 +08003725static void rtl_init_rxcfg(struct rtl8169_private *tp)
3726{
3727 void __iomem *ioaddr = tp->mmio_addr;
3728
3729 switch (tp->mac_version) {
3730 case RTL_GIGA_MAC_VER_01:
3731 case RTL_GIGA_MAC_VER_02:
3732 case RTL_GIGA_MAC_VER_03:
3733 case RTL_GIGA_MAC_VER_04:
3734 case RTL_GIGA_MAC_VER_05:
3735 case RTL_GIGA_MAC_VER_06:
3736 case RTL_GIGA_MAC_VER_10:
3737 case RTL_GIGA_MAC_VER_11:
3738 case RTL_GIGA_MAC_VER_12:
3739 case RTL_GIGA_MAC_VER_13:
3740 case RTL_GIGA_MAC_VER_14:
3741 case RTL_GIGA_MAC_VER_15:
3742 case RTL_GIGA_MAC_VER_16:
3743 case RTL_GIGA_MAC_VER_17:
3744 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3745 break;
3746 case RTL_GIGA_MAC_VER_18:
3747 case RTL_GIGA_MAC_VER_19:
3748 case RTL_GIGA_MAC_VER_20:
3749 case RTL_GIGA_MAC_VER_21:
3750 case RTL_GIGA_MAC_VER_22:
3751 case RTL_GIGA_MAC_VER_23:
3752 case RTL_GIGA_MAC_VER_24:
3753 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3754 break;
3755 default:
3756 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3757 break;
3758 }
3759}
3760
Hayes Wang92fc43b2011-07-06 15:58:03 +08003761static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3762{
3763 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3764}
3765
Francois Romieud58d46b2011-05-03 16:38:29 +02003766static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3767{
3768 rtl_generic_op(tp, tp->jumbo_ops.enable);
3769}
3770
3771static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3772{
3773 rtl_generic_op(tp, tp->jumbo_ops.disable);
3774}
3775
3776static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3777{
3778 void __iomem *ioaddr = tp->mmio_addr;
3779
3780 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3781 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3782 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3783}
3784
3785static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3786{
3787 void __iomem *ioaddr = tp->mmio_addr;
3788
3789 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3790 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3791 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3792}
3793
3794static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3795{
3796 void __iomem *ioaddr = tp->mmio_addr;
3797
3798 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3799}
3800
3801static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3802{
3803 void __iomem *ioaddr = tp->mmio_addr;
3804
3805 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3806}
3807
3808static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3809{
3810 void __iomem *ioaddr = tp->mmio_addr;
3811 struct pci_dev *pdev = tp->pci_dev;
3812
3813 RTL_W8(MaxTxPacketSize, 0x3f);
3814 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3815 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
3816 pci_write_config_byte(pdev, 0x79, 0x20);
3817}
3818
3819static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3820{
3821 void __iomem *ioaddr = tp->mmio_addr;
3822 struct pci_dev *pdev = tp->pci_dev;
3823
3824 RTL_W8(MaxTxPacketSize, 0x0c);
3825 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3826 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
3827 pci_write_config_byte(pdev, 0x79, 0x50);
3828}
3829
3830static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3831{
3832 rtl_tx_performance_tweak(tp->pci_dev,
3833 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3834}
3835
3836static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3837{
3838 rtl_tx_performance_tweak(tp->pci_dev,
3839 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3840}
3841
3842static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3843{
3844 void __iomem *ioaddr = tp->mmio_addr;
3845
3846 r8168b_0_hw_jumbo_enable(tp);
3847
3848 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
3849}
3850
3851static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3852{
3853 void __iomem *ioaddr = tp->mmio_addr;
3854
3855 r8168b_0_hw_jumbo_disable(tp);
3856
3857 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3858}
3859
3860static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
3861{
3862 struct jumbo_ops *ops = &tp->jumbo_ops;
3863
3864 switch (tp->mac_version) {
3865 case RTL_GIGA_MAC_VER_11:
3866 ops->disable = r8168b_0_hw_jumbo_disable;
3867 ops->enable = r8168b_0_hw_jumbo_enable;
3868 break;
3869 case RTL_GIGA_MAC_VER_12:
3870 case RTL_GIGA_MAC_VER_17:
3871 ops->disable = r8168b_1_hw_jumbo_disable;
3872 ops->enable = r8168b_1_hw_jumbo_enable;
3873 break;
3874 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
3875 case RTL_GIGA_MAC_VER_19:
3876 case RTL_GIGA_MAC_VER_20:
3877 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
3878 case RTL_GIGA_MAC_VER_22:
3879 case RTL_GIGA_MAC_VER_23:
3880 case RTL_GIGA_MAC_VER_24:
3881 case RTL_GIGA_MAC_VER_25:
3882 case RTL_GIGA_MAC_VER_26:
3883 ops->disable = r8168c_hw_jumbo_disable;
3884 ops->enable = r8168c_hw_jumbo_enable;
3885 break;
3886 case RTL_GIGA_MAC_VER_27:
3887 case RTL_GIGA_MAC_VER_28:
3888 ops->disable = r8168dp_hw_jumbo_disable;
3889 ops->enable = r8168dp_hw_jumbo_enable;
3890 break;
3891 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
3892 case RTL_GIGA_MAC_VER_32:
3893 case RTL_GIGA_MAC_VER_33:
3894 case RTL_GIGA_MAC_VER_34:
3895 ops->disable = r8168e_hw_jumbo_disable;
3896 ops->enable = r8168e_hw_jumbo_enable;
3897 break;
3898
3899 /*
3900 * No action needed for jumbo frames with 8169.
3901 * No jumbo for 810x at all.
3902 */
3903 default:
3904 ops->disable = NULL;
3905 ops->enable = NULL;
3906 break;
3907 }
3908}
3909
Francois Romieu6f43adc2011-04-29 15:05:51 +02003910static void rtl_hw_reset(struct rtl8169_private *tp)
3911{
3912 void __iomem *ioaddr = tp->mmio_addr;
3913 int i;
3914
3915 /* Soft reset the chip. */
3916 RTL_W8(ChipCmd, CmdReset);
3917
3918 /* Check that the chip has finished the reset. */
3919 for (i = 0; i < 100; i++) {
3920 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3921 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003922 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003923 }
Hayes Wang92fc43b2011-07-06 15:58:03 +08003924
3925 rtl8169_init_ring_indexes(tp);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003926}
3927
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003928static int __devinit
3929rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3930{
Francois Romieu0e485152007-02-20 00:00:26 +01003931 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3932 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003934 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003935 struct net_device *dev;
3936 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07003937 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01003938 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003940 if (netif_msg_drv(&debug)) {
3941 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3942 MODULENAME, RTL8169_VERSION);
3943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003946 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003947 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003948 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003949 rc = -ENOMEM;
3950 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 }
3952
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003954 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003956 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003957 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003958 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
Francois Romieuccdffb92008-07-26 14:26:06 +02003960 mii = &tp->mii;
3961 mii->dev = dev;
3962 mii->mdio_read = rtl_mdio_read;
3963 mii->mdio_write = rtl_mdio_write;
3964 mii->phy_id_mask = 0x1f;
3965 mii->reg_num_mask = 0x1f;
3966 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3967
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003968 /* disable ASPM completely as that cause random device stop working
3969 * problems as well as full system hangs for some PCIe devices users */
3970 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3971 PCIE_LINK_STATE_CLKPM);
3972
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3974 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003975 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003976 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003977 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 }
3979
françois romieu87aeec72010-04-26 11:42:06 +00003980 if (pci_set_mwi(pdev) < 0)
3981 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003984 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003985 netif_err(tp, probe, dev,
3986 "region #%d not an MMIO resource, aborting\n",
3987 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003989 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003991
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003993 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003994 netif_err(tp, probe, dev,
3995 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003997 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 }
3999
4000 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02004001 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004002 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00004003 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 }
4005
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004006 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007
4008 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07004009 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 tp->cp_cmd |= PCIDAC;
4011 dev->features |= NETIF_F_HIGHDMA;
4012 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07004013 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004015 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00004016 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 }
4018 }
4019
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02004021 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004022 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004023 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00004025 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 }
Francois Romieu6f43adc2011-04-29 15:05:51 +02004027 tp->mmio_addr = ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028
Jon Masone44daad2011-06-27 07:46:31 +00004029 if (!pci_is_pcie(pdev))
4030 netif_info(tp, probe, dev, "not PCI Express\n");
David S. Miller4300e8c2010-03-26 10:23:30 -07004031
Hayes Wange542a222011-07-06 15:58:04 +08004032 /* Identify chip attached to board */
4033 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
4034
4035 rtl_init_rxcfg(tp);
4036
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07004037 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038
Francois Romieu6f43adc2011-04-29 15:05:51 +02004039 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07004041 RTL_W16(IntrStatus, 0xffff);
4042
françois romieuca52efd2009-07-24 12:34:19 +00004043 pci_set_master(pdev);
4044
Francois Romieu7a8fc772011-03-01 17:18:33 +01004045 /*
4046 * Pretend we are using VLANs; This bypasses a nasty bug where
4047 * Interrupts stop flowing on high load on 8110SCd controllers.
4048 */
4049 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4050 tp->cp_cmd |= RxVlan;
4051
françois romieuc0e45c12011-01-03 15:08:04 +00004052 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004053 rtl_init_pll_power_ops(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02004054 rtl_init_jumbo_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00004055
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057
Francois Romieu85bffe62011-04-27 08:22:39 +02004058 chipset = tp->mac_version;
4059 tp->txd_version = rtl_chip_infos[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
Francois Romieu5d06a992006-02-23 00:47:58 +01004061 RTL_W8(Cfg9346, Cfg9346_Unlock);
4062 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
4063 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07004064 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
4065 tp->features |= RTL_FEATURE_WOL;
4066 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
4067 tp->features |= RTL_FEATURE_WOL;
Francois Romieufbac58f2007-10-04 22:51:38 +02004068 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01004069 RTL_W8(Cfg9346, Cfg9346_Lock);
4070
David S. Miller8decf862011-09-22 03:23:13 -04004071 if (rtl_tbi_enabled(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 tp->set_speed = rtl8169_set_speed_tbi;
4073 tp->get_settings = rtl8169_gset_tbi;
4074 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
4075 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
4076 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08004077 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 } else {
4079 tp->set_speed = rtl8169_set_speed_xmii;
4080 tp->get_settings = rtl8169_gset_xmii;
4081 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
4082 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
4083 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08004084 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 }
4086
Francois Romieudf58ef512008-10-09 14:35:58 -07004087 spin_lock_init(&tp->lock);
4088
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00004089 /* Get MAC address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 for (i = 0; i < MAC_ADDR_LEN; i++)
4091 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04004092 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
4096 dev->irq = pdev->irq;
4097 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004099 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100
Michał Mirosław350fb322011-04-08 06:35:56 +00004101 /* don't enable SG, IP_CSUM and TSO by default - it might not work
4102 * properly for all devices */
4103 dev->features |= NETIF_F_RXCSUM |
4104 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
4105
4106 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
4107 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
4108 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
4109 NETIF_F_HIGHDMA;
4110
4111 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4112 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
4113 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114
4115 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01004116 tp->hw_start = cfg->hw_start;
4117 tp->intr_event = cfg->intr_event;
4118 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
David S. Miller8decf862011-09-22 03:23:13 -04004120 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
4121 ~(RxBOVF | RxFOVF) : ~0;
4122
Francois Romieu2efa53f2007-03-09 00:00:05 +01004123 init_timer(&tp->timer);
4124 tp->timer.data = (unsigned long) dev;
4125 tp->timer.function = rtl8169_phy_timer;
4126
Francois Romieub6ffd972011-06-17 17:00:05 +02004127 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
François Romieu953a12c2011-04-24 17:38:48 +02004128
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004130 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00004131 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132
4133 pci_set_drvdata(pdev, dev);
4134
Joe Perchesbf82c182010-02-09 11:49:50 +00004135 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu85bffe62011-04-27 08:22:39 +02004136 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00004137 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Francois Romieud58d46b2011-05-03 16:38:29 +02004138 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
4139 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
4140 "tx checksumming: %s]\n",
4141 rtl_chip_infos[chipset].jumbo_max,
4142 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
4143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144
Francois Romieucecb5fd2011-04-01 10:21:07 +02004145 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4146 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4147 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00004148 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004149 }
françois romieub646d902011-01-03 15:08:21 +00004150
Bruno Prémont8b76ab32008-10-08 17:06:25 -07004151 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152
Alan Sternf3ec4f82010-06-08 15:23:51 -04004153 if (pci_dev_run_wake(pdev))
4154 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004155
Ivan Vecera0d672e92011-02-15 02:08:39 +00004156 netif_carrier_off(dev);
4157
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004158out:
4159 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
françois romieu87aeec72010-04-26 11:42:06 +00004161err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02004162 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004163 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00004164err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004165 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00004166err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004167 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004168 pci_disable_device(pdev);
4169err_out_free_dev_1:
4170 free_netdev(dev);
4171 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172}
4173
Francois Romieu07d3f512007-02-21 22:40:46 +01004174static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175{
4176 struct net_device *dev = pci_get_drvdata(pdev);
4177 struct rtl8169_private *tp = netdev_priv(dev);
4178
Francois Romieucecb5fd2011-04-01 10:21:07 +02004179 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4180 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4181 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00004182 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004183 }
françois romieub646d902011-01-03 15:08:21 +00004184
Tejun Heo23f333a2010-12-12 16:45:14 +01004185 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01004186
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08004188
François Romieu953a12c2011-04-24 17:38:48 +02004189 rtl_release_firmware(tp);
4190
Alan Sternf3ec4f82010-06-08 15:23:51 -04004191 if (pci_dev_run_wake(pdev))
4192 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004193
Ivan Veceracc098dc2009-11-29 23:12:52 -08004194 /* restore original MAC address */
4195 rtl_rar_set(tp, dev->perm_addr);
4196
Francois Romieufbac58f2007-10-04 22:51:38 +02004197 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 rtl8169_release_board(pdev, dev, tp->mmio_addr);
4199 pci_set_drvdata(pdev, NULL);
4200}
4201
Francois Romieub6ffd972011-06-17 17:00:05 +02004202static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4203{
4204 struct rtl_fw *rtl_fw;
4205 const char *name;
4206 int rc = -ENOMEM;
4207
4208 name = rtl_lookup_firmware_name(tp);
4209 if (!name)
4210 goto out_no_firmware;
4211
4212 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4213 if (!rtl_fw)
4214 goto err_warn;
4215
4216 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4217 if (rc < 0)
4218 goto err_free;
4219
Francois Romieufd112f22011-06-18 00:10:29 +02004220 rc = rtl_check_firmware(tp, rtl_fw);
4221 if (rc < 0)
4222 goto err_release_firmware;
4223
Francois Romieub6ffd972011-06-17 17:00:05 +02004224 tp->rtl_fw = rtl_fw;
4225out:
4226 return;
4227
Francois Romieufd112f22011-06-18 00:10:29 +02004228err_release_firmware:
4229 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004230err_free:
4231 kfree(rtl_fw);
4232err_warn:
4233 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4234 name, rc);
4235out_no_firmware:
4236 tp->rtl_fw = NULL;
4237 goto out;
4238}
4239
François Romieu953a12c2011-04-24 17:38:48 +02004240static void rtl_request_firmware(struct rtl8169_private *tp)
4241{
Francois Romieub6ffd972011-06-17 17:00:05 +02004242 if (IS_ERR(tp->rtl_fw))
4243 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004244}
4245
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246static int rtl8169_open(struct net_device *dev)
4247{
4248 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00004249 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02004251 int retval = -ENOMEM;
4252
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004253 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254
Neil Hormanc0cd8842010-03-29 13:16:02 -07004255 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004257 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004259 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
4260 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004262 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004264 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
4265 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02004267 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268
4269 retval = rtl8169_init_ring(dev);
4270 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02004271 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
David Howellsc4028952006-11-22 14:57:56 +00004273 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
Francois Romieu99f252b2007-04-02 22:59:59 +02004275 smp_mb();
4276
François Romieu953a12c2011-04-24 17:38:48 +02004277 rtl_request_firmware(tp);
4278
Francois Romieufbac58f2007-10-04 22:51:38 +02004279 retval = request_irq(dev->irq, rtl8169_interrupt,
4280 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02004281 dev->name, dev);
4282 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02004283 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02004284
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004285 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004286
françois romieueee3a962011-01-08 02:17:26 +00004287 rtl8169_init_phy(dev, tp);
4288
Michał Mirosław350fb322011-04-08 06:35:56 +00004289 rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00004290
françois romieu065c27c2011-01-03 15:08:12 +00004291 rtl_pll_power_up(tp);
4292
Francois Romieu07ce4062007-02-23 23:36:39 +01004293 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004295 tp->saved_wolopts = 0;
4296 pm_runtime_put_noidle(&pdev->dev);
4297
françois romieueee3a962011-01-08 02:17:26 +00004298 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299out:
4300 return retval;
4301
François Romieu953a12c2011-04-24 17:38:48 +02004302err_release_fw_2:
4303 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02004304 rtl8169_rx_clear(tp);
4305err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004306 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
4307 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004308 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02004309err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004310 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
4311 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004312 tp->TxDescArray = NULL;
4313err_pm_runtime_put:
4314 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 goto out;
4316}
4317
Hayes Wang92fc43b2011-07-06 15:58:03 +08004318static void rtl_rx_close(struct rtl8169_private *tp)
4319{
4320 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004321
Francois Romieu1687b562011-07-19 17:21:29 +02004322 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004323}
4324
françois romieue6de30d2011-01-03 15:08:37 +00004325static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326{
françois romieue6de30d2011-01-03 15:08:37 +00004327 void __iomem *ioaddr = tp->mmio_addr;
4328
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 /* Disable interrupts */
4330 rtl8169_irq_mask_and_ack(ioaddr);
4331
Hayes Wang92fc43b2011-07-06 15:58:03 +08004332 rtl_rx_close(tp);
4333
Hayes Wang5d2e1952011-02-22 17:26:22 +08004334 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004335 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4336 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00004337 while (RTL_R8(TxPoll) & NPQ)
4338 udelay(20);
Hayes Wangc2218922011-09-06 16:55:18 +08004339 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4340 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
4341 tp->mac_version == RTL_GIGA_MAC_VER_36) {
David S. Miller8decf862011-09-22 03:23:13 -04004342 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08004343 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4344 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004345 } else {
4346 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4347 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004348 }
4349
Hayes Wang92fc43b2011-07-06 15:58:03 +08004350 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351}
4352
Francois Romieu7f796d832007-06-11 23:04:41 +02004353static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004354{
4355 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004356
4357 /* Set DMA burst size and Interframe Gap Time */
4358 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4359 (InterFrameGap << TxInterFrameGapShift));
4360}
4361
Francois Romieu07ce4062007-02-23 23:36:39 +01004362static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363{
4364 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365
Francois Romieu07ce4062007-02-23 23:36:39 +01004366 tp->hw_start(dev);
4367
Francois Romieu07ce4062007-02-23 23:36:39 +01004368 netif_start_queue(dev);
4369}
4370
Francois Romieu7f796d832007-06-11 23:04:41 +02004371static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4372 void __iomem *ioaddr)
4373{
4374 /*
4375 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4376 * register to be written before TxDescAddrLow to work.
4377 * Switching from MMIO to I/O access fixes the issue as well.
4378 */
4379 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004380 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004381 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004382 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004383}
4384
4385static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4386{
4387 u16 cmd;
4388
4389 cmd = RTL_R16(CPlusCmd);
4390 RTL_W16(CPlusCmd, cmd);
4391 return cmd;
4392}
4393
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004394static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004395{
4396 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004397 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004398}
4399
Francois Romieu6dccd162007-02-13 23:38:05 +01004400static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4401{
Francois Romieu37441002011-06-17 22:58:54 +02004402 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004403 u32 mac_version;
4404 u32 clk;
4405 u32 val;
4406 } cfg2_info [] = {
4407 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4408 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4409 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4410 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004411 };
4412 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004413 unsigned int i;
4414 u32 clk;
4415
4416 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004417 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004418 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4419 RTL_W32(0x7c, p->val);
4420 break;
4421 }
4422 }
4423}
4424
Francois Romieu07ce4062007-02-23 23:36:39 +01004425static void rtl_hw_start_8169(struct net_device *dev)
4426{
4427 struct rtl8169_private *tp = netdev_priv(dev);
4428 void __iomem *ioaddr = tp->mmio_addr;
4429 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004430
Francois Romieu9cb427b2006-11-02 00:10:16 +01004431 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4432 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4433 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4434 }
4435
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004437 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4438 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4439 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4440 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004441 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4442
Hayes Wange542a222011-07-06 15:58:04 +08004443 rtl_init_rxcfg(tp);
4444
françois romieuf0298f82011-01-03 15:07:42 +00004445 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004447 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448
Francois Romieucecb5fd2011-04-01 10:21:07 +02004449 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4450 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4451 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4452 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004453 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454
Francois Romieu7f796d832007-06-11 23:04:41 +02004455 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004456
Francois Romieucecb5fd2011-04-01 10:21:07 +02004457 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4458 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004459 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004461 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 }
4463
Francois Romieubcf0bf92006-07-26 23:14:13 +02004464 RTL_W16(CPlusCmd, tp->cp_cmd);
4465
Francois Romieu6dccd162007-02-13 23:38:05 +01004466 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4467
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 /*
4469 * Undocumented corner. Supposedly:
4470 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4471 */
4472 RTL_W16(IntrMitigate, 0x0000);
4473
Francois Romieu7f796d832007-06-11 23:04:41 +02004474 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004475
Francois Romieucecb5fd2011-04-01 10:21:07 +02004476 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4477 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4478 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4479 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004480 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4481 rtl_set_rx_tx_config_registers(tp);
4482 }
4483
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004485
4486 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4487 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
4489 RTL_W32(RxMissed, 0);
4490
Francois Romieu07ce4062007-02-23 23:36:39 +01004491 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492
4493 /* no early-rx interrupts */
4494 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004495
4496 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01004497 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004498}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499
françois romieu650e8d52011-01-03 15:08:29 +00004500static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004501{
4502 u32 csi;
4503
4504 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00004505 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4506}
4507
françois romieue6de30d2011-01-03 15:08:37 +00004508static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4509{
4510 rtl_csi_access_enable(ioaddr, 0x17000000);
4511}
4512
françois romieu650e8d52011-01-03 15:08:29 +00004513static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4514{
4515 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02004516}
4517
4518struct ephy_info {
4519 unsigned int offset;
4520 u16 mask;
4521 u16 bits;
4522};
4523
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004524static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004525{
4526 u16 w;
4527
4528 while (len-- > 0) {
4529 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4530 rtl_ephy_write(ioaddr, e->offset, w);
4531 e++;
4532 }
4533}
4534
Francois Romieub726e492008-06-28 12:22:59 +02004535static void rtl_disable_clock_request(struct pci_dev *pdev)
4536{
Jon Masone44daad2011-06-27 07:46:31 +00004537 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004538
4539 if (cap) {
4540 u16 ctl;
4541
4542 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4543 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4544 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4545 }
4546}
4547
françois romieue6de30d2011-01-03 15:08:37 +00004548static void rtl_enable_clock_request(struct pci_dev *pdev)
4549{
Jon Masone44daad2011-06-27 07:46:31 +00004550 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004551
4552 if (cap) {
4553 u16 ctl;
4554
4555 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4556 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4557 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4558 }
4559}
4560
Francois Romieub726e492008-06-28 12:22:59 +02004561#define R8168_CPCMD_QUIRK_MASK (\
4562 EnableBist | \
4563 Mac_dbgo_oe | \
4564 Force_half_dup | \
4565 Force_rxflow_en | \
4566 Force_txflow_en | \
4567 Cxpl_dbg_sel | \
4568 ASF | \
4569 PktCntrDisable | \
4570 Mac_dbgo_sel)
4571
Francois Romieu219a1e92008-06-28 11:58:39 +02004572static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4573{
Francois Romieub726e492008-06-28 12:22:59 +02004574 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4575
4576 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4577
Francois Romieu2e68ae42008-06-28 12:00:55 +02004578 rtl_tx_performance_tweak(pdev,
4579 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004580}
4581
4582static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4583{
4584 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004585
françois romieuf0298f82011-01-03 15:07:42 +00004586 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004587
4588 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004589}
4590
4591static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4592{
Francois Romieub726e492008-06-28 12:22:59 +02004593 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4594
4595 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4596
Francois Romieu219a1e92008-06-28 11:58:39 +02004597 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004598
4599 rtl_disable_clock_request(pdev);
4600
4601 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004602}
4603
Francois Romieuef3386f2008-06-29 12:24:30 +02004604static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004605{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004606 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004607 { 0x01, 0, 0x0001 },
4608 { 0x02, 0x0800, 0x1000 },
4609 { 0x03, 0, 0x0042 },
4610 { 0x06, 0x0080, 0x0000 },
4611 { 0x07, 0, 0x2000 }
4612 };
4613
françois romieu650e8d52011-01-03 15:08:29 +00004614 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004615
4616 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4617
Francois Romieu219a1e92008-06-28 11:58:39 +02004618 __rtl_hw_start_8168cp(ioaddr, pdev);
4619}
4620
Francois Romieuef3386f2008-06-29 12:24:30 +02004621static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4622{
françois romieu650e8d52011-01-03 15:08:29 +00004623 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004624
4625 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4626
4627 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4628
4629 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4630}
4631
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004632static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4633{
françois romieu650e8d52011-01-03 15:08:29 +00004634 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004635
4636 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4637
4638 /* Magic. */
4639 RTL_W8(DBG_REG, 0x20);
4640
françois romieuf0298f82011-01-03 15:07:42 +00004641 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004642
4643 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4644
4645 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4646}
4647
Francois Romieu219a1e92008-06-28 11:58:39 +02004648static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4649{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004650 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004651 { 0x02, 0x0800, 0x1000 },
4652 { 0x03, 0, 0x0002 },
4653 { 0x06, 0x0080, 0x0000 }
4654 };
4655
françois romieu650e8d52011-01-03 15:08:29 +00004656 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004657
4658 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4659
4660 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4661
Francois Romieu219a1e92008-06-28 11:58:39 +02004662 __rtl_hw_start_8168cp(ioaddr, pdev);
4663}
4664
4665static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4666{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004667 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004668 { 0x01, 0, 0x0001 },
4669 { 0x03, 0x0400, 0x0220 }
4670 };
4671
françois romieu650e8d52011-01-03 15:08:29 +00004672 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004673
4674 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4675
Francois Romieu219a1e92008-06-28 11:58:39 +02004676 __rtl_hw_start_8168cp(ioaddr, pdev);
4677}
4678
Francois Romieu197ff762008-06-28 13:16:02 +02004679static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4680{
4681 rtl_hw_start_8168c_2(ioaddr, pdev);
4682}
4683
Francois Romieu6fb07052008-06-29 11:54:28 +02004684static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4685{
françois romieu650e8d52011-01-03 15:08:29 +00004686 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004687
4688 __rtl_hw_start_8168cp(ioaddr, pdev);
4689}
4690
Francois Romieu5b538df2008-07-20 16:22:45 +02004691static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4692{
françois romieu650e8d52011-01-03 15:08:29 +00004693 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004694
4695 rtl_disable_clock_request(pdev);
4696
françois romieuf0298f82011-01-03 15:07:42 +00004697 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004698
4699 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4700
4701 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4702}
4703
hayeswang4804b3b2011-03-21 01:50:29 +00004704static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4705{
4706 rtl_csi_access_enable_1(ioaddr);
4707
4708 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4709
4710 RTL_W8(MaxTxPacketSize, TxPacketMax);
4711
4712 rtl_disable_clock_request(pdev);
4713}
4714
françois romieue6de30d2011-01-03 15:08:37 +00004715static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4716{
4717 static const struct ephy_info e_info_8168d_4[] = {
4718 { 0x0b, ~0, 0x48 },
4719 { 0x19, 0x20, 0x50 },
4720 { 0x0c, ~0, 0x20 }
4721 };
4722 int i;
4723
4724 rtl_csi_access_enable_1(ioaddr);
4725
4726 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4727
4728 RTL_W8(MaxTxPacketSize, TxPacketMax);
4729
4730 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4731 const struct ephy_info *e = e_info_8168d_4 + i;
4732 u16 w;
4733
4734 w = rtl_ephy_read(ioaddr, e->offset);
4735 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4736 }
4737
4738 rtl_enable_clock_request(pdev);
4739}
4740
Hayes Wang70090422011-07-06 15:58:06 +08004741static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
hayeswang01dc7fe2011-03-21 01:50:28 +00004742{
Hayes Wang70090422011-07-06 15:58:06 +08004743 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004744 { 0x00, 0x0200, 0x0100 },
4745 { 0x00, 0x0000, 0x0004 },
4746 { 0x06, 0x0002, 0x0001 },
4747 { 0x06, 0x0000, 0x0030 },
4748 { 0x07, 0x0000, 0x2000 },
4749 { 0x00, 0x0000, 0x0020 },
4750 { 0x03, 0x5800, 0x2000 },
4751 { 0x03, 0x0000, 0x0001 },
4752 { 0x01, 0x0800, 0x1000 },
4753 { 0x07, 0x0000, 0x4000 },
4754 { 0x1e, 0x0000, 0x2000 },
4755 { 0x19, 0xffff, 0xfe6c },
4756 { 0x0a, 0x0000, 0x0040 }
4757 };
4758
4759 rtl_csi_access_enable_2(ioaddr);
4760
Hayes Wang70090422011-07-06 15:58:06 +08004761 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004762
4763 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4764
4765 RTL_W8(MaxTxPacketSize, TxPacketMax);
4766
4767 rtl_disable_clock_request(pdev);
4768
4769 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004770 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4771 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004772
Francois Romieucecb5fd2011-04-01 10:21:07 +02004773 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004774}
4775
Hayes Wang70090422011-07-06 15:58:06 +08004776static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4777{
4778 static const struct ephy_info e_info_8168e_2[] = {
4779 { 0x09, 0x0000, 0x0080 },
4780 { 0x19, 0x0000, 0x0224 }
4781 };
4782
4783 rtl_csi_access_enable_1(ioaddr);
4784
4785 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4786
4787 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4788
4789 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4790 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4791 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4792 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4793 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4794 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4795 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4796 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4797 ERIAR_EXGMAC);
4798
Hayes Wang3090bd92011-09-06 16:55:15 +08004799 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004800
4801 rtl_disable_clock_request(pdev);
4802
4803 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4804 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4805
4806 /* Adjust EEE LED frequency */
4807 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4808
4809 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4810 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4811 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4812}
4813
Hayes Wangc2218922011-09-06 16:55:18 +08004814static void rtl_hw_start_8168f_1(void __iomem *ioaddr, struct pci_dev *pdev)
4815{
4816 static const struct ephy_info e_info_8168f_1[] = {
4817 { 0x06, 0x00c0, 0x0020 },
4818 { 0x08, 0x0001, 0x0002 },
4819 { 0x09, 0x0000, 0x0080 },
4820 { 0x19, 0x0000, 0x0224 }
4821 };
4822
4823 rtl_csi_access_enable_1(ioaddr);
4824
4825 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4826
4827 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4828
4829 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4830 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4831 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4832 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4833 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4834 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4835 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4836 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4837 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4838 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
4839 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4840 ERIAR_EXGMAC);
4841
4842 RTL_W8(MaxTxPacketSize, EarlySize);
4843
4844 rtl_disable_clock_request(pdev);
4845
4846 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4847 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4848
4849 /* Adjust EEE LED frequency */
4850 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4851
4852 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4853 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4854 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4855}
4856
Francois Romieu07ce4062007-02-23 23:36:39 +01004857static void rtl_hw_start_8168(struct net_device *dev)
4858{
Francois Romieu2dd99532007-06-11 23:22:52 +02004859 struct rtl8169_private *tp = netdev_priv(dev);
4860 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004861 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004862
4863 RTL_W8(Cfg9346, Cfg9346_Unlock);
4864
françois romieuf0298f82011-01-03 15:07:42 +00004865 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004866
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004867 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004868
Francois Romieu0e485152007-02-20 00:00:26 +01004869 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004870
4871 RTL_W16(CPlusCmd, tp->cp_cmd);
4872
Francois Romieu0e485152007-02-20 00:00:26 +01004873 RTL_W16(IntrMitigate, 0x5151);
4874
4875 /* Work around for RxFIFO overflow. */
Ivan Vecerab5ba6d12011-01-27 12:24:11 +01004876 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
4877 tp->mac_version == RTL_GIGA_MAC_VER_22) {
Francois Romieu0e485152007-02-20 00:00:26 +01004878 tp->intr_event |= RxFIFOOver | PCSTimeout;
4879 tp->intr_event &= ~RxOverflow;
4880 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004881
4882 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4883
Francois Romieub8363902008-06-01 12:31:57 +02004884 rtl_set_rx_mode(dev);
4885
4886 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4887 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004888
4889 RTL_R8(IntrMask);
4890
Francois Romieu219a1e92008-06-28 11:58:39 +02004891 switch (tp->mac_version) {
4892 case RTL_GIGA_MAC_VER_11:
4893 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004894 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004895
4896 case RTL_GIGA_MAC_VER_12:
4897 case RTL_GIGA_MAC_VER_17:
4898 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004899 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004900
4901 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004902 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004903 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004904
4905 case RTL_GIGA_MAC_VER_19:
4906 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004907 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004908
4909 case RTL_GIGA_MAC_VER_20:
4910 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004911 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004912
Francois Romieu197ff762008-06-28 13:16:02 +02004913 case RTL_GIGA_MAC_VER_21:
4914 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004915 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004916
Francois Romieu6fb07052008-06-29 11:54:28 +02004917 case RTL_GIGA_MAC_VER_22:
4918 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004919 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004920
Francois Romieuef3386f2008-06-29 12:24:30 +02004921 case RTL_GIGA_MAC_VER_23:
4922 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004923 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004924
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004925 case RTL_GIGA_MAC_VER_24:
4926 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004927 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004928
Francois Romieu5b538df2008-07-20 16:22:45 +02004929 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004930 case RTL_GIGA_MAC_VER_26:
4931 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004932 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004933 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004934
françois romieue6de30d2011-01-03 15:08:37 +00004935 case RTL_GIGA_MAC_VER_28:
4936 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004937 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004938
hayeswang4804b3b2011-03-21 01:50:29 +00004939 case RTL_GIGA_MAC_VER_31:
4940 rtl_hw_start_8168dp(ioaddr, pdev);
4941 break;
4942
hayeswang01dc7fe2011-03-21 01:50:28 +00004943 case RTL_GIGA_MAC_VER_32:
4944 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004945 rtl_hw_start_8168e_1(ioaddr, pdev);
4946 break;
4947 case RTL_GIGA_MAC_VER_34:
4948 rtl_hw_start_8168e_2(ioaddr, pdev);
hayeswang01dc7fe2011-03-21 01:50:28 +00004949 break;
françois romieue6de30d2011-01-03 15:08:37 +00004950
Hayes Wangc2218922011-09-06 16:55:18 +08004951 case RTL_GIGA_MAC_VER_35:
4952 case RTL_GIGA_MAC_VER_36:
4953 rtl_hw_start_8168f_1(ioaddr, pdev);
4954 break;
4955
Francois Romieu219a1e92008-06-28 11:58:39 +02004956 default:
4957 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4958 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004959 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004960 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004961
Francois Romieu0e485152007-02-20 00:00:26 +01004962 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4963
Francois Romieub8363902008-06-01 12:31:57 +02004964 RTL_W8(Cfg9346, Cfg9346_Lock);
4965
Francois Romieu2dd99532007-06-11 23:22:52 +02004966 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004967
Francois Romieu0e485152007-02-20 00:00:26 +01004968 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004969}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970
Francois Romieu2857ffb2008-08-02 21:08:49 +02004971#define R810X_CPCMD_QUIRK_MASK (\
4972 EnableBist | \
4973 Mac_dbgo_oe | \
4974 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004975 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004976 Force_txflow_en | \
4977 Cxpl_dbg_sel | \
4978 ASF | \
4979 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004980 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004981
4982static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4983{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004984 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004985 { 0x01, 0, 0x6e65 },
4986 { 0x02, 0, 0x091f },
4987 { 0x03, 0, 0xc2f9 },
4988 { 0x06, 0, 0xafb5 },
4989 { 0x07, 0, 0x0e00 },
4990 { 0x19, 0, 0xec80 },
4991 { 0x01, 0, 0x2e65 },
4992 { 0x01, 0, 0x6e65 }
4993 };
4994 u8 cfg1;
4995
françois romieu650e8d52011-01-03 15:08:29 +00004996 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004997
4998 RTL_W8(DBG_REG, FIX_NAK_1);
4999
5000 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5001
5002 RTL_W8(Config1,
5003 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5004 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5005
5006 cfg1 = RTL_R8(Config1);
5007 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5008 RTL_W8(Config1, cfg1 & ~LEDS0);
5009
Francois Romieu2857ffb2008-08-02 21:08:49 +02005010 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
5011}
5012
5013static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
5014{
françois romieu650e8d52011-01-03 15:08:29 +00005015 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005016
5017 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5018
5019 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5020 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005021}
5022
5023static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
5024{
5025 rtl_hw_start_8102e_2(ioaddr, pdev);
5026
5027 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
5028}
5029
Hayes Wang5a5e4442011-02-22 17:26:21 +08005030static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
5031{
5032 static const struct ephy_info e_info_8105e_1[] = {
5033 { 0x07, 0, 0x4000 },
5034 { 0x19, 0, 0x0200 },
5035 { 0x19, 0, 0x0020 },
5036 { 0x1e, 0, 0x2000 },
5037 { 0x03, 0, 0x0001 },
5038 { 0x19, 0, 0x0100 },
5039 { 0x19, 0, 0x0004 },
5040 { 0x0a, 0, 0x0020 }
5041 };
5042
Francois Romieucecb5fd2011-04-01 10:21:07 +02005043 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005044 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5045
Francois Romieucecb5fd2011-04-01 10:21:07 +02005046 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005047 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5048
5049 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08005050 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005051
5052 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
5053}
5054
5055static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
5056{
5057 rtl_hw_start_8105e_1(ioaddr, pdev);
5058 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
5059}
5060
Francois Romieu07ce4062007-02-23 23:36:39 +01005061static void rtl_hw_start_8101(struct net_device *dev)
5062{
Francois Romieucdf1a602007-06-11 23:29:50 +02005063 struct rtl8169_private *tp = netdev_priv(dev);
5064 void __iomem *ioaddr = tp->mmio_addr;
5065 struct pci_dev *pdev = tp->pci_dev;
5066
Francois Romieucecb5fd2011-04-01 10:21:07 +02005067 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5068 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00005069 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02005070
5071 if (cap) {
5072 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
5073 PCI_EXP_DEVCTL_NOSNOOP_EN);
5074 }
Francois Romieucdf1a602007-06-11 23:29:50 +02005075 }
5076
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005077 RTL_W8(Cfg9346, Cfg9346_Unlock);
5078
Francois Romieu2857ffb2008-08-02 21:08:49 +02005079 switch (tp->mac_version) {
5080 case RTL_GIGA_MAC_VER_07:
5081 rtl_hw_start_8102e_1(ioaddr, pdev);
5082 break;
5083
5084 case RTL_GIGA_MAC_VER_08:
5085 rtl_hw_start_8102e_3(ioaddr, pdev);
5086 break;
5087
5088 case RTL_GIGA_MAC_VER_09:
5089 rtl_hw_start_8102e_2(ioaddr, pdev);
5090 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005091
5092 case RTL_GIGA_MAC_VER_29:
5093 rtl_hw_start_8105e_1(ioaddr, pdev);
5094 break;
5095 case RTL_GIGA_MAC_VER_30:
5096 rtl_hw_start_8105e_2(ioaddr, pdev);
5097 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02005098 }
5099
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005100 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02005101
françois romieuf0298f82011-01-03 15:07:42 +00005102 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02005103
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005104 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02005105
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005106 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02005107 RTL_W16(CPlusCmd, tp->cp_cmd);
5108
5109 RTL_W16(IntrMitigate, 0x0000);
5110
5111 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5112
5113 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5114 rtl_set_rx_tx_config_registers(tp);
5115
Francois Romieucdf1a602007-06-11 23:29:50 +02005116 RTL_R8(IntrMask);
5117
Francois Romieucdf1a602007-06-11 23:29:50 +02005118 rtl_set_rx_mode(dev);
5119
5120 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01005121
Francois Romieu0e485152007-02-20 00:00:26 +01005122 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123}
5124
5125static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5126{
Francois Romieud58d46b2011-05-03 16:38:29 +02005127 struct rtl8169_private *tp = netdev_priv(dev);
5128
5129 if (new_mtu < ETH_ZLEN ||
5130 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 return -EINVAL;
5132
Francois Romieud58d46b2011-05-03 16:38:29 +02005133 if (new_mtu > ETH_DATA_LEN)
5134 rtl_hw_jumbo_enable(tp);
5135 else
5136 rtl_hw_jumbo_disable(tp);
5137
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005139 netdev_update_features(dev);
5140
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005141 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142}
5143
5144static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5145{
Al Viro95e09182007-12-22 18:55:39 +00005146 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5148}
5149
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005150static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5151 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005153 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005154 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005155
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005156 kfree(*data_buff);
5157 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 rtl8169_make_unusable_by_asic(desc);
5159}
5160
5161static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5162{
5163 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5164
5165 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5166}
5167
5168static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5169 u32 rx_buf_sz)
5170{
5171 desc->addr = cpu_to_le64(mapping);
5172 wmb();
5173 rtl8169_mark_to_asic(desc, rx_buf_sz);
5174}
5175
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005176static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005178 return (void *)ALIGN((long)data, 16);
5179}
5180
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005181static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5182 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005183{
5184 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005186 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005187 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005188 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005190 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5191 if (!data)
5192 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005193
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005194 if (rtl8169_align(data) != data) {
5195 kfree(data);
5196 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5197 if (!data)
5198 return NULL;
5199 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005200
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005201 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005202 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005203 if (unlikely(dma_mapping_error(d, mapping))) {
5204 if (net_ratelimit())
5205 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005206 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208
5209 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005210 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005211
5212err_out:
5213 kfree(data);
5214 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215}
5216
5217static void rtl8169_rx_clear(struct rtl8169_private *tp)
5218{
Francois Romieu07d3f512007-02-21 22:40:46 +01005219 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220
5221 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005222 if (tp->Rx_databuff[i]) {
5223 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 tp->RxDescArray + i);
5225 }
5226 }
5227}
5228
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005229static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005231 desc->opts1 |= cpu_to_le32(RingEnd);
5232}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005233
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005234static int rtl8169_rx_fill(struct rtl8169_private *tp)
5235{
5236 unsigned int i;
5237
5238 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005239 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005240
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005241 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005243
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005244 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005245 if (!data) {
5246 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005247 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005248 }
5249 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005252 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5253 return 0;
5254
5255err_out:
5256 rtl8169_rx_clear(tp);
5257 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258}
5259
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260static int rtl8169_init_ring(struct net_device *dev)
5261{
5262 struct rtl8169_private *tp = netdev_priv(dev);
5263
5264 rtl8169_init_ring_indexes(tp);
5265
5266 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005267 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005269 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270}
5271
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005272static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 struct TxDesc *desc)
5274{
5275 unsigned int len = tx_skb->len;
5276
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005277 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5278
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 desc->opts1 = 0x00;
5280 desc->opts2 = 0x00;
5281 desc->addr = 0x00;
5282 tx_skb->len = 0;
5283}
5284
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005285static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5286 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287{
5288 unsigned int i;
5289
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005290 for (i = 0; i < n; i++) {
5291 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 struct ring_info *tx_skb = tp->tx_skb + entry;
5293 unsigned int len = tx_skb->len;
5294
5295 if (len) {
5296 struct sk_buff *skb = tx_skb->skb;
5297
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005298 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 tp->TxDescArray + entry);
5300 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005301 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 dev_kfree_skb(skb);
5303 tx_skb->skb = NULL;
5304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 }
5306 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005307}
5308
5309static void rtl8169_tx_clear(struct rtl8169_private *tp)
5310{
5311 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 tp->cur_tx = tp->dirty_tx = 0;
5313}
5314
David Howellsc4028952006-11-22 14:57:56 +00005315static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316{
5317 struct rtl8169_private *tp = netdev_priv(dev);
5318
David Howellsc4028952006-11-22 14:57:56 +00005319 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 schedule_delayed_work(&tp->task, 4);
5321}
5322
5323static void rtl8169_wait_for_quiescence(struct net_device *dev)
5324{
5325 struct rtl8169_private *tp = netdev_priv(dev);
5326 void __iomem *ioaddr = tp->mmio_addr;
5327
5328 synchronize_irq(dev->irq);
5329
5330 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005331 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332
5333 rtl8169_irq_mask_and_ack(ioaddr);
5334
David S. Millerd1d08d12008-01-07 20:53:33 -08005335 tp->intr_mask = 0xffff;
5336 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005337 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338}
5339
David Howellsc4028952006-11-22 14:57:56 +00005340static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341{
David Howellsc4028952006-11-22 14:57:56 +00005342 struct rtl8169_private *tp =
5343 container_of(work, struct rtl8169_private, task.work);
5344 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 int ret;
5346
Francois Romieueb2a0212007-02-15 23:37:21 +01005347 rtnl_lock();
5348
5349 if (!netif_running(dev))
5350 goto out_unlock;
5351
5352 rtl8169_wait_for_quiescence(dev);
5353 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354
5355 ret = rtl8169_open(dev);
5356 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005357 if (net_ratelimit())
5358 netif_err(tp, drv, dev,
5359 "reinit failure (status = %d). Rescheduling\n",
5360 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 rtl8169_schedule_work(dev, rtl8169_reinit_task);
5362 }
Francois Romieueb2a0212007-02-15 23:37:21 +01005363
5364out_unlock:
5365 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366}
5367
David Howellsc4028952006-11-22 14:57:56 +00005368static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369{
David Howellsc4028952006-11-22 14:57:56 +00005370 struct rtl8169_private *tp =
5371 container_of(work, struct rtl8169_private, task.work);
5372 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005373 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374
Francois Romieueb2a0212007-02-15 23:37:21 +01005375 rtnl_lock();
5376
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01005378 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379
5380 rtl8169_wait_for_quiescence(dev);
5381
Francois Romieu56de4142011-03-15 17:29:31 +01005382 for (i = 0; i < NUM_RX_DESC; i++)
5383 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5384
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 rtl8169_tx_clear(tp);
5386
Hayes Wang92fc43b2011-07-06 15:58:03 +08005387 rtl8169_hw_reset(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01005388 rtl_hw_start(dev);
5389 netif_wake_queue(dev);
5390 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieueb2a0212007-02-15 23:37:21 +01005391
5392out_unlock:
5393 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394}
5395
5396static void rtl8169_tx_timeout(struct net_device *dev)
5397{
5398 struct rtl8169_private *tp = netdev_priv(dev);
5399
françois romieue6de30d2011-01-03 15:08:37 +00005400 rtl8169_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401
5402 /* Let's wait a bit while any (async) irq lands on */
5403 rtl8169_schedule_work(dev, rtl8169_reset_task);
5404}
5405
5406static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005407 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408{
5409 struct skb_shared_info *info = skb_shinfo(skb);
5410 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005411 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005412 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413
5414 entry = tp->cur_tx;
5415 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
5416 skb_frag_t *frag = info->frags + cur_frag;
5417 dma_addr_t mapping;
5418 u32 status, len;
5419 void *addr;
5420
5421 entry = (entry + 1) % NUM_TX_DESC;
5422
5423 txd = tp->TxDescArray + entry;
5424 len = frag->size;
Ian Campbell929f6182011-08-31 00:47:06 +00005425 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005426 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005427 if (unlikely(dma_mapping_error(d, mapping))) {
5428 if (net_ratelimit())
5429 netif_err(tp, drv, tp->dev,
5430 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005431 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433
Francois Romieucecb5fd2011-04-01 10:21:07 +02005434 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005435 status = opts[0] | len |
5436 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437
5438 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005439 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 txd->addr = cpu_to_le64(mapping);
5441
5442 tp->tx_skb[entry].len = len;
5443 }
5444
5445 if (cur_frag) {
5446 tp->tx_skb[entry].skb = skb;
5447 txd->opts1 |= cpu_to_le32(LastFrag);
5448 }
5449
5450 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005451
5452err_out:
5453 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5454 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455}
5456
Francois Romieu2b7b4312011-04-18 22:53:24 -07005457static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5458 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005460 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005461 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005462 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463
Francois Romieu2b7b4312011-04-18 22:53:24 -07005464 if (mss) {
5465 opts[0] |= TD_LSO;
5466 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5467 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005468 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469
5470 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005471 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005473 opts[offset] |= info->checksum.udp;
5474 else
5475 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477}
5478
Stephen Hemminger613573252009-08-31 19:50:58 +00005479static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5480 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481{
5482 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005483 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 struct TxDesc *txd = tp->TxDescArray + entry;
5485 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005486 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 dma_addr_t mapping;
5488 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005489 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005490 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005491
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005493 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005494 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 }
5496
5497 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005498 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005500 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005501 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005502 if (unlikely(dma_mapping_error(d, mapping))) {
5503 if (net_ratelimit())
5504 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005505 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507
5508 tp->tx_skb[entry].len = len;
5509 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510
Francois Romieu2b7b4312011-04-18 22:53:24 -07005511 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5512 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005513
Francois Romieu2b7b4312011-04-18 22:53:24 -07005514 rtl8169_tso_csum(tp, skb, opts);
5515
5516 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005517 if (frags < 0)
5518 goto err_dma_1;
5519 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005520 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005521 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005522 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005523 tp->tx_skb[entry].skb = skb;
5524 }
5525
Francois Romieu2b7b4312011-04-18 22:53:24 -07005526 txd->opts2 = cpu_to_le32(opts[1]);
5527
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 wmb();
5529
Francois Romieucecb5fd2011-04-01 10:21:07 +02005530 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005531 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 txd->opts1 = cpu_to_le32(status);
5533
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 tp->cur_tx += frags + 1;
5535
David Dillow4c020a92010-03-03 16:33:10 +00005536 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537
Francois Romieucecb5fd2011-04-01 10:21:07 +02005538 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539
5540 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
5541 netif_stop_queue(dev);
5542 smp_rmb();
5543 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
5544 netif_wake_queue(dev);
5545 }
5546
Stephen Hemminger613573252009-08-31 19:50:58 +00005547 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005549err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005550 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005551err_dma_0:
5552 dev_kfree_skb(skb);
5553 dev->stats.tx_dropped++;
5554 return NETDEV_TX_OK;
5555
5556err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005558 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005559 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560}
5561
5562static void rtl8169_pcierr_interrupt(struct net_device *dev)
5563{
5564 struct rtl8169_private *tp = netdev_priv(dev);
5565 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 u16 pci_status, pci_cmd;
5567
5568 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5569 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5570
Joe Perchesbf82c182010-02-09 11:49:50 +00005571 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5572 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573
5574 /*
5575 * The recovery sequence below admits a very elaborated explanation:
5576 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005577 * - I did not see what else could be done;
5578 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 *
5580 * Feel free to adjust to your needs.
5581 */
Francois Romieua27993f2006-12-18 00:04:19 +01005582 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005583 pci_cmd &= ~PCI_COMMAND_PARITY;
5584 else
5585 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5586
5587 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588
5589 pci_write_config_word(pdev, PCI_STATUS,
5590 pci_status & (PCI_STATUS_DETECTED_PARITY |
5591 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5592 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5593
5594 /* The infamous DAC f*ckup only happens at boot time */
5595 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005596 void __iomem *ioaddr = tp->mmio_addr;
5597
Joe Perchesbf82c182010-02-09 11:49:50 +00005598 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 tp->cp_cmd &= ~PCIDAC;
5600 RTL_W16(CPlusCmd, tp->cp_cmd);
5601 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 }
5603
françois romieue6de30d2011-01-03 15:08:37 +00005604 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005605
5606 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607}
5608
Francois Romieu07d3f512007-02-21 22:40:46 +01005609static void rtl8169_tx_interrupt(struct net_device *dev,
5610 struct rtl8169_private *tp,
5611 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612{
5613 unsigned int dirty_tx, tx_left;
5614
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 dirty_tx = tp->dirty_tx;
5616 smp_rmb();
5617 tx_left = tp->cur_tx - dirty_tx;
5618
5619 while (tx_left > 0) {
5620 unsigned int entry = dirty_tx % NUM_TX_DESC;
5621 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 u32 status;
5623
5624 rmb();
5625 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5626 if (status & DescOwn)
5627 break;
5628
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005629 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5630 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005632 dev->stats.tx_packets++;
5633 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00005634 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 tx_skb->skb = NULL;
5636 }
5637 dirty_tx++;
5638 tx_left--;
5639 }
5640
5641 if (tp->dirty_tx != dirty_tx) {
5642 tp->dirty_tx = dirty_tx;
5643 smp_wmb();
5644 if (netif_queue_stopped(dev) &&
5645 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5646 netif_wake_queue(dev);
5647 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005648 /*
5649 * 8168 hack: TxPoll requests are lost when the Tx packets are
5650 * too close. Let's kick an extra TxPoll request when a burst
5651 * of start_xmit activity is detected (if it is not detected,
5652 * it is slow enough). -- FR
5653 */
5654 smp_rmb();
5655 if (tp->cur_tx != dirty_tx)
5656 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 }
5658}
5659
Francois Romieu126fa4b2005-05-12 20:09:17 -04005660static inline int rtl8169_fragmented_frame(u32 status)
5661{
5662 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5663}
5664
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005665static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 u32 status = opts1 & RxProtoMask;
5668
5669 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005670 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 skb->ip_summed = CHECKSUM_UNNECESSARY;
5672 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005673 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674}
5675
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005676static struct sk_buff *rtl8169_try_rx_copy(void *data,
5677 struct rtl8169_private *tp,
5678 int pkt_size,
5679 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005681 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005682 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005684 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005685 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005686 prefetch(data);
5687 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5688 if (skb)
5689 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005690 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5691
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005692 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693}
5694
Francois Romieu07d3f512007-02-21 22:40:46 +01005695static int rtl8169_rx_interrupt(struct net_device *dev,
5696 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005697 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698{
5699 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005700 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 cur_rx = tp->cur_rx;
5703 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005704 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005706 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005708 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 u32 status;
5710
5711 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005712 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713
5714 if (status & DescOwn)
5715 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005716 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005717 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5718 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005719 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005721 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005723 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005724 if (status & RxFOVF) {
5725 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005726 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005727 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005728 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005730 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005731 dma_addr_t addr = le64_to_cpu(desc->addr);
Francois Romieudeb9d93c2011-07-12 08:24:28 +02005732 int pkt_size = (status & 0x00003fff) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733
Francois Romieu126fa4b2005-05-12 20:09:17 -04005734 /*
5735 * The driver does not support incoming fragmented
5736 * frames. They are seen as a symptom of over-mtu
5737 * sized frames.
5738 */
5739 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005740 dev->stats.rx_dropped++;
5741 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005742 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005743 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005744 }
5745
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005746 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5747 tp, pkt_size, addr);
5748 rtl8169_mark_to_asic(desc, rx_buf_sz);
5749 if (!skb) {
5750 dev->stats.rx_dropped++;
5751 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 }
5753
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005754 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 skb_put(skb, pkt_size);
5756 skb->protocol = eth_type_trans(skb, dev);
5757
Francois Romieu7a8fc772011-03-01 17:18:33 +01005758 rtl8169_rx_vlan_tag(desc, skb);
5759
Francois Romieu56de4142011-03-15 17:29:31 +01005760 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761
Francois Romieucebf8cc2007-10-18 12:06:54 +02005762 dev->stats.rx_bytes += pkt_size;
5763 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005765
5766 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005767 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005768 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5769 desc->opts2 = 0;
5770 cur_rx++;
5771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 }
5773
5774 count = cur_rx - tp->cur_rx;
5775 tp->cur_rx = cur_rx;
5776
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005777 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778
5779 return count;
5780}
5781
Francois Romieu07d3f512007-02-21 22:40:46 +01005782static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783{
Francois Romieu07d3f512007-02-21 22:40:46 +01005784 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02005788 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789
David Dillowf11a3772009-05-22 15:29:34 +00005790 /* loop handling interrupts until we have no new ones or
5791 * we hit a invalid/hotplug case.
5792 */
Francois Romieu865c6522008-05-11 14:51:00 +02005793 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00005794 while (status && status != 0xffff) {
5795 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796
David Dillowf11a3772009-05-22 15:29:34 +00005797 /* Handle all of the error cases first. These will reset
5798 * the chip, so just exit the loop.
5799 */
5800 if (unlikely(!netif_running(dev))) {
Hayes Wang92fc43b2011-07-06 15:58:03 +08005801 rtl8169_hw_reset(tp);
David Dillowf11a3772009-05-22 15:29:34 +00005802 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 }
David Dillowf11a3772009-05-22 15:29:34 +00005804
Francois Romieu1519e572011-02-03 12:02:36 +01005805 if (unlikely(status & RxFIFOOver)) {
5806 switch (tp->mac_version) {
5807 /* Work around for rx fifo overflow */
5808 case RTL_GIGA_MAC_VER_11:
5809 case RTL_GIGA_MAC_VER_22:
5810 case RTL_GIGA_MAC_VER_26:
5811 netif_stop_queue(dev);
5812 rtl8169_tx_timeout(dev);
5813 goto done;
Francois Romieuf60ac8e2011-02-03 17:27:52 +01005814 /* Testers needed. */
5815 case RTL_GIGA_MAC_VER_17:
5816 case RTL_GIGA_MAC_VER_19:
5817 case RTL_GIGA_MAC_VER_20:
5818 case RTL_GIGA_MAC_VER_21:
5819 case RTL_GIGA_MAC_VER_23:
5820 case RTL_GIGA_MAC_VER_24:
5821 case RTL_GIGA_MAC_VER_27:
5822 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005823 case RTL_GIGA_MAC_VER_31:
Francois Romieu1519e572011-02-03 12:02:36 +01005824 /* Experimental science. Pktgen proof. */
5825 case RTL_GIGA_MAC_VER_12:
5826 case RTL_GIGA_MAC_VER_25:
5827 if (status == RxFIFOOver)
5828 goto done;
5829 break;
5830 default:
5831 break;
5832 }
David Dillowf11a3772009-05-22 15:29:34 +00005833 }
5834
5835 if (unlikely(status & SYSErr)) {
5836 rtl8169_pcierr_interrupt(dev);
5837 break;
5838 }
5839
5840 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005841 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00005842
5843 /* We need to see the lastest version of tp->intr_mask to
5844 * avoid ignoring an MSI interrupt and having to wait for
5845 * another event which may never come.
5846 */
5847 smp_rmb();
5848 if (status & tp->intr_mask & tp->napi_event) {
5849 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5850 tp->intr_mask = ~tp->napi_event;
5851
5852 if (likely(napi_schedule_prep(&tp->napi)))
5853 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00005854 else
5855 netif_info(tp, intr, dev,
5856 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00005857 }
5858
5859 /* We only get a new MSI interrupt when all active irq
5860 * sources on the chip have been acknowledged. So, ack
5861 * everything we've seen and check if new sources have become
5862 * active to avoid blocking all interrupts from the chip.
5863 */
5864 RTL_W16(IntrStatus,
5865 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5866 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 }
Francois Romieu1519e572011-02-03 12:02:36 +01005868done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 return IRQ_RETVAL(handled);
5870}
5871
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005872static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005874 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5875 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005877 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005879 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 rtl8169_tx_interrupt(dev, tp, ioaddr);
5881
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005882 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005883 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005884
5885 /* We need for force the visibility of tp->intr_mask
5886 * for other CPUs, as we can loose an MSI interrupt
5887 * and potentially wait for a retransmit timeout if we don't.
5888 * The posted write to IntrMask is safe, as it will
5889 * eventually make it to the chip and we won't loose anything
5890 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 */
David Dillowf11a3772009-05-22 15:29:34 +00005892 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00005893 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01005894 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 }
5896
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005897 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899
Francois Romieu523a6092008-09-10 22:28:56 +02005900static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5901{
5902 struct rtl8169_private *tp = netdev_priv(dev);
5903
5904 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5905 return;
5906
5907 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5908 RTL_W32(RxMissed, 0);
5909}
5910
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911static void rtl8169_down(struct net_device *dev)
5912{
5913 struct rtl8169_private *tp = netdev_priv(dev);
5914 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915
Francois Romieu4876cc12011-03-11 21:07:11 +01005916 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917
5918 netif_stop_queue(dev);
5919
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005920 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005921
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 spin_lock_irq(&tp->lock);
5923
Hayes Wang92fc43b2011-07-06 15:58:03 +08005924 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005925 /*
5926 * At this point device interrupts can not be enabled in any function,
5927 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5928 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5929 */
Francois Romieu523a6092008-09-10 22:28:56 +02005930 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931
5932 spin_unlock_irq(&tp->lock);
5933
5934 synchronize_irq(dev->irq);
5935
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005937 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 rtl8169_tx_clear(tp);
5940
5941 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005942
5943 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944}
5945
5946static int rtl8169_close(struct net_device *dev)
5947{
5948 struct rtl8169_private *tp = netdev_priv(dev);
5949 struct pci_dev *pdev = tp->pci_dev;
5950
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005951 pm_runtime_get_sync(&pdev->dev);
5952
Francois Romieucecb5fd2011-04-01 10:21:07 +02005953 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005954 rtl8169_update_counters(dev);
5955
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956 rtl8169_down(dev);
5957
5958 free_irq(dev->irq, dev);
5959
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005960 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5961 tp->RxPhyAddr);
5962 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5963 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 tp->TxDescArray = NULL;
5965 tp->RxDescArray = NULL;
5966
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005967 pm_runtime_put_sync(&pdev->dev);
5968
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 return 0;
5970}
5971
Francois Romieu07ce4062007-02-23 23:36:39 +01005972static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973{
5974 struct rtl8169_private *tp = netdev_priv(dev);
5975 void __iomem *ioaddr = tp->mmio_addr;
5976 unsigned long flags;
5977 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005978 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 u32 tmp = 0;
5980
5981 if (dev->flags & IFF_PROMISC) {
5982 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005983 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 rx_mode =
5985 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5986 AcceptAllPhys;
5987 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005988 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005989 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 /* Too many to filter perfectly -- accept all multicasts. */
5991 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5992 mc_filter[1] = mc_filter[0] = 0xffffffff;
5993 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +00005994 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01005995
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 rx_mode = AcceptBroadcast | AcceptMyPhys;
5997 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00005998 netdev_for_each_mc_addr(ha, dev) {
5999 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
6001 rx_mode |= AcceptMulticast;
6002 }
6003 }
6004
6005 spin_lock_irqsave(&tp->lock, flags);
6006
Francois Romieu1687b562011-07-19 17:21:29 +02006007 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008
Francois Romieuf887cce2008-07-17 22:24:18 +02006009 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01006010 u32 data = mc_filter[0];
6011
6012 mc_filter[0] = swab32(mc_filter[1]);
6013 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02006014 }
6015
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07006017 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018
Francois Romieu57a9f232007-06-04 22:10:15 +02006019 RTL_W32(RxConfig, tmp);
6020
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 spin_unlock_irqrestore(&tp->lock, flags);
6022}
6023
6024/**
6025 * rtl8169_get_stats - Get rtl8169 read/write statistics
6026 * @dev: The Ethernet Device to get statistics for
6027 *
6028 * Get TX/RX statistics for rtl8169
6029 */
6030static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
6031{
6032 struct rtl8169_private *tp = netdev_priv(dev);
6033 void __iomem *ioaddr = tp->mmio_addr;
6034 unsigned long flags;
6035
6036 if (netif_running(dev)) {
6037 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02006038 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 spin_unlock_irqrestore(&tp->lock, flags);
6040 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02006041
Francois Romieucebf8cc2007-10-18 12:06:54 +02006042 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043}
6044
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006045static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006046{
françois romieu065c27c2011-01-03 15:08:12 +00006047 struct rtl8169_private *tp = netdev_priv(dev);
6048
Francois Romieu5d06a992006-02-23 00:47:58 +01006049 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006050 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006051
françois romieu065c27c2011-01-03 15:08:12 +00006052 rtl_pll_power_down(tp);
6053
Francois Romieu5d06a992006-02-23 00:47:58 +01006054 netif_device_detach(dev);
6055 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006056}
Francois Romieu5d06a992006-02-23 00:47:58 +01006057
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006058#ifdef CONFIG_PM
6059
6060static int rtl8169_suspend(struct device *device)
6061{
6062 struct pci_dev *pdev = to_pci_dev(device);
6063 struct net_device *dev = pci_get_drvdata(pdev);
6064
6065 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02006066
Francois Romieu5d06a992006-02-23 00:47:58 +01006067 return 0;
6068}
6069
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006070static void __rtl8169_resume(struct net_device *dev)
6071{
françois romieu065c27c2011-01-03 15:08:12 +00006072 struct rtl8169_private *tp = netdev_priv(dev);
6073
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006074 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006075
6076 rtl_pll_power_up(tp);
6077
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006078 rtl8169_schedule_work(dev, rtl8169_reset_task);
6079}
6080
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006081static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006082{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006083 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01006084 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006085 struct rtl8169_private *tp = netdev_priv(dev);
6086
6087 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01006088
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006089 if (netif_running(dev))
6090 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006091
Francois Romieu5d06a992006-02-23 00:47:58 +01006092 return 0;
6093}
6094
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006095static int rtl8169_runtime_suspend(struct device *device)
6096{
6097 struct pci_dev *pdev = to_pci_dev(device);
6098 struct net_device *dev = pci_get_drvdata(pdev);
6099 struct rtl8169_private *tp = netdev_priv(dev);
6100
6101 if (!tp->TxDescArray)
6102 return 0;
6103
6104 spin_lock_irq(&tp->lock);
6105 tp->saved_wolopts = __rtl8169_get_wol(tp);
6106 __rtl8169_set_wol(tp, WAKE_ANY);
6107 spin_unlock_irq(&tp->lock);
6108
6109 rtl8169_net_suspend(dev);
6110
6111 return 0;
6112}
6113
6114static int rtl8169_runtime_resume(struct device *device)
6115{
6116 struct pci_dev *pdev = to_pci_dev(device);
6117 struct net_device *dev = pci_get_drvdata(pdev);
6118 struct rtl8169_private *tp = netdev_priv(dev);
6119
6120 if (!tp->TxDescArray)
6121 return 0;
6122
6123 spin_lock_irq(&tp->lock);
6124 __rtl8169_set_wol(tp, tp->saved_wolopts);
6125 tp->saved_wolopts = 0;
6126 spin_unlock_irq(&tp->lock);
6127
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006128 rtl8169_init_phy(dev, tp);
6129
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006130 __rtl8169_resume(dev);
6131
6132 return 0;
6133}
6134
6135static int rtl8169_runtime_idle(struct device *device)
6136{
6137 struct pci_dev *pdev = to_pci_dev(device);
6138 struct net_device *dev = pci_get_drvdata(pdev);
6139 struct rtl8169_private *tp = netdev_priv(dev);
6140
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00006141 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006142}
6143
Alexey Dobriyan47145212009-12-14 18:00:08 -08006144static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006145 .suspend = rtl8169_suspend,
6146 .resume = rtl8169_resume,
6147 .freeze = rtl8169_suspend,
6148 .thaw = rtl8169_resume,
6149 .poweroff = rtl8169_suspend,
6150 .restore = rtl8169_resume,
6151 .runtime_suspend = rtl8169_runtime_suspend,
6152 .runtime_resume = rtl8169_runtime_resume,
6153 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006154};
6155
6156#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6157
6158#else /* !CONFIG_PM */
6159
6160#define RTL8169_PM_OPS NULL
6161
6162#endif /* !CONFIG_PM */
6163
Francois Romieu1765f952008-09-13 17:21:40 +02006164static void rtl_shutdown(struct pci_dev *pdev)
6165{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006166 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006167 struct rtl8169_private *tp = netdev_priv(dev);
6168 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu1765f952008-09-13 17:21:40 +02006169
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006170 rtl8169_net_suspend(dev);
6171
Francois Romieucecb5fd2011-04-01 10:21:07 +02006172 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006173 rtl_rar_set(tp, dev->perm_addr);
6174
françois romieu4bb3f522009-06-17 11:41:45 +00006175 spin_lock_irq(&tp->lock);
6176
Hayes Wang92fc43b2011-07-06 15:58:03 +08006177 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006178
6179 spin_unlock_irq(&tp->lock);
6180
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006181 if (system_state == SYSTEM_POWER_OFF) {
Hayes Wangaaa89c02011-07-06 15:58:08 +08006182 /* WoL fails with 8168b when the receiver is disabled. */
6183 if ((tp->mac_version == RTL_GIGA_MAC_VER_11 ||
6184 tp->mac_version == RTL_GIGA_MAC_VER_12 ||
6185 tp->mac_version == RTL_GIGA_MAC_VER_17) &&
6186 (tp->features & RTL_FEATURE_WOL)) {
françois romieuca52efd2009-07-24 12:34:19 +00006187 pci_clear_master(pdev);
6188
6189 RTL_W8(ChipCmd, CmdRxEnb);
6190 /* PCI commit */
6191 RTL_R8(ChipCmd);
6192 }
6193
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006194 pci_wake_from_d3(pdev, true);
6195 pci_set_power_state(pdev, PCI_D3hot);
6196 }
6197}
Francois Romieu5d06a992006-02-23 00:47:58 +01006198
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199static struct pci_driver rtl8169_pci_driver = {
6200 .name = MODULENAME,
6201 .id_table = rtl8169_pci_tbl,
6202 .probe = rtl8169_init_one,
6203 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02006204 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006205 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206};
6207
Francois Romieu07d3f512007-02-21 22:40:46 +01006208static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209{
Jeff Garzik29917622006-08-19 17:48:59 -04006210 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211}
6212
Francois Romieu07d3f512007-02-21 22:40:46 +01006213static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214{
6215 pci_unregister_driver(&rtl8169_pci_driver);
6216}
6217
6218module_init(rtl8169_init_module);
6219module_exit(rtl8169_cleanup_module);