blob: 5ce9f640fa24c6f813f5af7f66ace76267650344 [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 Wang4f6b00e2011-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 Wang4f6b00e2011-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 Wang4f6b00e2011-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 Wang4f6b00e2011-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 Wang4f6b00e2011-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 Wang4f6b00e2011-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 Wang4f6b00e2011-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
David Howells7d12e782006-10-05 14:55:46 +0100777static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100779static void rtl_hw_start(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100780static void rtl_set_rx_mode(struct net_device *dev);
Francois Romieu99f252b2007-04-02 22:59:59 +0200781static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700782static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Francois Romieuda78dbf2012-01-26 14:18:23 +0100784static void rtl_lock_work(struct rtl8169_private *tp)
785{
786 mutex_lock(&tp->wk.mutex);
787}
788
789static void rtl_unlock_work(struct rtl8169_private *tp)
790{
791 mutex_unlock(&tp->wk.mutex);
792}
793
Francois Romieud58d46b2011-05-03 16:38:29 +0200794static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
795{
796 int cap = pci_pcie_cap(pdev);
797
798 if (cap) {
799 u16 ctl;
800
801 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
802 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
803 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
804 }
805}
806
françois romieub646d902011-01-03 15:08:21 +0000807static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
808{
809 void __iomem *ioaddr = tp->mmio_addr;
810 int i;
811
812 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
813 for (i = 0; i < 20; i++) {
814 udelay(100);
815 if (RTL_R32(OCPAR) & OCPAR_FLAG)
816 break;
817 }
818 return RTL_R32(OCPDR);
819}
820
821static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
822{
823 void __iomem *ioaddr = tp->mmio_addr;
824 int i;
825
826 RTL_W32(OCPDR, data);
827 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
828 for (i = 0; i < 20; i++) {
829 udelay(100);
830 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
831 break;
832 }
833}
834
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800835static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000836{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800837 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000838 int i;
839
840 RTL_W8(ERIDR, cmd);
841 RTL_W32(ERIAR, 0x800010e8);
842 msleep(2);
843 for (i = 0; i < 5; i++) {
844 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200845 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000846 break;
847 }
848
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800849 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000850}
851
852#define OOB_CMD_RESET 0x00
853#define OOB_CMD_DRIVER_START 0x05
854#define OOB_CMD_DRIVER_STOP 0x06
855
Francois Romieucecb5fd2011-04-01 10:21:07 +0200856static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
857{
858 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
859}
860
françois romieub646d902011-01-03 15:08:21 +0000861static void rtl8168_driver_start(struct rtl8169_private *tp)
862{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200863 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000864 int i;
865
866 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
867
Francois Romieucecb5fd2011-04-01 10:21:07 +0200868 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000869
françois romieub646d902011-01-03 15:08:21 +0000870 for (i = 0; i < 10; i++) {
871 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000872 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000873 break;
874 }
875}
876
877static void rtl8168_driver_stop(struct rtl8169_private *tp)
878{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200879 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000880 int i;
881
882 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
883
Francois Romieucecb5fd2011-04-01 10:21:07 +0200884 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000885
françois romieub646d902011-01-03 15:08:21 +0000886 for (i = 0; i < 10; i++) {
887 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000888 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000889 break;
890 }
891}
892
hayeswang4804b3b2011-03-21 01:50:29 +0000893static int r8168dp_check_dash(struct rtl8169_private *tp)
894{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200895 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000896
Francois Romieucecb5fd2011-04-01 10:21:07 +0200897 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000898}
françois romieub646d902011-01-03 15:08:21 +0000899
françois romieu4da19632011-01-03 15:07:55 +0000900static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
902 int i;
903
Francois Romieua6baf3a2007-11-08 23:23:21 +0100904 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Francois Romieu23714082006-01-29 00:49:09 +0100906 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100907 /*
908 * Check if the RTL8169 has completed writing to the specified
909 * MII register.
910 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200911 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 break;
Francois Romieu23714082006-01-29 00:49:09 +0100913 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700915 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700916 * According to hardware specs a 20us delay is required after write
917 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700918 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700919 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
françois romieu4da19632011-01-03 15:07:55 +0000922static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
924 int i, value = -1;
925
Francois Romieua6baf3a2007-11-08 23:23:21 +0100926 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Francois Romieu23714082006-01-29 00:49:09 +0100928 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100929 /*
930 * Check if the RTL8169 has completed retrieving data from
931 * the specified MII register.
932 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100934 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 break;
936 }
Francois Romieu23714082006-01-29 00:49:09 +0100937 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700939 /*
940 * According to hardware specs a 20us delay is required after read
941 * complete indication, but before sending next command.
942 */
943 udelay(20);
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return value;
946}
947
françois romieuc0e45c12011-01-03 15:08:04 +0000948static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
949{
950 int i;
951
952 RTL_W32(OCPDR, data |
953 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
954 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
955 RTL_W32(EPHY_RXER_NUM, 0);
956
957 for (i = 0; i < 100; i++) {
958 mdelay(1);
959 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
960 break;
961 }
962}
963
964static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
965{
966 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
967 (value & OCPDR_DATA_MASK));
968}
969
970static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
971{
972 int i;
973
974 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
975
976 mdelay(1);
977 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
978 RTL_W32(EPHY_RXER_NUM, 0);
979
980 for (i = 0; i < 100; i++) {
981 mdelay(1);
982 if (RTL_R32(OCPAR) & OCPAR_FLAG)
983 break;
984 }
985
986 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
987}
988
françois romieue6de30d2011-01-03 15:08:37 +0000989#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
990
991static void r8168dp_2_mdio_start(void __iomem *ioaddr)
992{
993 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
994}
995
996static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
997{
998 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
999}
1000
1001static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
1002{
1003 r8168dp_2_mdio_start(ioaddr);
1004
1005 r8169_mdio_write(ioaddr, reg_addr, value);
1006
1007 r8168dp_2_mdio_stop(ioaddr);
1008}
1009
1010static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
1011{
1012 int value;
1013
1014 r8168dp_2_mdio_start(ioaddr);
1015
1016 value = r8169_mdio_read(ioaddr, reg_addr);
1017
1018 r8168dp_2_mdio_stop(ioaddr);
1019
1020 return value;
1021}
1022
françois romieu4da19632011-01-03 15:07:55 +00001023static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001024{
françois romieuc0e45c12011-01-03 15:08:04 +00001025 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001026}
1027
françois romieu4da19632011-01-03 15:07:55 +00001028static int rtl_readphy(struct rtl8169_private *tp, int location)
1029{
françois romieuc0e45c12011-01-03 15:08:04 +00001030 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +00001031}
1032
1033static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1034{
1035 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1036}
1037
1038static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001039{
1040 int val;
1041
françois romieu4da19632011-01-03 15:07:55 +00001042 val = rtl_readphy(tp, reg_addr);
1043 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001044}
1045
Francois Romieuccdffb92008-07-26 14:26:06 +02001046static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1047 int val)
1048{
1049 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001050
françois romieu4da19632011-01-03 15:07:55 +00001051 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001052}
1053
1054static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1055{
1056 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001057
françois romieu4da19632011-01-03 15:07:55 +00001058 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001059}
1060
Francois Romieudacf8152008-08-02 20:44:13 +02001061static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
1062{
1063 unsigned int i;
1064
1065 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1066 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1067
1068 for (i = 0; i < 100; i++) {
1069 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1070 break;
1071 udelay(10);
1072 }
1073}
1074
1075static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1076{
1077 u16 value = 0xffff;
1078 unsigned int i;
1079
1080 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1081
1082 for (i = 0; i < 100; i++) {
1083 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1084 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1085 break;
1086 }
1087 udelay(10);
1088 }
1089
1090 return value;
1091}
1092
1093static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1094{
1095 unsigned int i;
1096
1097 RTL_W32(CSIDR, value);
1098 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1099 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1100
1101 for (i = 0; i < 100; i++) {
1102 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1103 break;
1104 udelay(10);
1105 }
1106}
1107
1108static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1109{
1110 u32 value = ~0x00;
1111 unsigned int i;
1112
1113 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1114 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1115
1116 for (i = 0; i < 100; i++) {
1117 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1118 value = RTL_R32(CSIDR);
1119 break;
1120 }
1121 udelay(10);
1122 }
1123
1124 return value;
1125}
1126
Hayes Wang133ac402011-07-06 15:58:05 +08001127static
1128void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1129{
1130 unsigned int i;
1131
1132 BUG_ON((addr & 3) || (mask == 0));
1133 RTL_W32(ERIDR, val);
1134 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1135
1136 for (i = 0; i < 100; i++) {
1137 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1138 break;
1139 udelay(100);
1140 }
1141}
1142
1143static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1144{
1145 u32 value = ~0x00;
1146 unsigned int i;
1147
1148 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1149
1150 for (i = 0; i < 100; i++) {
1151 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1152 value = RTL_R32(ERIDR);
1153 break;
1154 }
1155 udelay(100);
1156 }
1157
1158 return value;
1159}
1160
1161static void
1162rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1163{
1164 u32 val;
1165
1166 val = rtl_eri_read(ioaddr, addr, type);
1167 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1168}
1169
françois romieuc28aa382011-08-02 03:53:43 +00001170struct exgmac_reg {
1171 u16 addr;
1172 u16 mask;
1173 u32 val;
1174};
1175
1176static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1177 const struct exgmac_reg *r, int len)
1178{
1179 while (len-- > 0) {
1180 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1181 r++;
1182 }
1183}
1184
françois romieudaf9df62009-10-07 12:44:20 +00001185static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1186{
1187 u8 value = 0xff;
1188 unsigned int i;
1189
1190 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1191
1192 for (i = 0; i < 300; i++) {
1193 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1194 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1195 break;
1196 }
1197 udelay(100);
1198 }
1199
1200 return value;
1201}
1202
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001203static u16 rtl_get_events(struct rtl8169_private *tp)
1204{
1205 void __iomem *ioaddr = tp->mmio_addr;
1206
1207 return RTL_R16(IntrStatus);
1208}
1209
1210static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1211{
1212 void __iomem *ioaddr = tp->mmio_addr;
1213
1214 RTL_W16(IntrStatus, bits);
1215 mmiowb();
1216}
1217
1218static void rtl_irq_disable(struct rtl8169_private *tp)
1219{
1220 void __iomem *ioaddr = tp->mmio_addr;
1221
1222 RTL_W16(IntrMask, 0);
1223 mmiowb();
1224}
1225
Francois Romieu3e990ff2012-01-26 12:50:01 +01001226static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1227{
1228 void __iomem *ioaddr = tp->mmio_addr;
1229
1230 RTL_W16(IntrMask, bits);
1231}
1232
Francois Romieuda78dbf2012-01-26 14:18:23 +01001233#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1234#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1235#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1236
1237static void rtl_irq_enable_all(struct rtl8169_private *tp)
1238{
1239 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1240}
1241
françois romieu811fd302011-12-04 20:30:45 +00001242static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
françois romieu811fd302011-12-04 20:30:45 +00001244 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001246 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001247 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001248 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
françois romieu4da19632011-01-03 15:07:55 +00001251static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
françois romieu4da19632011-01-03 15:07:55 +00001253 void __iomem *ioaddr = tp->mmio_addr;
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 return RTL_R32(TBICSR) & TBIReset;
1256}
1257
françois romieu4da19632011-01-03 15:07:55 +00001258static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
françois romieu4da19632011-01-03 15:07:55 +00001260 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
1263static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1264{
1265 return RTL_R32(TBICSR) & TBILinkOk;
1266}
1267
1268static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1269{
1270 return RTL_R8(PHYstatus) & LinkStatus;
1271}
1272
françois romieu4da19632011-01-03 15:07:55 +00001273static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
françois romieu4da19632011-01-03 15:07:55 +00001275 void __iomem *ioaddr = tp->mmio_addr;
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1278}
1279
françois romieu4da19632011-01-03 15:07:55 +00001280static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
1282 unsigned int val;
1283
françois romieu4da19632011-01-03 15:07:55 +00001284 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1285 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
Hayes Wang70090422011-07-06 15:58:06 +08001288static void rtl_link_chg_patch(struct rtl8169_private *tp)
1289{
1290 void __iomem *ioaddr = tp->mmio_addr;
1291 struct net_device *dev = tp->dev;
1292
1293 if (!netif_running(dev))
1294 return;
1295
1296 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1297 if (RTL_R8(PHYstatus) & _1000bpsF) {
1298 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1299 0x00000011, ERIAR_EXGMAC);
1300 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1301 0x00000005, ERIAR_EXGMAC);
1302 } else if (RTL_R8(PHYstatus) & _100bps) {
1303 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1304 0x0000001f, ERIAR_EXGMAC);
1305 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1306 0x00000005, ERIAR_EXGMAC);
1307 } else {
1308 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1309 0x0000001f, ERIAR_EXGMAC);
1310 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1311 0x0000003f, ERIAR_EXGMAC);
1312 }
1313 /* Reset packet filter */
1314 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1315 ERIAR_EXGMAC);
1316 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1317 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001318 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1319 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1320 if (RTL_R8(PHYstatus) & _1000bpsF) {
1321 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1322 0x00000011, ERIAR_EXGMAC);
1323 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1324 0x00000005, ERIAR_EXGMAC);
1325 } else {
1326 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1327 0x0000001f, ERIAR_EXGMAC);
1328 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1329 0x0000003f, ERIAR_EXGMAC);
1330 }
Hayes Wang70090422011-07-06 15:58:06 +08001331 }
1332}
1333
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001334static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001335 struct rtl8169_private *tp,
1336 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001339 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001340 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001341 if (pm)
1342 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001344 if (net_ratelimit())
1345 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001346 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001348 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001349 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001350 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}
1353
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001354static void rtl8169_check_link_status(struct net_device *dev,
1355 struct rtl8169_private *tp,
1356 void __iomem *ioaddr)
1357{
1358 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1359}
1360
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001361#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1362
1363static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1364{
1365 void __iomem *ioaddr = tp->mmio_addr;
1366 u8 options;
1367 u32 wolopts = 0;
1368
1369 options = RTL_R8(Config1);
1370 if (!(options & PMEnable))
1371 return 0;
1372
1373 options = RTL_R8(Config3);
1374 if (options & LinkUp)
1375 wolopts |= WAKE_PHY;
1376 if (options & MagicPacket)
1377 wolopts |= WAKE_MAGIC;
1378
1379 options = RTL_R8(Config5);
1380 if (options & UWF)
1381 wolopts |= WAKE_UCAST;
1382 if (options & BWF)
1383 wolopts |= WAKE_BCAST;
1384 if (options & MWF)
1385 wolopts |= WAKE_MCAST;
1386
1387 return wolopts;
1388}
1389
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001390static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1391{
1392 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001393
Francois Romieuda78dbf2012-01-26 14:18:23 +01001394 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001395
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001396 wol->supported = WAKE_ANY;
1397 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001398
Francois Romieuda78dbf2012-01-26 14:18:23 +01001399 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001400}
1401
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001402static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001403{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001404 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001405 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001406 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001407 u32 opt;
1408 u16 reg;
1409 u8 mask;
1410 } cfg[] = {
1411 { WAKE_ANY, Config1, PMEnable },
1412 { WAKE_PHY, Config3, LinkUp },
1413 { WAKE_MAGIC, Config3, MagicPacket },
1414 { WAKE_UCAST, Config5, UWF },
1415 { WAKE_BCAST, Config5, BWF },
1416 { WAKE_MCAST, Config5, MWF },
1417 { WAKE_ANY, Config5, LanWake }
1418 };
1419
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001420 RTL_W8(Cfg9346, Cfg9346_Unlock);
1421
1422 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1423 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001424 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001425 options |= cfg[i].mask;
1426 RTL_W8(cfg[i].reg, options);
1427 }
1428
1429 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001430}
1431
1432static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1433{
1434 struct rtl8169_private *tp = netdev_priv(dev);
1435
Francois Romieuda78dbf2012-01-26 14:18:23 +01001436 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001437
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001438 if (wol->wolopts)
1439 tp->features |= RTL_FEATURE_WOL;
1440 else
1441 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001442 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001443
1444 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001445
françois romieuea809072010-11-08 13:23:58 +00001446 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1447
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001448 return 0;
1449}
1450
Francois Romieu31bd2042011-04-26 18:58:59 +02001451static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1452{
Francois Romieu85bffe62011-04-27 08:22:39 +02001453 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001454}
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456static void rtl8169_get_drvinfo(struct net_device *dev,
1457 struct ethtool_drvinfo *info)
1458{
1459 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001460 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Rick Jones68aad782011-11-07 13:29:27 +00001462 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1463 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1464 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001465 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001466 if (!IS_ERR_OR_NULL(rtl_fw))
1467 strlcpy(info->fw_version, rtl_fw->version,
1468 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
1471static int rtl8169_get_regs_len(struct net_device *dev)
1472{
1473 return R8169_REGS_SIZE;
1474}
1475
1476static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001477 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
1479 struct rtl8169_private *tp = netdev_priv(dev);
1480 void __iomem *ioaddr = tp->mmio_addr;
1481 int ret = 0;
1482 u32 reg;
1483
1484 reg = RTL_R32(TBICSR);
1485 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1486 (duplex == DUPLEX_FULL)) {
1487 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1488 } else if (autoneg == AUTONEG_ENABLE)
1489 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1490 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001491 netif_warn(tp, link, dev,
1492 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 ret = -EOPNOTSUPP;
1494 }
1495
1496 return ret;
1497}
1498
1499static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001500 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
1502 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001503 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001504 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Hayes Wang716b50a2011-02-22 17:26:18 +08001506 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001509 int auto_nego;
1510
françois romieu4da19632011-01-03 15:07:55 +00001511 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001512 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1513 ADVERTISE_100HALF | ADVERTISE_100FULL);
1514
1515 if (adv & ADVERTISED_10baseT_Half)
1516 auto_nego |= ADVERTISE_10HALF;
1517 if (adv & ADVERTISED_10baseT_Full)
1518 auto_nego |= ADVERTISE_10FULL;
1519 if (adv & ADVERTISED_100baseT_Half)
1520 auto_nego |= ADVERTISE_100HALF;
1521 if (adv & ADVERTISED_100baseT_Full)
1522 auto_nego |= ADVERTISE_100FULL;
1523
françois romieu3577aa12009-05-19 10:46:48 +00001524 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1525
françois romieu4da19632011-01-03 15:07:55 +00001526 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001527 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1528
1529 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001530 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001531 if (adv & ADVERTISED_1000baseT_Half)
1532 giga_ctrl |= ADVERTISE_1000HALF;
1533 if (adv & ADVERTISED_1000baseT_Full)
1534 giga_ctrl |= ADVERTISE_1000FULL;
1535 } else if (adv & (ADVERTISED_1000baseT_Half |
1536 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001537 netif_info(tp, link, dev,
1538 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001539 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
françois romieu3577aa12009-05-19 10:46:48 +00001542 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001543
françois romieu4da19632011-01-03 15:07:55 +00001544 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1545 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001546 } else {
1547 giga_ctrl = 0;
1548
1549 if (speed == SPEED_10)
1550 bmcr = 0;
1551 else if (speed == SPEED_100)
1552 bmcr = BMCR_SPEED100;
1553 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001554 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001555
1556 if (duplex == DUPLEX_FULL)
1557 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001558 }
1559
françois romieu4da19632011-01-03 15:07:55 +00001560 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001561
Francois Romieucecb5fd2011-04-01 10:21:07 +02001562 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1563 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001564 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001565 rtl_writephy(tp, 0x17, 0x2138);
1566 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001567 } else {
françois romieu4da19632011-01-03 15:07:55 +00001568 rtl_writephy(tp, 0x17, 0x2108);
1569 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001570 }
1571 }
1572
Oliver Neukum54405cd2011-01-06 21:55:13 +01001573 rc = 0;
1574out:
1575 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
1577
1578static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001579 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
1581 struct rtl8169_private *tp = netdev_priv(dev);
1582 int ret;
1583
Oliver Neukum54405cd2011-01-06 21:55:13 +01001584 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001585 if (ret < 0)
1586 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Francois Romieu4876cc12011-03-11 21:07:11 +01001588 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1589 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001591 }
1592out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 return ret;
1594}
1595
1596static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1597{
1598 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 int ret;
1600
Francois Romieu4876cc12011-03-11 21:07:11 +01001601 del_timer_sync(&tp->timer);
1602
Francois Romieuda78dbf2012-01-26 14:18:23 +01001603 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001604 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001605 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001606 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return ret;
1609}
1610
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001611static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1612 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
Francois Romieud58d46b2011-05-03 16:38:29 +02001614 struct rtl8169_private *tp = netdev_priv(dev);
1615
Francois Romieu2b7b4312011-04-18 22:53:24 -07001616 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001617 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Francois Romieud58d46b2011-05-03 16:38:29 +02001619 if (dev->mtu > JUMBO_1K &&
1620 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1621 features &= ~NETIF_F_IP_CSUM;
1622
Michał Mirosław350fb322011-04-08 06:35:56 +00001623 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624}
1625
Francois Romieuda78dbf2012-01-26 14:18:23 +01001626static void __rtl8169_set_features(struct net_device *dev,
1627 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
1629 struct rtl8169_private *tp = netdev_priv(dev);
Ben Greear6bbe0212012-02-10 15:04:33 +00001630 netdev_features_t changed = features ^ dev->features;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001631 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Ben Greear6bbe0212012-02-10 15:04:33 +00001633 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)))
1634 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Ben Greear6bbe0212012-02-10 15:04:33 +00001636 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)) {
1637 if (features & NETIF_F_RXCSUM)
1638 tp->cp_cmd |= RxChkSum;
1639 else
1640 tp->cp_cmd &= ~RxChkSum;
Michał Mirosław350fb322011-04-08 06:35:56 +00001641
Ben Greear6bbe0212012-02-10 15:04:33 +00001642 if (dev->features & NETIF_F_HW_VLAN_RX)
1643 tp->cp_cmd |= RxVlan;
1644 else
1645 tp->cp_cmd &= ~RxVlan;
1646
1647 RTL_W16(CPlusCmd, tp->cp_cmd);
1648 RTL_R16(CPlusCmd);
1649 }
1650 if (changed & NETIF_F_RXALL) {
1651 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1652 if (features & NETIF_F_RXALL)
1653 tmp |= (AcceptErr | AcceptRunt);
1654 RTL_W32(RxConfig, tmp);
1655 }
Francois Romieuda78dbf2012-01-26 14:18:23 +01001656}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Francois Romieuda78dbf2012-01-26 14:18:23 +01001658static int rtl8169_set_features(struct net_device *dev,
1659 netdev_features_t features)
1660{
1661 struct rtl8169_private *tp = netdev_priv(dev);
1662
1663 rtl_lock_work(tp);
1664 __rtl8169_set_features(dev, features);
1665 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 return 0;
1668}
1669
Francois Romieuda78dbf2012-01-26 14:18:23 +01001670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1672 struct sk_buff *skb)
1673{
Jesse Grosseab6d182010-10-20 13:56:03 +00001674 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1676}
1677
Francois Romieu7a8fc772011-03-01 17:18:33 +01001678static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
1680 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Francois Romieu7a8fc772011-03-01 17:18:33 +01001682 if (opts2 & RxVlanTag)
1683 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
Francois Romieuccdffb92008-07-26 14:26:06 +02001688static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
1690 struct rtl8169_private *tp = netdev_priv(dev);
1691 void __iomem *ioaddr = tp->mmio_addr;
1692 u32 status;
1693
1694 cmd->supported =
1695 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1696 cmd->port = PORT_FIBRE;
1697 cmd->transceiver = XCVR_INTERNAL;
1698
1699 status = RTL_R32(TBICSR);
1700 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1701 cmd->autoneg = !!(status & TBINwEnable);
1702
David Decotigny70739492011-04-27 18:32:40 +00001703 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001705
1706 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708
Francois Romieuccdffb92008-07-26 14:26:06 +02001709static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
1711 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Francois Romieuccdffb92008-07-26 14:26:06 +02001713 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714}
1715
1716static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1717{
1718 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001719 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Francois Romieuda78dbf2012-01-26 14:18:23 +01001721 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02001722 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001723 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Francois Romieuccdffb92008-07-26 14:26:06 +02001725 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
1728static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1729 void *p)
1730{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001731 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Francois Romieu5b0384f2006-08-16 16:00:01 +02001733 if (regs->len > R8169_REGS_SIZE)
1734 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Francois Romieuda78dbf2012-01-26 14:18:23 +01001736 rtl_lock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001737 memcpy_fromio(p, tp->mmio_addr, regs->len);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001738 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739}
1740
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001741static u32 rtl8169_get_msglevel(struct net_device *dev)
1742{
1743 struct rtl8169_private *tp = netdev_priv(dev);
1744
1745 return tp->msg_enable;
1746}
1747
1748static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1749{
1750 struct rtl8169_private *tp = netdev_priv(dev);
1751
1752 tp->msg_enable = value;
1753}
1754
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001755static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1756 "tx_packets",
1757 "rx_packets",
1758 "tx_errors",
1759 "rx_errors",
1760 "rx_missed",
1761 "align_errors",
1762 "tx_single_collisions",
1763 "tx_multi_collisions",
1764 "unicast",
1765 "broadcast",
1766 "multicast",
1767 "tx_aborted",
1768 "tx_underrun",
1769};
1770
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001771static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001772{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001773 switch (sset) {
1774 case ETH_SS_STATS:
1775 return ARRAY_SIZE(rtl8169_gstrings);
1776 default:
1777 return -EOPNOTSUPP;
1778 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001779}
1780
Ivan Vecera355423d2009-02-06 21:49:57 -08001781static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001782{
1783 struct rtl8169_private *tp = netdev_priv(dev);
1784 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001785 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001786 struct rtl8169_counters *counters;
1787 dma_addr_t paddr;
1788 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001789 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001790
Ivan Vecera355423d2009-02-06 21:49:57 -08001791 /*
1792 * Some chips are unable to dump tally counters when the receiver
1793 * is disabled.
1794 */
1795 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1796 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001797
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001798 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001799 if (!counters)
1800 return;
1801
1802 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001803 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001804 RTL_W32(CounterAddrLow, cmd);
1805 RTL_W32(CounterAddrLow, cmd | CounterDump);
1806
Ivan Vecera355423d2009-02-06 21:49:57 -08001807 while (wait--) {
1808 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001809 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001810 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001811 }
1812 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001813 }
1814
1815 RTL_W32(CounterAddrLow, 0);
1816 RTL_W32(CounterAddrHigh, 0);
1817
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001818 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001819}
1820
Ivan Vecera355423d2009-02-06 21:49:57 -08001821static void rtl8169_get_ethtool_stats(struct net_device *dev,
1822 struct ethtool_stats *stats, u64 *data)
1823{
1824 struct rtl8169_private *tp = netdev_priv(dev);
1825
1826 ASSERT_RTNL();
1827
1828 rtl8169_update_counters(dev);
1829
1830 data[0] = le64_to_cpu(tp->counters.tx_packets);
1831 data[1] = le64_to_cpu(tp->counters.rx_packets);
1832 data[2] = le64_to_cpu(tp->counters.tx_errors);
1833 data[3] = le32_to_cpu(tp->counters.rx_errors);
1834 data[4] = le16_to_cpu(tp->counters.rx_missed);
1835 data[5] = le16_to_cpu(tp->counters.align_errors);
1836 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1837 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1838 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1839 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1840 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1841 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1842 data[12] = le16_to_cpu(tp->counters.tx_underun);
1843}
1844
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001845static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1846{
1847 switch(stringset) {
1848 case ETH_SS_STATS:
1849 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1850 break;
1851 }
1852}
1853
Jeff Garzik7282d492006-09-13 14:30:00 -04001854static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 .get_drvinfo = rtl8169_get_drvinfo,
1856 .get_regs_len = rtl8169_get_regs_len,
1857 .get_link = ethtool_op_get_link,
1858 .get_settings = rtl8169_get_settings,
1859 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001860 .get_msglevel = rtl8169_get_msglevel,
1861 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001863 .get_wol = rtl8169_get_wol,
1864 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001865 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001866 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001867 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868};
1869
Francois Romieu07d3f512007-02-21 22:40:46 +01001870static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001871 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
Francois Romieu5d320a22011-05-08 17:47:36 +02001873 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001874 /*
1875 * The driver currently handles the 8168Bf and the 8168Be identically
1876 * but they can be identified more specifically through the test below
1877 * if needed:
1878 *
1879 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001880 *
1881 * Same thing for the 8101Eb and the 8101Ec:
1882 *
1883 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001884 */
Francois Romieu37441002011-06-17 22:58:54 +02001885 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001887 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 int mac_version;
1889 } mac_info[] = {
Hayes Wangc2218922011-09-06 16:55:18 +08001890 /* 8168F family. */
1891 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1892 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1893
hayeswang01dc7fe2011-03-21 01:50:28 +00001894 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001895 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001896 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1897 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1898 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1899
Francois Romieu5b538df2008-07-20 16:22:45 +02001900 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001901 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1902 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001903 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001904
françois romieue6de30d2011-01-03 15:08:37 +00001905 /* 8168DP family. */
1906 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1907 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001908 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001909
Francois Romieuef808d52008-06-29 13:10:54 +02001910 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001911 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001912 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001913 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001914 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001915 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1916 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001917 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001918 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001919 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001920
1921 /* 8168B family. */
1922 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1923 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1924 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1925 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1926
1927 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001928 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001929 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1930 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1931 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001932 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1933 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1934 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1935 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1936 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1937 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001938 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001939 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001940 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001941 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1942 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001943 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1944 /* FIXME: where did these entries come from ? -- FR */
1945 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1946 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1947
1948 /* 8110 family. */
1949 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1950 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1951 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1952 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1953 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1954 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1955
Jean Delvaref21b75e2009-05-26 20:54:48 -07001956 /* Catch-all */
1957 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001958 };
1959 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 u32 reg;
1961
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001962 reg = RTL_R32(TxConfig);
1963 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 p++;
1965 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001966
1967 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1968 netif_notice(tp, probe, dev,
1969 "unknown MAC, using family default\n");
1970 tp->mac_version = default_version;
1971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
1974static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1975{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001976 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
Francois Romieu867763c2007-08-17 18:21:58 +02001979struct phy_reg {
1980 u16 reg;
1981 u16 val;
1982};
1983
françois romieu4da19632011-01-03 15:07:55 +00001984static void rtl_writephy_batch(struct rtl8169_private *tp,
1985 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001986{
1987 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001988 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001989 regs++;
1990 }
1991}
1992
françois romieubca03d52011-01-03 15:07:31 +00001993#define PHY_READ 0x00000000
1994#define PHY_DATA_OR 0x10000000
1995#define PHY_DATA_AND 0x20000000
1996#define PHY_BJMPN 0x30000000
1997#define PHY_READ_EFUSE 0x40000000
1998#define PHY_READ_MAC_BYTE 0x50000000
1999#define PHY_WRITE_MAC_BYTE 0x60000000
2000#define PHY_CLEAR_READCOUNT 0x70000000
2001#define PHY_WRITE 0x80000000
2002#define PHY_READCOUNT_EQ_SKIP 0x90000000
2003#define PHY_COMP_EQ_SKIPN 0xa0000000
2004#define PHY_COMP_NEQ_SKIPN 0xb0000000
2005#define PHY_WRITE_PREVIOUS 0xc0000000
2006#define PHY_SKIPN 0xd0000000
2007#define PHY_DELAY_MS 0xe0000000
2008#define PHY_WRITE_ERI_WORD 0xf0000000
2009
Hayes Wang960aee62011-06-18 11:37:48 +02002010struct fw_info {
2011 u32 magic;
2012 char version[RTL_VER_SIZE];
2013 __le32 fw_start;
2014 __le32 fw_len;
2015 u8 chksum;
2016} __packed;
2017
Francois Romieu1c361ef2011-06-17 17:16:24 +02002018#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2019
2020static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002021{
Francois Romieub6ffd972011-06-17 17:00:05 +02002022 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002023 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002024 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2025 char *version = rtl_fw->version;
2026 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002027
Francois Romieu1c361ef2011-06-17 17:16:24 +02002028 if (fw->size < FW_OPCODE_SIZE)
2029 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002030
2031 if (!fw_info->magic) {
2032 size_t i, size, start;
2033 u8 checksum = 0;
2034
2035 if (fw->size < sizeof(*fw_info))
2036 goto out;
2037
2038 for (i = 0; i < fw->size; i++)
2039 checksum += fw->data[i];
2040 if (checksum != 0)
2041 goto out;
2042
2043 start = le32_to_cpu(fw_info->fw_start);
2044 if (start > fw->size)
2045 goto out;
2046
2047 size = le32_to_cpu(fw_info->fw_len);
2048 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2049 goto out;
2050
2051 memcpy(version, fw_info->version, RTL_VER_SIZE);
2052
2053 pa->code = (__le32 *)(fw->data + start);
2054 pa->size = size;
2055 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002056 if (fw->size % FW_OPCODE_SIZE)
2057 goto out;
2058
2059 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2060
2061 pa->code = (__le32 *)fw->data;
2062 pa->size = fw->size / FW_OPCODE_SIZE;
2063 }
2064 version[RTL_VER_SIZE - 1] = 0;
2065
2066 rc = true;
2067out:
2068 return rc;
2069}
2070
Francois Romieufd112f22011-06-18 00:10:29 +02002071static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2072 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002073{
Francois Romieufd112f22011-06-18 00:10:29 +02002074 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002075 size_t index;
2076
Francois Romieu1c361ef2011-06-17 17:16:24 +02002077 for (index = 0; index < pa->size; index++) {
2078 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002079 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002080
hayeswang42b82dc2011-01-10 02:07:25 +00002081 switch(action & 0xf0000000) {
2082 case PHY_READ:
2083 case PHY_DATA_OR:
2084 case PHY_DATA_AND:
2085 case PHY_READ_EFUSE:
2086 case PHY_CLEAR_READCOUNT:
2087 case PHY_WRITE:
2088 case PHY_WRITE_PREVIOUS:
2089 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002090 break;
2091
hayeswang42b82dc2011-01-10 02:07:25 +00002092 case PHY_BJMPN:
2093 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002094 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002095 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002096 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002097 }
2098 break;
2099 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002100 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002101 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002102 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002103 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002104 }
2105 break;
2106 case PHY_COMP_EQ_SKIPN:
2107 case PHY_COMP_NEQ_SKIPN:
2108 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002109 if (index + 1 + regno >= 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
2116 case PHY_READ_MAC_BYTE:
2117 case PHY_WRITE_MAC_BYTE:
2118 case PHY_WRITE_ERI_WORD:
2119 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002120 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002121 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002122 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002123 }
2124 }
Francois Romieufd112f22011-06-18 00:10:29 +02002125 rc = true;
2126out:
2127 return rc;
2128}
françois romieubca03d52011-01-03 15:07:31 +00002129
Francois Romieufd112f22011-06-18 00:10:29 +02002130static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2131{
2132 struct net_device *dev = tp->dev;
2133 int rc = -EINVAL;
2134
2135 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2136 netif_err(tp, ifup, dev, "invalid firwmare\n");
2137 goto out;
2138 }
2139
2140 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2141 rc = 0;
2142out:
2143 return rc;
2144}
2145
2146static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2147{
2148 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2149 u32 predata, count;
2150 size_t index;
2151
2152 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002153
Francois Romieu1c361ef2011-06-17 17:16:24 +02002154 for (index = 0; index < pa->size; ) {
2155 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002156 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002157 u32 regno = (action & 0x0fff0000) >> 16;
2158
2159 if (!action)
2160 break;
françois romieubca03d52011-01-03 15:07:31 +00002161
2162 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002163 case PHY_READ:
2164 predata = rtl_readphy(tp, regno);
2165 count++;
2166 index++;
françois romieubca03d52011-01-03 15:07:31 +00002167 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002168 case PHY_DATA_OR:
2169 predata |= data;
2170 index++;
2171 break;
2172 case PHY_DATA_AND:
2173 predata &= data;
2174 index++;
2175 break;
2176 case PHY_BJMPN:
2177 index -= regno;
2178 break;
2179 case PHY_READ_EFUSE:
2180 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2181 index++;
2182 break;
2183 case PHY_CLEAR_READCOUNT:
2184 count = 0;
2185 index++;
2186 break;
2187 case PHY_WRITE:
2188 rtl_writephy(tp, regno, data);
2189 index++;
2190 break;
2191 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002192 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002193 break;
2194 case PHY_COMP_EQ_SKIPN:
2195 if (predata == data)
2196 index += regno;
2197 index++;
2198 break;
2199 case PHY_COMP_NEQ_SKIPN:
2200 if (predata != data)
2201 index += regno;
2202 index++;
2203 break;
2204 case PHY_WRITE_PREVIOUS:
2205 rtl_writephy(tp, regno, predata);
2206 index++;
2207 break;
2208 case PHY_SKIPN:
2209 index += regno + 1;
2210 break;
2211 case PHY_DELAY_MS:
2212 mdelay(data);
2213 index++;
2214 break;
2215
2216 case PHY_READ_MAC_BYTE:
2217 case PHY_WRITE_MAC_BYTE:
2218 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002219 default:
2220 BUG();
2221 }
2222 }
2223}
2224
françois romieuf1e02ed2011-01-13 13:07:53 +00002225static void rtl_release_firmware(struct rtl8169_private *tp)
2226{
Francois Romieub6ffd972011-06-17 17:00:05 +02002227 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2228 release_firmware(tp->rtl_fw->fw);
2229 kfree(tp->rtl_fw);
2230 }
2231 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002232}
2233
François Romieu953a12c2011-04-24 17:38:48 +02002234static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002235{
Francois Romieub6ffd972011-06-17 17:00:05 +02002236 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002237
2238 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002239 if (!IS_ERR_OR_NULL(rtl_fw))
2240 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002241}
2242
2243static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2244{
2245 if (rtl_readphy(tp, reg) != val)
2246 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2247 else
2248 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002249}
2250
françois romieu4da19632011-01-03 15:07:55 +00002251static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002253 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002254 { 0x1f, 0x0001 },
2255 { 0x06, 0x006e },
2256 { 0x08, 0x0708 },
2257 { 0x15, 0x4000 },
2258 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
françois romieu0b9b5712009-08-10 19:44:56 +00002260 { 0x1f, 0x0001 },
2261 { 0x03, 0x00a1 },
2262 { 0x02, 0x0008 },
2263 { 0x01, 0x0120 },
2264 { 0x00, 0x1000 },
2265 { 0x04, 0x0800 },
2266 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
françois romieu0b9b5712009-08-10 19:44:56 +00002268 { 0x03, 0xff41 },
2269 { 0x02, 0xdf60 },
2270 { 0x01, 0x0140 },
2271 { 0x00, 0x0077 },
2272 { 0x04, 0x7800 },
2273 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
françois romieu0b9b5712009-08-10 19:44:56 +00002275 { 0x03, 0x802f },
2276 { 0x02, 0x4f02 },
2277 { 0x01, 0x0409 },
2278 { 0x00, 0xf0f9 },
2279 { 0x04, 0x9800 },
2280 { 0x04, 0x9000 },
2281
2282 { 0x03, 0xdf01 },
2283 { 0x02, 0xdf20 },
2284 { 0x01, 0xff95 },
2285 { 0x00, 0xba00 },
2286 { 0x04, 0xa800 },
2287 { 0x04, 0xa000 },
2288
2289 { 0x03, 0xff41 },
2290 { 0x02, 0xdf20 },
2291 { 0x01, 0x0140 },
2292 { 0x00, 0x00bb },
2293 { 0x04, 0xb800 },
2294 { 0x04, 0xb000 },
2295
2296 { 0x03, 0xdf41 },
2297 { 0x02, 0xdc60 },
2298 { 0x01, 0x6340 },
2299 { 0x00, 0x007d },
2300 { 0x04, 0xd800 },
2301 { 0x04, 0xd000 },
2302
2303 { 0x03, 0xdf01 },
2304 { 0x02, 0xdf20 },
2305 { 0x01, 0x100a },
2306 { 0x00, 0xa0ff },
2307 { 0x04, 0xf800 },
2308 { 0x04, 0xf000 },
2309
2310 { 0x1f, 0x0000 },
2311 { 0x0b, 0x0000 },
2312 { 0x00, 0x9200 }
2313 };
2314
françois romieu4da19632011-01-03 15:07:55 +00002315 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316}
2317
françois romieu4da19632011-01-03 15:07:55 +00002318static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002319{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002320 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002321 { 0x1f, 0x0002 },
2322 { 0x01, 0x90d0 },
2323 { 0x1f, 0x0000 }
2324 };
2325
françois romieu4da19632011-01-03 15:07:55 +00002326 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002327}
2328
françois romieu4da19632011-01-03 15:07:55 +00002329static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002330{
2331 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002332
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002333 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2334 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002335 return;
2336
françois romieu4da19632011-01-03 15:07:55 +00002337 rtl_writephy(tp, 0x1f, 0x0001);
2338 rtl_writephy(tp, 0x10, 0xf01b);
2339 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002340}
2341
françois romieu4da19632011-01-03 15:07:55 +00002342static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002343{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002344 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002345 { 0x1f, 0x0001 },
2346 { 0x04, 0x0000 },
2347 { 0x03, 0x00a1 },
2348 { 0x02, 0x0008 },
2349 { 0x01, 0x0120 },
2350 { 0x00, 0x1000 },
2351 { 0x04, 0x0800 },
2352 { 0x04, 0x9000 },
2353 { 0x03, 0x802f },
2354 { 0x02, 0x4f02 },
2355 { 0x01, 0x0409 },
2356 { 0x00, 0xf099 },
2357 { 0x04, 0x9800 },
2358 { 0x04, 0xa000 },
2359 { 0x03, 0xdf01 },
2360 { 0x02, 0xdf20 },
2361 { 0x01, 0xff95 },
2362 { 0x00, 0xba00 },
2363 { 0x04, 0xa800 },
2364 { 0x04, 0xf000 },
2365 { 0x03, 0xdf01 },
2366 { 0x02, 0xdf20 },
2367 { 0x01, 0x101a },
2368 { 0x00, 0xa0ff },
2369 { 0x04, 0xf800 },
2370 { 0x04, 0x0000 },
2371 { 0x1f, 0x0000 },
2372
2373 { 0x1f, 0x0001 },
2374 { 0x10, 0xf41b },
2375 { 0x14, 0xfb54 },
2376 { 0x18, 0xf5c7 },
2377 { 0x1f, 0x0000 },
2378
2379 { 0x1f, 0x0001 },
2380 { 0x17, 0x0cc0 },
2381 { 0x1f, 0x0000 }
2382 };
2383
françois romieu4da19632011-01-03 15:07:55 +00002384 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002385
françois romieu4da19632011-01-03 15:07:55 +00002386 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002387}
2388
françois romieu4da19632011-01-03 15:07:55 +00002389static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002390{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002391 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002392 { 0x1f, 0x0001 },
2393 { 0x04, 0x0000 },
2394 { 0x03, 0x00a1 },
2395 { 0x02, 0x0008 },
2396 { 0x01, 0x0120 },
2397 { 0x00, 0x1000 },
2398 { 0x04, 0x0800 },
2399 { 0x04, 0x9000 },
2400 { 0x03, 0x802f },
2401 { 0x02, 0x4f02 },
2402 { 0x01, 0x0409 },
2403 { 0x00, 0xf099 },
2404 { 0x04, 0x9800 },
2405 { 0x04, 0xa000 },
2406 { 0x03, 0xdf01 },
2407 { 0x02, 0xdf20 },
2408 { 0x01, 0xff95 },
2409 { 0x00, 0xba00 },
2410 { 0x04, 0xa800 },
2411 { 0x04, 0xf000 },
2412 { 0x03, 0xdf01 },
2413 { 0x02, 0xdf20 },
2414 { 0x01, 0x101a },
2415 { 0x00, 0xa0ff },
2416 { 0x04, 0xf800 },
2417 { 0x04, 0x0000 },
2418 { 0x1f, 0x0000 },
2419
2420 { 0x1f, 0x0001 },
2421 { 0x0b, 0x8480 },
2422 { 0x1f, 0x0000 },
2423
2424 { 0x1f, 0x0001 },
2425 { 0x18, 0x67c7 },
2426 { 0x04, 0x2000 },
2427 { 0x03, 0x002f },
2428 { 0x02, 0x4360 },
2429 { 0x01, 0x0109 },
2430 { 0x00, 0x3022 },
2431 { 0x04, 0x2800 },
2432 { 0x1f, 0x0000 },
2433
2434 { 0x1f, 0x0001 },
2435 { 0x17, 0x0cc0 },
2436 { 0x1f, 0x0000 }
2437 };
2438
françois romieu4da19632011-01-03 15:07:55 +00002439 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002440}
2441
françois romieu4da19632011-01-03 15:07:55 +00002442static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002443{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002444 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002445 { 0x10, 0xf41b },
2446 { 0x1f, 0x0000 }
2447 };
2448
françois romieu4da19632011-01-03 15:07:55 +00002449 rtl_writephy(tp, 0x1f, 0x0001);
2450 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002451
françois romieu4da19632011-01-03 15:07:55 +00002452 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002453}
2454
françois romieu4da19632011-01-03 15:07:55 +00002455static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002456{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002457 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002458 { 0x1f, 0x0001 },
2459 { 0x10, 0xf41b },
2460 { 0x1f, 0x0000 }
2461 };
2462
françois romieu4da19632011-01-03 15:07:55 +00002463 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002464}
2465
françois romieu4da19632011-01-03 15:07:55 +00002466static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002467{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002468 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002469 { 0x1f, 0x0000 },
2470 { 0x1d, 0x0f00 },
2471 { 0x1f, 0x0002 },
2472 { 0x0c, 0x1ec8 },
2473 { 0x1f, 0x0000 }
2474 };
2475
françois romieu4da19632011-01-03 15:07:55 +00002476 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002477}
2478
françois romieu4da19632011-01-03 15:07:55 +00002479static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002480{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002481 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002482 { 0x1f, 0x0001 },
2483 { 0x1d, 0x3d98 },
2484 { 0x1f, 0x0000 }
2485 };
2486
françois romieu4da19632011-01-03 15:07:55 +00002487 rtl_writephy(tp, 0x1f, 0x0000);
2488 rtl_patchphy(tp, 0x14, 1 << 5);
2489 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002490
françois romieu4da19632011-01-03 15:07:55 +00002491 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002492}
2493
françois romieu4da19632011-01-03 15:07:55 +00002494static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002495{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002496 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002497 { 0x1f, 0x0001 },
2498 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002499 { 0x1f, 0x0002 },
2500 { 0x00, 0x88d4 },
2501 { 0x01, 0x82b1 },
2502 { 0x03, 0x7002 },
2503 { 0x08, 0x9e30 },
2504 { 0x09, 0x01f0 },
2505 { 0x0a, 0x5500 },
2506 { 0x0c, 0x00c8 },
2507 { 0x1f, 0x0003 },
2508 { 0x12, 0xc096 },
2509 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002510 { 0x1f, 0x0000 },
2511 { 0x1f, 0x0000 },
2512 { 0x09, 0x2000 },
2513 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002514 };
2515
françois romieu4da19632011-01-03 15:07:55 +00002516 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002517
françois romieu4da19632011-01-03 15:07:55 +00002518 rtl_patchphy(tp, 0x14, 1 << 5);
2519 rtl_patchphy(tp, 0x0d, 1 << 5);
2520 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002521}
2522
françois romieu4da19632011-01-03 15:07:55 +00002523static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002524{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002525 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002526 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002527 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002528 { 0x03, 0x802f },
2529 { 0x02, 0x4f02 },
2530 { 0x01, 0x0409 },
2531 { 0x00, 0xf099 },
2532 { 0x04, 0x9800 },
2533 { 0x04, 0x9000 },
2534 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002535 { 0x1f, 0x0002 },
2536 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002537 { 0x06, 0x0761 },
2538 { 0x1f, 0x0003 },
2539 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002540 { 0x1f, 0x0000 }
2541 };
2542
françois romieu4da19632011-01-03 15:07:55 +00002543 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002544
françois romieu4da19632011-01-03 15:07:55 +00002545 rtl_patchphy(tp, 0x16, 1 << 0);
2546 rtl_patchphy(tp, 0x14, 1 << 5);
2547 rtl_patchphy(tp, 0x0d, 1 << 5);
2548 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002549}
2550
françois romieu4da19632011-01-03 15:07:55 +00002551static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002552{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002553 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002554 { 0x1f, 0x0001 },
2555 { 0x12, 0x2300 },
2556 { 0x1d, 0x3d98 },
2557 { 0x1f, 0x0002 },
2558 { 0x0c, 0x7eb8 },
2559 { 0x06, 0x5461 },
2560 { 0x1f, 0x0003 },
2561 { 0x16, 0x0f0a },
2562 { 0x1f, 0x0000 }
2563 };
2564
françois romieu4da19632011-01-03 15:07:55 +00002565 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002566
françois romieu4da19632011-01-03 15:07:55 +00002567 rtl_patchphy(tp, 0x16, 1 << 0);
2568 rtl_patchphy(tp, 0x14, 1 << 5);
2569 rtl_patchphy(tp, 0x0d, 1 << 5);
2570 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002571}
2572
françois romieu4da19632011-01-03 15:07:55 +00002573static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002574{
françois romieu4da19632011-01-03 15:07:55 +00002575 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002576}
2577
françois romieubca03d52011-01-03 15:07:31 +00002578static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002579{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002580 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002581 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002582 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002583 { 0x06, 0x4064 },
2584 { 0x07, 0x2863 },
2585 { 0x08, 0x059c },
2586 { 0x09, 0x26b4 },
2587 { 0x0a, 0x6a19 },
2588 { 0x0b, 0xdcc8 },
2589 { 0x10, 0xf06d },
2590 { 0x14, 0x7f68 },
2591 { 0x18, 0x7fd9 },
2592 { 0x1c, 0xf0ff },
2593 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002594 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002595 { 0x12, 0xf49f },
2596 { 0x13, 0x070b },
2597 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002598 { 0x14, 0x94c0 },
2599
2600 /*
2601 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002602 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002603 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002604 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002605 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002606 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002607 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002608 { 0x06, 0x5561 },
2609
2610 /*
2611 * Can not link to 1Gbps with bad cable
2612 * Decrease SNR threshold form 21.07dB to 19.04dB
2613 */
2614 { 0x1f, 0x0001 },
2615 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002616
2617 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002618 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002619 };
françois romieubca03d52011-01-03 15:07:31 +00002620 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002621
françois romieu4da19632011-01-03 15:07:55 +00002622 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002623
françois romieubca03d52011-01-03 15:07:31 +00002624 /*
2625 * Rx Error Issue
2626 * Fine Tune Switching regulator parameter
2627 */
françois romieu4da19632011-01-03 15:07:55 +00002628 rtl_writephy(tp, 0x1f, 0x0002);
2629 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2630 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002631
françois romieudaf9df62009-10-07 12:44:20 +00002632 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002633 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002634 { 0x1f, 0x0002 },
2635 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002636 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002637 { 0x05, 0x8330 },
2638 { 0x06, 0x669a },
2639 { 0x1f, 0x0002 }
2640 };
2641 int val;
2642
françois romieu4da19632011-01-03 15:07:55 +00002643 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002644
françois romieu4da19632011-01-03 15:07:55 +00002645 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002646
2647 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002648 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002649 0x0065, 0x0066, 0x0067, 0x0068,
2650 0x0069, 0x006a, 0x006b, 0x006c
2651 };
2652 int i;
2653
françois romieu4da19632011-01-03 15:07:55 +00002654 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002655
2656 val &= 0xff00;
2657 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002658 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002659 }
2660 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002661 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002662 { 0x1f, 0x0002 },
2663 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002664 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002665 { 0x05, 0x8330 },
2666 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002667 };
2668
françois romieu4da19632011-01-03 15:07:55 +00002669 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002670 }
2671
françois romieubca03d52011-01-03 15:07:31 +00002672 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002673 rtl_writephy(tp, 0x1f, 0x0002);
2674 rtl_patchphy(tp, 0x0d, 0x0300);
2675 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002676
françois romieubca03d52011-01-03 15:07:31 +00002677 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002678 rtl_writephy(tp, 0x1f, 0x0002);
2679 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2680 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002681
françois romieu4da19632011-01-03 15:07:55 +00002682 rtl_writephy(tp, 0x1f, 0x0005);
2683 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002684
2685 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002686
françois romieu4da19632011-01-03 15:07:55 +00002687 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002688}
2689
françois romieubca03d52011-01-03 15:07:31 +00002690static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002691{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002692 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002693 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002694 { 0x1f, 0x0001 },
2695 { 0x06, 0x4064 },
2696 { 0x07, 0x2863 },
2697 { 0x08, 0x059c },
2698 { 0x09, 0x26b4 },
2699 { 0x0a, 0x6a19 },
2700 { 0x0b, 0xdcc8 },
2701 { 0x10, 0xf06d },
2702 { 0x14, 0x7f68 },
2703 { 0x18, 0x7fd9 },
2704 { 0x1c, 0xf0ff },
2705 { 0x1d, 0x3d9c },
2706 { 0x1f, 0x0003 },
2707 { 0x12, 0xf49f },
2708 { 0x13, 0x070b },
2709 { 0x1a, 0x05ad },
2710 { 0x14, 0x94c0 },
2711
françois romieubca03d52011-01-03 15:07:31 +00002712 /*
2713 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002714 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002715 */
françois romieudaf9df62009-10-07 12:44:20 +00002716 { 0x1f, 0x0002 },
2717 { 0x06, 0x5561 },
2718 { 0x1f, 0x0005 },
2719 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002720 { 0x06, 0x5561 },
2721
2722 /*
2723 * Can not link to 1Gbps with bad cable
2724 * Decrease SNR threshold form 21.07dB to 19.04dB
2725 */
2726 { 0x1f, 0x0001 },
2727 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002728
2729 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002730 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002731 };
françois romieubca03d52011-01-03 15:07:31 +00002732 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002733
françois romieu4da19632011-01-03 15:07:55 +00002734 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002735
2736 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002737 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002738 { 0x1f, 0x0002 },
2739 { 0x05, 0x669a },
2740 { 0x1f, 0x0005 },
2741 { 0x05, 0x8330 },
2742 { 0x06, 0x669a },
2743
2744 { 0x1f, 0x0002 }
2745 };
2746 int val;
2747
françois romieu4da19632011-01-03 15:07:55 +00002748 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002749
françois romieu4da19632011-01-03 15:07:55 +00002750 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002751 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002752 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002753 0x0065, 0x0066, 0x0067, 0x0068,
2754 0x0069, 0x006a, 0x006b, 0x006c
2755 };
2756 int i;
2757
françois romieu4da19632011-01-03 15:07:55 +00002758 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002759
2760 val &= 0xff00;
2761 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002762 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002763 }
2764 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002765 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002766 { 0x1f, 0x0002 },
2767 { 0x05, 0x2642 },
2768 { 0x1f, 0x0005 },
2769 { 0x05, 0x8330 },
2770 { 0x06, 0x2642 }
2771 };
2772
françois romieu4da19632011-01-03 15:07:55 +00002773 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002774 }
2775
françois romieubca03d52011-01-03 15:07:31 +00002776 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002777 rtl_writephy(tp, 0x1f, 0x0002);
2778 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2779 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002780
françois romieubca03d52011-01-03 15:07:31 +00002781 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002782 rtl_writephy(tp, 0x1f, 0x0002);
2783 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002784
françois romieu4da19632011-01-03 15:07:55 +00002785 rtl_writephy(tp, 0x1f, 0x0005);
2786 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002787
2788 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002789
françois romieu4da19632011-01-03 15:07:55 +00002790 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002791}
2792
françois romieu4da19632011-01-03 15:07:55 +00002793static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002794{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002795 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002796 { 0x1f, 0x0002 },
2797 { 0x10, 0x0008 },
2798 { 0x0d, 0x006c },
2799
2800 { 0x1f, 0x0000 },
2801 { 0x0d, 0xf880 },
2802
2803 { 0x1f, 0x0001 },
2804 { 0x17, 0x0cc0 },
2805
2806 { 0x1f, 0x0001 },
2807 { 0x0b, 0xa4d8 },
2808 { 0x09, 0x281c },
2809 { 0x07, 0x2883 },
2810 { 0x0a, 0x6b35 },
2811 { 0x1d, 0x3da4 },
2812 { 0x1c, 0xeffd },
2813 { 0x14, 0x7f52 },
2814 { 0x18, 0x7fc6 },
2815 { 0x08, 0x0601 },
2816 { 0x06, 0x4063 },
2817 { 0x10, 0xf074 },
2818 { 0x1f, 0x0003 },
2819 { 0x13, 0x0789 },
2820 { 0x12, 0xf4bd },
2821 { 0x1a, 0x04fd },
2822 { 0x14, 0x84b0 },
2823 { 0x1f, 0x0000 },
2824 { 0x00, 0x9200 },
2825
2826 { 0x1f, 0x0005 },
2827 { 0x01, 0x0340 },
2828 { 0x1f, 0x0001 },
2829 { 0x04, 0x4000 },
2830 { 0x03, 0x1d21 },
2831 { 0x02, 0x0c32 },
2832 { 0x01, 0x0200 },
2833 { 0x00, 0x5554 },
2834 { 0x04, 0x4800 },
2835 { 0x04, 0x4000 },
2836 { 0x04, 0xf000 },
2837 { 0x03, 0xdf01 },
2838 { 0x02, 0xdf20 },
2839 { 0x01, 0x101a },
2840 { 0x00, 0xa0ff },
2841 { 0x04, 0xf800 },
2842 { 0x04, 0xf000 },
2843 { 0x1f, 0x0000 },
2844
2845 { 0x1f, 0x0007 },
2846 { 0x1e, 0x0023 },
2847 { 0x16, 0x0000 },
2848 { 0x1f, 0x0000 }
2849 };
2850
françois romieu4da19632011-01-03 15:07:55 +00002851 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002852}
2853
françois romieue6de30d2011-01-03 15:08:37 +00002854static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2855{
2856 static const struct phy_reg phy_reg_init[] = {
2857 { 0x1f, 0x0001 },
2858 { 0x17, 0x0cc0 },
2859
2860 { 0x1f, 0x0007 },
2861 { 0x1e, 0x002d },
2862 { 0x18, 0x0040 },
2863 { 0x1f, 0x0000 }
2864 };
2865
2866 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2867 rtl_patchphy(tp, 0x0d, 1 << 5);
2868}
2869
Hayes Wang70090422011-07-06 15:58:06 +08002870static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002871{
2872 static const struct phy_reg phy_reg_init[] = {
2873 /* Enable Delay cap */
2874 { 0x1f, 0x0005 },
2875 { 0x05, 0x8b80 },
2876 { 0x06, 0xc896 },
2877 { 0x1f, 0x0000 },
2878
2879 /* Channel estimation fine tune */
2880 { 0x1f, 0x0001 },
2881 { 0x0b, 0x6c20 },
2882 { 0x07, 0x2872 },
2883 { 0x1c, 0xefff },
2884 { 0x1f, 0x0003 },
2885 { 0x14, 0x6420 },
2886 { 0x1f, 0x0000 },
2887
2888 /* Update PFM & 10M TX idle timer */
2889 { 0x1f, 0x0007 },
2890 { 0x1e, 0x002f },
2891 { 0x15, 0x1919 },
2892 { 0x1f, 0x0000 },
2893
2894 { 0x1f, 0x0007 },
2895 { 0x1e, 0x00ac },
2896 { 0x18, 0x0006 },
2897 { 0x1f, 0x0000 }
2898 };
2899
Francois Romieu15ecd032011-04-27 13:52:22 -07002900 rtl_apply_firmware(tp);
2901
hayeswang01dc7fe2011-03-21 01:50:28 +00002902 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2903
2904 /* DCO enable for 10M IDLE Power */
2905 rtl_writephy(tp, 0x1f, 0x0007);
2906 rtl_writephy(tp, 0x1e, 0x0023);
2907 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2908 rtl_writephy(tp, 0x1f, 0x0000);
2909
2910 /* For impedance matching */
2911 rtl_writephy(tp, 0x1f, 0x0002);
2912 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002913 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002914
2915 /* PHY auto speed down */
2916 rtl_writephy(tp, 0x1f, 0x0007);
2917 rtl_writephy(tp, 0x1e, 0x002d);
2918 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2919 rtl_writephy(tp, 0x1f, 0x0000);
2920 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2921
2922 rtl_writephy(tp, 0x1f, 0x0005);
2923 rtl_writephy(tp, 0x05, 0x8b86);
2924 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2925 rtl_writephy(tp, 0x1f, 0x0000);
2926
2927 rtl_writephy(tp, 0x1f, 0x0005);
2928 rtl_writephy(tp, 0x05, 0x8b85);
2929 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2930 rtl_writephy(tp, 0x1f, 0x0007);
2931 rtl_writephy(tp, 0x1e, 0x0020);
2932 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2933 rtl_writephy(tp, 0x1f, 0x0006);
2934 rtl_writephy(tp, 0x00, 0x5a00);
2935 rtl_writephy(tp, 0x1f, 0x0000);
2936 rtl_writephy(tp, 0x0d, 0x0007);
2937 rtl_writephy(tp, 0x0e, 0x003c);
2938 rtl_writephy(tp, 0x0d, 0x4007);
2939 rtl_writephy(tp, 0x0e, 0x0000);
2940 rtl_writephy(tp, 0x0d, 0x0000);
2941}
2942
Hayes Wang70090422011-07-06 15:58:06 +08002943static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2944{
2945 static const struct phy_reg phy_reg_init[] = {
2946 /* Enable Delay cap */
2947 { 0x1f, 0x0004 },
2948 { 0x1f, 0x0007 },
2949 { 0x1e, 0x00ac },
2950 { 0x18, 0x0006 },
2951 { 0x1f, 0x0002 },
2952 { 0x1f, 0x0000 },
2953 { 0x1f, 0x0000 },
2954
2955 /* Channel estimation fine tune */
2956 { 0x1f, 0x0003 },
2957 { 0x09, 0xa20f },
2958 { 0x1f, 0x0000 },
2959 { 0x1f, 0x0000 },
2960
2961 /* Green Setting */
2962 { 0x1f, 0x0005 },
2963 { 0x05, 0x8b5b },
2964 { 0x06, 0x9222 },
2965 { 0x05, 0x8b6d },
2966 { 0x06, 0x8000 },
2967 { 0x05, 0x8b76 },
2968 { 0x06, 0x8000 },
2969 { 0x1f, 0x0000 }
2970 };
2971
2972 rtl_apply_firmware(tp);
2973
2974 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2975
2976 /* For 4-corner performance improve */
2977 rtl_writephy(tp, 0x1f, 0x0005);
2978 rtl_writephy(tp, 0x05, 0x8b80);
2979 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2980 rtl_writephy(tp, 0x1f, 0x0000);
2981
2982 /* PHY auto speed down */
2983 rtl_writephy(tp, 0x1f, 0x0004);
2984 rtl_writephy(tp, 0x1f, 0x0007);
2985 rtl_writephy(tp, 0x1e, 0x002d);
2986 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2987 rtl_writephy(tp, 0x1f, 0x0002);
2988 rtl_writephy(tp, 0x1f, 0x0000);
2989 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2990
2991 /* improve 10M EEE waveform */
2992 rtl_writephy(tp, 0x1f, 0x0005);
2993 rtl_writephy(tp, 0x05, 0x8b86);
2994 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2995 rtl_writephy(tp, 0x1f, 0x0000);
2996
2997 /* Improve 2-pair detection performance */
2998 rtl_writephy(tp, 0x1f, 0x0005);
2999 rtl_writephy(tp, 0x05, 0x8b85);
3000 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3001 rtl_writephy(tp, 0x1f, 0x0000);
3002
3003 /* EEE setting */
3004 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
3005 ERIAR_EXGMAC);
3006 rtl_writephy(tp, 0x1f, 0x0005);
3007 rtl_writephy(tp, 0x05, 0x8b85);
3008 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3009 rtl_writephy(tp, 0x1f, 0x0004);
3010 rtl_writephy(tp, 0x1f, 0x0007);
3011 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04003012 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003013 rtl_writephy(tp, 0x1f, 0x0002);
3014 rtl_writephy(tp, 0x1f, 0x0000);
3015 rtl_writephy(tp, 0x0d, 0x0007);
3016 rtl_writephy(tp, 0x0e, 0x003c);
3017 rtl_writephy(tp, 0x0d, 0x4007);
3018 rtl_writephy(tp, 0x0e, 0x0000);
3019 rtl_writephy(tp, 0x0d, 0x0000);
3020
3021 /* Green feature */
3022 rtl_writephy(tp, 0x1f, 0x0003);
3023 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3024 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3025 rtl_writephy(tp, 0x1f, 0x0000);
3026}
3027
Hayes Wangc2218922011-09-06 16:55:18 +08003028static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3029{
3030 static const struct phy_reg phy_reg_init[] = {
3031 /* Channel estimation fine tune */
3032 { 0x1f, 0x0003 },
3033 { 0x09, 0xa20f },
3034 { 0x1f, 0x0000 },
3035
3036 /* Modify green table for giga & fnet */
3037 { 0x1f, 0x0005 },
3038 { 0x05, 0x8b55 },
3039 { 0x06, 0x0000 },
3040 { 0x05, 0x8b5e },
3041 { 0x06, 0x0000 },
3042 { 0x05, 0x8b67 },
3043 { 0x06, 0x0000 },
3044 { 0x05, 0x8b70 },
3045 { 0x06, 0x0000 },
3046 { 0x1f, 0x0000 },
3047 { 0x1f, 0x0007 },
3048 { 0x1e, 0x0078 },
3049 { 0x17, 0x0000 },
3050 { 0x19, 0x00fb },
3051 { 0x1f, 0x0000 },
3052
3053 /* Modify green table for 10M */
3054 { 0x1f, 0x0005 },
3055 { 0x05, 0x8b79 },
3056 { 0x06, 0xaa00 },
3057 { 0x1f, 0x0000 },
3058
3059 /* Disable hiimpedance detection (RTCT) */
3060 { 0x1f, 0x0003 },
3061 { 0x01, 0x328a },
3062 { 0x1f, 0x0000 }
3063 };
3064
3065 rtl_apply_firmware(tp);
3066
3067 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3068
3069 /* For 4-corner performance improve */
3070 rtl_writephy(tp, 0x1f, 0x0005);
3071 rtl_writephy(tp, 0x05, 0x8b80);
3072 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3073 rtl_writephy(tp, 0x1f, 0x0000);
3074
3075 /* PHY auto speed down */
3076 rtl_writephy(tp, 0x1f, 0x0007);
3077 rtl_writephy(tp, 0x1e, 0x002d);
3078 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3079 rtl_writephy(tp, 0x1f, 0x0000);
3080 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3081
3082 /* Improve 10M EEE waveform */
3083 rtl_writephy(tp, 0x1f, 0x0005);
3084 rtl_writephy(tp, 0x05, 0x8b86);
3085 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3086 rtl_writephy(tp, 0x1f, 0x0000);
3087
3088 /* Improve 2-pair detection performance */
3089 rtl_writephy(tp, 0x1f, 0x0005);
3090 rtl_writephy(tp, 0x05, 0x8b85);
3091 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3092 rtl_writephy(tp, 0x1f, 0x0000);
3093}
3094
3095static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3096{
3097 rtl_apply_firmware(tp);
3098
3099 /* For 4-corner performance improve */
3100 rtl_writephy(tp, 0x1f, 0x0005);
3101 rtl_writephy(tp, 0x05, 0x8b80);
3102 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3103 rtl_writephy(tp, 0x1f, 0x0000);
3104
3105 /* PHY auto speed down */
3106 rtl_writephy(tp, 0x1f, 0x0007);
3107 rtl_writephy(tp, 0x1e, 0x002d);
3108 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3109 rtl_writephy(tp, 0x1f, 0x0000);
3110 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3111
3112 /* Improve 10M EEE waveform */
3113 rtl_writephy(tp, 0x1f, 0x0005);
3114 rtl_writephy(tp, 0x05, 0x8b86);
3115 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3116 rtl_writephy(tp, 0x1f, 0x0000);
3117}
3118
françois romieu4da19632011-01-03 15:07:55 +00003119static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003120{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003121 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003122 { 0x1f, 0x0003 },
3123 { 0x08, 0x441d },
3124 { 0x01, 0x9100 },
3125 { 0x1f, 0x0000 }
3126 };
3127
françois romieu4da19632011-01-03 15:07:55 +00003128 rtl_writephy(tp, 0x1f, 0x0000);
3129 rtl_patchphy(tp, 0x11, 1 << 12);
3130 rtl_patchphy(tp, 0x19, 1 << 13);
3131 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003132
françois romieu4da19632011-01-03 15:07:55 +00003133 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003134}
3135
Hayes Wang5a5e4442011-02-22 17:26:21 +08003136static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3137{
3138 static const struct phy_reg phy_reg_init[] = {
3139 { 0x1f, 0x0005 },
3140 { 0x1a, 0x0000 },
3141 { 0x1f, 0x0000 },
3142
3143 { 0x1f, 0x0004 },
3144 { 0x1c, 0x0000 },
3145 { 0x1f, 0x0000 },
3146
3147 { 0x1f, 0x0001 },
3148 { 0x15, 0x7701 },
3149 { 0x1f, 0x0000 }
3150 };
3151
3152 /* Disable ALDPS before ram code */
3153 rtl_writephy(tp, 0x1f, 0x0000);
3154 rtl_writephy(tp, 0x18, 0x0310);
3155 msleep(100);
3156
François Romieu953a12c2011-04-24 17:38:48 +02003157 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003158
3159 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3160}
3161
Francois Romieu5615d9f2007-08-17 17:50:46 +02003162static void rtl_hw_phy_config(struct net_device *dev)
3163{
3164 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003165
3166 rtl8169_print_mac_version(tp);
3167
3168 switch (tp->mac_version) {
3169 case RTL_GIGA_MAC_VER_01:
3170 break;
3171 case RTL_GIGA_MAC_VER_02:
3172 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003173 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003174 break;
3175 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003176 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003177 break;
françois romieu2e9558562009-08-10 19:44:19 +00003178 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003179 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003180 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003181 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003182 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003183 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003184 case RTL_GIGA_MAC_VER_07:
3185 case RTL_GIGA_MAC_VER_08:
3186 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003187 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003188 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003189 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003190 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003191 break;
3192 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003193 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003194 break;
3195 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003196 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003197 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003198 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003199 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003200 break;
3201 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003202 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003203 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003204 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003205 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003206 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003207 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003208 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003209 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003210 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003211 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003212 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003213 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003214 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003215 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003216 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003217 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003218 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003219 break;
3220 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003221 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003222 break;
3223 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003224 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003225 break;
françois romieue6de30d2011-01-03 15:08:37 +00003226 case RTL_GIGA_MAC_VER_28:
3227 rtl8168d_4_hw_phy_config(tp);
3228 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003229 case RTL_GIGA_MAC_VER_29:
3230 case RTL_GIGA_MAC_VER_30:
3231 rtl8105e_hw_phy_config(tp);
3232 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003233 case RTL_GIGA_MAC_VER_31:
3234 /* None. */
3235 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003236 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003237 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003238 rtl8168e_1_hw_phy_config(tp);
3239 break;
3240 case RTL_GIGA_MAC_VER_34:
3241 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003242 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003243 case RTL_GIGA_MAC_VER_35:
3244 rtl8168f_1_hw_phy_config(tp);
3245 break;
3246 case RTL_GIGA_MAC_VER_36:
3247 rtl8168f_2_hw_phy_config(tp);
3248 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003249
Francois Romieu5615d9f2007-08-17 17:50:46 +02003250 default:
3251 break;
3252 }
3253}
3254
Francois Romieuda78dbf2012-01-26 14:18:23 +01003255static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 struct timer_list *timer = &tp->timer;
3258 void __iomem *ioaddr = tp->mmio_addr;
3259 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3260
Francois Romieubcf0bf92006-07-26 23:14:13 +02003261 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
françois romieu4da19632011-01-03 15:07:55 +00003263 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003264 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 * A busy loop could burn quite a few cycles on nowadays CPU.
3266 * Let's delay the execution of the timer for a few ticks.
3267 */
3268 timeout = HZ/10;
3269 goto out_mod_timer;
3270 }
3271
3272 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01003273 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
Francois Romieuda78dbf2012-01-26 14:18:23 +01003275 netif_warn(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
françois romieu4da19632011-01-03 15:07:55 +00003277 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
3279out_mod_timer:
3280 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003281}
3282
3283static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3284{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003285 if (!test_and_set_bit(flag, tp->wk.flags))
3286 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003287}
3288
3289static void rtl8169_phy_timer(unsigned long __opaque)
3290{
3291 struct net_device *dev = (struct net_device *)__opaque;
3292 struct rtl8169_private *tp = netdev_priv(dev);
3293
Francois Romieu98ddf982012-01-31 10:47:34 +01003294 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295}
3296
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297#ifdef CONFIG_NET_POLL_CONTROLLER
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298static void rtl8169_netpoll(struct net_device *dev)
3299{
3300 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
Francois Romieuda78dbf2012-01-26 14:18:23 +01003302 rtl8169_interrupt(tp->pci_dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303}
3304#endif
3305
3306static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3307 void __iomem *ioaddr)
3308{
3309 iounmap(ioaddr);
3310 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003311 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 pci_disable_device(pdev);
3313 free_netdev(dev);
3314}
3315
Francois Romieubf793292006-11-01 00:53:05 +01003316static void rtl8169_phy_reset(struct net_device *dev,
3317 struct rtl8169_private *tp)
3318{
Francois Romieu07d3f512007-02-21 22:40:46 +01003319 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003320
françois romieu4da19632011-01-03 15:07:55 +00003321 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003322 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003323 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003324 return;
3325 msleep(1);
3326 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003327 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003328}
3329
David S. Miller8decf862011-09-22 03:23:13 -04003330static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3331{
3332 void __iomem *ioaddr = tp->mmio_addr;
3333
3334 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3335 (RTL_R8(PHYstatus) & TBI_Enable);
3336}
3337
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003338static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003340 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003341
Francois Romieu5615d9f2007-08-17 17:50:46 +02003342 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003343
Marcus Sundberg773328942008-07-10 21:28:08 +02003344 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3345 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3346 RTL_W8(0x82, 0x01);
3347 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003348
Francois Romieu6dccd162007-02-13 23:38:05 +01003349 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3350
3351 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3352 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003353
Francois Romieubcf0bf92006-07-26 23:14:13 +02003354 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003355 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3356 RTL_W8(0x82, 0x01);
3357 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003358 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003359 }
3360
Francois Romieubf793292006-11-01 00:53:05 +01003361 rtl8169_phy_reset(dev, tp);
3362
Oliver Neukum54405cd2011-01-06 21:55:13 +01003363 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003364 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3365 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3366 (tp->mii.supports_gmii ?
3367 ADVERTISED_1000baseT_Half |
3368 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003369
David S. Miller8decf862011-09-22 03:23:13 -04003370 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003371 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003372}
3373
Francois Romieu773d2022007-01-31 23:47:43 +01003374static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3375{
3376 void __iomem *ioaddr = tp->mmio_addr;
3377 u32 high;
3378 u32 low;
3379
3380 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3381 high = addr[4] | (addr[5] << 8);
3382
Francois Romieuda78dbf2012-01-26 14:18:23 +01003383 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003384
3385 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003386
Francois Romieu773d2022007-01-31 23:47:43 +01003387 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00003388 RTL_R32(MAC4);
3389
Francois Romieu78f1cd02010-03-27 19:35:46 -07003390 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00003391 RTL_R32(MAC0);
3392
françois romieuc28aa382011-08-02 03:53:43 +00003393 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3394 const struct exgmac_reg e[] = {
3395 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3396 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3397 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3398 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3399 low >> 16 },
3400 };
3401
3402 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3403 }
3404
Francois Romieu773d2022007-01-31 23:47:43 +01003405 RTL_W8(Cfg9346, Cfg9346_Lock);
3406
Francois Romieuda78dbf2012-01-26 14:18:23 +01003407 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003408}
3409
3410static int rtl_set_mac_address(struct net_device *dev, void *p)
3411{
3412 struct rtl8169_private *tp = netdev_priv(dev);
3413 struct sockaddr *addr = p;
3414
3415 if (!is_valid_ether_addr(addr->sa_data))
3416 return -EADDRNOTAVAIL;
3417
3418 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3419
3420 rtl_rar_set(tp, dev->dev_addr);
3421
3422 return 0;
3423}
3424
Francois Romieu5f787a12006-08-17 13:02:36 +02003425static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3426{
3427 struct rtl8169_private *tp = netdev_priv(dev);
3428 struct mii_ioctl_data *data = if_mii(ifr);
3429
Francois Romieu8b4ab282008-11-19 22:05:25 -08003430 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3431}
Francois Romieu5f787a12006-08-17 13:02:36 +02003432
Francois Romieucecb5fd2011-04-01 10:21:07 +02003433static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3434 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003435{
Francois Romieu5f787a12006-08-17 13:02:36 +02003436 switch (cmd) {
3437 case SIOCGMIIPHY:
3438 data->phy_id = 32; /* Internal PHY */
3439 return 0;
3440
3441 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003442 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003443 return 0;
3444
3445 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003446 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003447 return 0;
3448 }
3449 return -EOPNOTSUPP;
3450}
3451
Francois Romieu8b4ab282008-11-19 22:05:25 -08003452static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3453{
3454 return -EOPNOTSUPP;
3455}
3456
Francois Romieu0e485152007-02-20 00:00:26 +01003457static const struct rtl_cfg_info {
3458 void (*hw_start)(struct net_device *);
3459 unsigned int region;
3460 unsigned int align;
Francois Romieuda78dbf2012-01-26 14:18:23 +01003461 u16 event_slow;
Francois Romieuccdffb92008-07-26 14:26:06 +02003462 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07003463 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01003464} rtl_cfg_infos [] = {
3465 [RTL_CFG_0] = {
3466 .hw_start = rtl_hw_start_8169,
3467 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01003468 .align = 0,
Francois Romieuda78dbf2012-01-26 14:18:23 +01003469 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003470 .features = RTL_FEATURE_GMII,
3471 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01003472 },
3473 [RTL_CFG_1] = {
3474 .hw_start = rtl_hw_start_8168,
3475 .region = 2,
3476 .align = 8,
Francois Romieuda78dbf2012-01-26 14:18:23 +01003477 .event_slow = SYSErr | LinkChg | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003478 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3479 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01003480 },
3481 [RTL_CFG_2] = {
3482 .hw_start = rtl_hw_start_8101,
3483 .region = 2,
3484 .align = 8,
Francois Romieuda78dbf2012-01-26 14:18:23 +01003485 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
3486 PCSTimeout,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003487 .features = RTL_FEATURE_MSI,
3488 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01003489 }
3490};
3491
Francois Romieufbac58f2007-10-04 22:51:38 +02003492/* Cfg9346_Unlock assumed. */
françois romieu2ca6cf02011-12-15 08:37:43 +00003493static unsigned rtl_try_msi(struct rtl8169_private *tp,
Francois Romieufbac58f2007-10-04 22:51:38 +02003494 const struct rtl_cfg_info *cfg)
3495{
françois romieu2ca6cf02011-12-15 08:37:43 +00003496 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieufbac58f2007-10-04 22:51:38 +02003497 unsigned msi = 0;
3498 u8 cfg2;
3499
3500 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02003501 if (cfg->features & RTL_FEATURE_MSI) {
françois romieu2ca6cf02011-12-15 08:37:43 +00003502 if (pci_enable_msi(tp->pci_dev)) {
3503 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
Francois Romieufbac58f2007-10-04 22:51:38 +02003504 } else {
3505 cfg2 |= MSIEnable;
3506 msi = RTL_FEATURE_MSI;
3507 }
3508 }
françois romieu2ca6cf02011-12-15 08:37:43 +00003509 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3510 RTL_W8(Config2, cfg2);
Francois Romieufbac58f2007-10-04 22:51:38 +02003511 return msi;
3512}
3513
3514static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3515{
3516 if (tp->features & RTL_FEATURE_MSI) {
3517 pci_disable_msi(pdev);
3518 tp->features &= ~RTL_FEATURE_MSI;
3519 }
3520}
3521
françois romieuc0e45c12011-01-03 15:08:04 +00003522static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3523{
3524 struct mdio_ops *ops = &tp->mdio_ops;
3525
3526 switch (tp->mac_version) {
3527 case RTL_GIGA_MAC_VER_27:
3528 ops->write = r8168dp_1_mdio_write;
3529 ops->read = r8168dp_1_mdio_read;
3530 break;
françois romieue6de30d2011-01-03 15:08:37 +00003531 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003532 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003533 ops->write = r8168dp_2_mdio_write;
3534 ops->read = r8168dp_2_mdio_read;
3535 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003536 default:
3537 ops->write = r8169_mdio_write;
3538 ops->read = r8169_mdio_read;
3539 break;
3540 }
3541}
3542
David S. Miller1805b2f2011-10-24 18:18:09 -04003543static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3544{
3545 void __iomem *ioaddr = tp->mmio_addr;
3546
3547 switch (tp->mac_version) {
3548 case RTL_GIGA_MAC_VER_29:
3549 case RTL_GIGA_MAC_VER_30:
3550 case RTL_GIGA_MAC_VER_32:
3551 case RTL_GIGA_MAC_VER_33:
3552 case RTL_GIGA_MAC_VER_34:
3553 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3554 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3555 break;
3556 default:
3557 break;
3558 }
3559}
3560
3561static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3562{
3563 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3564 return false;
3565
3566 rtl_writephy(tp, 0x1f, 0x0000);
3567 rtl_writephy(tp, MII_BMCR, 0x0000);
3568
3569 rtl_wol_suspend_quirk(tp);
3570
3571 return true;
3572}
3573
françois romieu065c27c2011-01-03 15:08:12 +00003574static void r810x_phy_power_down(struct rtl8169_private *tp)
3575{
3576 rtl_writephy(tp, 0x1f, 0x0000);
3577 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3578}
3579
3580static void r810x_phy_power_up(struct rtl8169_private *tp)
3581{
3582 rtl_writephy(tp, 0x1f, 0x0000);
3583 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3584}
3585
3586static void r810x_pll_power_down(struct rtl8169_private *tp)
3587{
David S. Miller1805b2f2011-10-24 18:18:09 -04003588 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003589 return;
françois romieu065c27c2011-01-03 15:08:12 +00003590
3591 r810x_phy_power_down(tp);
3592}
3593
3594static void r810x_pll_power_up(struct rtl8169_private *tp)
3595{
3596 r810x_phy_power_up(tp);
3597}
3598
3599static void r8168_phy_power_up(struct rtl8169_private *tp)
3600{
3601 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003602 switch (tp->mac_version) {
3603 case RTL_GIGA_MAC_VER_11:
3604 case RTL_GIGA_MAC_VER_12:
3605 case RTL_GIGA_MAC_VER_17:
3606 case RTL_GIGA_MAC_VER_18:
3607 case RTL_GIGA_MAC_VER_19:
3608 case RTL_GIGA_MAC_VER_20:
3609 case RTL_GIGA_MAC_VER_21:
3610 case RTL_GIGA_MAC_VER_22:
3611 case RTL_GIGA_MAC_VER_23:
3612 case RTL_GIGA_MAC_VER_24:
3613 case RTL_GIGA_MAC_VER_25:
3614 case RTL_GIGA_MAC_VER_26:
3615 case RTL_GIGA_MAC_VER_27:
3616 case RTL_GIGA_MAC_VER_28:
3617 case RTL_GIGA_MAC_VER_31:
3618 rtl_writephy(tp, 0x0e, 0x0000);
3619 break;
3620 default:
3621 break;
3622 }
françois romieu065c27c2011-01-03 15:08:12 +00003623 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3624}
3625
3626static void r8168_phy_power_down(struct rtl8169_private *tp)
3627{
3628 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003629 switch (tp->mac_version) {
3630 case RTL_GIGA_MAC_VER_32:
3631 case RTL_GIGA_MAC_VER_33:
3632 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3633 break;
3634
3635 case RTL_GIGA_MAC_VER_11:
3636 case RTL_GIGA_MAC_VER_12:
3637 case RTL_GIGA_MAC_VER_17:
3638 case RTL_GIGA_MAC_VER_18:
3639 case RTL_GIGA_MAC_VER_19:
3640 case RTL_GIGA_MAC_VER_20:
3641 case RTL_GIGA_MAC_VER_21:
3642 case RTL_GIGA_MAC_VER_22:
3643 case RTL_GIGA_MAC_VER_23:
3644 case RTL_GIGA_MAC_VER_24:
3645 case RTL_GIGA_MAC_VER_25:
3646 case RTL_GIGA_MAC_VER_26:
3647 case RTL_GIGA_MAC_VER_27:
3648 case RTL_GIGA_MAC_VER_28:
3649 case RTL_GIGA_MAC_VER_31:
3650 rtl_writephy(tp, 0x0e, 0x0200);
3651 default:
3652 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3653 break;
3654 }
françois romieu065c27c2011-01-03 15:08:12 +00003655}
3656
3657static void r8168_pll_power_down(struct rtl8169_private *tp)
3658{
3659 void __iomem *ioaddr = tp->mmio_addr;
3660
Francois Romieucecb5fd2011-04-01 10:21:07 +02003661 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3662 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3663 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003664 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003665 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003666 }
françois romieu065c27c2011-01-03 15:08:12 +00003667
Francois Romieucecb5fd2011-04-01 10:21:07 +02003668 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3669 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003670 (RTL_R16(CPlusCmd) & ASF)) {
3671 return;
3672 }
3673
hayeswang01dc7fe2011-03-21 01:50:28 +00003674 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3675 tp->mac_version == RTL_GIGA_MAC_VER_33)
3676 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3677
David S. Miller1805b2f2011-10-24 18:18:09 -04003678 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003679 return;
françois romieu065c27c2011-01-03 15:08:12 +00003680
3681 r8168_phy_power_down(tp);
3682
3683 switch (tp->mac_version) {
3684 case RTL_GIGA_MAC_VER_25:
3685 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003686 case RTL_GIGA_MAC_VER_27:
3687 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003688 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003689 case RTL_GIGA_MAC_VER_32:
3690 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003691 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3692 break;
3693 }
3694}
3695
3696static void r8168_pll_power_up(struct rtl8169_private *tp)
3697{
3698 void __iomem *ioaddr = tp->mmio_addr;
3699
Francois Romieucecb5fd2011-04-01 10:21:07 +02003700 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3701 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3702 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003703 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003704 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003705 }
françois romieu065c27c2011-01-03 15:08:12 +00003706
3707 switch (tp->mac_version) {
3708 case RTL_GIGA_MAC_VER_25:
3709 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003710 case RTL_GIGA_MAC_VER_27:
3711 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003712 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003713 case RTL_GIGA_MAC_VER_32:
3714 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003715 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3716 break;
3717 }
3718
3719 r8168_phy_power_up(tp);
3720}
3721
Francois Romieud58d46b2011-05-03 16:38:29 +02003722static void rtl_generic_op(struct rtl8169_private *tp,
3723 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00003724{
3725 if (op)
3726 op(tp);
3727}
3728
3729static void rtl_pll_power_down(struct rtl8169_private *tp)
3730{
Francois Romieud58d46b2011-05-03 16:38:29 +02003731 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00003732}
3733
3734static void rtl_pll_power_up(struct rtl8169_private *tp)
3735{
Francois Romieud58d46b2011-05-03 16:38:29 +02003736 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00003737}
3738
3739static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3740{
3741 struct pll_power_ops *ops = &tp->pll_power_ops;
3742
3743 switch (tp->mac_version) {
3744 case RTL_GIGA_MAC_VER_07:
3745 case RTL_GIGA_MAC_VER_08:
3746 case RTL_GIGA_MAC_VER_09:
3747 case RTL_GIGA_MAC_VER_10:
3748 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003749 case RTL_GIGA_MAC_VER_29:
3750 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003751 ops->down = r810x_pll_power_down;
3752 ops->up = r810x_pll_power_up;
3753 break;
3754
3755 case RTL_GIGA_MAC_VER_11:
3756 case RTL_GIGA_MAC_VER_12:
3757 case RTL_GIGA_MAC_VER_17:
3758 case RTL_GIGA_MAC_VER_18:
3759 case RTL_GIGA_MAC_VER_19:
3760 case RTL_GIGA_MAC_VER_20:
3761 case RTL_GIGA_MAC_VER_21:
3762 case RTL_GIGA_MAC_VER_22:
3763 case RTL_GIGA_MAC_VER_23:
3764 case RTL_GIGA_MAC_VER_24:
3765 case RTL_GIGA_MAC_VER_25:
3766 case RTL_GIGA_MAC_VER_26:
3767 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003768 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003769 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003770 case RTL_GIGA_MAC_VER_32:
3771 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003772 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08003773 case RTL_GIGA_MAC_VER_35:
3774 case RTL_GIGA_MAC_VER_36:
françois romieu065c27c2011-01-03 15:08:12 +00003775 ops->down = r8168_pll_power_down;
3776 ops->up = r8168_pll_power_up;
3777 break;
3778
3779 default:
3780 ops->down = NULL;
3781 ops->up = NULL;
3782 break;
3783 }
3784}
3785
Hayes Wange542a222011-07-06 15:58:04 +08003786static void rtl_init_rxcfg(struct rtl8169_private *tp)
3787{
3788 void __iomem *ioaddr = tp->mmio_addr;
3789
3790 switch (tp->mac_version) {
3791 case RTL_GIGA_MAC_VER_01:
3792 case RTL_GIGA_MAC_VER_02:
3793 case RTL_GIGA_MAC_VER_03:
3794 case RTL_GIGA_MAC_VER_04:
3795 case RTL_GIGA_MAC_VER_05:
3796 case RTL_GIGA_MAC_VER_06:
3797 case RTL_GIGA_MAC_VER_10:
3798 case RTL_GIGA_MAC_VER_11:
3799 case RTL_GIGA_MAC_VER_12:
3800 case RTL_GIGA_MAC_VER_13:
3801 case RTL_GIGA_MAC_VER_14:
3802 case RTL_GIGA_MAC_VER_15:
3803 case RTL_GIGA_MAC_VER_16:
3804 case RTL_GIGA_MAC_VER_17:
3805 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3806 break;
3807 case RTL_GIGA_MAC_VER_18:
3808 case RTL_GIGA_MAC_VER_19:
3809 case RTL_GIGA_MAC_VER_20:
3810 case RTL_GIGA_MAC_VER_21:
3811 case RTL_GIGA_MAC_VER_22:
3812 case RTL_GIGA_MAC_VER_23:
3813 case RTL_GIGA_MAC_VER_24:
3814 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3815 break;
3816 default:
3817 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3818 break;
3819 }
3820}
3821
Hayes Wang92fc43b2011-07-06 15:58:03 +08003822static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3823{
3824 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3825}
3826
Francois Romieud58d46b2011-05-03 16:38:29 +02003827static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3828{
françois romieu9c5028e2012-03-02 04:43:14 +00003829 void __iomem *ioaddr = tp->mmio_addr;
3830
3831 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003832 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00003833 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003834}
3835
3836static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3837{
françois romieu9c5028e2012-03-02 04:43:14 +00003838 void __iomem *ioaddr = tp->mmio_addr;
3839
3840 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003841 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00003842 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003843}
3844
3845static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3846{
3847 void __iomem *ioaddr = tp->mmio_addr;
3848
3849 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3850 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3851 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3852}
3853
3854static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3855{
3856 void __iomem *ioaddr = tp->mmio_addr;
3857
3858 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3859 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3860 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3861}
3862
3863static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3864{
3865 void __iomem *ioaddr = tp->mmio_addr;
3866
3867 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3868}
3869
3870static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3871{
3872 void __iomem *ioaddr = tp->mmio_addr;
3873
3874 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3875}
3876
3877static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3878{
3879 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003880
3881 RTL_W8(MaxTxPacketSize, 0x3f);
3882 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3883 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003884 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003885}
3886
3887static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3888{
3889 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003890
3891 RTL_W8(MaxTxPacketSize, 0x0c);
3892 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3893 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003894 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003895}
3896
3897static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3898{
3899 rtl_tx_performance_tweak(tp->pci_dev,
3900 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3901}
3902
3903static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3904{
3905 rtl_tx_performance_tweak(tp->pci_dev,
3906 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3907}
3908
3909static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3910{
3911 void __iomem *ioaddr = tp->mmio_addr;
3912
3913 r8168b_0_hw_jumbo_enable(tp);
3914
3915 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
3916}
3917
3918static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3919{
3920 void __iomem *ioaddr = tp->mmio_addr;
3921
3922 r8168b_0_hw_jumbo_disable(tp);
3923
3924 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3925}
3926
3927static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
3928{
3929 struct jumbo_ops *ops = &tp->jumbo_ops;
3930
3931 switch (tp->mac_version) {
3932 case RTL_GIGA_MAC_VER_11:
3933 ops->disable = r8168b_0_hw_jumbo_disable;
3934 ops->enable = r8168b_0_hw_jumbo_enable;
3935 break;
3936 case RTL_GIGA_MAC_VER_12:
3937 case RTL_GIGA_MAC_VER_17:
3938 ops->disable = r8168b_1_hw_jumbo_disable;
3939 ops->enable = r8168b_1_hw_jumbo_enable;
3940 break;
3941 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
3942 case RTL_GIGA_MAC_VER_19:
3943 case RTL_GIGA_MAC_VER_20:
3944 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
3945 case RTL_GIGA_MAC_VER_22:
3946 case RTL_GIGA_MAC_VER_23:
3947 case RTL_GIGA_MAC_VER_24:
3948 case RTL_GIGA_MAC_VER_25:
3949 case RTL_GIGA_MAC_VER_26:
3950 ops->disable = r8168c_hw_jumbo_disable;
3951 ops->enable = r8168c_hw_jumbo_enable;
3952 break;
3953 case RTL_GIGA_MAC_VER_27:
3954 case RTL_GIGA_MAC_VER_28:
3955 ops->disable = r8168dp_hw_jumbo_disable;
3956 ops->enable = r8168dp_hw_jumbo_enable;
3957 break;
3958 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
3959 case RTL_GIGA_MAC_VER_32:
3960 case RTL_GIGA_MAC_VER_33:
3961 case RTL_GIGA_MAC_VER_34:
3962 ops->disable = r8168e_hw_jumbo_disable;
3963 ops->enable = r8168e_hw_jumbo_enable;
3964 break;
3965
3966 /*
3967 * No action needed for jumbo frames with 8169.
3968 * No jumbo for 810x at all.
3969 */
3970 default:
3971 ops->disable = NULL;
3972 ops->enable = NULL;
3973 break;
3974 }
3975}
3976
Francois Romieu6f43adc2011-04-29 15:05:51 +02003977static void rtl_hw_reset(struct rtl8169_private *tp)
3978{
3979 void __iomem *ioaddr = tp->mmio_addr;
3980 int i;
3981
3982 /* Soft reset the chip. */
3983 RTL_W8(ChipCmd, CmdReset);
3984
3985 /* Check that the chip has finished the reset. */
3986 for (i = 0; i < 100; i++) {
3987 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3988 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003989 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003990 }
3991}
3992
Francois Romieub6ffd972011-06-17 17:00:05 +02003993static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
3994{
3995 struct rtl_fw *rtl_fw;
3996 const char *name;
3997 int rc = -ENOMEM;
3998
3999 name = rtl_lookup_firmware_name(tp);
4000 if (!name)
4001 goto out_no_firmware;
4002
4003 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4004 if (!rtl_fw)
4005 goto err_warn;
4006
4007 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4008 if (rc < 0)
4009 goto err_free;
4010
Francois Romieufd112f22011-06-18 00:10:29 +02004011 rc = rtl_check_firmware(tp, rtl_fw);
4012 if (rc < 0)
4013 goto err_release_firmware;
4014
Francois Romieub6ffd972011-06-17 17:00:05 +02004015 tp->rtl_fw = rtl_fw;
4016out:
4017 return;
4018
Francois Romieufd112f22011-06-18 00:10:29 +02004019err_release_firmware:
4020 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004021err_free:
4022 kfree(rtl_fw);
4023err_warn:
4024 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4025 name, rc);
4026out_no_firmware:
4027 tp->rtl_fw = NULL;
4028 goto out;
4029}
4030
François Romieu953a12c2011-04-24 17:38:48 +02004031static void rtl_request_firmware(struct rtl8169_private *tp)
4032{
Francois Romieub6ffd972011-06-17 17:00:05 +02004033 if (IS_ERR(tp->rtl_fw))
4034 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004035}
4036
Francois Romieu4422bcd2012-01-26 11:23:32 +01004037static void rtl_task(struct work_struct *);
4038
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039static int rtl8169_open(struct net_device *dev)
4040{
4041 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00004042 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02004044 int retval = -ENOMEM;
4045
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004046 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
Neil Hormanc0cd8842010-03-29 13:16:02 -07004048 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004050 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004052 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
4053 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004055 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004057 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
4058 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02004060 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061
4062 retval = rtl8169_init_ring(dev);
4063 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02004064 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065
Francois Romieu4422bcd2012-01-26 11:23:32 +01004066 INIT_WORK(&tp->wk.work, rtl_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067
Francois Romieu99f252b2007-04-02 22:59:59 +02004068 smp_mb();
4069
François Romieu953a12c2011-04-24 17:38:48 +02004070 rtl_request_firmware(tp);
4071
Francois Romieufbac58f2007-10-04 22:51:38 +02004072 retval = request_irq(dev->irq, rtl8169_interrupt,
4073 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02004074 dev->name, dev);
4075 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02004076 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02004077
Francois Romieuda78dbf2012-01-26 14:18:23 +01004078 rtl_lock_work(tp);
4079
Francois Romieu6c4a70c2012-01-31 10:56:44 +01004080 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004081
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004082 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004083
françois romieueee3a962011-01-08 02:17:26 +00004084 rtl8169_init_phy(dev, tp);
4085
Francois Romieuda78dbf2012-01-26 14:18:23 +01004086 __rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00004087
françois romieu065c27c2011-01-03 15:08:12 +00004088 rtl_pll_power_up(tp);
4089
Francois Romieu07ce4062007-02-23 23:36:39 +01004090 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091
Francois Romieuda78dbf2012-01-26 14:18:23 +01004092 netif_start_queue(dev);
4093
4094 rtl_unlock_work(tp);
4095
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004096 tp->saved_wolopts = 0;
4097 pm_runtime_put_noidle(&pdev->dev);
4098
françois romieueee3a962011-01-08 02:17:26 +00004099 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100out:
4101 return retval;
4102
François Romieu953a12c2011-04-24 17:38:48 +02004103err_release_fw_2:
4104 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02004105 rtl8169_rx_clear(tp);
4106err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004107 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
4108 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004109 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02004110err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004111 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
4112 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004113 tp->TxDescArray = NULL;
4114err_pm_runtime_put:
4115 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 goto out;
4117}
4118
Hayes Wang92fc43b2011-07-06 15:58:03 +08004119static void rtl_rx_close(struct rtl8169_private *tp)
4120{
4121 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004122
Francois Romieu1687b562011-07-19 17:21:29 +02004123 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004124}
4125
françois romieue6de30d2011-01-03 15:08:37 +00004126static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127{
françois romieue6de30d2011-01-03 15:08:37 +00004128 void __iomem *ioaddr = tp->mmio_addr;
4129
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004131 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132
Hayes Wang92fc43b2011-07-06 15:58:03 +08004133 rtl_rx_close(tp);
4134
Hayes Wang5d2e1952011-02-22 17:26:22 +08004135 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004136 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4137 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00004138 while (RTL_R8(TxPoll) & NPQ)
4139 udelay(20);
Hayes Wangc2218922011-09-06 16:55:18 +08004140 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4141 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
4142 tp->mac_version == RTL_GIGA_MAC_VER_36) {
David S. Miller8decf862011-09-22 03:23:13 -04004143 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08004144 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4145 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004146 } else {
4147 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4148 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004149 }
4150
Hayes Wang92fc43b2011-07-06 15:58:03 +08004151 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152}
4153
Francois Romieu7f796d832007-06-11 23:04:41 +02004154static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004155{
4156 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004157
4158 /* Set DMA burst size and Interframe Gap Time */
4159 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4160 (InterFrameGap << TxInterFrameGapShift));
4161}
4162
Francois Romieu07ce4062007-02-23 23:36:39 +01004163static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164{
4165 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166
Francois Romieu07ce4062007-02-23 23:36:39 +01004167 tp->hw_start(dev);
4168
Francois Romieuda78dbf2012-01-26 14:18:23 +01004169 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004170}
4171
Francois Romieu7f796d832007-06-11 23:04:41 +02004172static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4173 void __iomem *ioaddr)
4174{
4175 /*
4176 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4177 * register to be written before TxDescAddrLow to work.
4178 * Switching from MMIO to I/O access fixes the issue as well.
4179 */
4180 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004181 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004182 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004183 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004184}
4185
4186static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4187{
4188 u16 cmd;
4189
4190 cmd = RTL_R16(CPlusCmd);
4191 RTL_W16(CPlusCmd, cmd);
4192 return cmd;
4193}
4194
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004195static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004196{
4197 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00004198 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004199}
4200
Francois Romieu6dccd162007-02-13 23:38:05 +01004201static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4202{
Francois Romieu37441002011-06-17 22:58:54 +02004203 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004204 u32 mac_version;
4205 u32 clk;
4206 u32 val;
4207 } cfg2_info [] = {
4208 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4209 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4210 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4211 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004212 };
4213 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004214 unsigned int i;
4215 u32 clk;
4216
4217 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004218 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004219 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4220 RTL_W32(0x7c, p->val);
4221 break;
4222 }
4223 }
4224}
4225
Francois Romieu07ce4062007-02-23 23:36:39 +01004226static void rtl_hw_start_8169(struct net_device *dev)
4227{
4228 struct rtl8169_private *tp = netdev_priv(dev);
4229 void __iomem *ioaddr = tp->mmio_addr;
4230 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004231
Francois Romieu9cb427b2006-11-02 00:10:16 +01004232 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4233 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4234 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4235 }
4236
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004238 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4239 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4240 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4241 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004242 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4243
Hayes Wange542a222011-07-06 15:58:04 +08004244 rtl_init_rxcfg(tp);
4245
françois romieuf0298f82011-01-03 15:07:42 +00004246 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004248 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
Francois Romieucecb5fd2011-04-01 10:21:07 +02004250 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4251 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4252 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4253 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004254 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255
Francois Romieu7f796d832007-06-11 23:04:41 +02004256 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004257
Francois Romieucecb5fd2011-04-01 10:21:07 +02004258 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4259 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004260 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004262 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 }
4264
Francois Romieubcf0bf92006-07-26 23:14:13 +02004265 RTL_W16(CPlusCmd, tp->cp_cmd);
4266
Francois Romieu6dccd162007-02-13 23:38:05 +01004267 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4268
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 /*
4270 * Undocumented corner. Supposedly:
4271 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4272 */
4273 RTL_W16(IntrMitigate, 0x0000);
4274
Francois Romieu7f796d832007-06-11 23:04:41 +02004275 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004276
Francois Romieucecb5fd2011-04-01 10:21:07 +02004277 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4278 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4279 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4280 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004281 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4282 rtl_set_rx_tx_config_registers(tp);
4283 }
4284
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004286
4287 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4288 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289
4290 RTL_W32(RxMissed, 0);
4291
Francois Romieu07ce4062007-02-23 23:36:39 +01004292 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293
4294 /* no early-rx interrupts */
4295 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004296}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297
françois romieu650e8d52011-01-03 15:08:29 +00004298static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004299{
4300 u32 csi;
4301
4302 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00004303 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4304}
4305
françois romieue6de30d2011-01-03 15:08:37 +00004306static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4307{
4308 rtl_csi_access_enable(ioaddr, 0x17000000);
4309}
4310
françois romieu650e8d52011-01-03 15:08:29 +00004311static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4312{
4313 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02004314}
4315
4316struct ephy_info {
4317 unsigned int offset;
4318 u16 mask;
4319 u16 bits;
4320};
4321
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004322static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004323{
4324 u16 w;
4325
4326 while (len-- > 0) {
4327 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4328 rtl_ephy_write(ioaddr, e->offset, w);
4329 e++;
4330 }
4331}
4332
Francois Romieub726e492008-06-28 12:22:59 +02004333static void rtl_disable_clock_request(struct pci_dev *pdev)
4334{
Jon Masone44daad2011-06-27 07:46:31 +00004335 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004336
4337 if (cap) {
4338 u16 ctl;
4339
4340 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4341 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4342 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4343 }
4344}
4345
françois romieue6de30d2011-01-03 15:08:37 +00004346static void rtl_enable_clock_request(struct pci_dev *pdev)
4347{
Jon Masone44daad2011-06-27 07:46:31 +00004348 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004349
4350 if (cap) {
4351 u16 ctl;
4352
4353 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4354 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4355 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4356 }
4357}
4358
Francois Romieub726e492008-06-28 12:22:59 +02004359#define R8168_CPCMD_QUIRK_MASK (\
4360 EnableBist | \
4361 Mac_dbgo_oe | \
4362 Force_half_dup | \
4363 Force_rxflow_en | \
4364 Force_txflow_en | \
4365 Cxpl_dbg_sel | \
4366 ASF | \
4367 PktCntrDisable | \
4368 Mac_dbgo_sel)
4369
Francois Romieu219a1e92008-06-28 11:58:39 +02004370static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4371{
Francois Romieub726e492008-06-28 12:22:59 +02004372 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4373
4374 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4375
Francois Romieu2e68ae42008-06-28 12:00:55 +02004376 rtl_tx_performance_tweak(pdev,
4377 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004378}
4379
4380static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4381{
4382 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004383
françois romieuf0298f82011-01-03 15:07:42 +00004384 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004385
4386 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004387}
4388
4389static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4390{
Francois Romieub726e492008-06-28 12:22:59 +02004391 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4392
4393 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4394
Francois Romieu219a1e92008-06-28 11:58:39 +02004395 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004396
4397 rtl_disable_clock_request(pdev);
4398
4399 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004400}
4401
Francois Romieuef3386f2008-06-29 12:24:30 +02004402static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004403{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004404 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004405 { 0x01, 0, 0x0001 },
4406 { 0x02, 0x0800, 0x1000 },
4407 { 0x03, 0, 0x0042 },
4408 { 0x06, 0x0080, 0x0000 },
4409 { 0x07, 0, 0x2000 }
4410 };
4411
françois romieu650e8d52011-01-03 15:08:29 +00004412 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004413
4414 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4415
Francois Romieu219a1e92008-06-28 11:58:39 +02004416 __rtl_hw_start_8168cp(ioaddr, pdev);
4417}
4418
Francois Romieuef3386f2008-06-29 12:24:30 +02004419static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4420{
françois romieu650e8d52011-01-03 15:08:29 +00004421 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004422
4423 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4424
4425 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4426
4427 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4428}
4429
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004430static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4431{
françois romieu650e8d52011-01-03 15:08:29 +00004432 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004433
4434 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4435
4436 /* Magic. */
4437 RTL_W8(DBG_REG, 0x20);
4438
françois romieuf0298f82011-01-03 15:07:42 +00004439 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004440
4441 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4442
4443 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4444}
4445
Francois Romieu219a1e92008-06-28 11:58:39 +02004446static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4447{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004448 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004449 { 0x02, 0x0800, 0x1000 },
4450 { 0x03, 0, 0x0002 },
4451 { 0x06, 0x0080, 0x0000 }
4452 };
4453
françois romieu650e8d52011-01-03 15:08:29 +00004454 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004455
4456 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4457
4458 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4459
Francois Romieu219a1e92008-06-28 11:58:39 +02004460 __rtl_hw_start_8168cp(ioaddr, pdev);
4461}
4462
4463static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4464{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004465 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004466 { 0x01, 0, 0x0001 },
4467 { 0x03, 0x0400, 0x0220 }
4468 };
4469
françois romieu650e8d52011-01-03 15:08:29 +00004470 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004471
4472 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4473
Francois Romieu219a1e92008-06-28 11:58:39 +02004474 __rtl_hw_start_8168cp(ioaddr, pdev);
4475}
4476
Francois Romieu197ff762008-06-28 13:16:02 +02004477static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4478{
4479 rtl_hw_start_8168c_2(ioaddr, pdev);
4480}
4481
Francois Romieu6fb07052008-06-29 11:54:28 +02004482static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4483{
françois romieu650e8d52011-01-03 15:08:29 +00004484 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004485
4486 __rtl_hw_start_8168cp(ioaddr, pdev);
4487}
4488
Francois Romieu5b538df2008-07-20 16:22:45 +02004489static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4490{
françois romieu650e8d52011-01-03 15:08:29 +00004491 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004492
4493 rtl_disable_clock_request(pdev);
4494
françois romieuf0298f82011-01-03 15:07:42 +00004495 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004496
4497 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4498
4499 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4500}
4501
hayeswang4804b3b2011-03-21 01:50:29 +00004502static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4503{
4504 rtl_csi_access_enable_1(ioaddr);
4505
4506 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4507
4508 RTL_W8(MaxTxPacketSize, TxPacketMax);
4509
4510 rtl_disable_clock_request(pdev);
4511}
4512
françois romieue6de30d2011-01-03 15:08:37 +00004513static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4514{
4515 static const struct ephy_info e_info_8168d_4[] = {
4516 { 0x0b, ~0, 0x48 },
4517 { 0x19, 0x20, 0x50 },
4518 { 0x0c, ~0, 0x20 }
4519 };
4520 int i;
4521
4522 rtl_csi_access_enable_1(ioaddr);
4523
4524 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4525
4526 RTL_W8(MaxTxPacketSize, TxPacketMax);
4527
4528 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4529 const struct ephy_info *e = e_info_8168d_4 + i;
4530 u16 w;
4531
4532 w = rtl_ephy_read(ioaddr, e->offset);
4533 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4534 }
4535
4536 rtl_enable_clock_request(pdev);
4537}
4538
Hayes Wang70090422011-07-06 15:58:06 +08004539static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
hayeswang01dc7fe2011-03-21 01:50:28 +00004540{
Hayes Wang70090422011-07-06 15:58:06 +08004541 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004542 { 0x00, 0x0200, 0x0100 },
4543 { 0x00, 0x0000, 0x0004 },
4544 { 0x06, 0x0002, 0x0001 },
4545 { 0x06, 0x0000, 0x0030 },
4546 { 0x07, 0x0000, 0x2000 },
4547 { 0x00, 0x0000, 0x0020 },
4548 { 0x03, 0x5800, 0x2000 },
4549 { 0x03, 0x0000, 0x0001 },
4550 { 0x01, 0x0800, 0x1000 },
4551 { 0x07, 0x0000, 0x4000 },
4552 { 0x1e, 0x0000, 0x2000 },
4553 { 0x19, 0xffff, 0xfe6c },
4554 { 0x0a, 0x0000, 0x0040 }
4555 };
4556
4557 rtl_csi_access_enable_2(ioaddr);
4558
Hayes Wang70090422011-07-06 15:58:06 +08004559 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004560
4561 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4562
4563 RTL_W8(MaxTxPacketSize, TxPacketMax);
4564
4565 rtl_disable_clock_request(pdev);
4566
4567 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004568 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4569 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004570
Francois Romieucecb5fd2011-04-01 10:21:07 +02004571 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004572}
4573
Hayes Wang70090422011-07-06 15:58:06 +08004574static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4575{
4576 static const struct ephy_info e_info_8168e_2[] = {
4577 { 0x09, 0x0000, 0x0080 },
4578 { 0x19, 0x0000, 0x0224 }
4579 };
4580
4581 rtl_csi_access_enable_1(ioaddr);
4582
4583 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4584
4585 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4586
4587 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4588 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4589 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4590 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4591 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4592 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4593 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4594 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4595 ERIAR_EXGMAC);
4596
Hayes Wang3090bd92011-09-06 16:55:15 +08004597 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004598
4599 rtl_disable_clock_request(pdev);
4600
4601 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4602 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4603
4604 /* Adjust EEE LED frequency */
4605 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4606
4607 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4608 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4609 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4610}
4611
Hayes Wangc2218922011-09-06 16:55:18 +08004612static void rtl_hw_start_8168f_1(void __iomem *ioaddr, struct pci_dev *pdev)
4613{
4614 static const struct ephy_info e_info_8168f_1[] = {
4615 { 0x06, 0x00c0, 0x0020 },
4616 { 0x08, 0x0001, 0x0002 },
4617 { 0x09, 0x0000, 0x0080 },
4618 { 0x19, 0x0000, 0x0224 }
4619 };
4620
4621 rtl_csi_access_enable_1(ioaddr);
4622
4623 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4624
4625 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4626
4627 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4628 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4629 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4630 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4631 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4632 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4633 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4634 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4635 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4636 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
4637 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4638 ERIAR_EXGMAC);
4639
4640 RTL_W8(MaxTxPacketSize, EarlySize);
4641
4642 rtl_disable_clock_request(pdev);
4643
4644 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4645 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4646
4647 /* Adjust EEE LED frequency */
4648 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4649
4650 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4651 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4652 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4653}
4654
Francois Romieu07ce4062007-02-23 23:36:39 +01004655static void rtl_hw_start_8168(struct net_device *dev)
4656{
Francois Romieu2dd99532007-06-11 23:22:52 +02004657 struct rtl8169_private *tp = netdev_priv(dev);
4658 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004659 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004660
4661 RTL_W8(Cfg9346, Cfg9346_Unlock);
4662
françois romieuf0298f82011-01-03 15:07:42 +00004663 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004664
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004665 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004666
Francois Romieu0e485152007-02-20 00:00:26 +01004667 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004668
4669 RTL_W16(CPlusCmd, tp->cp_cmd);
4670
Francois Romieu0e485152007-02-20 00:00:26 +01004671 RTL_W16(IntrMitigate, 0x5151);
4672
4673 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00004674 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01004675 tp->event_slow |= RxFIFOOver | PCSTimeout;
4676 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01004677 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004678
4679 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4680
Francois Romieub8363902008-06-01 12:31:57 +02004681 rtl_set_rx_mode(dev);
4682
4683 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4684 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004685
4686 RTL_R8(IntrMask);
4687
Francois Romieu219a1e92008-06-28 11:58:39 +02004688 switch (tp->mac_version) {
4689 case RTL_GIGA_MAC_VER_11:
4690 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004691 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004692
4693 case RTL_GIGA_MAC_VER_12:
4694 case RTL_GIGA_MAC_VER_17:
4695 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004696 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004697
4698 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004699 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004700 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004701
4702 case RTL_GIGA_MAC_VER_19:
4703 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004704 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004705
4706 case RTL_GIGA_MAC_VER_20:
4707 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004708 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004709
Francois Romieu197ff762008-06-28 13:16:02 +02004710 case RTL_GIGA_MAC_VER_21:
4711 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004712 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004713
Francois Romieu6fb07052008-06-29 11:54:28 +02004714 case RTL_GIGA_MAC_VER_22:
4715 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004716 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004717
Francois Romieuef3386f2008-06-29 12:24:30 +02004718 case RTL_GIGA_MAC_VER_23:
4719 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004720 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004721
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004722 case RTL_GIGA_MAC_VER_24:
4723 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004724 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004725
Francois Romieu5b538df2008-07-20 16:22:45 +02004726 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004727 case RTL_GIGA_MAC_VER_26:
4728 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004729 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004730 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004731
françois romieue6de30d2011-01-03 15:08:37 +00004732 case RTL_GIGA_MAC_VER_28:
4733 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004734 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004735
hayeswang4804b3b2011-03-21 01:50:29 +00004736 case RTL_GIGA_MAC_VER_31:
4737 rtl_hw_start_8168dp(ioaddr, pdev);
4738 break;
4739
hayeswang01dc7fe2011-03-21 01:50:28 +00004740 case RTL_GIGA_MAC_VER_32:
4741 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004742 rtl_hw_start_8168e_1(ioaddr, pdev);
4743 break;
4744 case RTL_GIGA_MAC_VER_34:
4745 rtl_hw_start_8168e_2(ioaddr, pdev);
hayeswang01dc7fe2011-03-21 01:50:28 +00004746 break;
françois romieue6de30d2011-01-03 15:08:37 +00004747
Hayes Wangc2218922011-09-06 16:55:18 +08004748 case RTL_GIGA_MAC_VER_35:
4749 case RTL_GIGA_MAC_VER_36:
4750 rtl_hw_start_8168f_1(ioaddr, pdev);
4751 break;
4752
Francois Romieu219a1e92008-06-28 11:58:39 +02004753 default:
4754 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4755 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004756 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004757 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004758
Francois Romieu0e485152007-02-20 00:00:26 +01004759 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4760
Francois Romieub8363902008-06-01 12:31:57 +02004761 RTL_W8(Cfg9346, Cfg9346_Lock);
4762
Francois Romieu2dd99532007-06-11 23:22:52 +02004763 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004764}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765
Francois Romieu2857ffb2008-08-02 21:08:49 +02004766#define R810X_CPCMD_QUIRK_MASK (\
4767 EnableBist | \
4768 Mac_dbgo_oe | \
4769 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004770 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004771 Force_txflow_en | \
4772 Cxpl_dbg_sel | \
4773 ASF | \
4774 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004775 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004776
4777static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4778{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004779 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004780 { 0x01, 0, 0x6e65 },
4781 { 0x02, 0, 0x091f },
4782 { 0x03, 0, 0xc2f9 },
4783 { 0x06, 0, 0xafb5 },
4784 { 0x07, 0, 0x0e00 },
4785 { 0x19, 0, 0xec80 },
4786 { 0x01, 0, 0x2e65 },
4787 { 0x01, 0, 0x6e65 }
4788 };
4789 u8 cfg1;
4790
françois romieu650e8d52011-01-03 15:08:29 +00004791 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004792
4793 RTL_W8(DBG_REG, FIX_NAK_1);
4794
4795 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4796
4797 RTL_W8(Config1,
4798 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4799 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4800
4801 cfg1 = RTL_R8(Config1);
4802 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4803 RTL_W8(Config1, cfg1 & ~LEDS0);
4804
Francois Romieu2857ffb2008-08-02 21:08:49 +02004805 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4806}
4807
4808static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4809{
françois romieu650e8d52011-01-03 15:08:29 +00004810 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004811
4812 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4813
4814 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4815 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004816}
4817
4818static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4819{
4820 rtl_hw_start_8102e_2(ioaddr, pdev);
4821
4822 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4823}
4824
Hayes Wang5a5e4442011-02-22 17:26:21 +08004825static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4826{
4827 static const struct ephy_info e_info_8105e_1[] = {
4828 { 0x07, 0, 0x4000 },
4829 { 0x19, 0, 0x0200 },
4830 { 0x19, 0, 0x0020 },
4831 { 0x1e, 0, 0x2000 },
4832 { 0x03, 0, 0x0001 },
4833 { 0x19, 0, 0x0100 },
4834 { 0x19, 0, 0x0004 },
4835 { 0x0a, 0, 0x0020 }
4836 };
4837
Francois Romieucecb5fd2011-04-01 10:21:07 +02004838 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004839 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4840
Francois Romieucecb5fd2011-04-01 10:21:07 +02004841 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004842 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4843
4844 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e2011-07-06 15:58:02 +08004845 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004846
4847 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4848}
4849
4850static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4851{
4852 rtl_hw_start_8105e_1(ioaddr, pdev);
4853 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4854}
4855
Francois Romieu07ce4062007-02-23 23:36:39 +01004856static void rtl_hw_start_8101(struct net_device *dev)
4857{
Francois Romieucdf1a602007-06-11 23:29:50 +02004858 struct rtl8169_private *tp = netdev_priv(dev);
4859 void __iomem *ioaddr = tp->mmio_addr;
4860 struct pci_dev *pdev = tp->pci_dev;
4861
Francois Romieuda78dbf2012-01-26 14:18:23 +01004862 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
4863 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00004864
Francois Romieucecb5fd2011-04-01 10:21:07 +02004865 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4866 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00004867 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02004868
4869 if (cap) {
4870 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4871 PCI_EXP_DEVCTL_NOSNOOP_EN);
4872 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004873 }
4874
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004875 RTL_W8(Cfg9346, Cfg9346_Unlock);
4876
Francois Romieu2857ffb2008-08-02 21:08:49 +02004877 switch (tp->mac_version) {
4878 case RTL_GIGA_MAC_VER_07:
4879 rtl_hw_start_8102e_1(ioaddr, pdev);
4880 break;
4881
4882 case RTL_GIGA_MAC_VER_08:
4883 rtl_hw_start_8102e_3(ioaddr, pdev);
4884 break;
4885
4886 case RTL_GIGA_MAC_VER_09:
4887 rtl_hw_start_8102e_2(ioaddr, pdev);
4888 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004889
4890 case RTL_GIGA_MAC_VER_29:
4891 rtl_hw_start_8105e_1(ioaddr, pdev);
4892 break;
4893 case RTL_GIGA_MAC_VER_30:
4894 rtl_hw_start_8105e_2(ioaddr, pdev);
4895 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004896 }
4897
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004898 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004899
françois romieuf0298f82011-01-03 15:07:42 +00004900 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004901
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004902 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004903
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004904 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004905 RTL_W16(CPlusCmd, tp->cp_cmd);
4906
4907 RTL_W16(IntrMitigate, 0x0000);
4908
4909 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4910
4911 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4912 rtl_set_rx_tx_config_registers(tp);
4913
Francois Romieucdf1a602007-06-11 23:29:50 +02004914 RTL_R8(IntrMask);
4915
Francois Romieucdf1a602007-06-11 23:29:50 +02004916 rtl_set_rx_mode(dev);
4917
4918 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919}
4920
4921static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4922{
Francois Romieud58d46b2011-05-03 16:38:29 +02004923 struct rtl8169_private *tp = netdev_priv(dev);
4924
4925 if (new_mtu < ETH_ZLEN ||
4926 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 return -EINVAL;
4928
Francois Romieud58d46b2011-05-03 16:38:29 +02004929 if (new_mtu > ETH_DATA_LEN)
4930 rtl_hw_jumbo_enable(tp);
4931 else
4932 rtl_hw_jumbo_disable(tp);
4933
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004935 netdev_update_features(dev);
4936
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004937 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938}
4939
4940static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4941{
Al Viro95e09182007-12-22 18:55:39 +00004942 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4944}
4945
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004946static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4947 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004949 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004950 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004951
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004952 kfree(*data_buff);
4953 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 rtl8169_make_unusable_by_asic(desc);
4955}
4956
4957static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4958{
4959 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4960
4961 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4962}
4963
4964static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4965 u32 rx_buf_sz)
4966{
4967 desc->addr = cpu_to_le64(mapping);
4968 wmb();
4969 rtl8169_mark_to_asic(desc, rx_buf_sz);
4970}
4971
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004972static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004974 return (void *)ALIGN((long)data, 16);
4975}
4976
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004977static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4978 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004979{
4980 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004982 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004983 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004984 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004986 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4987 if (!data)
4988 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004989
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004990 if (rtl8169_align(data) != data) {
4991 kfree(data);
4992 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4993 if (!data)
4994 return NULL;
4995 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004996
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004997 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004998 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004999 if (unlikely(dma_mapping_error(d, mapping))) {
5000 if (net_ratelimit())
5001 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005002 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004
5005 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005006 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005007
5008err_out:
5009 kfree(data);
5010 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011}
5012
5013static void rtl8169_rx_clear(struct rtl8169_private *tp)
5014{
Francois Romieu07d3f512007-02-21 22:40:46 +01005015 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016
5017 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005018 if (tp->Rx_databuff[i]) {
5019 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 tp->RxDescArray + i);
5021 }
5022 }
5023}
5024
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005025static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005027 desc->opts1 |= cpu_to_le32(RingEnd);
5028}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005029
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005030static int rtl8169_rx_fill(struct rtl8169_private *tp)
5031{
5032 unsigned int i;
5033
5034 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005035 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005036
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005037 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005039
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005040 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005041 if (!data) {
5042 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005043 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005044 }
5045 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005048 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5049 return 0;
5050
5051err_out:
5052 rtl8169_rx_clear(tp);
5053 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054}
5055
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056static int rtl8169_init_ring(struct net_device *dev)
5057{
5058 struct rtl8169_private *tp = netdev_priv(dev);
5059
5060 rtl8169_init_ring_indexes(tp);
5061
5062 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005063 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005065 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066}
5067
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005068static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 struct TxDesc *desc)
5070{
5071 unsigned int len = tx_skb->len;
5072
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005073 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5074
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 desc->opts1 = 0x00;
5076 desc->opts2 = 0x00;
5077 desc->addr = 0x00;
5078 tx_skb->len = 0;
5079}
5080
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005081static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5082 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083{
5084 unsigned int i;
5085
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005086 for (i = 0; i < n; i++) {
5087 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 struct ring_info *tx_skb = tp->tx_skb + entry;
5089 unsigned int len = tx_skb->len;
5090
5091 if (len) {
5092 struct sk_buff *skb = tx_skb->skb;
5093
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005094 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 tp->TxDescArray + entry);
5096 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005097 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 dev_kfree_skb(skb);
5099 tx_skb->skb = NULL;
5100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 }
5102 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005103}
5104
5105static void rtl8169_tx_clear(struct rtl8169_private *tp)
5106{
5107 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 tp->cur_tx = tp->dirty_tx = 0;
Igor Maravic036dafa2012-03-05 00:01:25 +01005109 netdev_reset_queue(tp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110}
5111
Francois Romieu4422bcd2012-01-26 11:23:32 +01005112static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113{
David Howellsc4028952006-11-22 14:57:56 +00005114 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005115 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116
Francois Romieuda78dbf2012-01-26 14:18:23 +01005117 napi_disable(&tp->napi);
5118 netif_stop_queue(dev);
5119 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120
françois romieuc7c2c392011-12-04 20:30:52 +00005121 rtl8169_hw_reset(tp);
5122
Francois Romieu56de4142011-03-15 17:29:31 +01005123 for (i = 0; i < NUM_RX_DESC; i++)
5124 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5125
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005127 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128
Francois Romieuda78dbf2012-01-26 14:18:23 +01005129 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01005130 rtl_hw_start(dev);
5131 netif_wake_queue(dev);
5132 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133}
5134
5135static void rtl8169_tx_timeout(struct net_device *dev)
5136{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005137 struct rtl8169_private *tp = netdev_priv(dev);
5138
5139 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140}
5141
5142static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005143 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144{
5145 struct skb_shared_info *info = skb_shinfo(skb);
5146 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005147 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005148 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149
5150 entry = tp->cur_tx;
5151 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005152 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 dma_addr_t mapping;
5154 u32 status, len;
5155 void *addr;
5156
5157 entry = (entry + 1) % NUM_TX_DESC;
5158
5159 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005160 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005161 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005162 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005163 if (unlikely(dma_mapping_error(d, mapping))) {
5164 if (net_ratelimit())
5165 netif_err(tp, drv, tp->dev,
5166 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005167 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169
Francois Romieucecb5fd2011-04-01 10:21:07 +02005170 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005171 status = opts[0] | len |
5172 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173
5174 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005175 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 txd->addr = cpu_to_le64(mapping);
5177
5178 tp->tx_skb[entry].len = len;
5179 }
5180
5181 if (cur_frag) {
5182 tp->tx_skb[entry].skb = skb;
5183 txd->opts1 |= cpu_to_le32(LastFrag);
5184 }
5185
5186 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005187
5188err_out:
5189 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5190 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191}
5192
Francois Romieu2b7b4312011-04-18 22:53:24 -07005193static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5194 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005196 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005197 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005198 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199
Francois Romieu2b7b4312011-04-18 22:53:24 -07005200 if (mss) {
5201 opts[0] |= TD_LSO;
5202 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5203 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005204 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205
5206 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005207 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005209 opts[offset] |= info->checksum.udp;
5210 else
5211 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213}
5214
Stephen Hemminger613573252009-08-31 19:50:58 +00005215static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5216 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217{
5218 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005219 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 struct TxDesc *txd = tp->TxDescArray + entry;
5221 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005222 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 dma_addr_t mapping;
5224 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005225 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005226 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005227
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005229 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005230 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 }
5232
5233 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005234 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005236 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005237 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005238 if (unlikely(dma_mapping_error(d, mapping))) {
5239 if (net_ratelimit())
5240 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005241 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243
5244 tp->tx_skb[entry].len = len;
5245 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246
Francois Romieu2b7b4312011-04-18 22:53:24 -07005247 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5248 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005249
Francois Romieu2b7b4312011-04-18 22:53:24 -07005250 rtl8169_tso_csum(tp, skb, opts);
5251
5252 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005253 if (frags < 0)
5254 goto err_dma_1;
5255 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005256 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005257 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005258 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005259 tp->tx_skb[entry].skb = skb;
5260 }
5261
Francois Romieu2b7b4312011-04-18 22:53:24 -07005262 txd->opts2 = cpu_to_le32(opts[1]);
5263
Igor Maravic036dafa2012-03-05 00:01:25 +01005264 netdev_sent_queue(dev, skb->len);
5265
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 wmb();
5267
Francois Romieucecb5fd2011-04-01 10:21:07 +02005268 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005269 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 txd->opts1 = cpu_to_le32(status);
5271
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 tp->cur_tx += frags + 1;
5273
David Dillow4c020a92010-03-03 16:33:10 +00005274 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275
Francois Romieucecb5fd2011-04-01 10:21:07 +02005276 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277
Francois Romieuda78dbf2012-01-26 14:18:23 +01005278 mmiowb();
5279
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01005281 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5282 * not miss a ring update when it notices a stopped queue.
5283 */
5284 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01005286 /* Sync with rtl_tx:
5287 * - publish queue status and cur_tx ring index (write barrier)
5288 * - refresh dirty_tx ring index (read barrier).
5289 * May the current thread have a pessimistic view of the ring
5290 * status and forget to wake up queue, a racing rtl_tx thread
5291 * can't.
5292 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005293 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
5295 netif_wake_queue(dev);
5296 }
5297
Stephen Hemminger613573252009-08-31 19:50:58 +00005298 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005300err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005301 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005302err_dma_0:
5303 dev_kfree_skb(skb);
5304 dev->stats.tx_dropped++;
5305 return NETDEV_TX_OK;
5306
5307err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005309 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005310 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311}
5312
5313static void rtl8169_pcierr_interrupt(struct net_device *dev)
5314{
5315 struct rtl8169_private *tp = netdev_priv(dev);
5316 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 u16 pci_status, pci_cmd;
5318
5319 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5320 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5321
Joe Perchesbf82c182010-02-09 11:49:50 +00005322 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5323 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324
5325 /*
5326 * The recovery sequence below admits a very elaborated explanation:
5327 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005328 * - I did not see what else could be done;
5329 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 *
5331 * Feel free to adjust to your needs.
5332 */
Francois Romieua27993f2006-12-18 00:04:19 +01005333 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005334 pci_cmd &= ~PCI_COMMAND_PARITY;
5335 else
5336 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5337
5338 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339
5340 pci_write_config_word(pdev, PCI_STATUS,
5341 pci_status & (PCI_STATUS_DETECTED_PARITY |
5342 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5343 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5344
5345 /* The infamous DAC f*ckup only happens at boot time */
5346 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005347 void __iomem *ioaddr = tp->mmio_addr;
5348
Joe Perchesbf82c182010-02-09 11:49:50 +00005349 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 tp->cp_cmd &= ~PCIDAC;
5351 RTL_W16(CPlusCmd, tp->cp_cmd);
5352 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 }
5354
françois romieue6de30d2011-01-03 15:08:37 +00005355 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005356
Francois Romieu98ddf982012-01-31 10:47:34 +01005357 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358}
5359
Igor Maravic036dafa2012-03-05 00:01:25 +01005360struct rtl_txc {
5361 int packets;
5362 int bytes;
5363};
5364
Francois Romieuda78dbf2012-01-26 14:18:23 +01005365static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366{
Igor Maravic036dafa2012-03-05 00:01:25 +01005367 struct rtl8169_stats *tx_stats = &tp->tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 unsigned int dirty_tx, tx_left;
Igor Maravic036dafa2012-03-05 00:01:25 +01005369 struct rtl_txc txc = { 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 dirty_tx = tp->dirty_tx;
5372 smp_rmb();
5373 tx_left = tp->cur_tx - dirty_tx;
5374
5375 while (tx_left > 0) {
5376 unsigned int entry = dirty_tx % NUM_TX_DESC;
5377 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 u32 status;
5379
5380 rmb();
5381 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5382 if (status & DescOwn)
5383 break;
5384
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005385 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5386 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 if (status & LastFrag) {
Igor Maravic036dafa2012-03-05 00:01:25 +01005388 struct sk_buff *skb = tx_skb->skb;
5389
5390 txc.packets++;
5391 txc.bytes += skb->len;
5392 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 tx_skb->skb = NULL;
5394 }
5395 dirty_tx++;
5396 tx_left--;
5397 }
5398
Igor Maravic036dafa2012-03-05 00:01:25 +01005399 u64_stats_update_begin(&tx_stats->syncp);
5400 tx_stats->packets += txc.packets;
5401 tx_stats->bytes += txc.bytes;
5402 u64_stats_update_end(&tx_stats->syncp);
5403
5404 netdev_completed_queue(dev, txc.packets, txc.bytes);
5405
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 if (tp->dirty_tx != dirty_tx) {
5407 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005408 /* Sync with rtl8169_start_xmit:
5409 * - publish dirty_tx ring index (write barrier)
5410 * - refresh cur_tx ring index and queue status (read barrier)
5411 * May the current thread miss the stopped queue condition,
5412 * a racing xmit thread can only have a right view of the
5413 * ring status.
5414 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005415 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 if (netif_queue_stopped(dev) &&
5417 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5418 netif_wake_queue(dev);
5419 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005420 /*
5421 * 8168 hack: TxPoll requests are lost when the Tx packets are
5422 * too close. Let's kick an extra TxPoll request when a burst
5423 * of start_xmit activity is detected (if it is not detected,
5424 * it is slow enough). -- FR
5425 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005426 if (tp->cur_tx != dirty_tx) {
5427 void __iomem *ioaddr = tp->mmio_addr;
5428
Francois Romieud78ae2d2007-08-26 20:08:19 +02005429 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 }
5432}
5433
Francois Romieu126fa4b2005-05-12 20:09:17 -04005434static inline int rtl8169_fragmented_frame(u32 status)
5435{
5436 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5437}
5438
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005439static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 u32 status = opts1 & RxProtoMask;
5442
5443 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005444 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 skb->ip_summed = CHECKSUM_UNNECESSARY;
5446 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005447 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448}
5449
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005450static struct sk_buff *rtl8169_try_rx_copy(void *data,
5451 struct rtl8169_private *tp,
5452 int pkt_size,
5453 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005455 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005456 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005458 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005459 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005460 prefetch(data);
5461 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5462 if (skb)
5463 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005464 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5465
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005466 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467}
5468
Francois Romieuda78dbf2012-01-26 14:18:23 +01005469static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470{
5471 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005472 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 cur_rx = tp->cur_rx;
5475 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005476 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005478 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005480 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 u32 status;
5482
5483 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005484 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485
5486 if (status & DescOwn)
5487 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005488 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005489 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5490 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005491 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005493 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005495 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005496 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005497 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005498 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005499 }
Ben Greear6bbe0212012-02-10 15:04:33 +00005500 if ((status & (RxRUNT | RxCRC)) &&
5501 !(status & (RxRWT | RxFOVF)) &&
5502 (dev->features & NETIF_F_RXALL))
5503 goto process_pkt;
5504
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005505 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005507 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00005508 dma_addr_t addr;
5509 int pkt_size;
5510
5511process_pkt:
5512 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00005513 if (likely(!(dev->features & NETIF_F_RXFCS)))
5514 pkt_size = (status & 0x00003fff) - 4;
5515 else
5516 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517
Francois Romieu126fa4b2005-05-12 20:09:17 -04005518 /*
5519 * The driver does not support incoming fragmented
5520 * frames. They are seen as a symptom of over-mtu
5521 * sized frames.
5522 */
5523 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005524 dev->stats.rx_dropped++;
5525 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005526 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005527 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005528 }
5529
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005530 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5531 tp, pkt_size, addr);
5532 rtl8169_mark_to_asic(desc, rx_buf_sz);
5533 if (!skb) {
5534 dev->stats.rx_dropped++;
5535 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 }
5537
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005538 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 skb_put(skb, pkt_size);
5540 skb->protocol = eth_type_trans(skb, dev);
5541
Francois Romieu7a8fc772011-03-01 17:18:33 +01005542 rtl8169_rx_vlan_tag(desc, skb);
5543
Francois Romieu56de4142011-03-15 17:29:31 +01005544 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545
Junchang Wang8027aa22012-03-04 23:30:32 +01005546 u64_stats_update_begin(&tp->rx_stats.syncp);
5547 tp->rx_stats.packets++;
5548 tp->rx_stats.bytes += pkt_size;
5549 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005551
5552 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005553 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005554 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5555 desc->opts2 = 0;
5556 cur_rx++;
5557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 }
5559
5560 count = cur_rx - tp->cur_rx;
5561 tp->cur_rx = cur_rx;
5562
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005563 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564
5565 return count;
5566}
5567
Francois Romieu07d3f512007-02-21 22:40:46 +01005568static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569{
Francois Romieu07d3f512007-02-21 22:40:46 +01005570 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005573 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005575 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005576 if (status && status != 0xffff) {
5577 status &= RTL_EVENT_NAPI | tp->event_slow;
5578 if (status) {
5579 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00005580
Francois Romieuda78dbf2012-01-26 14:18:23 +01005581 rtl_irq_disable(tp);
5582 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 return IRQ_RETVAL(handled);
5586}
5587
Francois Romieuda78dbf2012-01-26 14:18:23 +01005588/*
5589 * Workqueue context.
5590 */
5591static void rtl_slow_event_work(struct rtl8169_private *tp)
5592{
5593 struct net_device *dev = tp->dev;
5594 u16 status;
5595
5596 status = rtl_get_events(tp) & tp->event_slow;
5597 rtl_ack_events(tp, status);
5598
5599 if (unlikely(status & RxFIFOOver)) {
5600 switch (tp->mac_version) {
5601 /* Work around for rx fifo overflow */
5602 case RTL_GIGA_MAC_VER_11:
5603 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01005604 /* XXX - Hack alert. See rtl_task(). */
5605 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005606 default:
5607 break;
5608 }
5609 }
5610
5611 if (unlikely(status & SYSErr))
5612 rtl8169_pcierr_interrupt(dev);
5613
5614 if (status & LinkChg)
5615 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
5616
5617 napi_disable(&tp->napi);
5618 rtl_irq_disable(tp);
5619
5620 napi_enable(&tp->napi);
5621 napi_schedule(&tp->napi);
5622}
5623
Francois Romieu4422bcd2012-01-26 11:23:32 +01005624static void rtl_task(struct work_struct *work)
5625{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005626 static const struct {
5627 int bitnr;
5628 void (*action)(struct rtl8169_private *);
5629 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01005630 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005631 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
5632 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
5633 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
5634 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01005635 struct rtl8169_private *tp =
5636 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005637 struct net_device *dev = tp->dev;
5638 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01005639
Francois Romieuda78dbf2012-01-26 14:18:23 +01005640 rtl_lock_work(tp);
5641
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005642 if (!netif_running(dev) ||
5643 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01005644 goto out_unlock;
5645
5646 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
5647 bool pending;
5648
Francois Romieuda78dbf2012-01-26 14:18:23 +01005649 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005650 if (pending)
5651 rtl_work[i].action(tp);
5652 }
5653
5654out_unlock:
5655 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01005656}
5657
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005658static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005660 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5661 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005662 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
5663 int work_done= 0;
5664 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665
Francois Romieuda78dbf2012-01-26 14:18:23 +01005666 status = rtl_get_events(tp);
5667 rtl_ack_events(tp, status & ~tp->event_slow);
5668
5669 if (status & RTL_EVENT_NAPI_RX)
5670 work_done = rtl_rx(dev, tp, (u32) budget);
5671
5672 if (status & RTL_EVENT_NAPI_TX)
5673 rtl_tx(dev, tp);
5674
5675 if (status & tp->event_slow) {
5676 enable_mask &= ~tp->event_slow;
5677
5678 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
5679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005681 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005682 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005683
Francois Romieuda78dbf2012-01-26 14:18:23 +01005684 rtl_irq_enable(tp, enable_mask);
5685 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 }
5687
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005688 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690
Francois Romieu523a6092008-09-10 22:28:56 +02005691static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5692{
5693 struct rtl8169_private *tp = netdev_priv(dev);
5694
5695 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5696 return;
5697
5698 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5699 RTL_W32(RxMissed, 0);
5700}
5701
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702static void rtl8169_down(struct net_device *dev)
5703{
5704 struct rtl8169_private *tp = netdev_priv(dev);
5705 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706
Francois Romieu4876cc12011-03-11 21:07:11 +01005707 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005709 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005710 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711
Hayes Wang92fc43b2011-07-06 15:58:03 +08005712 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005713 /*
5714 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01005715 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
5716 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005717 */
Francois Romieu523a6092008-09-10 22:28:56 +02005718 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005721 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 rtl8169_tx_clear(tp);
5724
5725 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005726
5727 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728}
5729
5730static int rtl8169_close(struct net_device *dev)
5731{
5732 struct rtl8169_private *tp = netdev_priv(dev);
5733 struct pci_dev *pdev = tp->pci_dev;
5734
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005735 pm_runtime_get_sync(&pdev->dev);
5736
Francois Romieucecb5fd2011-04-01 10:21:07 +02005737 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005738 rtl8169_update_counters(dev);
5739
Francois Romieuda78dbf2012-01-26 14:18:23 +01005740 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005741 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005742
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005744 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745
5746 free_irq(dev->irq, dev);
5747
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005748 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5749 tp->RxPhyAddr);
5750 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5751 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 tp->TxDescArray = NULL;
5753 tp->RxDescArray = NULL;
5754
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005755 pm_runtime_put_sync(&pdev->dev);
5756
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 return 0;
5758}
5759
Francois Romieu07ce4062007-02-23 23:36:39 +01005760static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761{
5762 struct rtl8169_private *tp = netdev_priv(dev);
5763 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005765 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 u32 tmp = 0;
5767
5768 if (dev->flags & IFF_PROMISC) {
5769 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005770 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 rx_mode =
5772 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5773 AcceptAllPhys;
5774 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005775 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005776 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 /* Too many to filter perfectly -- accept all multicasts. */
5778 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5779 mc_filter[1] = mc_filter[0] = 0xffffffff;
5780 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +00005781 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01005782
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 rx_mode = AcceptBroadcast | AcceptMyPhys;
5784 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +00005785 netdev_for_each_mc_addr(ha, dev) {
5786 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5788 rx_mode |= AcceptMulticast;
5789 }
5790 }
5791
Ben Greear6bbe0212012-02-10 15:04:33 +00005792 if (dev->features & NETIF_F_RXALL)
5793 rx_mode |= (AcceptErr | AcceptRunt);
5794
Francois Romieu1687b562011-07-19 17:21:29 +02005795 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796
Francois Romieuf887cce2008-07-17 22:24:18 +02005797 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01005798 u32 data = mc_filter[0];
5799
5800 mc_filter[0] = swab32(mc_filter[1]);
5801 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005802 }
5803
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07005805 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806
Francois Romieu57a9f232007-06-04 22:10:15 +02005807 RTL_W32(RxConfig, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808}
5809
Junchang Wang8027aa22012-03-04 23:30:32 +01005810static struct rtnl_link_stats64 *
5811rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812{
5813 struct rtl8169_private *tp = netdev_priv(dev);
5814 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01005815 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816
Francois Romieuda78dbf2012-01-26 14:18:23 +01005817 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02005818 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02005819
Junchang Wang8027aa22012-03-04 23:30:32 +01005820 do {
5821 start = u64_stats_fetch_begin_bh(&tp->rx_stats.syncp);
5822 stats->rx_packets = tp->rx_stats.packets;
5823 stats->rx_bytes = tp->rx_stats.bytes;
5824 } while (u64_stats_fetch_retry_bh(&tp->rx_stats.syncp, start));
5825
5826
5827 do {
5828 start = u64_stats_fetch_begin_bh(&tp->tx_stats.syncp);
5829 stats->tx_packets = tp->tx_stats.packets;
5830 stats->tx_bytes = tp->tx_stats.bytes;
5831 } while (u64_stats_fetch_retry_bh(&tp->tx_stats.syncp, start));
5832
5833 stats->rx_dropped = dev->stats.rx_dropped;
5834 stats->tx_dropped = dev->stats.tx_dropped;
5835 stats->rx_length_errors = dev->stats.rx_length_errors;
5836 stats->rx_errors = dev->stats.rx_errors;
5837 stats->rx_crc_errors = dev->stats.rx_crc_errors;
5838 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
5839 stats->rx_missed_errors = dev->stats.rx_missed_errors;
5840
5841 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842}
5843
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005844static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005845{
françois romieu065c27c2011-01-03 15:08:12 +00005846 struct rtl8169_private *tp = netdev_priv(dev);
5847
Francois Romieu5d06a992006-02-23 00:47:58 +01005848 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005849 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005850
5851 netif_device_detach(dev);
5852 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005853
5854 rtl_lock_work(tp);
5855 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005856 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005857 rtl_unlock_work(tp);
5858
5859 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005860}
Francois Romieu5d06a992006-02-23 00:47:58 +01005861
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005862#ifdef CONFIG_PM
5863
5864static int rtl8169_suspend(struct device *device)
5865{
5866 struct pci_dev *pdev = to_pci_dev(device);
5867 struct net_device *dev = pci_get_drvdata(pdev);
5868
5869 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005870
Francois Romieu5d06a992006-02-23 00:47:58 +01005871 return 0;
5872}
5873
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005874static void __rtl8169_resume(struct net_device *dev)
5875{
françois romieu065c27c2011-01-03 15:08:12 +00005876 struct rtl8169_private *tp = netdev_priv(dev);
5877
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005878 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005879
5880 rtl_pll_power_up(tp);
5881
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005882 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005883
Francois Romieu98ddf982012-01-31 10:47:34 +01005884 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005885}
5886
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005887static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005888{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005889 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005890 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005891 struct rtl8169_private *tp = netdev_priv(dev);
5892
5893 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005894
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005895 if (netif_running(dev))
5896 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005897
Francois Romieu5d06a992006-02-23 00:47:58 +01005898 return 0;
5899}
5900
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005901static int rtl8169_runtime_suspend(struct device *device)
5902{
5903 struct pci_dev *pdev = to_pci_dev(device);
5904 struct net_device *dev = pci_get_drvdata(pdev);
5905 struct rtl8169_private *tp = netdev_priv(dev);
5906
5907 if (!tp->TxDescArray)
5908 return 0;
5909
Francois Romieuda78dbf2012-01-26 14:18:23 +01005910 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005911 tp->saved_wolopts = __rtl8169_get_wol(tp);
5912 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005913 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005914
5915 rtl8169_net_suspend(dev);
5916
5917 return 0;
5918}
5919
5920static int rtl8169_runtime_resume(struct device *device)
5921{
5922 struct pci_dev *pdev = to_pci_dev(device);
5923 struct net_device *dev = pci_get_drvdata(pdev);
5924 struct rtl8169_private *tp = netdev_priv(dev);
5925
5926 if (!tp->TxDescArray)
5927 return 0;
5928
Francois Romieuda78dbf2012-01-26 14:18:23 +01005929 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005930 __rtl8169_set_wol(tp, tp->saved_wolopts);
5931 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005932 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005933
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005934 rtl8169_init_phy(dev, tp);
5935
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005936 __rtl8169_resume(dev);
5937
5938 return 0;
5939}
5940
5941static int rtl8169_runtime_idle(struct device *device)
5942{
5943 struct pci_dev *pdev = to_pci_dev(device);
5944 struct net_device *dev = pci_get_drvdata(pdev);
5945 struct rtl8169_private *tp = netdev_priv(dev);
5946
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005947 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005948}
5949
Alexey Dobriyan47145212009-12-14 18:00:08 -08005950static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005951 .suspend = rtl8169_suspend,
5952 .resume = rtl8169_resume,
5953 .freeze = rtl8169_suspend,
5954 .thaw = rtl8169_resume,
5955 .poweroff = rtl8169_suspend,
5956 .restore = rtl8169_resume,
5957 .runtime_suspend = rtl8169_runtime_suspend,
5958 .runtime_resume = rtl8169_runtime_resume,
5959 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005960};
5961
5962#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5963
5964#else /* !CONFIG_PM */
5965
5966#define RTL8169_PM_OPS NULL
5967
5968#endif /* !CONFIG_PM */
5969
David S. Miller1805b2f2011-10-24 18:18:09 -04005970static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
5971{
5972 void __iomem *ioaddr = tp->mmio_addr;
5973
5974 /* WoL fails with 8168b when the receiver is disabled. */
5975 switch (tp->mac_version) {
5976 case RTL_GIGA_MAC_VER_11:
5977 case RTL_GIGA_MAC_VER_12:
5978 case RTL_GIGA_MAC_VER_17:
5979 pci_clear_master(tp->pci_dev);
5980
5981 RTL_W8(ChipCmd, CmdRxEnb);
5982 /* PCI commit */
5983 RTL_R8(ChipCmd);
5984 break;
5985 default:
5986 break;
5987 }
5988}
5989
Francois Romieu1765f952008-09-13 17:21:40 +02005990static void rtl_shutdown(struct pci_dev *pdev)
5991{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005992 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005993 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00005994 struct device *d = &pdev->dev;
5995
5996 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02005997
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005998 rtl8169_net_suspend(dev);
5999
Francois Romieucecb5fd2011-04-01 10:21:07 +02006000 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006001 rtl_rar_set(tp, dev->perm_addr);
6002
Hayes Wang92fc43b2011-07-06 15:58:03 +08006003 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006004
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006005 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006006 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6007 rtl_wol_suspend_quirk(tp);
6008 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006009 }
6010
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006011 pci_wake_from_d3(pdev, true);
6012 pci_set_power_state(pdev, PCI_D3hot);
6013 }
françois romieu2a15cd22012-03-06 01:14:12 +00006014
6015 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006016}
Francois Romieu5d06a992006-02-23 00:47:58 +01006017
Francois Romieue27566e2012-03-08 09:54:01 +01006018static void __devexit rtl_remove_one(struct pci_dev *pdev)
6019{
6020 struct net_device *dev = pci_get_drvdata(pdev);
6021 struct rtl8169_private *tp = netdev_priv(dev);
6022
6023 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6024 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6025 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6026 rtl8168_driver_stop(tp);
6027 }
6028
6029 cancel_work_sync(&tp->wk.work);
6030
6031 unregister_netdev(dev);
6032
6033 rtl_release_firmware(tp);
6034
6035 if (pci_dev_run_wake(pdev))
6036 pm_runtime_get_noresume(&pdev->dev);
6037
6038 /* restore original MAC address */
6039 rtl_rar_set(tp, dev->perm_addr);
6040
6041 rtl_disable_msi(pdev, tp);
6042 rtl8169_release_board(pdev, dev, tp->mmio_addr);
6043 pci_set_drvdata(pdev, NULL);
6044}
6045
Francois Romieufa9c3852012-03-08 10:01:50 +01006046static const struct net_device_ops rtl_netdev_ops = {
6047 .ndo_open = rtl8169_open,
6048 .ndo_stop = rtl8169_close,
6049 .ndo_get_stats64 = rtl8169_get_stats64,
6050 .ndo_start_xmit = rtl8169_start_xmit,
6051 .ndo_tx_timeout = rtl8169_tx_timeout,
6052 .ndo_validate_addr = eth_validate_addr,
6053 .ndo_change_mtu = rtl8169_change_mtu,
6054 .ndo_fix_features = rtl8169_fix_features,
6055 .ndo_set_features = rtl8169_set_features,
6056 .ndo_set_mac_address = rtl_set_mac_address,
6057 .ndo_do_ioctl = rtl8169_ioctl,
6058 .ndo_set_rx_mode = rtl_set_rx_mode,
6059#ifdef CONFIG_NET_POLL_CONTROLLER
6060 .ndo_poll_controller = rtl8169_netpoll,
6061#endif
6062
6063};
6064
Francois Romieu3b6cf252012-03-08 09:59:04 +01006065static int __devinit
6066rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6067{
6068 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
6069 const unsigned int region = cfg->region;
6070 struct rtl8169_private *tp;
6071 struct mii_if_info *mii;
6072 struct net_device *dev;
6073 void __iomem *ioaddr;
6074 int chipset, i;
6075 int rc;
6076
6077 if (netif_msg_drv(&debug)) {
6078 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
6079 MODULENAME, RTL8169_VERSION);
6080 }
6081
6082 dev = alloc_etherdev(sizeof (*tp));
6083 if (!dev) {
6084 rc = -ENOMEM;
6085 goto out;
6086 }
6087
6088 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01006089 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006090 tp = netdev_priv(dev);
6091 tp->dev = dev;
6092 tp->pci_dev = pdev;
6093 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
6094
6095 mii = &tp->mii;
6096 mii->dev = dev;
6097 mii->mdio_read = rtl_mdio_read;
6098 mii->mdio_write = rtl_mdio_write;
6099 mii->phy_id_mask = 0x1f;
6100 mii->reg_num_mask = 0x1f;
6101 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
6102
6103 /* disable ASPM completely as that cause random device stop working
6104 * problems as well as full system hangs for some PCIe devices users */
6105 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6106 PCIE_LINK_STATE_CLKPM);
6107
6108 /* enable device (incl. PCI PM wakeup and hotplug setup) */
6109 rc = pci_enable_device(pdev);
6110 if (rc < 0) {
6111 netif_err(tp, probe, dev, "enable failure\n");
6112 goto err_out_free_dev_1;
6113 }
6114
6115 if (pci_set_mwi(pdev) < 0)
6116 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
6117
6118 /* make sure PCI base addr 1 is MMIO */
6119 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
6120 netif_err(tp, probe, dev,
6121 "region #%d not an MMIO resource, aborting\n",
6122 region);
6123 rc = -ENODEV;
6124 goto err_out_mwi_2;
6125 }
6126
6127 /* check for weird/broken PCI region reporting */
6128 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
6129 netif_err(tp, probe, dev,
6130 "Invalid PCI region size(s), aborting\n");
6131 rc = -ENODEV;
6132 goto err_out_mwi_2;
6133 }
6134
6135 rc = pci_request_regions(pdev, MODULENAME);
6136 if (rc < 0) {
6137 netif_err(tp, probe, dev, "could not request regions\n");
6138 goto err_out_mwi_2;
6139 }
6140
6141 tp->cp_cmd = RxChkSum;
6142
6143 if ((sizeof(dma_addr_t) > 4) &&
6144 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
6145 tp->cp_cmd |= PCIDAC;
6146 dev->features |= NETIF_F_HIGHDMA;
6147 } else {
6148 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6149 if (rc < 0) {
6150 netif_err(tp, probe, dev, "DMA configuration failed\n");
6151 goto err_out_free_res_3;
6152 }
6153 }
6154
6155 /* ioremap MMIO region */
6156 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
6157 if (!ioaddr) {
6158 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
6159 rc = -EIO;
6160 goto err_out_free_res_3;
6161 }
6162 tp->mmio_addr = ioaddr;
6163
6164 if (!pci_is_pcie(pdev))
6165 netif_info(tp, probe, dev, "not PCI Express\n");
6166
6167 /* Identify chip attached to board */
6168 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
6169
6170 rtl_init_rxcfg(tp);
6171
6172 rtl_irq_disable(tp);
6173
6174 rtl_hw_reset(tp);
6175
6176 rtl_ack_events(tp, 0xffff);
6177
6178 pci_set_master(pdev);
6179
6180 /*
6181 * Pretend we are using VLANs; This bypasses a nasty bug where
6182 * Interrupts stop flowing on high load on 8110SCd controllers.
6183 */
6184 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6185 tp->cp_cmd |= RxVlan;
6186
6187 rtl_init_mdio_ops(tp);
6188 rtl_init_pll_power_ops(tp);
6189 rtl_init_jumbo_ops(tp);
6190
6191 rtl8169_print_mac_version(tp);
6192
6193 chipset = tp->mac_version;
6194 tp->txd_version = rtl_chip_infos[chipset].txd_version;
6195
6196 RTL_W8(Cfg9346, Cfg9346_Unlock);
6197 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
6198 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
6199 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
6200 tp->features |= RTL_FEATURE_WOL;
6201 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
6202 tp->features |= RTL_FEATURE_WOL;
6203 tp->features |= rtl_try_msi(tp, cfg);
6204 RTL_W8(Cfg9346, Cfg9346_Lock);
6205
6206 if (rtl_tbi_enabled(tp)) {
6207 tp->set_speed = rtl8169_set_speed_tbi;
6208 tp->get_settings = rtl8169_gset_tbi;
6209 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
6210 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
6211 tp->link_ok = rtl8169_tbi_link_ok;
6212 tp->do_ioctl = rtl_tbi_ioctl;
6213 } else {
6214 tp->set_speed = rtl8169_set_speed_xmii;
6215 tp->get_settings = rtl8169_gset_xmii;
6216 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
6217 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
6218 tp->link_ok = rtl8169_xmii_link_ok;
6219 tp->do_ioctl = rtl_xmii_ioctl;
6220 }
6221
6222 mutex_init(&tp->wk.mutex);
6223
6224 /* Get MAC address */
6225 for (i = 0; i < ETH_ALEN; i++)
6226 dev->dev_addr[i] = RTL_R8(MAC0 + i);
6227 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
6228
6229 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
6230 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
6231 dev->irq = pdev->irq;
6232 dev->base_addr = (unsigned long) ioaddr;
6233
6234 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
6235
6236 /* don't enable SG, IP_CSUM and TSO by default - it might not work
6237 * properly for all devices */
6238 dev->features |= NETIF_F_RXCSUM |
6239 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6240
6241 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6242 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6243 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6244 NETIF_F_HIGHDMA;
6245
6246 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6247 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
6248 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
6249
6250 dev->hw_features |= NETIF_F_RXALL;
6251 dev->hw_features |= NETIF_F_RXFCS;
6252
6253 tp->hw_start = cfg->hw_start;
6254 tp->event_slow = cfg->event_slow;
6255
6256 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
6257 ~(RxBOVF | RxFOVF) : ~0;
6258
6259 init_timer(&tp->timer);
6260 tp->timer.data = (unsigned long) dev;
6261 tp->timer.function = rtl8169_phy_timer;
6262
6263 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
6264
6265 rc = register_netdev(dev);
6266 if (rc < 0)
6267 goto err_out_msi_4;
6268
6269 pci_set_drvdata(pdev, dev);
6270
6271 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
6272 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
6273 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
6274 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
6275 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
6276 "tx checksumming: %s]\n",
6277 rtl_chip_infos[chipset].jumbo_max,
6278 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
6279 }
6280
6281 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6282 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6283 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6284 rtl8168_driver_start(tp);
6285 }
6286
6287 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
6288
6289 if (pci_dev_run_wake(pdev))
6290 pm_runtime_put_noidle(&pdev->dev);
6291
6292 netif_carrier_off(dev);
6293
6294out:
6295 return rc;
6296
6297err_out_msi_4:
6298 rtl_disable_msi(pdev, tp);
6299 iounmap(ioaddr);
6300err_out_free_res_3:
6301 pci_release_regions(pdev);
6302err_out_mwi_2:
6303 pci_clear_mwi(pdev);
6304 pci_disable_device(pdev);
6305err_out_free_dev_1:
6306 free_netdev(dev);
6307 goto out;
6308}
6309
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310static struct pci_driver rtl8169_pci_driver = {
6311 .name = MODULENAME,
6312 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01006313 .probe = rtl_init_one,
Francois Romieue27566e2012-03-08 09:54:01 +01006314 .remove = __devexit_p(rtl_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02006315 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006316 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317};
6318
Francois Romieu07d3f512007-02-21 22:40:46 +01006319static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320{
Jeff Garzik29917622006-08-19 17:48:59 -04006321 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322}
6323
Francois Romieu07d3f512007-02-21 22:40:46 +01006324static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325{
6326 pci_unregister_driver(&rtl8169_pci_driver);
6327}
6328
6329module_init(rtl8169_init_module);
6330module_exit(rtl8169_cleanup_module);