blob: 3455d37a63a508c459dba6123eec066cb88efa99 [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 Romieuf0e837d2005-09-30 16:54:02 -070063 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define TX_BUFFS_AVAIL(tp) \
66 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
69 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050070static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Francois Romieu9c14cea2008-07-05 00:21:15 +020072#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
75#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
76
77#define R8169_REGS_SIZE 256
78#define R8169_NAPI_WEIGHT 64
79#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
80#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
81#define RX_BUF_SIZE 1536 /* Rx Buffer size */
82#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
83#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
84
85#define RTL8169_TX_TIMEOUT (6*HZ)
86#define RTL8169_PHY_TIMEOUT (10*HZ)
87
françois romieuea8dbdd2009-03-15 01:10:50 +000088#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
89#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020090#define RTL_EEPROM_SIG_ADDR 0x0000
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* write/read MMIO register */
93#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
94#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
95#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
96#define RTL_R8(reg) readb (ioaddr + (reg))
97#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +000098#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200101 RTL_GIGA_MAC_VER_01 = 0,
102 RTL_GIGA_MAC_VER_02,
103 RTL_GIGA_MAC_VER_03,
104 RTL_GIGA_MAC_VER_04,
105 RTL_GIGA_MAC_VER_05,
106 RTL_GIGA_MAC_VER_06,
107 RTL_GIGA_MAC_VER_07,
108 RTL_GIGA_MAC_VER_08,
109 RTL_GIGA_MAC_VER_09,
110 RTL_GIGA_MAC_VER_10,
111 RTL_GIGA_MAC_VER_11,
112 RTL_GIGA_MAC_VER_12,
113 RTL_GIGA_MAC_VER_13,
114 RTL_GIGA_MAC_VER_14,
115 RTL_GIGA_MAC_VER_15,
116 RTL_GIGA_MAC_VER_16,
117 RTL_GIGA_MAC_VER_17,
118 RTL_GIGA_MAC_VER_18,
119 RTL_GIGA_MAC_VER_19,
120 RTL_GIGA_MAC_VER_20,
121 RTL_GIGA_MAC_VER_21,
122 RTL_GIGA_MAC_VER_22,
123 RTL_GIGA_MAC_VER_23,
124 RTL_GIGA_MAC_VER_24,
125 RTL_GIGA_MAC_VER_25,
126 RTL_GIGA_MAC_VER_26,
127 RTL_GIGA_MAC_VER_27,
128 RTL_GIGA_MAC_VER_28,
129 RTL_GIGA_MAC_VER_29,
130 RTL_GIGA_MAC_VER_30,
131 RTL_GIGA_MAC_VER_31,
132 RTL_GIGA_MAC_VER_32,
133 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800134 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800135 RTL_GIGA_MAC_VER_35,
136 RTL_GIGA_MAC_VER_36,
Francois Romieu85bffe62011-04-27 08:22:39 +0200137 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
Francois Romieu2b7b4312011-04-18 22:53:24 -0700140enum rtl_tx_desc_version {
141 RTL_TD_0 = 0,
142 RTL_TD_1 = 1,
143};
144
Francois Romieud58d46b2011-05-03 16:38:29 +0200145#define JUMBO_1K ETH_DATA_LEN
146#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
147#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
148#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
149#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
150
151#define _R(NAME,TD,FW,SZ,B) { \
152 .name = NAME, \
153 .txd_version = TD, \
154 .fw_name = FW, \
155 .jumbo_max = SZ, \
156 .jumbo_tx_csum = B \
157}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800159static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700161 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200162 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200163 u16 jumbo_max;
164 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200165} rtl_chip_infos[] = {
166 /* PCI devices. */
167 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200168 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200169 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200170 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200171 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200172 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200173 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200174 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200175 [RTL_GIGA_MAC_VER_05] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200176 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200177 [RTL_GIGA_MAC_VER_06] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200178 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200179 /* PCI-E devices. */
180 [RTL_GIGA_MAC_VER_07] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200181 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200182 [RTL_GIGA_MAC_VER_08] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200183 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200184 [RTL_GIGA_MAC_VER_09] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200185 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200186 [RTL_GIGA_MAC_VER_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200187 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200188 [RTL_GIGA_MAC_VER_11] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200189 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200190 [RTL_GIGA_MAC_VER_12] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200191 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200192 [RTL_GIGA_MAC_VER_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200193 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200194 [RTL_GIGA_MAC_VER_14] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200195 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200196 [RTL_GIGA_MAC_VER_15] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200197 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200198 [RTL_GIGA_MAC_VER_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200199 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200200 [RTL_GIGA_MAC_VER_17] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200201 _R("RTL8168b/8111b", RTL_TD_1, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200202 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200203 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200204 [RTL_GIGA_MAC_VER_19] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200205 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200206 [RTL_GIGA_MAC_VER_20] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200207 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200208 [RTL_GIGA_MAC_VER_21] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200209 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200210 [RTL_GIGA_MAC_VER_22] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200211 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200212 [RTL_GIGA_MAC_VER_23] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200213 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200214 [RTL_GIGA_MAC_VER_24] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200215 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200216 [RTL_GIGA_MAC_VER_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200217 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
218 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200219 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200220 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
221 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200222 [RTL_GIGA_MAC_VER_27] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200223 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200224 [RTL_GIGA_MAC_VER_28] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200225 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200226 [RTL_GIGA_MAC_VER_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200227 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
228 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200229 [RTL_GIGA_MAC_VER_30] =
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_31] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200233 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200234 [RTL_GIGA_MAC_VER_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200235 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
236 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200237 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200238 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
239 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800240 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200241 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
242 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800243 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200244 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
245 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800246 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200247 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
248 JUMBO_9K, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249};
250#undef _R
251
Francois Romieubcf0bf92006-07-26 23:14:13 +0200252enum cfg_version {
253 RTL_CFG_0 = 0x00,
254 RTL_CFG_1,
255 RTL_CFG_2
256};
257
Francois Romieu07ce4062007-02-23 23:36:39 +0100258static void rtl_hw_start_8169(struct net_device *);
259static void rtl_hw_start_8168(struct net_device *);
260static void rtl_hw_start_8101(struct net_device *);
261
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000262static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200263 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200264 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200265 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100266 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200267 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
268 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000269 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200270 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200271 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
272 { PCI_VENDOR_ID_LINKSYS, 0x1032,
273 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100274 { 0x0001, 0x8168,
275 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 {0,},
277};
278
279MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
280
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000281static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700282static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200283static struct {
284 u32 msg_enable;
285} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Francois Romieu07d3f512007-02-21 22:40:46 +0100287enum rtl_registers {
288 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100289 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100290 MAR0 = 8, /* Multicast filter. */
291 CounterAddrLow = 0x10,
292 CounterAddrHigh = 0x14,
293 TxDescStartAddrLow = 0x20,
294 TxDescStartAddrHigh = 0x24,
295 TxHDescStartAddrLow = 0x28,
296 TxHDescStartAddrHigh = 0x2c,
297 FLASH = 0x30,
298 ERSR = 0x36,
299 ChipCmd = 0x37,
300 TxPoll = 0x38,
301 IntrMask = 0x3c,
302 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700303
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800304 TxConfig = 0x40,
305#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
306#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
307
308 RxConfig = 0x44,
309#define RX128_INT_EN (1 << 15) /* 8111c and later */
310#define RX_MULTI_EN (1 << 14) /* 8111c only */
311#define RXCFG_FIFO_SHIFT 13
312 /* No threshold before first PCI xfer */
313#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
314#define RXCFG_DMA_SHIFT 8
315 /* Unlimited maximum PCI burst. */
316#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700317
Francois Romieu07d3f512007-02-21 22:40:46 +0100318 RxMissed = 0x4c,
319 Cfg9346 = 0x50,
320 Config0 = 0x51,
321 Config1 = 0x52,
322 Config2 = 0x53,
323 Config3 = 0x54,
324 Config4 = 0x55,
325 Config5 = 0x56,
326 MultiIntr = 0x5c,
327 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100328 PHYstatus = 0x6c,
329 RxMaxSize = 0xda,
330 CPlusCmd = 0xe0,
331 IntrMitigate = 0xe2,
332 RxDescAddrLow = 0xe4,
333 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000334 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
335
336#define NoEarlyTx 0x3f /* Max value : no early transmit. */
337
338 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
339
340#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800341#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000342
Francois Romieu07d3f512007-02-21 22:40:46 +0100343 FuncEvent = 0xf0,
344 FuncEventMask = 0xf4,
345 FuncPresetState = 0xf8,
346 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347};
348
Francois Romieuf162a5d2008-06-01 22:37:49 +0200349enum rtl8110_registers {
350 TBICSR = 0x64,
351 TBI_ANAR = 0x68,
352 TBI_LPAR = 0x6a,
353};
354
355enum rtl8168_8101_registers {
356 CSIDR = 0x64,
357 CSIAR = 0x68,
358#define CSIAR_FLAG 0x80000000
359#define CSIAR_WRITE_CMD 0x80000000
360#define CSIAR_BYTE_ENABLE 0x0f
361#define CSIAR_BYTE_ENABLE_SHIFT 12
362#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000363 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200364 EPHYAR = 0x80,
365#define EPHYAR_FLAG 0x80000000
366#define EPHYAR_WRITE_CMD 0x80000000
367#define EPHYAR_REG_MASK 0x1f
368#define EPHYAR_REG_SHIFT 16
369#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800370 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800371#define PFM_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200372 DBG_REG = 0xd1,
373#define FIX_NAK_1 (1 << 4)
374#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800375 TWSI = 0xd2,
376 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800377#define NOW_IS_OOB (1 << 7)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800378#define EN_NDP (1 << 3)
379#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000380 EFUSEAR = 0xdc,
381#define EFUSEAR_FLAG 0x80000000
382#define EFUSEAR_WRITE_CMD 0x80000000
383#define EFUSEAR_READ_CMD 0x00000000
384#define EFUSEAR_REG_MASK 0x03ff
385#define EFUSEAR_REG_SHIFT 8
386#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200387};
388
françois romieuc0e45c12011-01-03 15:08:04 +0000389enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800390 LED_FREQ = 0x1a,
391 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000392 ERIDR = 0x70,
393 ERIAR = 0x74,
394#define ERIAR_FLAG 0x80000000
395#define ERIAR_WRITE_CMD 0x80000000
396#define ERIAR_READ_CMD 0x00000000
397#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000398#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800399#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
400#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
401#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
402#define ERIAR_MASK_SHIFT 12
403#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
404#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
405#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000406 EPHY_RXER_NUM = 0x7c,
407 OCPDR = 0xb0, /* OCP GPHY access */
408#define OCPDR_WRITE_CMD 0x80000000
409#define OCPDR_READ_CMD 0x00000000
410#define OCPDR_REG_MASK 0x7f
411#define OCPDR_GPHY_REG_SHIFT 16
412#define OCPDR_DATA_MASK 0xffff
413 OCPAR = 0xb4,
414#define OCPAR_FLAG 0x80000000
415#define OCPAR_GPHY_WRITE_CMD 0x8000f060
416#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000417 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
418 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200419#define TXPLA_RST (1 << 29)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800420#define PWM_EN (1 << 22)
françois romieuc0e45c12011-01-03 15:08:04 +0000421};
422
Francois Romieu07d3f512007-02-21 22:40:46 +0100423enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100425 SYSErr = 0x8000,
426 PCSTimeout = 0x4000,
427 SWInt = 0x0100,
428 TxDescUnavail = 0x0080,
429 RxFIFOOver = 0x0040,
430 LinkChg = 0x0020,
431 RxOverflow = 0x0010,
432 TxErr = 0x0008,
433 TxOK = 0x0004,
434 RxErr = 0x0002,
435 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400438 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200439 RxFOVF = (1 << 23),
440 RxRWT = (1 << 22),
441 RxRES = (1 << 21),
442 RxRUNT = (1 << 20),
443 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800446 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100447 CmdReset = 0x10,
448 CmdRxEnb = 0x08,
449 CmdTxEnb = 0x04,
450 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Francois Romieu275391a2007-02-23 23:50:28 +0100452 /* TXPoll register p.5 */
453 HPQ = 0x80, /* Poll cmd on the high prio queue */
454 NPQ = 0x40, /* Poll cmd on the low prio queue */
455 FSWInt = 0x01, /* Forced software interrupt */
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100458 Cfg9346_Lock = 0x00,
459 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100462 AcceptErr = 0x20,
463 AcceptRunt = 0x10,
464 AcceptBroadcast = 0x08,
465 AcceptMulticast = 0x04,
466 AcceptMyPhys = 0x02,
467 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200468#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /* TxConfigBits */
471 TxInterFrameGapShift = 24,
472 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
473
Francois Romieu5d06a992006-02-23 00:47:58 +0100474 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200475 LEDS1 = (1 << 7),
476 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200477 Speed_down = (1 << 4),
478 MEMMAP = (1 << 3),
479 IOMAP = (1 << 2),
480 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100481 PMEnable = (1 << 0), /* Power Management Enable */
482
Francois Romieu6dccd162007-02-13 23:38:05 +0100483 /* Config2 register p. 25 */
françois romieu2ca6cf02011-12-15 08:37:43 +0000484 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100485 PCI_Clock_66MHz = 0x01,
486 PCI_Clock_33MHz = 0x00,
487
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100488 /* Config3 register p.25 */
489 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
490 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200491 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200492 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100493
Francois Romieud58d46b2011-05-03 16:38:29 +0200494 /* Config4 register */
495 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
496
Francois Romieu5d06a992006-02-23 00:47:58 +0100497 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100498 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
499 MWF = (1 << 5), /* Accept Multicast wakeup frame */
500 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200501 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100502 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100503 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /* TBICSR p.28 */
506 TBIReset = 0x80000000,
507 TBILoopback = 0x40000000,
508 TBINwEnable = 0x20000000,
509 TBINwRestart = 0x10000000,
510 TBILinkOk = 0x02000000,
511 TBINwComplete = 0x01000000,
512
513 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200514 EnableBist = (1 << 15), // 8168 8101
515 Mac_dbgo_oe = (1 << 14), // 8168 8101
516 Normal_mode = (1 << 13), // unused
517 Force_half_dup = (1 << 12), // 8168 8101
518 Force_rxflow_en = (1 << 11), // 8168 8101
519 Force_txflow_en = (1 << 10), // 8168 8101
520 Cxpl_dbg_sel = (1 << 9), // 8168 8101
521 ASF = (1 << 8), // 8168 8101
522 PktCntrDisable = (1 << 7), // 8168 8101
523 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 RxVlan = (1 << 6),
525 RxChkSum = (1 << 5),
526 PCIDAC = (1 << 4),
527 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100528 INTT_0 = 0x0000, // 8168
529 INTT_1 = 0x0001, // 8168
530 INTT_2 = 0x0002, // 8168
531 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100534 TBI_Enable = 0x80,
535 TxFlowCtrl = 0x40,
536 RxFlowCtrl = 0x20,
537 _1000bpsF = 0x10,
538 _100bps = 0x08,
539 _10bps = 0x04,
540 LinkStatus = 0x02,
541 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100544 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200545
546 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100547 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548};
549
Francois Romieu2b7b4312011-04-18 22:53:24 -0700550enum rtl_desc_bit {
551 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
553 RingEnd = (1 << 30), /* End of descriptor ring */
554 FirstFrag = (1 << 29), /* First segment of a packet */
555 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700556};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Francois Romieu2b7b4312011-04-18 22:53:24 -0700558/* Generic case. */
559enum rtl_tx_desc_bit {
560 /* First doubleword. */
561 TD_LSO = (1 << 27), /* Large Send Offload */
562#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Francois Romieu2b7b4312011-04-18 22:53:24 -0700564 /* Second doubleword. */
565 TxVlanTag = (1 << 17), /* Add VLAN tag */
566};
567
568/* 8169, 8168b and 810x except 8102e. */
569enum rtl_tx_desc_bit_0 {
570 /* First doubleword. */
571#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
572 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
573 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
574 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
575};
576
577/* 8102e, 8168c and beyond. */
578enum rtl_tx_desc_bit_1 {
579 /* Second doubleword. */
580#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
581 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
582 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
583 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
584};
585
586static const struct rtl_tx_desc_info {
587 struct {
588 u32 udp;
589 u32 tcp;
590 } checksum;
591 u16 mss_shift;
592 u16 opts_offset;
593} tx_desc_info [] = {
594 [RTL_TD_0] = {
595 .checksum = {
596 .udp = TD0_IP_CS | TD0_UDP_CS,
597 .tcp = TD0_IP_CS | TD0_TCP_CS
598 },
599 .mss_shift = TD0_MSS_SHIFT,
600 .opts_offset = 0
601 },
602 [RTL_TD_1] = {
603 .checksum = {
604 .udp = TD1_IP_CS | TD1_UDP_CS,
605 .tcp = TD1_IP_CS | TD1_TCP_CS
606 },
607 .mss_shift = TD1_MSS_SHIFT,
608 .opts_offset = 1
609 }
610};
611
612enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* Rx private */
614 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
615 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
616
617#define RxProtoUDP (PID1)
618#define RxProtoTCP (PID0)
619#define RxProtoIP (PID1 | PID0)
620#define RxProtoMask RxProtoIP
621
622 IPFail = (1 << 16), /* IP checksum failed */
623 UDPFail = (1 << 15), /* UDP/IP checksum failed */
624 TCPFail = (1 << 14), /* TCP/IP checksum failed */
625 RxVlanTag = (1 << 16), /* VLAN tag available */
626};
627
628#define RsvdMask 0x3fffc000
629
630struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200631 __le32 opts1;
632 __le32 opts2;
633 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634};
635
636struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200637 __le32 opts1;
638 __le32 opts2;
639 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640};
641
642struct ring_info {
643 struct sk_buff *skb;
644 u32 len;
645 u8 __pad[sizeof(void *) - sizeof(u32)];
646};
647
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200648enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200649 RTL_FEATURE_WOL = (1 << 0),
650 RTL_FEATURE_MSI = (1 << 1),
651 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200652};
653
Ivan Vecera355423d2009-02-06 21:49:57 -0800654struct rtl8169_counters {
655 __le64 tx_packets;
656 __le64 rx_packets;
657 __le64 tx_errors;
658 __le32 rx_errors;
659 __le16 rx_missed;
660 __le16 align_errors;
661 __le32 tx_one_collision;
662 __le32 tx_multi_collision;
663 __le64 rx_unicast;
664 __le64 rx_broadcast;
665 __le32 rx_multicast;
666 __le16 tx_aborted;
667 __le16 tx_underun;
668};
669
Francois Romieuda78dbf2012-01-26 14:18:23 +0100670enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100671 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100672 RTL_FLAG_TASK_SLOW_PENDING,
673 RTL_FLAG_TASK_RESET_PENDING,
674 RTL_FLAG_TASK_PHY_PENDING,
675 RTL_FLAG_MAX
676};
677
Junchang Wang8027aa22012-03-04 23:30:32 +0100678struct rtl8169_stats {
679 u64 packets;
680 u64 bytes;
681 struct u64_stats_sync syncp;
682};
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684struct rtl8169_private {
685 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200686 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000687 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700688 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200689 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700690 u16 txd_version;
691 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
693 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
694 u32 dirty_rx;
695 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100696 struct rtl8169_stats rx_stats;
697 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
699 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
700 dma_addr_t TxPhyAddr;
701 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000702 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 struct timer_list timer;
705 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100706
707 u16 event_slow;
françois romieuc0e45c12011-01-03 15:08:04 +0000708
709 struct mdio_ops {
710 void (*write)(void __iomem *, int, int);
711 int (*read)(void __iomem *, int);
712 } mdio_ops;
713
françois romieu065c27c2011-01-03 15:08:12 +0000714 struct pll_power_ops {
715 void (*down)(struct rtl8169_private *);
716 void (*up)(struct rtl8169_private *);
717 } pll_power_ops;
718
Francois Romieud58d46b2011-05-03 16:38:29 +0200719 struct jumbo_ops {
720 void (*enable)(struct rtl8169_private *);
721 void (*disable)(struct rtl8169_private *);
722 } jumbo_ops;
723
Oliver Neukum54405cd2011-01-06 21:55:13 +0100724 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200725 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000726 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100727 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000728 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800730 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100731
732 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100733 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
734 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100735 struct work_struct work;
736 } wk;
737
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200738 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200739
740 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800741 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000742 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400743 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000744
Francois Romieub6ffd972011-06-17 17:00:05 +0200745 struct rtl_fw {
746 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200747
748#define RTL_VER_SIZE 32
749
750 char version[RTL_VER_SIZE];
751
752 struct rtl_fw_phy_action {
753 __le32 *code;
754 size_t size;
755 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200756 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300757#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758};
759
Ralf Baechle979b6c12005-06-13 14:30:40 -0700760MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700763MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200764module_param_named(debug, debug.msg_enable, int, 0);
765MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766MODULE_LICENSE("GPL");
767MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000768MODULE_FIRMWARE(FIRMWARE_8168D_1);
769MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000770MODULE_FIRMWARE(FIRMWARE_8168E_1);
771MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400772MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800773MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800774MODULE_FIRMWARE(FIRMWARE_8168F_1);
775MODULE_FIRMWARE(FIRMWARE_8168F_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000778static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
779 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100780static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100782static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100784static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785static void rtl8169_tx_timeout(struct net_device *dev);
Junchang Wang8027aa22012-03-04 23:30:32 +0100786static struct rtnl_link_stats64 *rtl8169_get_stats64(struct net_device *dev,
787 struct rtnl_link_stats64
788 *stats);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200789static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Francois Romieu99f252b2007-04-02 22:59:59 +0200790static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700791static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Francois Romieuda78dbf2012-01-26 14:18:23 +0100793static void rtl_lock_work(struct rtl8169_private *tp)
794{
795 mutex_lock(&tp->wk.mutex);
796}
797
798static void rtl_unlock_work(struct rtl8169_private *tp)
799{
800 mutex_unlock(&tp->wk.mutex);
801}
802
Francois Romieud58d46b2011-05-03 16:38:29 +0200803static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
804{
805 int cap = pci_pcie_cap(pdev);
806
807 if (cap) {
808 u16 ctl;
809
810 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
811 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
812 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
813 }
814}
815
françois romieub646d902011-01-03 15:08:21 +0000816static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
817{
818 void __iomem *ioaddr = tp->mmio_addr;
819 int i;
820
821 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
822 for (i = 0; i < 20; i++) {
823 udelay(100);
824 if (RTL_R32(OCPAR) & OCPAR_FLAG)
825 break;
826 }
827 return RTL_R32(OCPDR);
828}
829
830static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
831{
832 void __iomem *ioaddr = tp->mmio_addr;
833 int i;
834
835 RTL_W32(OCPDR, data);
836 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
837 for (i = 0; i < 20; i++) {
838 udelay(100);
839 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
840 break;
841 }
842}
843
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800844static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000845{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800846 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000847 int i;
848
849 RTL_W8(ERIDR, cmd);
850 RTL_W32(ERIAR, 0x800010e8);
851 msleep(2);
852 for (i = 0; i < 5; i++) {
853 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200854 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000855 break;
856 }
857
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800858 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000859}
860
861#define OOB_CMD_RESET 0x00
862#define OOB_CMD_DRIVER_START 0x05
863#define OOB_CMD_DRIVER_STOP 0x06
864
Francois Romieucecb5fd2011-04-01 10:21:07 +0200865static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
866{
867 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
868}
869
françois romieub646d902011-01-03 15:08:21 +0000870static void rtl8168_driver_start(struct rtl8169_private *tp)
871{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200872 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000873 int i;
874
875 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
876
Francois Romieucecb5fd2011-04-01 10:21:07 +0200877 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000878
françois romieub646d902011-01-03 15:08:21 +0000879 for (i = 0; i < 10; i++) {
880 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000881 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000882 break;
883 }
884}
885
886static void rtl8168_driver_stop(struct rtl8169_private *tp)
887{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200888 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000889 int i;
890
891 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
892
Francois Romieucecb5fd2011-04-01 10:21:07 +0200893 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000894
françois romieub646d902011-01-03 15:08:21 +0000895 for (i = 0; i < 10; i++) {
896 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000897 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000898 break;
899 }
900}
901
hayeswang4804b3b2011-03-21 01:50:29 +0000902static int r8168dp_check_dash(struct rtl8169_private *tp)
903{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200904 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000905
Francois Romieucecb5fd2011-04-01 10:21:07 +0200906 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000907}
françois romieub646d902011-01-03 15:08:21 +0000908
françois romieu4da19632011-01-03 15:07:55 +0000909static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 int i;
912
Francois Romieua6baf3a2007-11-08 23:23:21 +0100913 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Francois Romieu23714082006-01-29 00:49:09 +0100915 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100916 /*
917 * Check if the RTL8169 has completed writing to the specified
918 * MII register.
919 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200920 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 break;
Francois Romieu23714082006-01-29 00:49:09 +0100922 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700924 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700925 * According to hardware specs a 20us delay is required after write
926 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700927 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700928 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
françois romieu4da19632011-01-03 15:07:55 +0000931static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
933 int i, value = -1;
934
Francois Romieua6baf3a2007-11-08 23:23:21 +0100935 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Francois Romieu23714082006-01-29 00:49:09 +0100937 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100938 /*
939 * Check if the RTL8169 has completed retrieving data from
940 * the specified MII register.
941 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100943 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 break;
945 }
Francois Romieu23714082006-01-29 00:49:09 +0100946 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700948 /*
949 * According to hardware specs a 20us delay is required after read
950 * complete indication, but before sending next command.
951 */
952 udelay(20);
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return value;
955}
956
françois romieuc0e45c12011-01-03 15:08:04 +0000957static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
958{
959 int i;
960
961 RTL_W32(OCPDR, data |
962 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
963 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
964 RTL_W32(EPHY_RXER_NUM, 0);
965
966 for (i = 0; i < 100; i++) {
967 mdelay(1);
968 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
969 break;
970 }
971}
972
973static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
974{
975 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
976 (value & OCPDR_DATA_MASK));
977}
978
979static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
980{
981 int i;
982
983 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
984
985 mdelay(1);
986 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
987 RTL_W32(EPHY_RXER_NUM, 0);
988
989 for (i = 0; i < 100; i++) {
990 mdelay(1);
991 if (RTL_R32(OCPAR) & OCPAR_FLAG)
992 break;
993 }
994
995 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
996}
997
françois romieue6de30d2011-01-03 15:08:37 +0000998#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
999
1000static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1001{
1002 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1003}
1004
1005static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1006{
1007 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1008}
1009
1010static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
1011{
1012 r8168dp_2_mdio_start(ioaddr);
1013
1014 r8169_mdio_write(ioaddr, reg_addr, value);
1015
1016 r8168dp_2_mdio_stop(ioaddr);
1017}
1018
1019static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
1020{
1021 int value;
1022
1023 r8168dp_2_mdio_start(ioaddr);
1024
1025 value = r8169_mdio_read(ioaddr, reg_addr);
1026
1027 r8168dp_2_mdio_stop(ioaddr);
1028
1029 return value;
1030}
1031
françois romieu4da19632011-01-03 15:07:55 +00001032static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001033{
françois romieuc0e45c12011-01-03 15:08:04 +00001034 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001035}
1036
françois romieu4da19632011-01-03 15:07:55 +00001037static int rtl_readphy(struct rtl8169_private *tp, int location)
1038{
françois romieuc0e45c12011-01-03 15:08:04 +00001039 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +00001040}
1041
1042static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1043{
1044 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1045}
1046
1047static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001048{
1049 int val;
1050
françois romieu4da19632011-01-03 15:07:55 +00001051 val = rtl_readphy(tp, reg_addr);
1052 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001053}
1054
Francois Romieuccdffb92008-07-26 14:26:06 +02001055static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1056 int val)
1057{
1058 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001059
françois romieu4da19632011-01-03 15:07:55 +00001060 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001061}
1062
1063static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1064{
1065 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001066
françois romieu4da19632011-01-03 15:07:55 +00001067 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001068}
1069
Francois Romieudacf8152008-08-02 20:44:13 +02001070static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
1071{
1072 unsigned int i;
1073
1074 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1075 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1076
1077 for (i = 0; i < 100; i++) {
1078 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1079 break;
1080 udelay(10);
1081 }
1082}
1083
1084static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1085{
1086 u16 value = 0xffff;
1087 unsigned int i;
1088
1089 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1090
1091 for (i = 0; i < 100; i++) {
1092 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1093 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1094 break;
1095 }
1096 udelay(10);
1097 }
1098
1099 return value;
1100}
1101
1102static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1103{
1104 unsigned int i;
1105
1106 RTL_W32(CSIDR, value);
1107 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1108 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1109
1110 for (i = 0; i < 100; i++) {
1111 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1112 break;
1113 udelay(10);
1114 }
1115}
1116
1117static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1118{
1119 u32 value = ~0x00;
1120 unsigned int i;
1121
1122 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1123 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1124
1125 for (i = 0; i < 100; i++) {
1126 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1127 value = RTL_R32(CSIDR);
1128 break;
1129 }
1130 udelay(10);
1131 }
1132
1133 return value;
1134}
1135
Hayes Wang133ac402011-07-06 15:58:05 +08001136static
1137void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1138{
1139 unsigned int i;
1140
1141 BUG_ON((addr & 3) || (mask == 0));
1142 RTL_W32(ERIDR, val);
1143 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1144
1145 for (i = 0; i < 100; i++) {
1146 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1147 break;
1148 udelay(100);
1149 }
1150}
1151
1152static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1153{
1154 u32 value = ~0x00;
1155 unsigned int i;
1156
1157 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1158
1159 for (i = 0; i < 100; i++) {
1160 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1161 value = RTL_R32(ERIDR);
1162 break;
1163 }
1164 udelay(100);
1165 }
1166
1167 return value;
1168}
1169
1170static void
1171rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1172{
1173 u32 val;
1174
1175 val = rtl_eri_read(ioaddr, addr, type);
1176 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1177}
1178
françois romieuc28aa382011-08-02 03:53:43 +00001179struct exgmac_reg {
1180 u16 addr;
1181 u16 mask;
1182 u32 val;
1183};
1184
1185static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1186 const struct exgmac_reg *r, int len)
1187{
1188 while (len-- > 0) {
1189 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1190 r++;
1191 }
1192}
1193
françois romieudaf9df62009-10-07 12:44:20 +00001194static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1195{
1196 u8 value = 0xff;
1197 unsigned int i;
1198
1199 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1200
1201 for (i = 0; i < 300; i++) {
1202 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1203 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1204 break;
1205 }
1206 udelay(100);
1207 }
1208
1209 return value;
1210}
1211
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001212static u16 rtl_get_events(struct rtl8169_private *tp)
1213{
1214 void __iomem *ioaddr = tp->mmio_addr;
1215
1216 return RTL_R16(IntrStatus);
1217}
1218
1219static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1220{
1221 void __iomem *ioaddr = tp->mmio_addr;
1222
1223 RTL_W16(IntrStatus, bits);
1224 mmiowb();
1225}
1226
1227static void rtl_irq_disable(struct rtl8169_private *tp)
1228{
1229 void __iomem *ioaddr = tp->mmio_addr;
1230
1231 RTL_W16(IntrMask, 0);
1232 mmiowb();
1233}
1234
Francois Romieu3e990ff2012-01-26 12:50:01 +01001235static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1236{
1237 void __iomem *ioaddr = tp->mmio_addr;
1238
1239 RTL_W16(IntrMask, bits);
1240}
1241
Francois Romieuda78dbf2012-01-26 14:18:23 +01001242#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1243#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1244#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1245
1246static void rtl_irq_enable_all(struct rtl8169_private *tp)
1247{
1248 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1249}
1250
françois romieu811fd302011-12-04 20:30:45 +00001251static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
françois romieu811fd302011-12-04 20:30:45 +00001253 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001255 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001256 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001257 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
françois romieu4da19632011-01-03 15:07:55 +00001260static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
françois romieu4da19632011-01-03 15:07:55 +00001262 void __iomem *ioaddr = tp->mmio_addr;
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return RTL_R32(TBICSR) & TBIReset;
1265}
1266
françois romieu4da19632011-01-03 15:07:55 +00001267static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
françois romieu4da19632011-01-03 15:07:55 +00001269 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
1272static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1273{
1274 return RTL_R32(TBICSR) & TBILinkOk;
1275}
1276
1277static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1278{
1279 return RTL_R8(PHYstatus) & LinkStatus;
1280}
1281
françois romieu4da19632011-01-03 15:07:55 +00001282static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
françois romieu4da19632011-01-03 15:07:55 +00001284 void __iomem *ioaddr = tp->mmio_addr;
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1287}
1288
françois romieu4da19632011-01-03 15:07:55 +00001289static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 unsigned int val;
1292
françois romieu4da19632011-01-03 15:07:55 +00001293 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1294 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
1296
Hayes Wang70090422011-07-06 15:58:06 +08001297static void rtl_link_chg_patch(struct rtl8169_private *tp)
1298{
1299 void __iomem *ioaddr = tp->mmio_addr;
1300 struct net_device *dev = tp->dev;
1301
1302 if (!netif_running(dev))
1303 return;
1304
1305 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1306 if (RTL_R8(PHYstatus) & _1000bpsF) {
1307 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1308 0x00000011, ERIAR_EXGMAC);
1309 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1310 0x00000005, ERIAR_EXGMAC);
1311 } else if (RTL_R8(PHYstatus) & _100bps) {
1312 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1313 0x0000001f, ERIAR_EXGMAC);
1314 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1315 0x00000005, ERIAR_EXGMAC);
1316 } else {
1317 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1318 0x0000001f, ERIAR_EXGMAC);
1319 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1320 0x0000003f, ERIAR_EXGMAC);
1321 }
1322 /* Reset packet filter */
1323 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1324 ERIAR_EXGMAC);
1325 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1326 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001327 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1328 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1329 if (RTL_R8(PHYstatus) & _1000bpsF) {
1330 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1331 0x00000011, ERIAR_EXGMAC);
1332 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1333 0x00000005, ERIAR_EXGMAC);
1334 } else {
1335 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1336 0x0000001f, ERIAR_EXGMAC);
1337 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1338 0x0000003f, ERIAR_EXGMAC);
1339 }
Hayes Wang70090422011-07-06 15:58:06 +08001340 }
1341}
1342
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001343static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001344 struct rtl8169_private *tp,
1345 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001348 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001349 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001350 if (pm)
1351 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001353 if (net_ratelimit())
1354 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001355 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001357 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001358 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001359 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001363static void rtl8169_check_link_status(struct net_device *dev,
1364 struct rtl8169_private *tp,
1365 void __iomem *ioaddr)
1366{
1367 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1368}
1369
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001370#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1371
1372static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1373{
1374 void __iomem *ioaddr = tp->mmio_addr;
1375 u8 options;
1376 u32 wolopts = 0;
1377
1378 options = RTL_R8(Config1);
1379 if (!(options & PMEnable))
1380 return 0;
1381
1382 options = RTL_R8(Config3);
1383 if (options & LinkUp)
1384 wolopts |= WAKE_PHY;
1385 if (options & MagicPacket)
1386 wolopts |= WAKE_MAGIC;
1387
1388 options = RTL_R8(Config5);
1389 if (options & UWF)
1390 wolopts |= WAKE_UCAST;
1391 if (options & BWF)
1392 wolopts |= WAKE_BCAST;
1393 if (options & MWF)
1394 wolopts |= WAKE_MCAST;
1395
1396 return wolopts;
1397}
1398
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001399static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1400{
1401 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001402
Francois Romieuda78dbf2012-01-26 14:18:23 +01001403 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001404
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001405 wol->supported = WAKE_ANY;
1406 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001407
Francois Romieuda78dbf2012-01-26 14:18:23 +01001408 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001409}
1410
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001411static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001412{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001413 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001414 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001415 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001416 u32 opt;
1417 u16 reg;
1418 u8 mask;
1419 } cfg[] = {
1420 { WAKE_ANY, Config1, PMEnable },
1421 { WAKE_PHY, Config3, LinkUp },
1422 { WAKE_MAGIC, Config3, MagicPacket },
1423 { WAKE_UCAST, Config5, UWF },
1424 { WAKE_BCAST, Config5, BWF },
1425 { WAKE_MCAST, Config5, MWF },
1426 { WAKE_ANY, Config5, LanWake }
1427 };
1428
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001429 RTL_W8(Cfg9346, Cfg9346_Unlock);
1430
1431 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1432 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001433 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001434 options |= cfg[i].mask;
1435 RTL_W8(cfg[i].reg, options);
1436 }
1437
1438 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001439}
1440
1441static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1442{
1443 struct rtl8169_private *tp = netdev_priv(dev);
1444
Francois Romieuda78dbf2012-01-26 14:18:23 +01001445 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001446
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001447 if (wol->wolopts)
1448 tp->features |= RTL_FEATURE_WOL;
1449 else
1450 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001451 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001452
1453 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001454
françois romieuea809072010-11-08 13:23:58 +00001455 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1456
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001457 return 0;
1458}
1459
Francois Romieu31bd2042011-04-26 18:58:59 +02001460static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1461{
Francois Romieu85bffe62011-04-27 08:22:39 +02001462 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001463}
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465static void rtl8169_get_drvinfo(struct net_device *dev,
1466 struct ethtool_drvinfo *info)
1467{
1468 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001469 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Rick Jones68aad782011-11-07 13:29:27 +00001471 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1472 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1473 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001474 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001475 if (!IS_ERR_OR_NULL(rtl_fw))
1476 strlcpy(info->fw_version, rtl_fw->version,
1477 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
1480static int rtl8169_get_regs_len(struct net_device *dev)
1481{
1482 return R8169_REGS_SIZE;
1483}
1484
1485static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001486 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
1488 struct rtl8169_private *tp = netdev_priv(dev);
1489 void __iomem *ioaddr = tp->mmio_addr;
1490 int ret = 0;
1491 u32 reg;
1492
1493 reg = RTL_R32(TBICSR);
1494 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1495 (duplex == DUPLEX_FULL)) {
1496 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1497 } else if (autoneg == AUTONEG_ENABLE)
1498 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1499 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001500 netif_warn(tp, link, dev,
1501 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 ret = -EOPNOTSUPP;
1503 }
1504
1505 return ret;
1506}
1507
1508static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001509 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
1511 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001512 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001513 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Hayes Wang716b50a2011-02-22 17:26:18 +08001515 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001518 int auto_nego;
1519
françois romieu4da19632011-01-03 15:07:55 +00001520 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001521 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1522 ADVERTISE_100HALF | ADVERTISE_100FULL);
1523
1524 if (adv & ADVERTISED_10baseT_Half)
1525 auto_nego |= ADVERTISE_10HALF;
1526 if (adv & ADVERTISED_10baseT_Full)
1527 auto_nego |= ADVERTISE_10FULL;
1528 if (adv & ADVERTISED_100baseT_Half)
1529 auto_nego |= ADVERTISE_100HALF;
1530 if (adv & ADVERTISED_100baseT_Full)
1531 auto_nego |= ADVERTISE_100FULL;
1532
françois romieu3577aa12009-05-19 10:46:48 +00001533 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1534
françois romieu4da19632011-01-03 15:07:55 +00001535 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001536 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1537
1538 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001539 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001540 if (adv & ADVERTISED_1000baseT_Half)
1541 giga_ctrl |= ADVERTISE_1000HALF;
1542 if (adv & ADVERTISED_1000baseT_Full)
1543 giga_ctrl |= ADVERTISE_1000FULL;
1544 } else if (adv & (ADVERTISED_1000baseT_Half |
1545 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001546 netif_info(tp, link, dev,
1547 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001548 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
françois romieu3577aa12009-05-19 10:46:48 +00001551 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001552
françois romieu4da19632011-01-03 15:07:55 +00001553 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1554 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001555 } else {
1556 giga_ctrl = 0;
1557
1558 if (speed == SPEED_10)
1559 bmcr = 0;
1560 else if (speed == SPEED_100)
1561 bmcr = BMCR_SPEED100;
1562 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001563 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001564
1565 if (duplex == DUPLEX_FULL)
1566 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001567 }
1568
françois romieu4da19632011-01-03 15:07:55 +00001569 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001570
Francois Romieucecb5fd2011-04-01 10:21:07 +02001571 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1572 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001573 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001574 rtl_writephy(tp, 0x17, 0x2138);
1575 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001576 } else {
françois romieu4da19632011-01-03 15:07:55 +00001577 rtl_writephy(tp, 0x17, 0x2108);
1578 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001579 }
1580 }
1581
Oliver Neukum54405cd2011-01-06 21:55:13 +01001582 rc = 0;
1583out:
1584 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585}
1586
1587static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001588 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
1590 struct rtl8169_private *tp = netdev_priv(dev);
1591 int ret;
1592
Oliver Neukum54405cd2011-01-06 21:55:13 +01001593 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001594 if (ret < 0)
1595 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Francois Romieu4876cc12011-03-11 21:07:11 +01001597 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1598 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001600 }
1601out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return ret;
1603}
1604
1605static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1606{
1607 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 int ret;
1609
Francois Romieu4876cc12011-03-11 21:07:11 +01001610 del_timer_sync(&tp->timer);
1611
Francois Romieuda78dbf2012-01-26 14:18:23 +01001612 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001613 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001614 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001615 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return ret;
1618}
1619
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001620static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1621 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
Francois Romieud58d46b2011-05-03 16:38:29 +02001623 struct rtl8169_private *tp = netdev_priv(dev);
1624
Francois Romieu2b7b4312011-04-18 22:53:24 -07001625 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001626 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Francois Romieud58d46b2011-05-03 16:38:29 +02001628 if (dev->mtu > JUMBO_1K &&
1629 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1630 features &= ~NETIF_F_IP_CSUM;
1631
Michał Mirosław350fb322011-04-08 06:35:56 +00001632 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
1634
Francois Romieuda78dbf2012-01-26 14:18:23 +01001635static void __rtl8169_set_features(struct net_device *dev,
1636 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
1638 struct rtl8169_private *tp = netdev_priv(dev);
Ben Greear6bbe0212012-02-10 15:04:33 +00001639 netdev_features_t changed = features ^ dev->features;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001640 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Ben Greear6bbe0212012-02-10 15:04:33 +00001642 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)))
1643 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Ben Greear6bbe0212012-02-10 15:04:33 +00001645 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)) {
1646 if (features & NETIF_F_RXCSUM)
1647 tp->cp_cmd |= RxChkSum;
1648 else
1649 tp->cp_cmd &= ~RxChkSum;
Michał Mirosław350fb322011-04-08 06:35:56 +00001650
Ben Greear6bbe0212012-02-10 15:04:33 +00001651 if (dev->features & NETIF_F_HW_VLAN_RX)
1652 tp->cp_cmd |= RxVlan;
1653 else
1654 tp->cp_cmd &= ~RxVlan;
1655
1656 RTL_W16(CPlusCmd, tp->cp_cmd);
1657 RTL_R16(CPlusCmd);
1658 }
1659 if (changed & NETIF_F_RXALL) {
1660 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1661 if (features & NETIF_F_RXALL)
1662 tmp |= (AcceptErr | AcceptRunt);
1663 RTL_W32(RxConfig, tmp);
1664 }
Francois Romieuda78dbf2012-01-26 14:18:23 +01001665}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Francois Romieuda78dbf2012-01-26 14:18:23 +01001667static int rtl8169_set_features(struct net_device *dev,
1668 netdev_features_t features)
1669{
1670 struct rtl8169_private *tp = netdev_priv(dev);
1671
1672 rtl_lock_work(tp);
1673 __rtl8169_set_features(dev, features);
1674 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 return 0;
1677}
1678
Francois Romieuda78dbf2012-01-26 14:18:23 +01001679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1681 struct sk_buff *skb)
1682{
Jesse Grosseab6d182010-10-20 13:56:03 +00001683 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1685}
1686
Francois Romieu7a8fc772011-03-01 17:18:33 +01001687static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
1689 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Francois Romieu7a8fc772011-03-01 17:18:33 +01001691 if (opts2 & RxVlanTag)
1692 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695}
1696
Francois Romieuccdffb92008-07-26 14:26:06 +02001697static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
1699 struct rtl8169_private *tp = netdev_priv(dev);
1700 void __iomem *ioaddr = tp->mmio_addr;
1701 u32 status;
1702
1703 cmd->supported =
1704 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1705 cmd->port = PORT_FIBRE;
1706 cmd->transceiver = XCVR_INTERNAL;
1707
1708 status = RTL_R32(TBICSR);
1709 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1710 cmd->autoneg = !!(status & TBINwEnable);
1711
David Decotigny70739492011-04-27 18:32:40 +00001712 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001714
1715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
1717
Francois Romieuccdffb92008-07-26 14:26:06 +02001718static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
1720 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Francois Romieuccdffb92008-07-26 14:26:06 +02001722 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
1724
1725static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1726{
1727 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001728 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Francois Romieuda78dbf2012-01-26 14:18:23 +01001730 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02001731 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001732 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Francois Romieuccdffb92008-07-26 14:26:06 +02001734 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735}
1736
1737static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1738 void *p)
1739{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001740 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Francois Romieu5b0384f2006-08-16 16:00:01 +02001742 if (regs->len > R8169_REGS_SIZE)
1743 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Francois Romieuda78dbf2012-01-26 14:18:23 +01001745 rtl_lock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001746 memcpy_fromio(p, tp->mmio_addr, regs->len);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001747 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748}
1749
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001750static u32 rtl8169_get_msglevel(struct net_device *dev)
1751{
1752 struct rtl8169_private *tp = netdev_priv(dev);
1753
1754 return tp->msg_enable;
1755}
1756
1757static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1758{
1759 struct rtl8169_private *tp = netdev_priv(dev);
1760
1761 tp->msg_enable = value;
1762}
1763
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001764static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1765 "tx_packets",
1766 "rx_packets",
1767 "tx_errors",
1768 "rx_errors",
1769 "rx_missed",
1770 "align_errors",
1771 "tx_single_collisions",
1772 "tx_multi_collisions",
1773 "unicast",
1774 "broadcast",
1775 "multicast",
1776 "tx_aborted",
1777 "tx_underrun",
1778};
1779
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001780static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001781{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001782 switch (sset) {
1783 case ETH_SS_STATS:
1784 return ARRAY_SIZE(rtl8169_gstrings);
1785 default:
1786 return -EOPNOTSUPP;
1787 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001788}
1789
Ivan Vecera355423d2009-02-06 21:49:57 -08001790static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001791{
1792 struct rtl8169_private *tp = netdev_priv(dev);
1793 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001794 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001795 struct rtl8169_counters *counters;
1796 dma_addr_t paddr;
1797 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001798 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001799
Ivan Vecera355423d2009-02-06 21:49:57 -08001800 /*
1801 * Some chips are unable to dump tally counters when the receiver
1802 * is disabled.
1803 */
1804 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1805 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001806
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001807 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001808 if (!counters)
1809 return;
1810
1811 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001812 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001813 RTL_W32(CounterAddrLow, cmd);
1814 RTL_W32(CounterAddrLow, cmd | CounterDump);
1815
Ivan Vecera355423d2009-02-06 21:49:57 -08001816 while (wait--) {
1817 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001818 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001819 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001820 }
1821 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001822 }
1823
1824 RTL_W32(CounterAddrLow, 0);
1825 RTL_W32(CounterAddrHigh, 0);
1826
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001827 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001828}
1829
Ivan Vecera355423d2009-02-06 21:49:57 -08001830static void rtl8169_get_ethtool_stats(struct net_device *dev,
1831 struct ethtool_stats *stats, u64 *data)
1832{
1833 struct rtl8169_private *tp = netdev_priv(dev);
1834
1835 ASSERT_RTNL();
1836
1837 rtl8169_update_counters(dev);
1838
1839 data[0] = le64_to_cpu(tp->counters.tx_packets);
1840 data[1] = le64_to_cpu(tp->counters.rx_packets);
1841 data[2] = le64_to_cpu(tp->counters.tx_errors);
1842 data[3] = le32_to_cpu(tp->counters.rx_errors);
1843 data[4] = le16_to_cpu(tp->counters.rx_missed);
1844 data[5] = le16_to_cpu(tp->counters.align_errors);
1845 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1846 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1847 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1848 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1849 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1850 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1851 data[12] = le16_to_cpu(tp->counters.tx_underun);
1852}
1853
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001854static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1855{
1856 switch(stringset) {
1857 case ETH_SS_STATS:
1858 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1859 break;
1860 }
1861}
1862
Jeff Garzik7282d492006-09-13 14:30:00 -04001863static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 .get_drvinfo = rtl8169_get_drvinfo,
1865 .get_regs_len = rtl8169_get_regs_len,
1866 .get_link = ethtool_op_get_link,
1867 .get_settings = rtl8169_get_settings,
1868 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001869 .get_msglevel = rtl8169_get_msglevel,
1870 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001872 .get_wol = rtl8169_get_wol,
1873 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001874 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001875 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001876 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877};
1878
Francois Romieu07d3f512007-02-21 22:40:46 +01001879static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001880 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
Francois Romieu5d320a22011-05-08 17:47:36 +02001882 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001883 /*
1884 * The driver currently handles the 8168Bf and the 8168Be identically
1885 * but they can be identified more specifically through the test below
1886 * if needed:
1887 *
1888 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001889 *
1890 * Same thing for the 8101Eb and the 8101Ec:
1891 *
1892 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001893 */
Francois Romieu37441002011-06-17 22:58:54 +02001894 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001896 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 int mac_version;
1898 } mac_info[] = {
Hayes Wangc2218922011-09-06 16:55:18 +08001899 /* 8168F family. */
1900 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1901 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1902
hayeswang01dc7fe2011-03-21 01:50:28 +00001903 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001904 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001905 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1906 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1907 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1908
Francois Romieu5b538df2008-07-20 16:22:45 +02001909 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001910 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1911 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001912 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001913
françois romieue6de30d2011-01-03 15:08:37 +00001914 /* 8168DP family. */
1915 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1916 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001917 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001918
Francois Romieuef808d52008-06-29 13:10:54 +02001919 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001920 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001921 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001922 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001923 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001924 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1925 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001926 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001927 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001928 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001929
1930 /* 8168B family. */
1931 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1932 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1933 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1934 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1935
1936 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001937 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001938 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1939 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1940 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001941 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1942 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1943 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1944 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1945 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1946 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001947 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001948 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001949 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001950 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1951 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001952 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1953 /* FIXME: where did these entries come from ? -- FR */
1954 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1955 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1956
1957 /* 8110 family. */
1958 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1959 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1960 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1961 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1962 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1963 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1964
Jean Delvaref21b75e2009-05-26 20:54:48 -07001965 /* Catch-all */
1966 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001967 };
1968 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 u32 reg;
1970
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001971 reg = RTL_R32(TxConfig);
1972 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 p++;
1974 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001975
1976 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1977 netif_notice(tp, probe, dev,
1978 "unknown MAC, using family default\n");
1979 tp->mac_version = default_version;
1980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
1983static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1984{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001985 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986}
1987
Francois Romieu867763c2007-08-17 18:21:58 +02001988struct phy_reg {
1989 u16 reg;
1990 u16 val;
1991};
1992
françois romieu4da19632011-01-03 15:07:55 +00001993static void rtl_writephy_batch(struct rtl8169_private *tp,
1994 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001995{
1996 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001997 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001998 regs++;
1999 }
2000}
2001
françois romieubca03d52011-01-03 15:07:31 +00002002#define PHY_READ 0x00000000
2003#define PHY_DATA_OR 0x10000000
2004#define PHY_DATA_AND 0x20000000
2005#define PHY_BJMPN 0x30000000
2006#define PHY_READ_EFUSE 0x40000000
2007#define PHY_READ_MAC_BYTE 0x50000000
2008#define PHY_WRITE_MAC_BYTE 0x60000000
2009#define PHY_CLEAR_READCOUNT 0x70000000
2010#define PHY_WRITE 0x80000000
2011#define PHY_READCOUNT_EQ_SKIP 0x90000000
2012#define PHY_COMP_EQ_SKIPN 0xa0000000
2013#define PHY_COMP_NEQ_SKIPN 0xb0000000
2014#define PHY_WRITE_PREVIOUS 0xc0000000
2015#define PHY_SKIPN 0xd0000000
2016#define PHY_DELAY_MS 0xe0000000
2017#define PHY_WRITE_ERI_WORD 0xf0000000
2018
Hayes Wang960aee62011-06-18 11:37:48 +02002019struct fw_info {
2020 u32 magic;
2021 char version[RTL_VER_SIZE];
2022 __le32 fw_start;
2023 __le32 fw_len;
2024 u8 chksum;
2025} __packed;
2026
Francois Romieu1c361ef2011-06-17 17:16:24 +02002027#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2028
2029static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002030{
Francois Romieub6ffd972011-06-17 17:00:05 +02002031 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002032 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002033 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2034 char *version = rtl_fw->version;
2035 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002036
Francois Romieu1c361ef2011-06-17 17:16:24 +02002037 if (fw->size < FW_OPCODE_SIZE)
2038 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002039
2040 if (!fw_info->magic) {
2041 size_t i, size, start;
2042 u8 checksum = 0;
2043
2044 if (fw->size < sizeof(*fw_info))
2045 goto out;
2046
2047 for (i = 0; i < fw->size; i++)
2048 checksum += fw->data[i];
2049 if (checksum != 0)
2050 goto out;
2051
2052 start = le32_to_cpu(fw_info->fw_start);
2053 if (start > fw->size)
2054 goto out;
2055
2056 size = le32_to_cpu(fw_info->fw_len);
2057 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2058 goto out;
2059
2060 memcpy(version, fw_info->version, RTL_VER_SIZE);
2061
2062 pa->code = (__le32 *)(fw->data + start);
2063 pa->size = size;
2064 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002065 if (fw->size % FW_OPCODE_SIZE)
2066 goto out;
2067
2068 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2069
2070 pa->code = (__le32 *)fw->data;
2071 pa->size = fw->size / FW_OPCODE_SIZE;
2072 }
2073 version[RTL_VER_SIZE - 1] = 0;
2074
2075 rc = true;
2076out:
2077 return rc;
2078}
2079
Francois Romieufd112f22011-06-18 00:10:29 +02002080static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2081 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002082{
Francois Romieufd112f22011-06-18 00:10:29 +02002083 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002084 size_t index;
2085
Francois Romieu1c361ef2011-06-17 17:16:24 +02002086 for (index = 0; index < pa->size; index++) {
2087 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002088 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002089
hayeswang42b82dc2011-01-10 02:07:25 +00002090 switch(action & 0xf0000000) {
2091 case PHY_READ:
2092 case PHY_DATA_OR:
2093 case PHY_DATA_AND:
2094 case PHY_READ_EFUSE:
2095 case PHY_CLEAR_READCOUNT:
2096 case PHY_WRITE:
2097 case PHY_WRITE_PREVIOUS:
2098 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002099 break;
2100
hayeswang42b82dc2011-01-10 02:07:25 +00002101 case PHY_BJMPN:
2102 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002103 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002104 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002105 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002106 }
2107 break;
2108 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002109 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002110 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002111 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002112 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002113 }
2114 break;
2115 case PHY_COMP_EQ_SKIPN:
2116 case PHY_COMP_NEQ_SKIPN:
2117 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002118 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002119 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002120 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002121 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002122 }
2123 break;
2124
2125 case PHY_READ_MAC_BYTE:
2126 case PHY_WRITE_MAC_BYTE:
2127 case PHY_WRITE_ERI_WORD:
2128 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002129 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002130 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002131 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002132 }
2133 }
Francois Romieufd112f22011-06-18 00:10:29 +02002134 rc = true;
2135out:
2136 return rc;
2137}
françois romieubca03d52011-01-03 15:07:31 +00002138
Francois Romieufd112f22011-06-18 00:10:29 +02002139static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2140{
2141 struct net_device *dev = tp->dev;
2142 int rc = -EINVAL;
2143
2144 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2145 netif_err(tp, ifup, dev, "invalid firwmare\n");
2146 goto out;
2147 }
2148
2149 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2150 rc = 0;
2151out:
2152 return rc;
2153}
2154
2155static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2156{
2157 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2158 u32 predata, count;
2159 size_t index;
2160
2161 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002162
Francois Romieu1c361ef2011-06-17 17:16:24 +02002163 for (index = 0; index < pa->size; ) {
2164 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002165 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002166 u32 regno = (action & 0x0fff0000) >> 16;
2167
2168 if (!action)
2169 break;
françois romieubca03d52011-01-03 15:07:31 +00002170
2171 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002172 case PHY_READ:
2173 predata = rtl_readphy(tp, regno);
2174 count++;
2175 index++;
françois romieubca03d52011-01-03 15:07:31 +00002176 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002177 case PHY_DATA_OR:
2178 predata |= data;
2179 index++;
2180 break;
2181 case PHY_DATA_AND:
2182 predata &= data;
2183 index++;
2184 break;
2185 case PHY_BJMPN:
2186 index -= regno;
2187 break;
2188 case PHY_READ_EFUSE:
2189 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2190 index++;
2191 break;
2192 case PHY_CLEAR_READCOUNT:
2193 count = 0;
2194 index++;
2195 break;
2196 case PHY_WRITE:
2197 rtl_writephy(tp, regno, data);
2198 index++;
2199 break;
2200 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002201 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002202 break;
2203 case PHY_COMP_EQ_SKIPN:
2204 if (predata == data)
2205 index += regno;
2206 index++;
2207 break;
2208 case PHY_COMP_NEQ_SKIPN:
2209 if (predata != data)
2210 index += regno;
2211 index++;
2212 break;
2213 case PHY_WRITE_PREVIOUS:
2214 rtl_writephy(tp, regno, predata);
2215 index++;
2216 break;
2217 case PHY_SKIPN:
2218 index += regno + 1;
2219 break;
2220 case PHY_DELAY_MS:
2221 mdelay(data);
2222 index++;
2223 break;
2224
2225 case PHY_READ_MAC_BYTE:
2226 case PHY_WRITE_MAC_BYTE:
2227 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002228 default:
2229 BUG();
2230 }
2231 }
2232}
2233
françois romieuf1e02ed2011-01-13 13:07:53 +00002234static void rtl_release_firmware(struct rtl8169_private *tp)
2235{
Francois Romieub6ffd972011-06-17 17:00:05 +02002236 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2237 release_firmware(tp->rtl_fw->fw);
2238 kfree(tp->rtl_fw);
2239 }
2240 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002241}
2242
François Romieu953a12c2011-04-24 17:38:48 +02002243static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002244{
Francois Romieub6ffd972011-06-17 17:00:05 +02002245 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002246
2247 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002248 if (!IS_ERR_OR_NULL(rtl_fw))
2249 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002250}
2251
2252static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2253{
2254 if (rtl_readphy(tp, reg) != val)
2255 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2256 else
2257 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002258}
2259
françois romieu4da19632011-01-03 15:07:55 +00002260static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002262 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002263 { 0x1f, 0x0001 },
2264 { 0x06, 0x006e },
2265 { 0x08, 0x0708 },
2266 { 0x15, 0x4000 },
2267 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
françois romieu0b9b5712009-08-10 19:44:56 +00002269 { 0x1f, 0x0001 },
2270 { 0x03, 0x00a1 },
2271 { 0x02, 0x0008 },
2272 { 0x01, 0x0120 },
2273 { 0x00, 0x1000 },
2274 { 0x04, 0x0800 },
2275 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
françois romieu0b9b5712009-08-10 19:44:56 +00002277 { 0x03, 0xff41 },
2278 { 0x02, 0xdf60 },
2279 { 0x01, 0x0140 },
2280 { 0x00, 0x0077 },
2281 { 0x04, 0x7800 },
2282 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
françois romieu0b9b5712009-08-10 19:44:56 +00002284 { 0x03, 0x802f },
2285 { 0x02, 0x4f02 },
2286 { 0x01, 0x0409 },
2287 { 0x00, 0xf0f9 },
2288 { 0x04, 0x9800 },
2289 { 0x04, 0x9000 },
2290
2291 { 0x03, 0xdf01 },
2292 { 0x02, 0xdf20 },
2293 { 0x01, 0xff95 },
2294 { 0x00, 0xba00 },
2295 { 0x04, 0xa800 },
2296 { 0x04, 0xa000 },
2297
2298 { 0x03, 0xff41 },
2299 { 0x02, 0xdf20 },
2300 { 0x01, 0x0140 },
2301 { 0x00, 0x00bb },
2302 { 0x04, 0xb800 },
2303 { 0x04, 0xb000 },
2304
2305 { 0x03, 0xdf41 },
2306 { 0x02, 0xdc60 },
2307 { 0x01, 0x6340 },
2308 { 0x00, 0x007d },
2309 { 0x04, 0xd800 },
2310 { 0x04, 0xd000 },
2311
2312 { 0x03, 0xdf01 },
2313 { 0x02, 0xdf20 },
2314 { 0x01, 0x100a },
2315 { 0x00, 0xa0ff },
2316 { 0x04, 0xf800 },
2317 { 0x04, 0xf000 },
2318
2319 { 0x1f, 0x0000 },
2320 { 0x0b, 0x0000 },
2321 { 0x00, 0x9200 }
2322 };
2323
françois romieu4da19632011-01-03 15:07:55 +00002324 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}
2326
françois romieu4da19632011-01-03 15:07:55 +00002327static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002328{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002329 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002330 { 0x1f, 0x0002 },
2331 { 0x01, 0x90d0 },
2332 { 0x1f, 0x0000 }
2333 };
2334
françois romieu4da19632011-01-03 15:07:55 +00002335 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002336}
2337
françois romieu4da19632011-01-03 15:07:55 +00002338static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002339{
2340 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002341
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002342 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2343 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002344 return;
2345
françois romieu4da19632011-01-03 15:07:55 +00002346 rtl_writephy(tp, 0x1f, 0x0001);
2347 rtl_writephy(tp, 0x10, 0xf01b);
2348 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002349}
2350
françois romieu4da19632011-01-03 15:07:55 +00002351static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002352{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002353 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002354 { 0x1f, 0x0001 },
2355 { 0x04, 0x0000 },
2356 { 0x03, 0x00a1 },
2357 { 0x02, 0x0008 },
2358 { 0x01, 0x0120 },
2359 { 0x00, 0x1000 },
2360 { 0x04, 0x0800 },
2361 { 0x04, 0x9000 },
2362 { 0x03, 0x802f },
2363 { 0x02, 0x4f02 },
2364 { 0x01, 0x0409 },
2365 { 0x00, 0xf099 },
2366 { 0x04, 0x9800 },
2367 { 0x04, 0xa000 },
2368 { 0x03, 0xdf01 },
2369 { 0x02, 0xdf20 },
2370 { 0x01, 0xff95 },
2371 { 0x00, 0xba00 },
2372 { 0x04, 0xa800 },
2373 { 0x04, 0xf000 },
2374 { 0x03, 0xdf01 },
2375 { 0x02, 0xdf20 },
2376 { 0x01, 0x101a },
2377 { 0x00, 0xa0ff },
2378 { 0x04, 0xf800 },
2379 { 0x04, 0x0000 },
2380 { 0x1f, 0x0000 },
2381
2382 { 0x1f, 0x0001 },
2383 { 0x10, 0xf41b },
2384 { 0x14, 0xfb54 },
2385 { 0x18, 0xf5c7 },
2386 { 0x1f, 0x0000 },
2387
2388 { 0x1f, 0x0001 },
2389 { 0x17, 0x0cc0 },
2390 { 0x1f, 0x0000 }
2391 };
2392
françois romieu4da19632011-01-03 15:07:55 +00002393 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002394
françois romieu4da19632011-01-03 15:07:55 +00002395 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002396}
2397
françois romieu4da19632011-01-03 15:07:55 +00002398static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002399{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002400 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002401 { 0x1f, 0x0001 },
2402 { 0x04, 0x0000 },
2403 { 0x03, 0x00a1 },
2404 { 0x02, 0x0008 },
2405 { 0x01, 0x0120 },
2406 { 0x00, 0x1000 },
2407 { 0x04, 0x0800 },
2408 { 0x04, 0x9000 },
2409 { 0x03, 0x802f },
2410 { 0x02, 0x4f02 },
2411 { 0x01, 0x0409 },
2412 { 0x00, 0xf099 },
2413 { 0x04, 0x9800 },
2414 { 0x04, 0xa000 },
2415 { 0x03, 0xdf01 },
2416 { 0x02, 0xdf20 },
2417 { 0x01, 0xff95 },
2418 { 0x00, 0xba00 },
2419 { 0x04, 0xa800 },
2420 { 0x04, 0xf000 },
2421 { 0x03, 0xdf01 },
2422 { 0x02, 0xdf20 },
2423 { 0x01, 0x101a },
2424 { 0x00, 0xa0ff },
2425 { 0x04, 0xf800 },
2426 { 0x04, 0x0000 },
2427 { 0x1f, 0x0000 },
2428
2429 { 0x1f, 0x0001 },
2430 { 0x0b, 0x8480 },
2431 { 0x1f, 0x0000 },
2432
2433 { 0x1f, 0x0001 },
2434 { 0x18, 0x67c7 },
2435 { 0x04, 0x2000 },
2436 { 0x03, 0x002f },
2437 { 0x02, 0x4360 },
2438 { 0x01, 0x0109 },
2439 { 0x00, 0x3022 },
2440 { 0x04, 0x2800 },
2441 { 0x1f, 0x0000 },
2442
2443 { 0x1f, 0x0001 },
2444 { 0x17, 0x0cc0 },
2445 { 0x1f, 0x0000 }
2446 };
2447
françois romieu4da19632011-01-03 15:07:55 +00002448 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002449}
2450
françois romieu4da19632011-01-03 15:07:55 +00002451static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002452{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002453 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002454 { 0x10, 0xf41b },
2455 { 0x1f, 0x0000 }
2456 };
2457
françois romieu4da19632011-01-03 15:07:55 +00002458 rtl_writephy(tp, 0x1f, 0x0001);
2459 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002460
françois romieu4da19632011-01-03 15:07:55 +00002461 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002462}
2463
françois romieu4da19632011-01-03 15:07:55 +00002464static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002465{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002466 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002467 { 0x1f, 0x0001 },
2468 { 0x10, 0xf41b },
2469 { 0x1f, 0x0000 }
2470 };
2471
françois romieu4da19632011-01-03 15:07:55 +00002472 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002473}
2474
françois romieu4da19632011-01-03 15:07:55 +00002475static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002476{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002477 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002478 { 0x1f, 0x0000 },
2479 { 0x1d, 0x0f00 },
2480 { 0x1f, 0x0002 },
2481 { 0x0c, 0x1ec8 },
2482 { 0x1f, 0x0000 }
2483 };
2484
françois romieu4da19632011-01-03 15:07:55 +00002485 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002486}
2487
françois romieu4da19632011-01-03 15:07:55 +00002488static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002489{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002490 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002491 { 0x1f, 0x0001 },
2492 { 0x1d, 0x3d98 },
2493 { 0x1f, 0x0000 }
2494 };
2495
françois romieu4da19632011-01-03 15:07:55 +00002496 rtl_writephy(tp, 0x1f, 0x0000);
2497 rtl_patchphy(tp, 0x14, 1 << 5);
2498 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002499
françois romieu4da19632011-01-03 15:07:55 +00002500 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002501}
2502
françois romieu4da19632011-01-03 15:07:55 +00002503static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002504{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002505 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002506 { 0x1f, 0x0001 },
2507 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002508 { 0x1f, 0x0002 },
2509 { 0x00, 0x88d4 },
2510 { 0x01, 0x82b1 },
2511 { 0x03, 0x7002 },
2512 { 0x08, 0x9e30 },
2513 { 0x09, 0x01f0 },
2514 { 0x0a, 0x5500 },
2515 { 0x0c, 0x00c8 },
2516 { 0x1f, 0x0003 },
2517 { 0x12, 0xc096 },
2518 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002519 { 0x1f, 0x0000 },
2520 { 0x1f, 0x0000 },
2521 { 0x09, 0x2000 },
2522 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002523 };
2524
françois romieu4da19632011-01-03 15:07:55 +00002525 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002526
françois romieu4da19632011-01-03 15:07:55 +00002527 rtl_patchphy(tp, 0x14, 1 << 5);
2528 rtl_patchphy(tp, 0x0d, 1 << 5);
2529 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002530}
2531
françois romieu4da19632011-01-03 15:07:55 +00002532static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002533{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002534 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002535 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002536 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002537 { 0x03, 0x802f },
2538 { 0x02, 0x4f02 },
2539 { 0x01, 0x0409 },
2540 { 0x00, 0xf099 },
2541 { 0x04, 0x9800 },
2542 { 0x04, 0x9000 },
2543 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002544 { 0x1f, 0x0002 },
2545 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002546 { 0x06, 0x0761 },
2547 { 0x1f, 0x0003 },
2548 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002549 { 0x1f, 0x0000 }
2550 };
2551
françois romieu4da19632011-01-03 15:07:55 +00002552 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002553
françois romieu4da19632011-01-03 15:07:55 +00002554 rtl_patchphy(tp, 0x16, 1 << 0);
2555 rtl_patchphy(tp, 0x14, 1 << 5);
2556 rtl_patchphy(tp, 0x0d, 1 << 5);
2557 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002558}
2559
françois romieu4da19632011-01-03 15:07:55 +00002560static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002561{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002562 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002563 { 0x1f, 0x0001 },
2564 { 0x12, 0x2300 },
2565 { 0x1d, 0x3d98 },
2566 { 0x1f, 0x0002 },
2567 { 0x0c, 0x7eb8 },
2568 { 0x06, 0x5461 },
2569 { 0x1f, 0x0003 },
2570 { 0x16, 0x0f0a },
2571 { 0x1f, 0x0000 }
2572 };
2573
françois romieu4da19632011-01-03 15:07:55 +00002574 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002575
françois romieu4da19632011-01-03 15:07:55 +00002576 rtl_patchphy(tp, 0x16, 1 << 0);
2577 rtl_patchphy(tp, 0x14, 1 << 5);
2578 rtl_patchphy(tp, 0x0d, 1 << 5);
2579 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002580}
2581
françois romieu4da19632011-01-03 15:07:55 +00002582static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002583{
françois romieu4da19632011-01-03 15:07:55 +00002584 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002585}
2586
françois romieubca03d52011-01-03 15:07:31 +00002587static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002588{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002589 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002590 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002591 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002592 { 0x06, 0x4064 },
2593 { 0x07, 0x2863 },
2594 { 0x08, 0x059c },
2595 { 0x09, 0x26b4 },
2596 { 0x0a, 0x6a19 },
2597 { 0x0b, 0xdcc8 },
2598 { 0x10, 0xf06d },
2599 { 0x14, 0x7f68 },
2600 { 0x18, 0x7fd9 },
2601 { 0x1c, 0xf0ff },
2602 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002603 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002604 { 0x12, 0xf49f },
2605 { 0x13, 0x070b },
2606 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002607 { 0x14, 0x94c0 },
2608
2609 /*
2610 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002611 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002612 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002613 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002614 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002615 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002616 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002617 { 0x06, 0x5561 },
2618
2619 /*
2620 * Can not link to 1Gbps with bad cable
2621 * Decrease SNR threshold form 21.07dB to 19.04dB
2622 */
2623 { 0x1f, 0x0001 },
2624 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002625
2626 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002627 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002628 };
françois romieubca03d52011-01-03 15:07:31 +00002629 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002630
françois romieu4da19632011-01-03 15:07:55 +00002631 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002632
françois romieubca03d52011-01-03 15:07:31 +00002633 /*
2634 * Rx Error Issue
2635 * Fine Tune Switching regulator parameter
2636 */
françois romieu4da19632011-01-03 15:07:55 +00002637 rtl_writephy(tp, 0x1f, 0x0002);
2638 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2639 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002640
françois romieudaf9df62009-10-07 12:44:20 +00002641 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002642 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002643 { 0x1f, 0x0002 },
2644 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002645 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002646 { 0x05, 0x8330 },
2647 { 0x06, 0x669a },
2648 { 0x1f, 0x0002 }
2649 };
2650 int val;
2651
françois romieu4da19632011-01-03 15:07:55 +00002652 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002653
françois romieu4da19632011-01-03 15:07:55 +00002654 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002655
2656 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002657 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002658 0x0065, 0x0066, 0x0067, 0x0068,
2659 0x0069, 0x006a, 0x006b, 0x006c
2660 };
2661 int i;
2662
françois romieu4da19632011-01-03 15:07:55 +00002663 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002664
2665 val &= 0xff00;
2666 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002667 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002668 }
2669 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002670 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002671 { 0x1f, 0x0002 },
2672 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002673 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002674 { 0x05, 0x8330 },
2675 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002676 };
2677
françois romieu4da19632011-01-03 15:07:55 +00002678 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002679 }
2680
françois romieubca03d52011-01-03 15:07:31 +00002681 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002682 rtl_writephy(tp, 0x1f, 0x0002);
2683 rtl_patchphy(tp, 0x0d, 0x0300);
2684 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002685
françois romieubca03d52011-01-03 15:07:31 +00002686 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002687 rtl_writephy(tp, 0x1f, 0x0002);
2688 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2689 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002690
françois romieu4da19632011-01-03 15:07:55 +00002691 rtl_writephy(tp, 0x1f, 0x0005);
2692 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002693
2694 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002695
françois romieu4da19632011-01-03 15:07:55 +00002696 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002697}
2698
françois romieubca03d52011-01-03 15:07:31 +00002699static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002700{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002701 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002702 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002703 { 0x1f, 0x0001 },
2704 { 0x06, 0x4064 },
2705 { 0x07, 0x2863 },
2706 { 0x08, 0x059c },
2707 { 0x09, 0x26b4 },
2708 { 0x0a, 0x6a19 },
2709 { 0x0b, 0xdcc8 },
2710 { 0x10, 0xf06d },
2711 { 0x14, 0x7f68 },
2712 { 0x18, 0x7fd9 },
2713 { 0x1c, 0xf0ff },
2714 { 0x1d, 0x3d9c },
2715 { 0x1f, 0x0003 },
2716 { 0x12, 0xf49f },
2717 { 0x13, 0x070b },
2718 { 0x1a, 0x05ad },
2719 { 0x14, 0x94c0 },
2720
françois romieubca03d52011-01-03 15:07:31 +00002721 /*
2722 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002723 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002724 */
françois romieudaf9df62009-10-07 12:44:20 +00002725 { 0x1f, 0x0002 },
2726 { 0x06, 0x5561 },
2727 { 0x1f, 0x0005 },
2728 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002729 { 0x06, 0x5561 },
2730
2731 /*
2732 * Can not link to 1Gbps with bad cable
2733 * Decrease SNR threshold form 21.07dB to 19.04dB
2734 */
2735 { 0x1f, 0x0001 },
2736 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002737
2738 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002739 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002740 };
françois romieubca03d52011-01-03 15:07:31 +00002741 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002742
françois romieu4da19632011-01-03 15:07:55 +00002743 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002744
2745 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002746 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002747 { 0x1f, 0x0002 },
2748 { 0x05, 0x669a },
2749 { 0x1f, 0x0005 },
2750 { 0x05, 0x8330 },
2751 { 0x06, 0x669a },
2752
2753 { 0x1f, 0x0002 }
2754 };
2755 int val;
2756
françois romieu4da19632011-01-03 15:07:55 +00002757 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002758
françois romieu4da19632011-01-03 15:07:55 +00002759 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002760 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002761 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002762 0x0065, 0x0066, 0x0067, 0x0068,
2763 0x0069, 0x006a, 0x006b, 0x006c
2764 };
2765 int i;
2766
françois romieu4da19632011-01-03 15:07:55 +00002767 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002768
2769 val &= 0xff00;
2770 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002771 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002772 }
2773 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002774 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002775 { 0x1f, 0x0002 },
2776 { 0x05, 0x2642 },
2777 { 0x1f, 0x0005 },
2778 { 0x05, 0x8330 },
2779 { 0x06, 0x2642 }
2780 };
2781
françois romieu4da19632011-01-03 15:07:55 +00002782 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002783 }
2784
françois romieubca03d52011-01-03 15:07:31 +00002785 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002786 rtl_writephy(tp, 0x1f, 0x0002);
2787 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2788 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002789
françois romieubca03d52011-01-03 15:07:31 +00002790 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002791 rtl_writephy(tp, 0x1f, 0x0002);
2792 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002793
françois romieu4da19632011-01-03 15:07:55 +00002794 rtl_writephy(tp, 0x1f, 0x0005);
2795 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002796
2797 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002798
françois romieu4da19632011-01-03 15:07:55 +00002799 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002800}
2801
françois romieu4da19632011-01-03 15:07:55 +00002802static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002803{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002804 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002805 { 0x1f, 0x0002 },
2806 { 0x10, 0x0008 },
2807 { 0x0d, 0x006c },
2808
2809 { 0x1f, 0x0000 },
2810 { 0x0d, 0xf880 },
2811
2812 { 0x1f, 0x0001 },
2813 { 0x17, 0x0cc0 },
2814
2815 { 0x1f, 0x0001 },
2816 { 0x0b, 0xa4d8 },
2817 { 0x09, 0x281c },
2818 { 0x07, 0x2883 },
2819 { 0x0a, 0x6b35 },
2820 { 0x1d, 0x3da4 },
2821 { 0x1c, 0xeffd },
2822 { 0x14, 0x7f52 },
2823 { 0x18, 0x7fc6 },
2824 { 0x08, 0x0601 },
2825 { 0x06, 0x4063 },
2826 { 0x10, 0xf074 },
2827 { 0x1f, 0x0003 },
2828 { 0x13, 0x0789 },
2829 { 0x12, 0xf4bd },
2830 { 0x1a, 0x04fd },
2831 { 0x14, 0x84b0 },
2832 { 0x1f, 0x0000 },
2833 { 0x00, 0x9200 },
2834
2835 { 0x1f, 0x0005 },
2836 { 0x01, 0x0340 },
2837 { 0x1f, 0x0001 },
2838 { 0x04, 0x4000 },
2839 { 0x03, 0x1d21 },
2840 { 0x02, 0x0c32 },
2841 { 0x01, 0x0200 },
2842 { 0x00, 0x5554 },
2843 { 0x04, 0x4800 },
2844 { 0x04, 0x4000 },
2845 { 0x04, 0xf000 },
2846 { 0x03, 0xdf01 },
2847 { 0x02, 0xdf20 },
2848 { 0x01, 0x101a },
2849 { 0x00, 0xa0ff },
2850 { 0x04, 0xf800 },
2851 { 0x04, 0xf000 },
2852 { 0x1f, 0x0000 },
2853
2854 { 0x1f, 0x0007 },
2855 { 0x1e, 0x0023 },
2856 { 0x16, 0x0000 },
2857 { 0x1f, 0x0000 }
2858 };
2859
françois romieu4da19632011-01-03 15:07:55 +00002860 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002861}
2862
françois romieue6de30d2011-01-03 15:08:37 +00002863static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2864{
2865 static const struct phy_reg phy_reg_init[] = {
2866 { 0x1f, 0x0001 },
2867 { 0x17, 0x0cc0 },
2868
2869 { 0x1f, 0x0007 },
2870 { 0x1e, 0x002d },
2871 { 0x18, 0x0040 },
2872 { 0x1f, 0x0000 }
2873 };
2874
2875 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2876 rtl_patchphy(tp, 0x0d, 1 << 5);
2877}
2878
Hayes Wang70090422011-07-06 15:58:06 +08002879static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002880{
2881 static const struct phy_reg phy_reg_init[] = {
2882 /* Enable Delay cap */
2883 { 0x1f, 0x0005 },
2884 { 0x05, 0x8b80 },
2885 { 0x06, 0xc896 },
2886 { 0x1f, 0x0000 },
2887
2888 /* Channel estimation fine tune */
2889 { 0x1f, 0x0001 },
2890 { 0x0b, 0x6c20 },
2891 { 0x07, 0x2872 },
2892 { 0x1c, 0xefff },
2893 { 0x1f, 0x0003 },
2894 { 0x14, 0x6420 },
2895 { 0x1f, 0x0000 },
2896
2897 /* Update PFM & 10M TX idle timer */
2898 { 0x1f, 0x0007 },
2899 { 0x1e, 0x002f },
2900 { 0x15, 0x1919 },
2901 { 0x1f, 0x0000 },
2902
2903 { 0x1f, 0x0007 },
2904 { 0x1e, 0x00ac },
2905 { 0x18, 0x0006 },
2906 { 0x1f, 0x0000 }
2907 };
2908
Francois Romieu15ecd032011-04-27 13:52:22 -07002909 rtl_apply_firmware(tp);
2910
hayeswang01dc7fe2011-03-21 01:50:28 +00002911 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2912
2913 /* DCO enable for 10M IDLE Power */
2914 rtl_writephy(tp, 0x1f, 0x0007);
2915 rtl_writephy(tp, 0x1e, 0x0023);
2916 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2917 rtl_writephy(tp, 0x1f, 0x0000);
2918
2919 /* For impedance matching */
2920 rtl_writephy(tp, 0x1f, 0x0002);
2921 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002922 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002923
2924 /* PHY auto speed down */
2925 rtl_writephy(tp, 0x1f, 0x0007);
2926 rtl_writephy(tp, 0x1e, 0x002d);
2927 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2928 rtl_writephy(tp, 0x1f, 0x0000);
2929 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2930
2931 rtl_writephy(tp, 0x1f, 0x0005);
2932 rtl_writephy(tp, 0x05, 0x8b86);
2933 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2934 rtl_writephy(tp, 0x1f, 0x0000);
2935
2936 rtl_writephy(tp, 0x1f, 0x0005);
2937 rtl_writephy(tp, 0x05, 0x8b85);
2938 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2939 rtl_writephy(tp, 0x1f, 0x0007);
2940 rtl_writephy(tp, 0x1e, 0x0020);
2941 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2942 rtl_writephy(tp, 0x1f, 0x0006);
2943 rtl_writephy(tp, 0x00, 0x5a00);
2944 rtl_writephy(tp, 0x1f, 0x0000);
2945 rtl_writephy(tp, 0x0d, 0x0007);
2946 rtl_writephy(tp, 0x0e, 0x003c);
2947 rtl_writephy(tp, 0x0d, 0x4007);
2948 rtl_writephy(tp, 0x0e, 0x0000);
2949 rtl_writephy(tp, 0x0d, 0x0000);
2950}
2951
Hayes Wang70090422011-07-06 15:58:06 +08002952static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2953{
2954 static const struct phy_reg phy_reg_init[] = {
2955 /* Enable Delay cap */
2956 { 0x1f, 0x0004 },
2957 { 0x1f, 0x0007 },
2958 { 0x1e, 0x00ac },
2959 { 0x18, 0x0006 },
2960 { 0x1f, 0x0002 },
2961 { 0x1f, 0x0000 },
2962 { 0x1f, 0x0000 },
2963
2964 /* Channel estimation fine tune */
2965 { 0x1f, 0x0003 },
2966 { 0x09, 0xa20f },
2967 { 0x1f, 0x0000 },
2968 { 0x1f, 0x0000 },
2969
2970 /* Green Setting */
2971 { 0x1f, 0x0005 },
2972 { 0x05, 0x8b5b },
2973 { 0x06, 0x9222 },
2974 { 0x05, 0x8b6d },
2975 { 0x06, 0x8000 },
2976 { 0x05, 0x8b76 },
2977 { 0x06, 0x8000 },
2978 { 0x1f, 0x0000 }
2979 };
2980
2981 rtl_apply_firmware(tp);
2982
2983 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2984
2985 /* For 4-corner performance improve */
2986 rtl_writephy(tp, 0x1f, 0x0005);
2987 rtl_writephy(tp, 0x05, 0x8b80);
2988 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2989 rtl_writephy(tp, 0x1f, 0x0000);
2990
2991 /* PHY auto speed down */
2992 rtl_writephy(tp, 0x1f, 0x0004);
2993 rtl_writephy(tp, 0x1f, 0x0007);
2994 rtl_writephy(tp, 0x1e, 0x002d);
2995 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2996 rtl_writephy(tp, 0x1f, 0x0002);
2997 rtl_writephy(tp, 0x1f, 0x0000);
2998 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2999
3000 /* improve 10M EEE waveform */
3001 rtl_writephy(tp, 0x1f, 0x0005);
3002 rtl_writephy(tp, 0x05, 0x8b86);
3003 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3004 rtl_writephy(tp, 0x1f, 0x0000);
3005
3006 /* Improve 2-pair detection performance */
3007 rtl_writephy(tp, 0x1f, 0x0005);
3008 rtl_writephy(tp, 0x05, 0x8b85);
3009 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3010 rtl_writephy(tp, 0x1f, 0x0000);
3011
3012 /* EEE setting */
3013 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
3014 ERIAR_EXGMAC);
3015 rtl_writephy(tp, 0x1f, 0x0005);
3016 rtl_writephy(tp, 0x05, 0x8b85);
3017 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3018 rtl_writephy(tp, 0x1f, 0x0004);
3019 rtl_writephy(tp, 0x1f, 0x0007);
3020 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04003021 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003022 rtl_writephy(tp, 0x1f, 0x0002);
3023 rtl_writephy(tp, 0x1f, 0x0000);
3024 rtl_writephy(tp, 0x0d, 0x0007);
3025 rtl_writephy(tp, 0x0e, 0x003c);
3026 rtl_writephy(tp, 0x0d, 0x4007);
3027 rtl_writephy(tp, 0x0e, 0x0000);
3028 rtl_writephy(tp, 0x0d, 0x0000);
3029
3030 /* Green feature */
3031 rtl_writephy(tp, 0x1f, 0x0003);
3032 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3033 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3034 rtl_writephy(tp, 0x1f, 0x0000);
3035}
3036
Hayes Wangc2218922011-09-06 16:55:18 +08003037static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3038{
3039 static const struct phy_reg phy_reg_init[] = {
3040 /* Channel estimation fine tune */
3041 { 0x1f, 0x0003 },
3042 { 0x09, 0xa20f },
3043 { 0x1f, 0x0000 },
3044
3045 /* Modify green table for giga & fnet */
3046 { 0x1f, 0x0005 },
3047 { 0x05, 0x8b55 },
3048 { 0x06, 0x0000 },
3049 { 0x05, 0x8b5e },
3050 { 0x06, 0x0000 },
3051 { 0x05, 0x8b67 },
3052 { 0x06, 0x0000 },
3053 { 0x05, 0x8b70 },
3054 { 0x06, 0x0000 },
3055 { 0x1f, 0x0000 },
3056 { 0x1f, 0x0007 },
3057 { 0x1e, 0x0078 },
3058 { 0x17, 0x0000 },
3059 { 0x19, 0x00fb },
3060 { 0x1f, 0x0000 },
3061
3062 /* Modify green table for 10M */
3063 { 0x1f, 0x0005 },
3064 { 0x05, 0x8b79 },
3065 { 0x06, 0xaa00 },
3066 { 0x1f, 0x0000 },
3067
3068 /* Disable hiimpedance detection (RTCT) */
3069 { 0x1f, 0x0003 },
3070 { 0x01, 0x328a },
3071 { 0x1f, 0x0000 }
3072 };
3073
3074 rtl_apply_firmware(tp);
3075
3076 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3077
3078 /* For 4-corner performance improve */
3079 rtl_writephy(tp, 0x1f, 0x0005);
3080 rtl_writephy(tp, 0x05, 0x8b80);
3081 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3082 rtl_writephy(tp, 0x1f, 0x0000);
3083
3084 /* PHY auto speed down */
3085 rtl_writephy(tp, 0x1f, 0x0007);
3086 rtl_writephy(tp, 0x1e, 0x002d);
3087 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3088 rtl_writephy(tp, 0x1f, 0x0000);
3089 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3090
3091 /* Improve 10M EEE waveform */
3092 rtl_writephy(tp, 0x1f, 0x0005);
3093 rtl_writephy(tp, 0x05, 0x8b86);
3094 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3095 rtl_writephy(tp, 0x1f, 0x0000);
3096
3097 /* Improve 2-pair detection performance */
3098 rtl_writephy(tp, 0x1f, 0x0005);
3099 rtl_writephy(tp, 0x05, 0x8b85);
3100 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3101 rtl_writephy(tp, 0x1f, 0x0000);
3102}
3103
3104static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3105{
3106 rtl_apply_firmware(tp);
3107
3108 /* For 4-corner performance improve */
3109 rtl_writephy(tp, 0x1f, 0x0005);
3110 rtl_writephy(tp, 0x05, 0x8b80);
3111 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3112 rtl_writephy(tp, 0x1f, 0x0000);
3113
3114 /* PHY auto speed down */
3115 rtl_writephy(tp, 0x1f, 0x0007);
3116 rtl_writephy(tp, 0x1e, 0x002d);
3117 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3118 rtl_writephy(tp, 0x1f, 0x0000);
3119 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3120
3121 /* Improve 10M EEE waveform */
3122 rtl_writephy(tp, 0x1f, 0x0005);
3123 rtl_writephy(tp, 0x05, 0x8b86);
3124 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3125 rtl_writephy(tp, 0x1f, 0x0000);
3126}
3127
françois romieu4da19632011-01-03 15:07:55 +00003128static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003129{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003130 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003131 { 0x1f, 0x0003 },
3132 { 0x08, 0x441d },
3133 { 0x01, 0x9100 },
3134 { 0x1f, 0x0000 }
3135 };
3136
françois romieu4da19632011-01-03 15:07:55 +00003137 rtl_writephy(tp, 0x1f, 0x0000);
3138 rtl_patchphy(tp, 0x11, 1 << 12);
3139 rtl_patchphy(tp, 0x19, 1 << 13);
3140 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003141
françois romieu4da19632011-01-03 15:07:55 +00003142 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003143}
3144
Hayes Wang5a5e4442011-02-22 17:26:21 +08003145static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3146{
3147 static const struct phy_reg phy_reg_init[] = {
3148 { 0x1f, 0x0005 },
3149 { 0x1a, 0x0000 },
3150 { 0x1f, 0x0000 },
3151
3152 { 0x1f, 0x0004 },
3153 { 0x1c, 0x0000 },
3154 { 0x1f, 0x0000 },
3155
3156 { 0x1f, 0x0001 },
3157 { 0x15, 0x7701 },
3158 { 0x1f, 0x0000 }
3159 };
3160
3161 /* Disable ALDPS before ram code */
3162 rtl_writephy(tp, 0x1f, 0x0000);
3163 rtl_writephy(tp, 0x18, 0x0310);
3164 msleep(100);
3165
François Romieu953a12c2011-04-24 17:38:48 +02003166 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003167
3168 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3169}
3170
Francois Romieu5615d9f2007-08-17 17:50:46 +02003171static void rtl_hw_phy_config(struct net_device *dev)
3172{
3173 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003174
3175 rtl8169_print_mac_version(tp);
3176
3177 switch (tp->mac_version) {
3178 case RTL_GIGA_MAC_VER_01:
3179 break;
3180 case RTL_GIGA_MAC_VER_02:
3181 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003182 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003183 break;
3184 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003185 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003186 break;
françois romieu2e9558562009-08-10 19:44:19 +00003187 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003188 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003189 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003190 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003191 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003192 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003193 case RTL_GIGA_MAC_VER_07:
3194 case RTL_GIGA_MAC_VER_08:
3195 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003196 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003197 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003198 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003199 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003200 break;
3201 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003202 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003203 break;
3204 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003205 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003206 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003207 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003208 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003209 break;
3210 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003211 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003212 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003213 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003214 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003215 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003216 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003217 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003218 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003219 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003220 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003221 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003222 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003223 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003224 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003225 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003226 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003227 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003228 break;
3229 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003230 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003231 break;
3232 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003233 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003234 break;
françois romieue6de30d2011-01-03 15:08:37 +00003235 case RTL_GIGA_MAC_VER_28:
3236 rtl8168d_4_hw_phy_config(tp);
3237 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003238 case RTL_GIGA_MAC_VER_29:
3239 case RTL_GIGA_MAC_VER_30:
3240 rtl8105e_hw_phy_config(tp);
3241 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003242 case RTL_GIGA_MAC_VER_31:
3243 /* None. */
3244 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003245 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003246 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003247 rtl8168e_1_hw_phy_config(tp);
3248 break;
3249 case RTL_GIGA_MAC_VER_34:
3250 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003251 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003252 case RTL_GIGA_MAC_VER_35:
3253 rtl8168f_1_hw_phy_config(tp);
3254 break;
3255 case RTL_GIGA_MAC_VER_36:
3256 rtl8168f_2_hw_phy_config(tp);
3257 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003258
Francois Romieu5615d9f2007-08-17 17:50:46 +02003259 default:
3260 break;
3261 }
3262}
3263
Francois Romieuda78dbf2012-01-26 14:18:23 +01003264static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 struct timer_list *timer = &tp->timer;
3267 void __iomem *ioaddr = tp->mmio_addr;
3268 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3269
Francois Romieubcf0bf92006-07-26 23:14:13 +02003270 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
françois romieu4da19632011-01-03 15:07:55 +00003272 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003273 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 * A busy loop could burn quite a few cycles on nowadays CPU.
3275 * Let's delay the execution of the timer for a few ticks.
3276 */
3277 timeout = HZ/10;
3278 goto out_mod_timer;
3279 }
3280
3281 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01003282 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Francois Romieuda78dbf2012-01-26 14:18:23 +01003284 netif_warn(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
françois romieu4da19632011-01-03 15:07:55 +00003286 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
3288out_mod_timer:
3289 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003290}
3291
3292static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3293{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003294 if (!test_and_set_bit(flag, tp->wk.flags))
3295 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003296}
3297
3298static void rtl8169_phy_timer(unsigned long __opaque)
3299{
3300 struct net_device *dev = (struct net_device *)__opaque;
3301 struct rtl8169_private *tp = netdev_priv(dev);
3302
Francois Romieu98ddf982012-01-31 10:47:34 +01003303 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304}
3305
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306#ifdef CONFIG_NET_POLL_CONTROLLER
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307static void rtl8169_netpoll(struct net_device *dev)
3308{
3309 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Francois Romieuda78dbf2012-01-26 14:18:23 +01003311 rtl8169_interrupt(tp->pci_dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312}
3313#endif
3314
3315static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3316 void __iomem *ioaddr)
3317{
3318 iounmap(ioaddr);
3319 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003320 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 pci_disable_device(pdev);
3322 free_netdev(dev);
3323}
3324
Francois Romieubf793292006-11-01 00:53:05 +01003325static void rtl8169_phy_reset(struct net_device *dev,
3326 struct rtl8169_private *tp)
3327{
Francois Romieu07d3f512007-02-21 22:40:46 +01003328 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003329
françois romieu4da19632011-01-03 15:07:55 +00003330 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003331 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003332 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003333 return;
3334 msleep(1);
3335 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003336 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003337}
3338
David S. Miller8decf862011-09-22 03:23:13 -04003339static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3340{
3341 void __iomem *ioaddr = tp->mmio_addr;
3342
3343 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3344 (RTL_R8(PHYstatus) & TBI_Enable);
3345}
3346
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003347static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003349 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003350
Francois Romieu5615d9f2007-08-17 17:50:46 +02003351 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003352
Marcus Sundberg773328942008-07-10 21:28:08 +02003353 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3354 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3355 RTL_W8(0x82, 0x01);
3356 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003357
Francois Romieu6dccd162007-02-13 23:38:05 +01003358 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3359
3360 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3361 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003362
Francois Romieubcf0bf92006-07-26 23:14:13 +02003363 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003364 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3365 RTL_W8(0x82, 0x01);
3366 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003367 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003368 }
3369
Francois Romieubf793292006-11-01 00:53:05 +01003370 rtl8169_phy_reset(dev, tp);
3371
Oliver Neukum54405cd2011-01-06 21:55:13 +01003372 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003373 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3374 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3375 (tp->mii.supports_gmii ?
3376 ADVERTISED_1000baseT_Half |
3377 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003378
David S. Miller8decf862011-09-22 03:23:13 -04003379 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003380 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003381}
3382
Francois Romieu773d2022007-01-31 23:47:43 +01003383static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3384{
3385 void __iomem *ioaddr = tp->mmio_addr;
3386 u32 high;
3387 u32 low;
3388
3389 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3390 high = addr[4] | (addr[5] << 8);
3391
Francois Romieuda78dbf2012-01-26 14:18:23 +01003392 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003393
3394 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003395
Francois Romieu773d2022007-01-31 23:47:43 +01003396 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00003397 RTL_R32(MAC4);
3398
Francois Romieu78f1cd02010-03-27 19:35:46 -07003399 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00003400 RTL_R32(MAC0);
3401
françois romieuc28aa382011-08-02 03:53:43 +00003402 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3403 const struct exgmac_reg e[] = {
3404 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3405 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3406 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3407 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3408 low >> 16 },
3409 };
3410
3411 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3412 }
3413
Francois Romieu773d2022007-01-31 23:47:43 +01003414 RTL_W8(Cfg9346, Cfg9346_Lock);
3415
Francois Romieuda78dbf2012-01-26 14:18:23 +01003416 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003417}
3418
3419static int rtl_set_mac_address(struct net_device *dev, void *p)
3420{
3421 struct rtl8169_private *tp = netdev_priv(dev);
3422 struct sockaddr *addr = p;
3423
3424 if (!is_valid_ether_addr(addr->sa_data))
3425 return -EADDRNOTAVAIL;
3426
3427 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3428
3429 rtl_rar_set(tp, dev->dev_addr);
3430
3431 return 0;
3432}
3433
Francois Romieu5f787a12006-08-17 13:02:36 +02003434static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3435{
3436 struct rtl8169_private *tp = netdev_priv(dev);
3437 struct mii_ioctl_data *data = if_mii(ifr);
3438
Francois Romieu8b4ab282008-11-19 22:05:25 -08003439 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3440}
Francois Romieu5f787a12006-08-17 13:02:36 +02003441
Francois Romieucecb5fd2011-04-01 10:21:07 +02003442static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3443 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003444{
Francois Romieu5f787a12006-08-17 13:02:36 +02003445 switch (cmd) {
3446 case SIOCGMIIPHY:
3447 data->phy_id = 32; /* Internal PHY */
3448 return 0;
3449
3450 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003451 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003452 return 0;
3453
3454 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003455 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003456 return 0;
3457 }
3458 return -EOPNOTSUPP;
3459}
3460
Francois Romieu8b4ab282008-11-19 22:05:25 -08003461static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3462{
3463 return -EOPNOTSUPP;
3464}
3465
Francois Romieu0e485152007-02-20 00:00:26 +01003466static const struct rtl_cfg_info {
3467 void (*hw_start)(struct net_device *);
3468 unsigned int region;
3469 unsigned int align;
Francois Romieuda78dbf2012-01-26 14:18:23 +01003470 u16 event_slow;
Francois Romieuccdffb92008-07-26 14:26:06 +02003471 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07003472 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01003473} rtl_cfg_infos [] = {
3474 [RTL_CFG_0] = {
3475 .hw_start = rtl_hw_start_8169,
3476 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01003477 .align = 0,
Francois Romieuda78dbf2012-01-26 14:18:23 +01003478 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003479 .features = RTL_FEATURE_GMII,
3480 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01003481 },
3482 [RTL_CFG_1] = {
3483 .hw_start = rtl_hw_start_8168,
3484 .region = 2,
3485 .align = 8,
Francois Romieuda78dbf2012-01-26 14:18:23 +01003486 .event_slow = SYSErr | LinkChg | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003487 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3488 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01003489 },
3490 [RTL_CFG_2] = {
3491 .hw_start = rtl_hw_start_8101,
3492 .region = 2,
3493 .align = 8,
Francois Romieuda78dbf2012-01-26 14:18:23 +01003494 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
3495 PCSTimeout,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003496 .features = RTL_FEATURE_MSI,
3497 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01003498 }
3499};
3500
Francois Romieufbac58f2007-10-04 22:51:38 +02003501/* Cfg9346_Unlock assumed. */
françois romieu2ca6cf02011-12-15 08:37:43 +00003502static unsigned rtl_try_msi(struct rtl8169_private *tp,
Francois Romieufbac58f2007-10-04 22:51:38 +02003503 const struct rtl_cfg_info *cfg)
3504{
françois romieu2ca6cf02011-12-15 08:37:43 +00003505 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieufbac58f2007-10-04 22:51:38 +02003506 unsigned msi = 0;
3507 u8 cfg2;
3508
3509 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02003510 if (cfg->features & RTL_FEATURE_MSI) {
françois romieu2ca6cf02011-12-15 08:37:43 +00003511 if (pci_enable_msi(tp->pci_dev)) {
3512 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
Francois Romieufbac58f2007-10-04 22:51:38 +02003513 } else {
3514 cfg2 |= MSIEnable;
3515 msi = RTL_FEATURE_MSI;
3516 }
3517 }
françois romieu2ca6cf02011-12-15 08:37:43 +00003518 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3519 RTL_W8(Config2, cfg2);
Francois Romieufbac58f2007-10-04 22:51:38 +02003520 return msi;
3521}
3522
3523static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3524{
3525 if (tp->features & RTL_FEATURE_MSI) {
3526 pci_disable_msi(pdev);
3527 tp->features &= ~RTL_FEATURE_MSI;
3528 }
3529}
3530
Francois Romieu8b4ab282008-11-19 22:05:25 -08003531static const struct net_device_ops rtl8169_netdev_ops = {
3532 .ndo_open = rtl8169_open,
3533 .ndo_stop = rtl8169_close,
Junchang Wang8027aa22012-03-04 23:30:32 +01003534 .ndo_get_stats64 = rtl8169_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -08003535 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003536 .ndo_tx_timeout = rtl8169_tx_timeout,
3537 .ndo_validate_addr = eth_validate_addr,
3538 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00003539 .ndo_fix_features = rtl8169_fix_features,
3540 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003541 .ndo_set_mac_address = rtl_set_mac_address,
3542 .ndo_do_ioctl = rtl8169_ioctl,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003543 .ndo_set_rx_mode = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003544#ifdef CONFIG_NET_POLL_CONTROLLER
3545 .ndo_poll_controller = rtl8169_netpoll,
3546#endif
3547
3548};
3549
françois romieuc0e45c12011-01-03 15:08:04 +00003550static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3551{
3552 struct mdio_ops *ops = &tp->mdio_ops;
3553
3554 switch (tp->mac_version) {
3555 case RTL_GIGA_MAC_VER_27:
3556 ops->write = r8168dp_1_mdio_write;
3557 ops->read = r8168dp_1_mdio_read;
3558 break;
françois romieue6de30d2011-01-03 15:08:37 +00003559 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003560 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003561 ops->write = r8168dp_2_mdio_write;
3562 ops->read = r8168dp_2_mdio_read;
3563 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003564 default:
3565 ops->write = r8169_mdio_write;
3566 ops->read = r8169_mdio_read;
3567 break;
3568 }
3569}
3570
David S. Miller1805b2f2011-10-24 18:18:09 -04003571static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3572{
3573 void __iomem *ioaddr = tp->mmio_addr;
3574
3575 switch (tp->mac_version) {
3576 case RTL_GIGA_MAC_VER_29:
3577 case RTL_GIGA_MAC_VER_30:
3578 case RTL_GIGA_MAC_VER_32:
3579 case RTL_GIGA_MAC_VER_33:
3580 case RTL_GIGA_MAC_VER_34:
3581 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3582 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3583 break;
3584 default:
3585 break;
3586 }
3587}
3588
3589static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3590{
3591 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3592 return false;
3593
3594 rtl_writephy(tp, 0x1f, 0x0000);
3595 rtl_writephy(tp, MII_BMCR, 0x0000);
3596
3597 rtl_wol_suspend_quirk(tp);
3598
3599 return true;
3600}
3601
françois romieu065c27c2011-01-03 15:08:12 +00003602static void r810x_phy_power_down(struct rtl8169_private *tp)
3603{
3604 rtl_writephy(tp, 0x1f, 0x0000);
3605 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3606}
3607
3608static void r810x_phy_power_up(struct rtl8169_private *tp)
3609{
3610 rtl_writephy(tp, 0x1f, 0x0000);
3611 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3612}
3613
3614static void r810x_pll_power_down(struct rtl8169_private *tp)
3615{
David S. Miller1805b2f2011-10-24 18:18:09 -04003616 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003617 return;
françois romieu065c27c2011-01-03 15:08:12 +00003618
3619 r810x_phy_power_down(tp);
3620}
3621
3622static void r810x_pll_power_up(struct rtl8169_private *tp)
3623{
3624 r810x_phy_power_up(tp);
3625}
3626
3627static void r8168_phy_power_up(struct rtl8169_private *tp)
3628{
3629 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003630 switch (tp->mac_version) {
3631 case RTL_GIGA_MAC_VER_11:
3632 case RTL_GIGA_MAC_VER_12:
3633 case RTL_GIGA_MAC_VER_17:
3634 case RTL_GIGA_MAC_VER_18:
3635 case RTL_GIGA_MAC_VER_19:
3636 case RTL_GIGA_MAC_VER_20:
3637 case RTL_GIGA_MAC_VER_21:
3638 case RTL_GIGA_MAC_VER_22:
3639 case RTL_GIGA_MAC_VER_23:
3640 case RTL_GIGA_MAC_VER_24:
3641 case RTL_GIGA_MAC_VER_25:
3642 case RTL_GIGA_MAC_VER_26:
3643 case RTL_GIGA_MAC_VER_27:
3644 case RTL_GIGA_MAC_VER_28:
3645 case RTL_GIGA_MAC_VER_31:
3646 rtl_writephy(tp, 0x0e, 0x0000);
3647 break;
3648 default:
3649 break;
3650 }
françois romieu065c27c2011-01-03 15:08:12 +00003651 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3652}
3653
3654static void r8168_phy_power_down(struct rtl8169_private *tp)
3655{
3656 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003657 switch (tp->mac_version) {
3658 case RTL_GIGA_MAC_VER_32:
3659 case RTL_GIGA_MAC_VER_33:
3660 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3661 break;
3662
3663 case RTL_GIGA_MAC_VER_11:
3664 case RTL_GIGA_MAC_VER_12:
3665 case RTL_GIGA_MAC_VER_17:
3666 case RTL_GIGA_MAC_VER_18:
3667 case RTL_GIGA_MAC_VER_19:
3668 case RTL_GIGA_MAC_VER_20:
3669 case RTL_GIGA_MAC_VER_21:
3670 case RTL_GIGA_MAC_VER_22:
3671 case RTL_GIGA_MAC_VER_23:
3672 case RTL_GIGA_MAC_VER_24:
3673 case RTL_GIGA_MAC_VER_25:
3674 case RTL_GIGA_MAC_VER_26:
3675 case RTL_GIGA_MAC_VER_27:
3676 case RTL_GIGA_MAC_VER_28:
3677 case RTL_GIGA_MAC_VER_31:
3678 rtl_writephy(tp, 0x0e, 0x0200);
3679 default:
3680 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3681 break;
3682 }
françois romieu065c27c2011-01-03 15:08:12 +00003683}
3684
3685static void r8168_pll_power_down(struct rtl8169_private *tp)
3686{
3687 void __iomem *ioaddr = tp->mmio_addr;
3688
Francois Romieucecb5fd2011-04-01 10:21:07 +02003689 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3690 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3691 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003692 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003693 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003694 }
françois romieu065c27c2011-01-03 15:08:12 +00003695
Francois Romieucecb5fd2011-04-01 10:21:07 +02003696 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3697 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003698 (RTL_R16(CPlusCmd) & ASF)) {
3699 return;
3700 }
3701
hayeswang01dc7fe2011-03-21 01:50:28 +00003702 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3703 tp->mac_version == RTL_GIGA_MAC_VER_33)
3704 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3705
David S. Miller1805b2f2011-10-24 18:18:09 -04003706 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003707 return;
françois romieu065c27c2011-01-03 15:08:12 +00003708
3709 r8168_phy_power_down(tp);
3710
3711 switch (tp->mac_version) {
3712 case RTL_GIGA_MAC_VER_25:
3713 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003714 case RTL_GIGA_MAC_VER_27:
3715 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003716 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003717 case RTL_GIGA_MAC_VER_32:
3718 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003719 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3720 break;
3721 }
3722}
3723
3724static void r8168_pll_power_up(struct rtl8169_private *tp)
3725{
3726 void __iomem *ioaddr = tp->mmio_addr;
3727
Francois Romieucecb5fd2011-04-01 10:21:07 +02003728 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3729 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3730 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003731 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003732 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003733 }
françois romieu065c27c2011-01-03 15:08:12 +00003734
3735 switch (tp->mac_version) {
3736 case RTL_GIGA_MAC_VER_25:
3737 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003738 case RTL_GIGA_MAC_VER_27:
3739 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003740 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003741 case RTL_GIGA_MAC_VER_32:
3742 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003743 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3744 break;
3745 }
3746
3747 r8168_phy_power_up(tp);
3748}
3749
Francois Romieud58d46b2011-05-03 16:38:29 +02003750static void rtl_generic_op(struct rtl8169_private *tp,
3751 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00003752{
3753 if (op)
3754 op(tp);
3755}
3756
3757static void rtl_pll_power_down(struct rtl8169_private *tp)
3758{
Francois Romieud58d46b2011-05-03 16:38:29 +02003759 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00003760}
3761
3762static void rtl_pll_power_up(struct rtl8169_private *tp)
3763{
Francois Romieud58d46b2011-05-03 16:38:29 +02003764 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00003765}
3766
3767static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3768{
3769 struct pll_power_ops *ops = &tp->pll_power_ops;
3770
3771 switch (tp->mac_version) {
3772 case RTL_GIGA_MAC_VER_07:
3773 case RTL_GIGA_MAC_VER_08:
3774 case RTL_GIGA_MAC_VER_09:
3775 case RTL_GIGA_MAC_VER_10:
3776 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003777 case RTL_GIGA_MAC_VER_29:
3778 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003779 ops->down = r810x_pll_power_down;
3780 ops->up = r810x_pll_power_up;
3781 break;
3782
3783 case RTL_GIGA_MAC_VER_11:
3784 case RTL_GIGA_MAC_VER_12:
3785 case RTL_GIGA_MAC_VER_17:
3786 case RTL_GIGA_MAC_VER_18:
3787 case RTL_GIGA_MAC_VER_19:
3788 case RTL_GIGA_MAC_VER_20:
3789 case RTL_GIGA_MAC_VER_21:
3790 case RTL_GIGA_MAC_VER_22:
3791 case RTL_GIGA_MAC_VER_23:
3792 case RTL_GIGA_MAC_VER_24:
3793 case RTL_GIGA_MAC_VER_25:
3794 case RTL_GIGA_MAC_VER_26:
3795 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003796 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003797 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003798 case RTL_GIGA_MAC_VER_32:
3799 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003800 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08003801 case RTL_GIGA_MAC_VER_35:
3802 case RTL_GIGA_MAC_VER_36:
françois romieu065c27c2011-01-03 15:08:12 +00003803 ops->down = r8168_pll_power_down;
3804 ops->up = r8168_pll_power_up;
3805 break;
3806
3807 default:
3808 ops->down = NULL;
3809 ops->up = NULL;
3810 break;
3811 }
3812}
3813
Hayes Wange542a222011-07-06 15:58:04 +08003814static void rtl_init_rxcfg(struct rtl8169_private *tp)
3815{
3816 void __iomem *ioaddr = tp->mmio_addr;
3817
3818 switch (tp->mac_version) {
3819 case RTL_GIGA_MAC_VER_01:
3820 case RTL_GIGA_MAC_VER_02:
3821 case RTL_GIGA_MAC_VER_03:
3822 case RTL_GIGA_MAC_VER_04:
3823 case RTL_GIGA_MAC_VER_05:
3824 case RTL_GIGA_MAC_VER_06:
3825 case RTL_GIGA_MAC_VER_10:
3826 case RTL_GIGA_MAC_VER_11:
3827 case RTL_GIGA_MAC_VER_12:
3828 case RTL_GIGA_MAC_VER_13:
3829 case RTL_GIGA_MAC_VER_14:
3830 case RTL_GIGA_MAC_VER_15:
3831 case RTL_GIGA_MAC_VER_16:
3832 case RTL_GIGA_MAC_VER_17:
3833 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3834 break;
3835 case RTL_GIGA_MAC_VER_18:
3836 case RTL_GIGA_MAC_VER_19:
3837 case RTL_GIGA_MAC_VER_20:
3838 case RTL_GIGA_MAC_VER_21:
3839 case RTL_GIGA_MAC_VER_22:
3840 case RTL_GIGA_MAC_VER_23:
3841 case RTL_GIGA_MAC_VER_24:
3842 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3843 break;
3844 default:
3845 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3846 break;
3847 }
3848}
3849
Hayes Wang92fc43b2011-07-06 15:58:03 +08003850static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3851{
3852 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3853}
3854
Francois Romieud58d46b2011-05-03 16:38:29 +02003855static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3856{
françois romieu9c5028e2012-03-02 04:43:14 +00003857 void __iomem *ioaddr = tp->mmio_addr;
3858
3859 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003860 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00003861 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003862}
3863
3864static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3865{
françois romieu9c5028e2012-03-02 04:43:14 +00003866 void __iomem *ioaddr = tp->mmio_addr;
3867
3868 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003869 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00003870 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003871}
3872
3873static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3874{
3875 void __iomem *ioaddr = tp->mmio_addr;
3876
3877 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3878 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3879 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3880}
3881
3882static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3883{
3884 void __iomem *ioaddr = tp->mmio_addr;
3885
3886 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3887 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3888 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3889}
3890
3891static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3892{
3893 void __iomem *ioaddr = tp->mmio_addr;
3894
3895 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3896}
3897
3898static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3899{
3900 void __iomem *ioaddr = tp->mmio_addr;
3901
3902 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3903}
3904
3905static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3906{
3907 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003908
3909 RTL_W8(MaxTxPacketSize, 0x3f);
3910 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3911 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003912 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003913}
3914
3915static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3916{
3917 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003918
3919 RTL_W8(MaxTxPacketSize, 0x0c);
3920 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3921 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003922 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003923}
3924
3925static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3926{
3927 rtl_tx_performance_tweak(tp->pci_dev,
3928 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3929}
3930
3931static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3932{
3933 rtl_tx_performance_tweak(tp->pci_dev,
3934 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3935}
3936
3937static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3938{
3939 void __iomem *ioaddr = tp->mmio_addr;
3940
3941 r8168b_0_hw_jumbo_enable(tp);
3942
3943 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
3944}
3945
3946static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3947{
3948 void __iomem *ioaddr = tp->mmio_addr;
3949
3950 r8168b_0_hw_jumbo_disable(tp);
3951
3952 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3953}
3954
3955static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
3956{
3957 struct jumbo_ops *ops = &tp->jumbo_ops;
3958
3959 switch (tp->mac_version) {
3960 case RTL_GIGA_MAC_VER_11:
3961 ops->disable = r8168b_0_hw_jumbo_disable;
3962 ops->enable = r8168b_0_hw_jumbo_enable;
3963 break;
3964 case RTL_GIGA_MAC_VER_12:
3965 case RTL_GIGA_MAC_VER_17:
3966 ops->disable = r8168b_1_hw_jumbo_disable;
3967 ops->enable = r8168b_1_hw_jumbo_enable;
3968 break;
3969 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
3970 case RTL_GIGA_MAC_VER_19:
3971 case RTL_GIGA_MAC_VER_20:
3972 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
3973 case RTL_GIGA_MAC_VER_22:
3974 case RTL_GIGA_MAC_VER_23:
3975 case RTL_GIGA_MAC_VER_24:
3976 case RTL_GIGA_MAC_VER_25:
3977 case RTL_GIGA_MAC_VER_26:
3978 ops->disable = r8168c_hw_jumbo_disable;
3979 ops->enable = r8168c_hw_jumbo_enable;
3980 break;
3981 case RTL_GIGA_MAC_VER_27:
3982 case RTL_GIGA_MAC_VER_28:
3983 ops->disable = r8168dp_hw_jumbo_disable;
3984 ops->enable = r8168dp_hw_jumbo_enable;
3985 break;
3986 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
3987 case RTL_GIGA_MAC_VER_32:
3988 case RTL_GIGA_MAC_VER_33:
3989 case RTL_GIGA_MAC_VER_34:
3990 ops->disable = r8168e_hw_jumbo_disable;
3991 ops->enable = r8168e_hw_jumbo_enable;
3992 break;
3993
3994 /*
3995 * No action needed for jumbo frames with 8169.
3996 * No jumbo for 810x at all.
3997 */
3998 default:
3999 ops->disable = NULL;
4000 ops->enable = NULL;
4001 break;
4002 }
4003}
4004
Francois Romieu6f43adc2011-04-29 15:05:51 +02004005static void rtl_hw_reset(struct rtl8169_private *tp)
4006{
4007 void __iomem *ioaddr = tp->mmio_addr;
4008 int i;
4009
4010 /* Soft reset the chip. */
4011 RTL_W8(ChipCmd, CmdReset);
4012
4013 /* Check that the chip has finished the reset. */
4014 for (i = 0; i < 100; i++) {
4015 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
4016 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004017 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004018 }
4019}
4020
Francois Romieub6ffd972011-06-17 17:00:05 +02004021static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4022{
4023 struct rtl_fw *rtl_fw;
4024 const char *name;
4025 int rc = -ENOMEM;
4026
4027 name = rtl_lookup_firmware_name(tp);
4028 if (!name)
4029 goto out_no_firmware;
4030
4031 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4032 if (!rtl_fw)
4033 goto err_warn;
4034
4035 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4036 if (rc < 0)
4037 goto err_free;
4038
Francois Romieufd112f22011-06-18 00:10:29 +02004039 rc = rtl_check_firmware(tp, rtl_fw);
4040 if (rc < 0)
4041 goto err_release_firmware;
4042
Francois Romieub6ffd972011-06-17 17:00:05 +02004043 tp->rtl_fw = rtl_fw;
4044out:
4045 return;
4046
Francois Romieufd112f22011-06-18 00:10:29 +02004047err_release_firmware:
4048 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004049err_free:
4050 kfree(rtl_fw);
4051err_warn:
4052 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4053 name, rc);
4054out_no_firmware:
4055 tp->rtl_fw = NULL;
4056 goto out;
4057}
4058
François Romieu953a12c2011-04-24 17:38:48 +02004059static void rtl_request_firmware(struct rtl8169_private *tp)
4060{
Francois Romieub6ffd972011-06-17 17:00:05 +02004061 if (IS_ERR(tp->rtl_fw))
4062 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004063}
4064
Francois Romieu4422bcd2012-01-26 11:23:32 +01004065static void rtl_task(struct work_struct *);
4066
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067static int rtl8169_open(struct net_device *dev)
4068{
4069 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00004070 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02004072 int retval = -ENOMEM;
4073
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004074 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075
Neil Hormanc0cd8842010-03-29 13:16:02 -07004076 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004078 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004080 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
4081 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004083 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004085 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
4086 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02004088 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089
4090 retval = rtl8169_init_ring(dev);
4091 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02004092 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
Francois Romieu4422bcd2012-01-26 11:23:32 +01004094 INIT_WORK(&tp->wk.work, rtl_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
Francois Romieu99f252b2007-04-02 22:59:59 +02004096 smp_mb();
4097
François Romieu953a12c2011-04-24 17:38:48 +02004098 rtl_request_firmware(tp);
4099
Francois Romieufbac58f2007-10-04 22:51:38 +02004100 retval = request_irq(dev->irq, rtl8169_interrupt,
4101 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02004102 dev->name, dev);
4103 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02004104 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02004105
Francois Romieuda78dbf2012-01-26 14:18:23 +01004106 rtl_lock_work(tp);
4107
Francois Romieu6c4a70c2012-01-31 10:56:44 +01004108 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004109
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004110 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004111
françois romieueee3a962011-01-08 02:17:26 +00004112 rtl8169_init_phy(dev, tp);
4113
Francois Romieuda78dbf2012-01-26 14:18:23 +01004114 __rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00004115
françois romieu065c27c2011-01-03 15:08:12 +00004116 rtl_pll_power_up(tp);
4117
Francois Romieu07ce4062007-02-23 23:36:39 +01004118 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
Francois Romieuda78dbf2012-01-26 14:18:23 +01004120 netif_start_queue(dev);
4121
4122 rtl_unlock_work(tp);
4123
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004124 tp->saved_wolopts = 0;
4125 pm_runtime_put_noidle(&pdev->dev);
4126
françois romieueee3a962011-01-08 02:17:26 +00004127 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128out:
4129 return retval;
4130
François Romieu953a12c2011-04-24 17:38:48 +02004131err_release_fw_2:
4132 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02004133 rtl8169_rx_clear(tp);
4134err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004135 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
4136 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004137 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02004138err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004139 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
4140 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004141 tp->TxDescArray = NULL;
4142err_pm_runtime_put:
4143 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 goto out;
4145}
4146
Hayes Wang92fc43b2011-07-06 15:58:03 +08004147static void rtl_rx_close(struct rtl8169_private *tp)
4148{
4149 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004150
Francois Romieu1687b562011-07-19 17:21:29 +02004151 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004152}
4153
françois romieue6de30d2011-01-03 15:08:37 +00004154static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155{
françois romieue6de30d2011-01-03 15:08:37 +00004156 void __iomem *ioaddr = tp->mmio_addr;
4157
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004159 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
Hayes Wang92fc43b2011-07-06 15:58:03 +08004161 rtl_rx_close(tp);
4162
Hayes Wang5d2e1952011-02-22 17:26:22 +08004163 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004164 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4165 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00004166 while (RTL_R8(TxPoll) & NPQ)
4167 udelay(20);
Hayes Wangc2218922011-09-06 16:55:18 +08004168 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4169 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
4170 tp->mac_version == RTL_GIGA_MAC_VER_36) {
David S. Miller8decf862011-09-22 03:23:13 -04004171 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08004172 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4173 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004174 } else {
4175 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4176 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004177 }
4178
Hayes Wang92fc43b2011-07-06 15:58:03 +08004179 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180}
4181
Francois Romieu7f796d832007-06-11 23:04:41 +02004182static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004183{
4184 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004185
4186 /* Set DMA burst size and Interframe Gap Time */
4187 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4188 (InterFrameGap << TxInterFrameGapShift));
4189}
4190
Francois Romieu07ce4062007-02-23 23:36:39 +01004191static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192{
4193 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194
Francois Romieu07ce4062007-02-23 23:36:39 +01004195 tp->hw_start(dev);
4196
Francois Romieuda78dbf2012-01-26 14:18:23 +01004197 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004198}
4199
Francois Romieu7f796d832007-06-11 23:04:41 +02004200static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4201 void __iomem *ioaddr)
4202{
4203 /*
4204 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4205 * register to be written before TxDescAddrLow to work.
4206 * Switching from MMIO to I/O access fixes the issue as well.
4207 */
4208 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004209 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004210 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004211 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004212}
4213
4214static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4215{
4216 u16 cmd;
4217
4218 cmd = RTL_R16(CPlusCmd);
4219 RTL_W16(CPlusCmd, cmd);
4220 return cmd;
4221}
4222
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004223static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004224{
4225 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004226 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004227}
4228
Francois Romieu6dccd162007-02-13 23:38:05 +01004229static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4230{
Francois Romieu37441002011-06-17 22:58:54 +02004231 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004232 u32 mac_version;
4233 u32 clk;
4234 u32 val;
4235 } cfg2_info [] = {
4236 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4237 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4238 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4239 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004240 };
4241 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004242 unsigned int i;
4243 u32 clk;
4244
4245 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004246 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004247 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4248 RTL_W32(0x7c, p->val);
4249 break;
4250 }
4251 }
4252}
4253
Francois Romieu07ce4062007-02-23 23:36:39 +01004254static void rtl_hw_start_8169(struct net_device *dev)
4255{
4256 struct rtl8169_private *tp = netdev_priv(dev);
4257 void __iomem *ioaddr = tp->mmio_addr;
4258 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004259
Francois Romieu9cb427b2006-11-02 00:10:16 +01004260 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4261 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4262 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4263 }
4264
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004266 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4267 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4268 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4269 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004270 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4271
Hayes Wange542a222011-07-06 15:58:04 +08004272 rtl_init_rxcfg(tp);
4273
françois romieuf0298f82011-01-03 15:07:42 +00004274 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004276 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
Francois Romieucecb5fd2011-04-01 10:21:07 +02004278 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4279 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4280 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4281 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004282 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283
Francois Romieu7f796d832007-06-11 23:04:41 +02004284 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004285
Francois Romieucecb5fd2011-04-01 10:21:07 +02004286 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4287 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004288 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004290 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 }
4292
Francois Romieubcf0bf92006-07-26 23:14:13 +02004293 RTL_W16(CPlusCmd, tp->cp_cmd);
4294
Francois Romieu6dccd162007-02-13 23:38:05 +01004295 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4296
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 /*
4298 * Undocumented corner. Supposedly:
4299 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4300 */
4301 RTL_W16(IntrMitigate, 0x0000);
4302
Francois Romieu7f796d832007-06-11 23:04:41 +02004303 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004304
Francois Romieucecb5fd2011-04-01 10:21:07 +02004305 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4306 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4307 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4308 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004309 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4310 rtl_set_rx_tx_config_registers(tp);
4311 }
4312
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004314
4315 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4316 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317
4318 RTL_W32(RxMissed, 0);
4319
Francois Romieu07ce4062007-02-23 23:36:39 +01004320 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321
4322 /* no early-rx interrupts */
4323 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004324}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325
françois romieu650e8d52011-01-03 15:08:29 +00004326static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004327{
4328 u32 csi;
4329
4330 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00004331 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4332}
4333
françois romieue6de30d2011-01-03 15:08:37 +00004334static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4335{
4336 rtl_csi_access_enable(ioaddr, 0x17000000);
4337}
4338
françois romieu650e8d52011-01-03 15:08:29 +00004339static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4340{
4341 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02004342}
4343
4344struct ephy_info {
4345 unsigned int offset;
4346 u16 mask;
4347 u16 bits;
4348};
4349
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004350static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004351{
4352 u16 w;
4353
4354 while (len-- > 0) {
4355 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4356 rtl_ephy_write(ioaddr, e->offset, w);
4357 e++;
4358 }
4359}
4360
Francois Romieub726e492008-06-28 12:22:59 +02004361static void rtl_disable_clock_request(struct pci_dev *pdev)
4362{
Jon Masone44daad2011-06-27 07:46:31 +00004363 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004364
4365 if (cap) {
4366 u16 ctl;
4367
4368 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4369 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4370 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4371 }
4372}
4373
françois romieue6de30d2011-01-03 15:08:37 +00004374static void rtl_enable_clock_request(struct pci_dev *pdev)
4375{
Jon Masone44daad2011-06-27 07:46:31 +00004376 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004377
4378 if (cap) {
4379 u16 ctl;
4380
4381 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4382 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4383 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4384 }
4385}
4386
Francois Romieub726e492008-06-28 12:22:59 +02004387#define R8168_CPCMD_QUIRK_MASK (\
4388 EnableBist | \
4389 Mac_dbgo_oe | \
4390 Force_half_dup | \
4391 Force_rxflow_en | \
4392 Force_txflow_en | \
4393 Cxpl_dbg_sel | \
4394 ASF | \
4395 PktCntrDisable | \
4396 Mac_dbgo_sel)
4397
Francois Romieu219a1e92008-06-28 11:58:39 +02004398static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4399{
Francois Romieub726e492008-06-28 12:22:59 +02004400 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4401
4402 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4403
Francois Romieu2e68ae42008-06-28 12:00:55 +02004404 rtl_tx_performance_tweak(pdev,
4405 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004406}
4407
4408static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4409{
4410 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004411
françois romieuf0298f82011-01-03 15:07:42 +00004412 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004413
4414 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004415}
4416
4417static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4418{
Francois Romieub726e492008-06-28 12:22:59 +02004419 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4420
4421 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4422
Francois Romieu219a1e92008-06-28 11:58:39 +02004423 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004424
4425 rtl_disable_clock_request(pdev);
4426
4427 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004428}
4429
Francois Romieuef3386f2008-06-29 12:24:30 +02004430static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004431{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004432 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004433 { 0x01, 0, 0x0001 },
4434 { 0x02, 0x0800, 0x1000 },
4435 { 0x03, 0, 0x0042 },
4436 { 0x06, 0x0080, 0x0000 },
4437 { 0x07, 0, 0x2000 }
4438 };
4439
françois romieu650e8d52011-01-03 15:08:29 +00004440 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004441
4442 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4443
Francois Romieu219a1e92008-06-28 11:58:39 +02004444 __rtl_hw_start_8168cp(ioaddr, pdev);
4445}
4446
Francois Romieuef3386f2008-06-29 12:24:30 +02004447static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4448{
françois romieu650e8d52011-01-03 15:08:29 +00004449 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004450
4451 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4452
4453 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4454
4455 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4456}
4457
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004458static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4459{
françois romieu650e8d52011-01-03 15:08:29 +00004460 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004461
4462 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4463
4464 /* Magic. */
4465 RTL_W8(DBG_REG, 0x20);
4466
françois romieuf0298f82011-01-03 15:07:42 +00004467 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004468
4469 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4470
4471 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4472}
4473
Francois Romieu219a1e92008-06-28 11:58:39 +02004474static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4475{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004476 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004477 { 0x02, 0x0800, 0x1000 },
4478 { 0x03, 0, 0x0002 },
4479 { 0x06, 0x0080, 0x0000 }
4480 };
4481
françois romieu650e8d52011-01-03 15:08:29 +00004482 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004483
4484 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4485
4486 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4487
Francois Romieu219a1e92008-06-28 11:58:39 +02004488 __rtl_hw_start_8168cp(ioaddr, pdev);
4489}
4490
4491static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4492{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004493 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004494 { 0x01, 0, 0x0001 },
4495 { 0x03, 0x0400, 0x0220 }
4496 };
4497
françois romieu650e8d52011-01-03 15:08:29 +00004498 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004499
4500 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4501
Francois Romieu219a1e92008-06-28 11:58:39 +02004502 __rtl_hw_start_8168cp(ioaddr, pdev);
4503}
4504
Francois Romieu197ff762008-06-28 13:16:02 +02004505static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4506{
4507 rtl_hw_start_8168c_2(ioaddr, pdev);
4508}
4509
Francois Romieu6fb07052008-06-29 11:54:28 +02004510static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4511{
françois romieu650e8d52011-01-03 15:08:29 +00004512 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004513
4514 __rtl_hw_start_8168cp(ioaddr, pdev);
4515}
4516
Francois Romieu5b538df2008-07-20 16:22:45 +02004517static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4518{
françois romieu650e8d52011-01-03 15:08:29 +00004519 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004520
4521 rtl_disable_clock_request(pdev);
4522
françois romieuf0298f82011-01-03 15:07:42 +00004523 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004524
4525 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4526
4527 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4528}
4529
hayeswang4804b3b2011-03-21 01:50:29 +00004530static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4531{
4532 rtl_csi_access_enable_1(ioaddr);
4533
4534 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4535
4536 RTL_W8(MaxTxPacketSize, TxPacketMax);
4537
4538 rtl_disable_clock_request(pdev);
4539}
4540
françois romieue6de30d2011-01-03 15:08:37 +00004541static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4542{
4543 static const struct ephy_info e_info_8168d_4[] = {
4544 { 0x0b, ~0, 0x48 },
4545 { 0x19, 0x20, 0x50 },
4546 { 0x0c, ~0, 0x20 }
4547 };
4548 int i;
4549
4550 rtl_csi_access_enable_1(ioaddr);
4551
4552 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4553
4554 RTL_W8(MaxTxPacketSize, TxPacketMax);
4555
4556 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4557 const struct ephy_info *e = e_info_8168d_4 + i;
4558 u16 w;
4559
4560 w = rtl_ephy_read(ioaddr, e->offset);
4561 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4562 }
4563
4564 rtl_enable_clock_request(pdev);
4565}
4566
Hayes Wang70090422011-07-06 15:58:06 +08004567static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
hayeswang01dc7fe2011-03-21 01:50:28 +00004568{
Hayes Wang70090422011-07-06 15:58:06 +08004569 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004570 { 0x00, 0x0200, 0x0100 },
4571 { 0x00, 0x0000, 0x0004 },
4572 { 0x06, 0x0002, 0x0001 },
4573 { 0x06, 0x0000, 0x0030 },
4574 { 0x07, 0x0000, 0x2000 },
4575 { 0x00, 0x0000, 0x0020 },
4576 { 0x03, 0x5800, 0x2000 },
4577 { 0x03, 0x0000, 0x0001 },
4578 { 0x01, 0x0800, 0x1000 },
4579 { 0x07, 0x0000, 0x4000 },
4580 { 0x1e, 0x0000, 0x2000 },
4581 { 0x19, 0xffff, 0xfe6c },
4582 { 0x0a, 0x0000, 0x0040 }
4583 };
4584
4585 rtl_csi_access_enable_2(ioaddr);
4586
Hayes Wang70090422011-07-06 15:58:06 +08004587 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004588
4589 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4590
4591 RTL_W8(MaxTxPacketSize, TxPacketMax);
4592
4593 rtl_disable_clock_request(pdev);
4594
4595 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004596 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4597 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004598
Francois Romieucecb5fd2011-04-01 10:21:07 +02004599 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004600}
4601
Hayes Wang70090422011-07-06 15:58:06 +08004602static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4603{
4604 static const struct ephy_info e_info_8168e_2[] = {
4605 { 0x09, 0x0000, 0x0080 },
4606 { 0x19, 0x0000, 0x0224 }
4607 };
4608
4609 rtl_csi_access_enable_1(ioaddr);
4610
4611 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4612
4613 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4614
4615 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4616 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4617 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4618 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4619 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4620 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4621 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4622 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4623 ERIAR_EXGMAC);
4624
Hayes Wang3090bd92011-09-06 16:55:15 +08004625 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004626
4627 rtl_disable_clock_request(pdev);
4628
4629 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4630 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4631
4632 /* Adjust EEE LED frequency */
4633 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4634
4635 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4636 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4637 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4638}
4639
Hayes Wangc2218922011-09-06 16:55:18 +08004640static void rtl_hw_start_8168f_1(void __iomem *ioaddr, struct pci_dev *pdev)
4641{
4642 static const struct ephy_info e_info_8168f_1[] = {
4643 { 0x06, 0x00c0, 0x0020 },
4644 { 0x08, 0x0001, 0x0002 },
4645 { 0x09, 0x0000, 0x0080 },
4646 { 0x19, 0x0000, 0x0224 }
4647 };
4648
4649 rtl_csi_access_enable_1(ioaddr);
4650
4651 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4652
4653 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4654
4655 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4656 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4657 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4658 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4659 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4660 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4661 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4662 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4663 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4664 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
4665 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4666 ERIAR_EXGMAC);
4667
4668 RTL_W8(MaxTxPacketSize, EarlySize);
4669
4670 rtl_disable_clock_request(pdev);
4671
4672 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4673 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4674
4675 /* Adjust EEE LED frequency */
4676 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4677
4678 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4679 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4680 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4681}
4682
Francois Romieu07ce4062007-02-23 23:36:39 +01004683static void rtl_hw_start_8168(struct net_device *dev)
4684{
Francois Romieu2dd99532007-06-11 23:22:52 +02004685 struct rtl8169_private *tp = netdev_priv(dev);
4686 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004687 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004688
4689 RTL_W8(Cfg9346, Cfg9346_Unlock);
4690
françois romieuf0298f82011-01-03 15:07:42 +00004691 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004692
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004693 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004694
Francois Romieu0e485152007-02-20 00:00:26 +01004695 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004696
4697 RTL_W16(CPlusCmd, tp->cp_cmd);
4698
Francois Romieu0e485152007-02-20 00:00:26 +01004699 RTL_W16(IntrMitigate, 0x5151);
4700
4701 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00004702 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01004703 tp->event_slow |= RxFIFOOver | PCSTimeout;
4704 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01004705 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004706
4707 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4708
Francois Romieub8363902008-06-01 12:31:57 +02004709 rtl_set_rx_mode(dev);
4710
4711 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4712 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004713
4714 RTL_R8(IntrMask);
4715
Francois Romieu219a1e92008-06-28 11:58:39 +02004716 switch (tp->mac_version) {
4717 case RTL_GIGA_MAC_VER_11:
4718 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004719 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004720
4721 case RTL_GIGA_MAC_VER_12:
4722 case RTL_GIGA_MAC_VER_17:
4723 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004724 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004725
4726 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004727 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004728 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004729
4730 case RTL_GIGA_MAC_VER_19:
4731 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004732 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004733
4734 case RTL_GIGA_MAC_VER_20:
4735 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004736 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004737
Francois Romieu197ff762008-06-28 13:16:02 +02004738 case RTL_GIGA_MAC_VER_21:
4739 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004740 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004741
Francois Romieu6fb07052008-06-29 11:54:28 +02004742 case RTL_GIGA_MAC_VER_22:
4743 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004744 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004745
Francois Romieuef3386f2008-06-29 12:24:30 +02004746 case RTL_GIGA_MAC_VER_23:
4747 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004748 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004749
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004750 case RTL_GIGA_MAC_VER_24:
4751 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004752 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004753
Francois Romieu5b538df2008-07-20 16:22:45 +02004754 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004755 case RTL_GIGA_MAC_VER_26:
4756 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004757 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004758 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004759
françois romieue6de30d2011-01-03 15:08:37 +00004760 case RTL_GIGA_MAC_VER_28:
4761 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004762 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004763
hayeswang4804b3b2011-03-21 01:50:29 +00004764 case RTL_GIGA_MAC_VER_31:
4765 rtl_hw_start_8168dp(ioaddr, pdev);
4766 break;
4767
hayeswang01dc7fe2011-03-21 01:50:28 +00004768 case RTL_GIGA_MAC_VER_32:
4769 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004770 rtl_hw_start_8168e_1(ioaddr, pdev);
4771 break;
4772 case RTL_GIGA_MAC_VER_34:
4773 rtl_hw_start_8168e_2(ioaddr, pdev);
hayeswang01dc7fe2011-03-21 01:50:28 +00004774 break;
françois romieue6de30d2011-01-03 15:08:37 +00004775
Hayes Wangc2218922011-09-06 16:55:18 +08004776 case RTL_GIGA_MAC_VER_35:
4777 case RTL_GIGA_MAC_VER_36:
4778 rtl_hw_start_8168f_1(ioaddr, pdev);
4779 break;
4780
Francois Romieu219a1e92008-06-28 11:58:39 +02004781 default:
4782 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4783 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004784 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004785 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004786
Francois Romieu0e485152007-02-20 00:00:26 +01004787 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4788
Francois Romieub8363902008-06-01 12:31:57 +02004789 RTL_W8(Cfg9346, Cfg9346_Lock);
4790
Francois Romieu2dd99532007-06-11 23:22:52 +02004791 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004792}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
Francois Romieu2857ffb2008-08-02 21:08:49 +02004794#define R810X_CPCMD_QUIRK_MASK (\
4795 EnableBist | \
4796 Mac_dbgo_oe | \
4797 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004798 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004799 Force_txflow_en | \
4800 Cxpl_dbg_sel | \
4801 ASF | \
4802 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004803 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004804
4805static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4806{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004807 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004808 { 0x01, 0, 0x6e65 },
4809 { 0x02, 0, 0x091f },
4810 { 0x03, 0, 0xc2f9 },
4811 { 0x06, 0, 0xafb5 },
4812 { 0x07, 0, 0x0e00 },
4813 { 0x19, 0, 0xec80 },
4814 { 0x01, 0, 0x2e65 },
4815 { 0x01, 0, 0x6e65 }
4816 };
4817 u8 cfg1;
4818
françois romieu650e8d52011-01-03 15:08:29 +00004819 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004820
4821 RTL_W8(DBG_REG, FIX_NAK_1);
4822
4823 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4824
4825 RTL_W8(Config1,
4826 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4827 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4828
4829 cfg1 = RTL_R8(Config1);
4830 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4831 RTL_W8(Config1, cfg1 & ~LEDS0);
4832
Francois Romieu2857ffb2008-08-02 21:08:49 +02004833 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4834}
4835
4836static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4837{
françois romieu650e8d52011-01-03 15:08:29 +00004838 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004839
4840 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4841
4842 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4843 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004844}
4845
4846static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4847{
4848 rtl_hw_start_8102e_2(ioaddr, pdev);
4849
4850 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4851}
4852
Hayes Wang5a5e4442011-02-22 17:26:21 +08004853static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4854{
4855 static const struct ephy_info e_info_8105e_1[] = {
4856 { 0x07, 0, 0x4000 },
4857 { 0x19, 0, 0x0200 },
4858 { 0x19, 0, 0x0020 },
4859 { 0x1e, 0, 0x2000 },
4860 { 0x03, 0, 0x0001 },
4861 { 0x19, 0, 0x0100 },
4862 { 0x19, 0, 0x0004 },
4863 { 0x0a, 0, 0x0020 }
4864 };
4865
Francois Romieucecb5fd2011-04-01 10:21:07 +02004866 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004867 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4868
Francois Romieucecb5fd2011-04-01 10:21:07 +02004869 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004870 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4871
4872 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08004873 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004874
4875 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4876}
4877
4878static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4879{
4880 rtl_hw_start_8105e_1(ioaddr, pdev);
4881 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4882}
4883
Francois Romieu07ce4062007-02-23 23:36:39 +01004884static void rtl_hw_start_8101(struct net_device *dev)
4885{
Francois Romieucdf1a602007-06-11 23:29:50 +02004886 struct rtl8169_private *tp = netdev_priv(dev);
4887 void __iomem *ioaddr = tp->mmio_addr;
4888 struct pci_dev *pdev = tp->pci_dev;
4889
Francois Romieuda78dbf2012-01-26 14:18:23 +01004890 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
4891 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00004892
Francois Romieucecb5fd2011-04-01 10:21:07 +02004893 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4894 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00004895 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02004896
4897 if (cap) {
4898 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4899 PCI_EXP_DEVCTL_NOSNOOP_EN);
4900 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004901 }
4902
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004903 RTL_W8(Cfg9346, Cfg9346_Unlock);
4904
Francois Romieu2857ffb2008-08-02 21:08:49 +02004905 switch (tp->mac_version) {
4906 case RTL_GIGA_MAC_VER_07:
4907 rtl_hw_start_8102e_1(ioaddr, pdev);
4908 break;
4909
4910 case RTL_GIGA_MAC_VER_08:
4911 rtl_hw_start_8102e_3(ioaddr, pdev);
4912 break;
4913
4914 case RTL_GIGA_MAC_VER_09:
4915 rtl_hw_start_8102e_2(ioaddr, pdev);
4916 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004917
4918 case RTL_GIGA_MAC_VER_29:
4919 rtl_hw_start_8105e_1(ioaddr, pdev);
4920 break;
4921 case RTL_GIGA_MAC_VER_30:
4922 rtl_hw_start_8105e_2(ioaddr, pdev);
4923 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004924 }
4925
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004926 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004927
françois romieuf0298f82011-01-03 15:07:42 +00004928 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004929
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004930 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004931
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004932 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004933 RTL_W16(CPlusCmd, tp->cp_cmd);
4934
4935 RTL_W16(IntrMitigate, 0x0000);
4936
4937 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4938
4939 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4940 rtl_set_rx_tx_config_registers(tp);
4941
Francois Romieucdf1a602007-06-11 23:29:50 +02004942 RTL_R8(IntrMask);
4943
Francois Romieucdf1a602007-06-11 23:29:50 +02004944 rtl_set_rx_mode(dev);
4945
4946 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947}
4948
4949static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4950{
Francois Romieud58d46b2011-05-03 16:38:29 +02004951 struct rtl8169_private *tp = netdev_priv(dev);
4952
4953 if (new_mtu < ETH_ZLEN ||
4954 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 return -EINVAL;
4956
Francois Romieud58d46b2011-05-03 16:38:29 +02004957 if (new_mtu > ETH_DATA_LEN)
4958 rtl_hw_jumbo_enable(tp);
4959 else
4960 rtl_hw_jumbo_disable(tp);
4961
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004963 netdev_update_features(dev);
4964
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004965 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966}
4967
4968static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4969{
Al Viro95e09182007-12-22 18:55:39 +00004970 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4972}
4973
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004974static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4975 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004977 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004978 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004979
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004980 kfree(*data_buff);
4981 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 rtl8169_make_unusable_by_asic(desc);
4983}
4984
4985static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4986{
4987 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4988
4989 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4990}
4991
4992static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4993 u32 rx_buf_sz)
4994{
4995 desc->addr = cpu_to_le64(mapping);
4996 wmb();
4997 rtl8169_mark_to_asic(desc, rx_buf_sz);
4998}
4999
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005000static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005002 return (void *)ALIGN((long)data, 16);
5003}
5004
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005005static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5006 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005007{
5008 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005010 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005011 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005012 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005014 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5015 if (!data)
5016 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005017
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005018 if (rtl8169_align(data) != data) {
5019 kfree(data);
5020 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5021 if (!data)
5022 return NULL;
5023 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005024
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005025 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005026 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005027 if (unlikely(dma_mapping_error(d, mapping))) {
5028 if (net_ratelimit())
5029 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005030 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032
5033 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005034 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005035
5036err_out:
5037 kfree(data);
5038 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039}
5040
5041static void rtl8169_rx_clear(struct rtl8169_private *tp)
5042{
Francois Romieu07d3f512007-02-21 22:40:46 +01005043 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044
5045 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005046 if (tp->Rx_databuff[i]) {
5047 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 tp->RxDescArray + i);
5049 }
5050 }
5051}
5052
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005053static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005055 desc->opts1 |= cpu_to_le32(RingEnd);
5056}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005057
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005058static int rtl8169_rx_fill(struct rtl8169_private *tp)
5059{
5060 unsigned int i;
5061
5062 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005063 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005064
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005065 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005067
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005068 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005069 if (!data) {
5070 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005071 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005072 }
5073 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005076 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5077 return 0;
5078
5079err_out:
5080 rtl8169_rx_clear(tp);
5081 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082}
5083
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084static int rtl8169_init_ring(struct net_device *dev)
5085{
5086 struct rtl8169_private *tp = netdev_priv(dev);
5087
5088 rtl8169_init_ring_indexes(tp);
5089
5090 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005091 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005093 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094}
5095
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005096static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 struct TxDesc *desc)
5098{
5099 unsigned int len = tx_skb->len;
5100
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005101 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5102
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 desc->opts1 = 0x00;
5104 desc->opts2 = 0x00;
5105 desc->addr = 0x00;
5106 tx_skb->len = 0;
5107}
5108
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005109static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5110 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111{
5112 unsigned int i;
5113
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005114 for (i = 0; i < n; i++) {
5115 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 struct ring_info *tx_skb = tp->tx_skb + entry;
5117 unsigned int len = tx_skb->len;
5118
5119 if (len) {
5120 struct sk_buff *skb = tx_skb->skb;
5121
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005122 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 tp->TxDescArray + entry);
5124 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005125 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 dev_kfree_skb(skb);
5127 tx_skb->skb = NULL;
5128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 }
5130 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005131}
5132
5133static void rtl8169_tx_clear(struct rtl8169_private *tp)
5134{
5135 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 tp->cur_tx = tp->dirty_tx = 0;
Igor Maravic036dafa2012-03-05 00:01:25 +01005137 netdev_reset_queue(tp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138}
5139
Francois Romieu4422bcd2012-01-26 11:23:32 +01005140static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141{
David Howellsc4028952006-11-22 14:57:56 +00005142 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005143 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144
Francois Romieuda78dbf2012-01-26 14:18:23 +01005145 napi_disable(&tp->napi);
5146 netif_stop_queue(dev);
5147 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
françois romieuc7c2c392011-12-04 20:30:52 +00005149 rtl8169_hw_reset(tp);
5150
Francois Romieu56de4142011-03-15 17:29:31 +01005151 for (i = 0; i < NUM_RX_DESC; i++)
5152 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5153
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005155 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156
Francois Romieuda78dbf2012-01-26 14:18:23 +01005157 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01005158 rtl_hw_start(dev);
5159 netif_wake_queue(dev);
5160 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161}
5162
5163static void rtl8169_tx_timeout(struct net_device *dev)
5164{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005165 struct rtl8169_private *tp = netdev_priv(dev);
5166
5167 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168}
5169
5170static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005171 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172{
5173 struct skb_shared_info *info = skb_shinfo(skb);
5174 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005175 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005176 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177
5178 entry = tp->cur_tx;
5179 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005180 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 dma_addr_t mapping;
5182 u32 status, len;
5183 void *addr;
5184
5185 entry = (entry + 1) % NUM_TX_DESC;
5186
5187 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005188 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005189 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005190 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005191 if (unlikely(dma_mapping_error(d, mapping))) {
5192 if (net_ratelimit())
5193 netif_err(tp, drv, tp->dev,
5194 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005195 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197
Francois Romieucecb5fd2011-04-01 10:21:07 +02005198 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005199 status = opts[0] | len |
5200 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201
5202 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005203 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 txd->addr = cpu_to_le64(mapping);
5205
5206 tp->tx_skb[entry].len = len;
5207 }
5208
5209 if (cur_frag) {
5210 tp->tx_skb[entry].skb = skb;
5211 txd->opts1 |= cpu_to_le32(LastFrag);
5212 }
5213
5214 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005215
5216err_out:
5217 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5218 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219}
5220
Francois Romieu2b7b4312011-04-18 22:53:24 -07005221static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5222 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005224 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005225 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005226 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227
Francois Romieu2b7b4312011-04-18 22:53:24 -07005228 if (mss) {
5229 opts[0] |= TD_LSO;
5230 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5231 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005232 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233
5234 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005235 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005237 opts[offset] |= info->checksum.udp;
5238 else
5239 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241}
5242
Stephen Hemminger613573252009-08-31 19:50:58 +00005243static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5244 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245{
5246 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005247 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 struct TxDesc *txd = tp->TxDescArray + entry;
5249 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005250 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 dma_addr_t mapping;
5252 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005253 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005254 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005255
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005257 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005258 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 }
5260
5261 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005262 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005264 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005265 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005266 if (unlikely(dma_mapping_error(d, mapping))) {
5267 if (net_ratelimit())
5268 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005269 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271
5272 tp->tx_skb[entry].len = len;
5273 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274
Francois Romieu2b7b4312011-04-18 22:53:24 -07005275 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5276 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005277
Francois Romieu2b7b4312011-04-18 22:53:24 -07005278 rtl8169_tso_csum(tp, skb, opts);
5279
5280 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005281 if (frags < 0)
5282 goto err_dma_1;
5283 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005284 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005285 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005286 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005287 tp->tx_skb[entry].skb = skb;
5288 }
5289
Francois Romieu2b7b4312011-04-18 22:53:24 -07005290 txd->opts2 = cpu_to_le32(opts[1]);
5291
Igor Maravic036dafa2012-03-05 00:01:25 +01005292 netdev_sent_queue(dev, skb->len);
5293
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 wmb();
5295
Francois Romieucecb5fd2011-04-01 10:21:07 +02005296 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005297 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 txd->opts1 = cpu_to_le32(status);
5299
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 tp->cur_tx += frags + 1;
5301
David Dillow4c020a92010-03-03 16:33:10 +00005302 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303
Francois Romieucecb5fd2011-04-01 10:21:07 +02005304 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305
Francois Romieuda78dbf2012-01-26 14:18:23 +01005306 mmiowb();
5307
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01005309 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5310 * not miss a ring update when it notices a stopped queue.
5311 */
5312 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01005314 /* Sync with rtl_tx:
5315 * - publish queue status and cur_tx ring index (write barrier)
5316 * - refresh dirty_tx ring index (read barrier).
5317 * May the current thread have a pessimistic view of the ring
5318 * status and forget to wake up queue, a racing rtl_tx thread
5319 * can't.
5320 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005321 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
5323 netif_wake_queue(dev);
5324 }
5325
Stephen Hemminger613573252009-08-31 19:50:58 +00005326 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005328err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005329 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005330err_dma_0:
5331 dev_kfree_skb(skb);
5332 dev->stats.tx_dropped++;
5333 return NETDEV_TX_OK;
5334
5335err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005337 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005338 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339}
5340
5341static void rtl8169_pcierr_interrupt(struct net_device *dev)
5342{
5343 struct rtl8169_private *tp = netdev_priv(dev);
5344 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 u16 pci_status, pci_cmd;
5346
5347 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5348 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5349
Joe Perchesbf82c182010-02-09 11:49:50 +00005350 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5351 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352
5353 /*
5354 * The recovery sequence below admits a very elaborated explanation:
5355 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005356 * - I did not see what else could be done;
5357 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 *
5359 * Feel free to adjust to your needs.
5360 */
Francois Romieua27993f2006-12-18 00:04:19 +01005361 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005362 pci_cmd &= ~PCI_COMMAND_PARITY;
5363 else
5364 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5365
5366 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367
5368 pci_write_config_word(pdev, PCI_STATUS,
5369 pci_status & (PCI_STATUS_DETECTED_PARITY |
5370 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5371 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5372
5373 /* The infamous DAC f*ckup only happens at boot time */
5374 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005375 void __iomem *ioaddr = tp->mmio_addr;
5376
Joe Perchesbf82c182010-02-09 11:49:50 +00005377 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 tp->cp_cmd &= ~PCIDAC;
5379 RTL_W16(CPlusCmd, tp->cp_cmd);
5380 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 }
5382
françois romieue6de30d2011-01-03 15:08:37 +00005383 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005384
Francois Romieu98ddf982012-01-31 10:47:34 +01005385 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386}
5387
Igor Maravic036dafa2012-03-05 00:01:25 +01005388struct rtl_txc {
5389 int packets;
5390 int bytes;
5391};
5392
Francois Romieuda78dbf2012-01-26 14:18:23 +01005393static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394{
Igor Maravic036dafa2012-03-05 00:01:25 +01005395 struct rtl8169_stats *tx_stats = &tp->tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 unsigned int dirty_tx, tx_left;
Igor Maravic036dafa2012-03-05 00:01:25 +01005397 struct rtl_txc txc = { 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 dirty_tx = tp->dirty_tx;
5400 smp_rmb();
5401 tx_left = tp->cur_tx - dirty_tx;
5402
5403 while (tx_left > 0) {
5404 unsigned int entry = dirty_tx % NUM_TX_DESC;
5405 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 u32 status;
5407
5408 rmb();
5409 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5410 if (status & DescOwn)
5411 break;
5412
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005413 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5414 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 if (status & LastFrag) {
Igor Maravic036dafa2012-03-05 00:01:25 +01005416 struct sk_buff *skb = tx_skb->skb;
5417
5418 txc.packets++;
5419 txc.bytes += skb->len;
5420 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 tx_skb->skb = NULL;
5422 }
5423 dirty_tx++;
5424 tx_left--;
5425 }
5426
Igor Maravic036dafa2012-03-05 00:01:25 +01005427 u64_stats_update_begin(&tx_stats->syncp);
5428 tx_stats->packets += txc.packets;
5429 tx_stats->bytes += txc.bytes;
5430 u64_stats_update_end(&tx_stats->syncp);
5431
5432 netdev_completed_queue(dev, txc.packets, txc.bytes);
5433
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 if (tp->dirty_tx != dirty_tx) {
5435 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005436 /* Sync with rtl8169_start_xmit:
5437 * - publish dirty_tx ring index (write barrier)
5438 * - refresh cur_tx ring index and queue status (read barrier)
5439 * May the current thread miss the stopped queue condition,
5440 * a racing xmit thread can only have a right view of the
5441 * ring status.
5442 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005443 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 if (netif_queue_stopped(dev) &&
5445 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5446 netif_wake_queue(dev);
5447 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005448 /*
5449 * 8168 hack: TxPoll requests are lost when the Tx packets are
5450 * too close. Let's kick an extra TxPoll request when a burst
5451 * of start_xmit activity is detected (if it is not detected,
5452 * it is slow enough). -- FR
5453 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005454 if (tp->cur_tx != dirty_tx) {
5455 void __iomem *ioaddr = tp->mmio_addr;
5456
Francois Romieud78ae2d2007-08-26 20:08:19 +02005457 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 }
5460}
5461
Francois Romieu126fa4b2005-05-12 20:09:17 -04005462static inline int rtl8169_fragmented_frame(u32 status)
5463{
5464 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5465}
5466
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005467static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 u32 status = opts1 & RxProtoMask;
5470
5471 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005472 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 skb->ip_summed = CHECKSUM_UNNECESSARY;
5474 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005475 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476}
5477
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005478static struct sk_buff *rtl8169_try_rx_copy(void *data,
5479 struct rtl8169_private *tp,
5480 int pkt_size,
5481 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005483 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005484 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005486 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005487 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005488 prefetch(data);
5489 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5490 if (skb)
5491 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005492 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5493
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005494 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495}
5496
Francois Romieuda78dbf2012-01-26 14:18:23 +01005497static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498{
5499 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005500 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 cur_rx = tp->cur_rx;
5503 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005504 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005506 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005508 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 u32 status;
5510
5511 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005512 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513
5514 if (status & DescOwn)
5515 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005516 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005517 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5518 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005519 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005521 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005523 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005524 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005525 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005526 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005527 }
Ben Greear6bbe0212012-02-10 15:04:33 +00005528 if ((status & (RxRUNT | RxCRC)) &&
5529 !(status & (RxRWT | RxFOVF)) &&
5530 (dev->features & NETIF_F_RXALL))
5531 goto process_pkt;
5532
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005533 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005535 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00005536 dma_addr_t addr;
5537 int pkt_size;
5538
5539process_pkt:
5540 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00005541 if (likely(!(dev->features & NETIF_F_RXFCS)))
5542 pkt_size = (status & 0x00003fff) - 4;
5543 else
5544 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545
Francois Romieu126fa4b2005-05-12 20:09:17 -04005546 /*
5547 * The driver does not support incoming fragmented
5548 * frames. They are seen as a symptom of over-mtu
5549 * sized frames.
5550 */
5551 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005552 dev->stats.rx_dropped++;
5553 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005554 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005555 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005556 }
5557
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005558 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5559 tp, pkt_size, addr);
5560 rtl8169_mark_to_asic(desc, rx_buf_sz);
5561 if (!skb) {
5562 dev->stats.rx_dropped++;
5563 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 }
5565
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005566 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 skb_put(skb, pkt_size);
5568 skb->protocol = eth_type_trans(skb, dev);
5569
Francois Romieu7a8fc772011-03-01 17:18:33 +01005570 rtl8169_rx_vlan_tag(desc, skb);
5571
Francois Romieu56de4142011-03-15 17:29:31 +01005572 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573
Junchang Wang8027aa22012-03-04 23:30:32 +01005574 u64_stats_update_begin(&tp->rx_stats.syncp);
5575 tp->rx_stats.packets++;
5576 tp->rx_stats.bytes += pkt_size;
5577 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005579
5580 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005581 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005582 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5583 desc->opts2 = 0;
5584 cur_rx++;
5585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 }
5587
5588 count = cur_rx - tp->cur_rx;
5589 tp->cur_rx = cur_rx;
5590
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005591 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
5593 return count;
5594}
5595
Francois Romieu07d3f512007-02-21 22:40:46 +01005596static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597{
Francois Romieu07d3f512007-02-21 22:40:46 +01005598 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005601 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005603 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005604 if (status && status != 0xffff) {
5605 status &= RTL_EVENT_NAPI | tp->event_slow;
5606 if (status) {
5607 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00005608
Francois Romieuda78dbf2012-01-26 14:18:23 +01005609 rtl_irq_disable(tp);
5610 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 return IRQ_RETVAL(handled);
5614}
5615
Francois Romieuda78dbf2012-01-26 14:18:23 +01005616/*
5617 * Workqueue context.
5618 */
5619static void rtl_slow_event_work(struct rtl8169_private *tp)
5620{
5621 struct net_device *dev = tp->dev;
5622 u16 status;
5623
5624 status = rtl_get_events(tp) & tp->event_slow;
5625 rtl_ack_events(tp, status);
5626
5627 if (unlikely(status & RxFIFOOver)) {
5628 switch (tp->mac_version) {
5629 /* Work around for rx fifo overflow */
5630 case RTL_GIGA_MAC_VER_11:
5631 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01005632 /* XXX - Hack alert. See rtl_task(). */
5633 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005634 default:
5635 break;
5636 }
5637 }
5638
5639 if (unlikely(status & SYSErr))
5640 rtl8169_pcierr_interrupt(dev);
5641
5642 if (status & LinkChg)
5643 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
5644
5645 napi_disable(&tp->napi);
5646 rtl_irq_disable(tp);
5647
5648 napi_enable(&tp->napi);
5649 napi_schedule(&tp->napi);
5650}
5651
Francois Romieu4422bcd2012-01-26 11:23:32 +01005652static void rtl_task(struct work_struct *work)
5653{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005654 static const struct {
5655 int bitnr;
5656 void (*action)(struct rtl8169_private *);
5657 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01005658 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005659 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
5660 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
5661 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
5662 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01005663 struct rtl8169_private *tp =
5664 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005665 struct net_device *dev = tp->dev;
5666 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01005667
Francois Romieuda78dbf2012-01-26 14:18:23 +01005668 rtl_lock_work(tp);
5669
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005670 if (!netif_running(dev) ||
5671 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01005672 goto out_unlock;
5673
5674 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
5675 bool pending;
5676
Francois Romieuda78dbf2012-01-26 14:18:23 +01005677 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005678 if (pending)
5679 rtl_work[i].action(tp);
5680 }
5681
5682out_unlock:
5683 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01005684}
5685
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005686static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005688 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5689 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005690 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
5691 int work_done= 0;
5692 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693
Francois Romieuda78dbf2012-01-26 14:18:23 +01005694 status = rtl_get_events(tp);
5695 rtl_ack_events(tp, status & ~tp->event_slow);
5696
5697 if (status & RTL_EVENT_NAPI_RX)
5698 work_done = rtl_rx(dev, tp, (u32) budget);
5699
5700 if (status & RTL_EVENT_NAPI_TX)
5701 rtl_tx(dev, tp);
5702
5703 if (status & tp->event_slow) {
5704 enable_mask &= ~tp->event_slow;
5705
5706 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
5707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005709 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005710 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005711
Francois Romieuda78dbf2012-01-26 14:18:23 +01005712 rtl_irq_enable(tp, enable_mask);
5713 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 }
5715
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005716 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718
Francois Romieu523a6092008-09-10 22:28:56 +02005719static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5720{
5721 struct rtl8169_private *tp = netdev_priv(dev);
5722
5723 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5724 return;
5725
5726 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5727 RTL_W32(RxMissed, 0);
5728}
5729
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730static void rtl8169_down(struct net_device *dev)
5731{
5732 struct rtl8169_private *tp = netdev_priv(dev);
5733 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734
Francois Romieu4876cc12011-03-11 21:07:11 +01005735 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005737 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005738 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739
Hayes Wang92fc43b2011-07-06 15:58:03 +08005740 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005741 /*
5742 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01005743 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
5744 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005745 */
Francois Romieu523a6092008-09-10 22:28:56 +02005746 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005749 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 rtl8169_tx_clear(tp);
5752
5753 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005754
5755 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756}
5757
5758static int rtl8169_close(struct net_device *dev)
5759{
5760 struct rtl8169_private *tp = netdev_priv(dev);
5761 struct pci_dev *pdev = tp->pci_dev;
5762
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005763 pm_runtime_get_sync(&pdev->dev);
5764
Francois Romieucecb5fd2011-04-01 10:21:07 +02005765 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005766 rtl8169_update_counters(dev);
5767
Francois Romieuda78dbf2012-01-26 14:18:23 +01005768 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005769 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005770
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005772 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773
5774 free_irq(dev->irq, dev);
5775
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005776 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5777 tp->RxPhyAddr);
5778 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5779 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 tp->TxDescArray = NULL;
5781 tp->RxDescArray = NULL;
5782
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005783 pm_runtime_put_sync(&pdev->dev);
5784
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 return 0;
5786}
5787
Francois Romieu07ce4062007-02-23 23:36:39 +01005788static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789{
5790 struct rtl8169_private *tp = netdev_priv(dev);
5791 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005793 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 u32 tmp = 0;
5795
5796 if (dev->flags & IFF_PROMISC) {
5797 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005798 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 rx_mode =
5800 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5801 AcceptAllPhys;
5802 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005803 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005804 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 /* Too many to filter perfectly -- accept all multicasts. */
5806 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5807 mc_filter[1] = mc_filter[0] = 0xffffffff;
5808 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +00005809 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01005810
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 rx_mode = AcceptBroadcast | AcceptMyPhys;
5812 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00005813 netdev_for_each_mc_addr(ha, dev) {
5814 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5816 rx_mode |= AcceptMulticast;
5817 }
5818 }
5819
Ben Greear6bbe0212012-02-10 15:04:33 +00005820 if (dev->features & NETIF_F_RXALL)
5821 rx_mode |= (AcceptErr | AcceptRunt);
5822
Francois Romieu1687b562011-07-19 17:21:29 +02005823 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824
Francois Romieuf887cce2008-07-17 22:24:18 +02005825 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01005826 u32 data = mc_filter[0];
5827
5828 mc_filter[0] = swab32(mc_filter[1]);
5829 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005830 }
5831
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07005833 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834
Francois Romieu57a9f232007-06-04 22:10:15 +02005835 RTL_W32(RxConfig, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836}
5837
Junchang Wang8027aa22012-03-04 23:30:32 +01005838static struct rtnl_link_stats64 *
5839rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840{
5841 struct rtl8169_private *tp = netdev_priv(dev);
5842 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01005843 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844
Francois Romieuda78dbf2012-01-26 14:18:23 +01005845 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02005846 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02005847
Junchang Wang8027aa22012-03-04 23:30:32 +01005848 do {
5849 start = u64_stats_fetch_begin_bh(&tp->rx_stats.syncp);
5850 stats->rx_packets = tp->rx_stats.packets;
5851 stats->rx_bytes = tp->rx_stats.bytes;
5852 } while (u64_stats_fetch_retry_bh(&tp->rx_stats.syncp, start));
5853
5854
5855 do {
5856 start = u64_stats_fetch_begin_bh(&tp->tx_stats.syncp);
5857 stats->tx_packets = tp->tx_stats.packets;
5858 stats->tx_bytes = tp->tx_stats.bytes;
5859 } while (u64_stats_fetch_retry_bh(&tp->tx_stats.syncp, start));
5860
5861 stats->rx_dropped = dev->stats.rx_dropped;
5862 stats->tx_dropped = dev->stats.tx_dropped;
5863 stats->rx_length_errors = dev->stats.rx_length_errors;
5864 stats->rx_errors = dev->stats.rx_errors;
5865 stats->rx_crc_errors = dev->stats.rx_crc_errors;
5866 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
5867 stats->rx_missed_errors = dev->stats.rx_missed_errors;
5868
5869 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870}
5871
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005872static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005873{
françois romieu065c27c2011-01-03 15:08:12 +00005874 struct rtl8169_private *tp = netdev_priv(dev);
5875
Francois Romieu5d06a992006-02-23 00:47:58 +01005876 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005877 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005878
5879 netif_device_detach(dev);
5880 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005881
5882 rtl_lock_work(tp);
5883 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005884 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005885 rtl_unlock_work(tp);
5886
5887 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005888}
Francois Romieu5d06a992006-02-23 00:47:58 +01005889
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005890#ifdef CONFIG_PM
5891
5892static int rtl8169_suspend(struct device *device)
5893{
5894 struct pci_dev *pdev = to_pci_dev(device);
5895 struct net_device *dev = pci_get_drvdata(pdev);
5896
5897 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005898
Francois Romieu5d06a992006-02-23 00:47:58 +01005899 return 0;
5900}
5901
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005902static void __rtl8169_resume(struct net_device *dev)
5903{
françois romieu065c27c2011-01-03 15:08:12 +00005904 struct rtl8169_private *tp = netdev_priv(dev);
5905
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005906 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005907
5908 rtl_pll_power_up(tp);
5909
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005910 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005911
Francois Romieu98ddf982012-01-31 10:47:34 +01005912 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005913}
5914
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005915static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005916{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005917 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005918 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005919 struct rtl8169_private *tp = netdev_priv(dev);
5920
5921 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005922
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005923 if (netif_running(dev))
5924 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005925
Francois Romieu5d06a992006-02-23 00:47:58 +01005926 return 0;
5927}
5928
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005929static int rtl8169_runtime_suspend(struct device *device)
5930{
5931 struct pci_dev *pdev = to_pci_dev(device);
5932 struct net_device *dev = pci_get_drvdata(pdev);
5933 struct rtl8169_private *tp = netdev_priv(dev);
5934
5935 if (!tp->TxDescArray)
5936 return 0;
5937
Francois Romieuda78dbf2012-01-26 14:18:23 +01005938 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005939 tp->saved_wolopts = __rtl8169_get_wol(tp);
5940 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005941 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005942
5943 rtl8169_net_suspend(dev);
5944
5945 return 0;
5946}
5947
5948static int rtl8169_runtime_resume(struct device *device)
5949{
5950 struct pci_dev *pdev = to_pci_dev(device);
5951 struct net_device *dev = pci_get_drvdata(pdev);
5952 struct rtl8169_private *tp = netdev_priv(dev);
5953
5954 if (!tp->TxDescArray)
5955 return 0;
5956
Francois Romieuda78dbf2012-01-26 14:18:23 +01005957 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005958 __rtl8169_set_wol(tp, tp->saved_wolopts);
5959 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005960 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005961
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005962 rtl8169_init_phy(dev, tp);
5963
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005964 __rtl8169_resume(dev);
5965
5966 return 0;
5967}
5968
5969static int rtl8169_runtime_idle(struct device *device)
5970{
5971 struct pci_dev *pdev = to_pci_dev(device);
5972 struct net_device *dev = pci_get_drvdata(pdev);
5973 struct rtl8169_private *tp = netdev_priv(dev);
5974
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005975 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005976}
5977
Alexey Dobriyan47145212009-12-14 18:00:08 -08005978static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005979 .suspend = rtl8169_suspend,
5980 .resume = rtl8169_resume,
5981 .freeze = rtl8169_suspend,
5982 .thaw = rtl8169_resume,
5983 .poweroff = rtl8169_suspend,
5984 .restore = rtl8169_resume,
5985 .runtime_suspend = rtl8169_runtime_suspend,
5986 .runtime_resume = rtl8169_runtime_resume,
5987 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005988};
5989
5990#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5991
5992#else /* !CONFIG_PM */
5993
5994#define RTL8169_PM_OPS NULL
5995
5996#endif /* !CONFIG_PM */
5997
David S. Miller1805b2f2011-10-24 18:18:09 -04005998static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
5999{
6000 void __iomem *ioaddr = tp->mmio_addr;
6001
6002 /* WoL fails with 8168b when the receiver is disabled. */
6003 switch (tp->mac_version) {
6004 case RTL_GIGA_MAC_VER_11:
6005 case RTL_GIGA_MAC_VER_12:
6006 case RTL_GIGA_MAC_VER_17:
6007 pci_clear_master(tp->pci_dev);
6008
6009 RTL_W8(ChipCmd, CmdRxEnb);
6010 /* PCI commit */
6011 RTL_R8(ChipCmd);
6012 break;
6013 default:
6014 break;
6015 }
6016}
6017
Francois Romieu1765f952008-09-13 17:21:40 +02006018static void rtl_shutdown(struct pci_dev *pdev)
6019{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006020 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006021 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00006022 struct device *d = &pdev->dev;
6023
6024 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02006025
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006026 rtl8169_net_suspend(dev);
6027
Francois Romieucecb5fd2011-04-01 10:21:07 +02006028 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006029 rtl_rar_set(tp, dev->perm_addr);
6030
Hayes Wang92fc43b2011-07-06 15:58:03 +08006031 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006032
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006033 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006034 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6035 rtl_wol_suspend_quirk(tp);
6036 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006037 }
6038
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006039 pci_wake_from_d3(pdev, true);
6040 pci_set_power_state(pdev, PCI_D3hot);
6041 }
françois romieu2a15cd22012-03-06 01:14:12 +00006042
6043 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006044}
Francois Romieu5d06a992006-02-23 00:47:58 +01006045
Francois Romieue27566e2012-03-08 09:54:01 +01006046static void __devexit rtl_remove_one(struct pci_dev *pdev)
6047{
6048 struct net_device *dev = pci_get_drvdata(pdev);
6049 struct rtl8169_private *tp = netdev_priv(dev);
6050
6051 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6052 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6053 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6054 rtl8168_driver_stop(tp);
6055 }
6056
6057 cancel_work_sync(&tp->wk.work);
6058
6059 unregister_netdev(dev);
6060
6061 rtl_release_firmware(tp);
6062
6063 if (pci_dev_run_wake(pdev))
6064 pm_runtime_get_noresume(&pdev->dev);
6065
6066 /* restore original MAC address */
6067 rtl_rar_set(tp, dev->perm_addr);
6068
6069 rtl_disable_msi(pdev, tp);
6070 rtl8169_release_board(pdev, dev, tp->mmio_addr);
6071 pci_set_drvdata(pdev, NULL);
6072}
6073
Francois Romieu3b6cf252012-03-08 09:59:04 +01006074static int __devinit
6075rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6076{
6077 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
6078 const unsigned int region = cfg->region;
6079 struct rtl8169_private *tp;
6080 struct mii_if_info *mii;
6081 struct net_device *dev;
6082 void __iomem *ioaddr;
6083 int chipset, i;
6084 int rc;
6085
6086 if (netif_msg_drv(&debug)) {
6087 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
6088 MODULENAME, RTL8169_VERSION);
6089 }
6090
6091 dev = alloc_etherdev(sizeof (*tp));
6092 if (!dev) {
6093 rc = -ENOMEM;
6094 goto out;
6095 }
6096
6097 SET_NETDEV_DEV(dev, &pdev->dev);
6098 dev->netdev_ops = &rtl8169_netdev_ops;
6099 tp = netdev_priv(dev);
6100 tp->dev = dev;
6101 tp->pci_dev = pdev;
6102 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
6103
6104 mii = &tp->mii;
6105 mii->dev = dev;
6106 mii->mdio_read = rtl_mdio_read;
6107 mii->mdio_write = rtl_mdio_write;
6108 mii->phy_id_mask = 0x1f;
6109 mii->reg_num_mask = 0x1f;
6110 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
6111
6112 /* disable ASPM completely as that cause random device stop working
6113 * problems as well as full system hangs for some PCIe devices users */
6114 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6115 PCIE_LINK_STATE_CLKPM);
6116
6117 /* enable device (incl. PCI PM wakeup and hotplug setup) */
6118 rc = pci_enable_device(pdev);
6119 if (rc < 0) {
6120 netif_err(tp, probe, dev, "enable failure\n");
6121 goto err_out_free_dev_1;
6122 }
6123
6124 if (pci_set_mwi(pdev) < 0)
6125 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
6126
6127 /* make sure PCI base addr 1 is MMIO */
6128 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
6129 netif_err(tp, probe, dev,
6130 "region #%d not an MMIO resource, aborting\n",
6131 region);
6132 rc = -ENODEV;
6133 goto err_out_mwi_2;
6134 }
6135
6136 /* check for weird/broken PCI region reporting */
6137 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
6138 netif_err(tp, probe, dev,
6139 "Invalid PCI region size(s), aborting\n");
6140 rc = -ENODEV;
6141 goto err_out_mwi_2;
6142 }
6143
6144 rc = pci_request_regions(pdev, MODULENAME);
6145 if (rc < 0) {
6146 netif_err(tp, probe, dev, "could not request regions\n");
6147 goto err_out_mwi_2;
6148 }
6149
6150 tp->cp_cmd = RxChkSum;
6151
6152 if ((sizeof(dma_addr_t) > 4) &&
6153 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
6154 tp->cp_cmd |= PCIDAC;
6155 dev->features |= NETIF_F_HIGHDMA;
6156 } else {
6157 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6158 if (rc < 0) {
6159 netif_err(tp, probe, dev, "DMA configuration failed\n");
6160 goto err_out_free_res_3;
6161 }
6162 }
6163
6164 /* ioremap MMIO region */
6165 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
6166 if (!ioaddr) {
6167 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
6168 rc = -EIO;
6169 goto err_out_free_res_3;
6170 }
6171 tp->mmio_addr = ioaddr;
6172
6173 if (!pci_is_pcie(pdev))
6174 netif_info(tp, probe, dev, "not PCI Express\n");
6175
6176 /* Identify chip attached to board */
6177 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
6178
6179 rtl_init_rxcfg(tp);
6180
6181 rtl_irq_disable(tp);
6182
6183 rtl_hw_reset(tp);
6184
6185 rtl_ack_events(tp, 0xffff);
6186
6187 pci_set_master(pdev);
6188
6189 /*
6190 * Pretend we are using VLANs; This bypasses a nasty bug where
6191 * Interrupts stop flowing on high load on 8110SCd controllers.
6192 */
6193 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6194 tp->cp_cmd |= RxVlan;
6195
6196 rtl_init_mdio_ops(tp);
6197 rtl_init_pll_power_ops(tp);
6198 rtl_init_jumbo_ops(tp);
6199
6200 rtl8169_print_mac_version(tp);
6201
6202 chipset = tp->mac_version;
6203 tp->txd_version = rtl_chip_infos[chipset].txd_version;
6204
6205 RTL_W8(Cfg9346, Cfg9346_Unlock);
6206 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
6207 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
6208 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
6209 tp->features |= RTL_FEATURE_WOL;
6210 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
6211 tp->features |= RTL_FEATURE_WOL;
6212 tp->features |= rtl_try_msi(tp, cfg);
6213 RTL_W8(Cfg9346, Cfg9346_Lock);
6214
6215 if (rtl_tbi_enabled(tp)) {
6216 tp->set_speed = rtl8169_set_speed_tbi;
6217 tp->get_settings = rtl8169_gset_tbi;
6218 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
6219 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
6220 tp->link_ok = rtl8169_tbi_link_ok;
6221 tp->do_ioctl = rtl_tbi_ioctl;
6222 } else {
6223 tp->set_speed = rtl8169_set_speed_xmii;
6224 tp->get_settings = rtl8169_gset_xmii;
6225 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
6226 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
6227 tp->link_ok = rtl8169_xmii_link_ok;
6228 tp->do_ioctl = rtl_xmii_ioctl;
6229 }
6230
6231 mutex_init(&tp->wk.mutex);
6232
6233 /* Get MAC address */
6234 for (i = 0; i < ETH_ALEN; i++)
6235 dev->dev_addr[i] = RTL_R8(MAC0 + i);
6236 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
6237
6238 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
6239 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
6240 dev->irq = pdev->irq;
6241 dev->base_addr = (unsigned long) ioaddr;
6242
6243 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
6244
6245 /* don't enable SG, IP_CSUM and TSO by default - it might not work
6246 * properly for all devices */
6247 dev->features |= NETIF_F_RXCSUM |
6248 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6249
6250 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6251 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6252 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6253 NETIF_F_HIGHDMA;
6254
6255 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6256 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
6257 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
6258
6259 dev->hw_features |= NETIF_F_RXALL;
6260 dev->hw_features |= NETIF_F_RXFCS;
6261
6262 tp->hw_start = cfg->hw_start;
6263 tp->event_slow = cfg->event_slow;
6264
6265 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
6266 ~(RxBOVF | RxFOVF) : ~0;
6267
6268 init_timer(&tp->timer);
6269 tp->timer.data = (unsigned long) dev;
6270 tp->timer.function = rtl8169_phy_timer;
6271
6272 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
6273
6274 rc = register_netdev(dev);
6275 if (rc < 0)
6276 goto err_out_msi_4;
6277
6278 pci_set_drvdata(pdev, dev);
6279
6280 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
6281 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
6282 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
6283 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
6284 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
6285 "tx checksumming: %s]\n",
6286 rtl_chip_infos[chipset].jumbo_max,
6287 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
6288 }
6289
6290 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6291 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6292 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6293 rtl8168_driver_start(tp);
6294 }
6295
6296 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
6297
6298 if (pci_dev_run_wake(pdev))
6299 pm_runtime_put_noidle(&pdev->dev);
6300
6301 netif_carrier_off(dev);
6302
6303out:
6304 return rc;
6305
6306err_out_msi_4:
6307 rtl_disable_msi(pdev, tp);
6308 iounmap(ioaddr);
6309err_out_free_res_3:
6310 pci_release_regions(pdev);
6311err_out_mwi_2:
6312 pci_clear_mwi(pdev);
6313 pci_disable_device(pdev);
6314err_out_free_dev_1:
6315 free_netdev(dev);
6316 goto out;
6317}
6318
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319static struct pci_driver rtl8169_pci_driver = {
6320 .name = MODULENAME,
6321 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01006322 .probe = rtl_init_one,
Francois Romieue27566e2012-03-08 09:54:01 +01006323 .remove = __devexit_p(rtl_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02006324 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006325 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326};
6327
Francois Romieu07d3f512007-02-21 22:40:46 +01006328static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329{
Jeff Garzik29917622006-08-19 17:48:59 -04006330 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331}
6332
Francois Romieu07d3f512007-02-21 22:40:46 +01006333static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334{
6335 pci_unregister_driver(&rtl8169_pci_driver);
6336}
6337
6338module_init(rtl8169_init_module);
6339module_exit(rtl8169_cleanup_module);