blob: 8d4515ed7946c4311d46db5b0b723f6447422d8d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Francois Romieu07d3f512007-02-21 22:40:46 +01002 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3 *
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
7 *
8 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/delay.h>
17#include <linux/ethtool.h>
18#include <linux/mii.h>
19#include <linux/if_vlan.h>
20#include <linux/crc32.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/tcp.h>
24#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000025#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000027#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000028#include <linux/firmware.h>
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +000029#include <linux/pci-aspm.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040030#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Francois Romieu99f252b2007-04-02 22:59:59 +020032#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/io.h>
34#include <asm/irq.h>
35
Francois Romieu865c6522008-05-11 14:51:00 +020036#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MODULENAME "r8169"
38#define PFX MODULENAME ": "
39
françois romieubca03d52011-01-03 15:07:31 +000040#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
41#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000042#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
43#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080044#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080045#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
46#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080047#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#ifdef RTL8169_DEBUG
50#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020051 if (!(expr)) { \
52 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070053 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020054 }
Joe Perches06fa7352007-10-18 21:15:00 +020055#define dprintk(fmt, args...) \
56 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#else
58#define assert(expr) do {} while (0)
59#define dprintk(fmt, args...) do {} while (0)
60#endif /* RTL8169_DEBUG */
61
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020062#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d92005-09-30 16:54:02 -070063 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define TX_BUFFS_AVAIL(tp) \
66 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
69 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050070static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Francois Romieu9c14cea2008-07-05 00:21:15 +020072#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
75#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
76
77#define R8169_REGS_SIZE 256
78#define R8169_NAPI_WEIGHT 64
79#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
80#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
81#define RX_BUF_SIZE 1536 /* Rx Buffer size */
82#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
83#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
84
85#define RTL8169_TX_TIMEOUT (6*HZ)
86#define RTL8169_PHY_TIMEOUT (10*HZ)
87
françois romieuea8dbdd2009-03-15 01:10:50 +000088#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
89#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020090#define RTL_EEPROM_SIG_ADDR 0x0000
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* write/read MMIO register */
93#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
94#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
95#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
96#define RTL_R8(reg) readb (ioaddr + (reg))
97#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +000098#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200101 RTL_GIGA_MAC_VER_01 = 0,
102 RTL_GIGA_MAC_VER_02,
103 RTL_GIGA_MAC_VER_03,
104 RTL_GIGA_MAC_VER_04,
105 RTL_GIGA_MAC_VER_05,
106 RTL_GIGA_MAC_VER_06,
107 RTL_GIGA_MAC_VER_07,
108 RTL_GIGA_MAC_VER_08,
109 RTL_GIGA_MAC_VER_09,
110 RTL_GIGA_MAC_VER_10,
111 RTL_GIGA_MAC_VER_11,
112 RTL_GIGA_MAC_VER_12,
113 RTL_GIGA_MAC_VER_13,
114 RTL_GIGA_MAC_VER_14,
115 RTL_GIGA_MAC_VER_15,
116 RTL_GIGA_MAC_VER_16,
117 RTL_GIGA_MAC_VER_17,
118 RTL_GIGA_MAC_VER_18,
119 RTL_GIGA_MAC_VER_19,
120 RTL_GIGA_MAC_VER_20,
121 RTL_GIGA_MAC_VER_21,
122 RTL_GIGA_MAC_VER_22,
123 RTL_GIGA_MAC_VER_23,
124 RTL_GIGA_MAC_VER_24,
125 RTL_GIGA_MAC_VER_25,
126 RTL_GIGA_MAC_VER_26,
127 RTL_GIGA_MAC_VER_27,
128 RTL_GIGA_MAC_VER_28,
129 RTL_GIGA_MAC_VER_29,
130 RTL_GIGA_MAC_VER_30,
131 RTL_GIGA_MAC_VER_31,
132 RTL_GIGA_MAC_VER_32,
133 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800134 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800135 RTL_GIGA_MAC_VER_35,
136 RTL_GIGA_MAC_VER_36,
Francois Romieu85bffe62011-04-27 08:22:39 +0200137 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
Francois Romieu2b7b4312011-04-18 22:53:24 -0700140enum rtl_tx_desc_version {
141 RTL_TD_0 = 0,
142 RTL_TD_1 = 1,
143};
144
Francois Romieud58d46b2011-05-03 16:38:29 +0200145#define JUMBO_1K ETH_DATA_LEN
146#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
147#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
148#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
149#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
150
151#define _R(NAME,TD,FW,SZ,B) { \
152 .name = NAME, \
153 .txd_version = TD, \
154 .fw_name = FW, \
155 .jumbo_max = SZ, \
156 .jumbo_tx_csum = B \
157}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800159static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700161 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200162 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200163 u16 jumbo_max;
164 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200165} rtl_chip_infos[] = {
166 /* PCI devices. */
167 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200168 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200169 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200170 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200171 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200172 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200173 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200174 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200175 [RTL_GIGA_MAC_VER_05] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200176 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200177 [RTL_GIGA_MAC_VER_06] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200178 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200179 /* PCI-E devices. */
180 [RTL_GIGA_MAC_VER_07] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200181 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200182 [RTL_GIGA_MAC_VER_08] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200183 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200184 [RTL_GIGA_MAC_VER_09] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200185 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200186 [RTL_GIGA_MAC_VER_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200187 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200188 [RTL_GIGA_MAC_VER_11] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200189 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200190 [RTL_GIGA_MAC_VER_12] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200191 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200192 [RTL_GIGA_MAC_VER_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200193 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200194 [RTL_GIGA_MAC_VER_14] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200195 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200196 [RTL_GIGA_MAC_VER_15] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200197 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200198 [RTL_GIGA_MAC_VER_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200199 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200200 [RTL_GIGA_MAC_VER_17] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200201 _R("RTL8168b/8111b", RTL_TD_1, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200202 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200203 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200204 [RTL_GIGA_MAC_VER_19] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200205 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200206 [RTL_GIGA_MAC_VER_20] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200207 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200208 [RTL_GIGA_MAC_VER_21] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200209 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200210 [RTL_GIGA_MAC_VER_22] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200211 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200212 [RTL_GIGA_MAC_VER_23] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200213 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200214 [RTL_GIGA_MAC_VER_24] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200215 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200216 [RTL_GIGA_MAC_VER_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200217 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
218 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200219 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200220 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
221 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200222 [RTL_GIGA_MAC_VER_27] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200223 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200224 [RTL_GIGA_MAC_VER_28] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200225 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200226 [RTL_GIGA_MAC_VER_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200227 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
228 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200229 [RTL_GIGA_MAC_VER_30] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200230 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
231 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200232 [RTL_GIGA_MAC_VER_31] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200233 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200234 [RTL_GIGA_MAC_VER_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200235 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
236 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200237 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200238 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
239 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800240 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200241 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
242 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800243 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200244 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
245 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800246 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200247 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
248 JUMBO_9K, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249};
250#undef _R
251
Francois Romieubcf0bf92006-07-26 23:14:13 +0200252enum cfg_version {
253 RTL_CFG_0 = 0x00,
254 RTL_CFG_1,
255 RTL_CFG_2
256};
257
Francois Romieu07ce4062007-02-23 23:36:39 +0100258static void rtl_hw_start_8169(struct net_device *);
259static void rtl_hw_start_8168(struct net_device *);
260static void rtl_hw_start_8101(struct net_device *);
261
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000262static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200263 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200264 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200265 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100266 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200267 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
268 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000269 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200270 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200271 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
272 { PCI_VENDOR_ID_LINKSYS, 0x1032,
273 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100274 { 0x0001, 0x8168,
275 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 {0,},
277};
278
279MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
280
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000281static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700282static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200283static struct {
284 u32 msg_enable;
285} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Francois Romieu07d3f512007-02-21 22:40:46 +0100287enum rtl_registers {
288 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100289 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100290 MAR0 = 8, /* Multicast filter. */
291 CounterAddrLow = 0x10,
292 CounterAddrHigh = 0x14,
293 TxDescStartAddrLow = 0x20,
294 TxDescStartAddrHigh = 0x24,
295 TxHDescStartAddrLow = 0x28,
296 TxHDescStartAddrHigh = 0x2c,
297 FLASH = 0x30,
298 ERSR = 0x36,
299 ChipCmd = 0x37,
300 TxPoll = 0x38,
301 IntrMask = 0x3c,
302 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700303
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800304 TxConfig = 0x40,
305#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
306#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
307
308 RxConfig = 0x44,
309#define RX128_INT_EN (1 << 15) /* 8111c and later */
310#define RX_MULTI_EN (1 << 14) /* 8111c only */
311#define RXCFG_FIFO_SHIFT 13
312 /* No threshold before first PCI xfer */
313#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
314#define RXCFG_DMA_SHIFT 8
315 /* Unlimited maximum PCI burst. */
316#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700317
Francois Romieu07d3f512007-02-21 22:40:46 +0100318 RxMissed = 0x4c,
319 Cfg9346 = 0x50,
320 Config0 = 0x51,
321 Config1 = 0x52,
322 Config2 = 0x53,
323 Config3 = 0x54,
324 Config4 = 0x55,
325 Config5 = 0x56,
326 MultiIntr = 0x5c,
327 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100328 PHYstatus = 0x6c,
329 RxMaxSize = 0xda,
330 CPlusCmd = 0xe0,
331 IntrMitigate = 0xe2,
332 RxDescAddrLow = 0xe4,
333 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000334 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
335
336#define NoEarlyTx 0x3f /* Max value : no early transmit. */
337
338 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
339
340#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800341#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000342
Francois Romieu07d3f512007-02-21 22:40:46 +0100343 FuncEvent = 0xf0,
344 FuncEventMask = 0xf4,
345 FuncPresetState = 0xf8,
346 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347};
348
Francois Romieuf162a5d2008-06-01 22:37:49 +0200349enum rtl8110_registers {
350 TBICSR = 0x64,
351 TBI_ANAR = 0x68,
352 TBI_LPAR = 0x6a,
353};
354
355enum rtl8168_8101_registers {
356 CSIDR = 0x64,
357 CSIAR = 0x68,
358#define CSIAR_FLAG 0x80000000
359#define CSIAR_WRITE_CMD 0x80000000
360#define CSIAR_BYTE_ENABLE 0x0f
361#define CSIAR_BYTE_ENABLE_SHIFT 12
362#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000363 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200364 EPHYAR = 0x80,
365#define EPHYAR_FLAG 0x80000000
366#define EPHYAR_WRITE_CMD 0x80000000
367#define EPHYAR_REG_MASK 0x1f
368#define EPHYAR_REG_SHIFT 16
369#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800370 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800371#define PFM_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200372 DBG_REG = 0xd1,
373#define FIX_NAK_1 (1 << 4)
374#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800375 TWSI = 0xd2,
376 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800377#define NOW_IS_OOB (1 << 7)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800378#define EN_NDP (1 << 3)
379#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000380 EFUSEAR = 0xdc,
381#define EFUSEAR_FLAG 0x80000000
382#define EFUSEAR_WRITE_CMD 0x80000000
383#define EFUSEAR_READ_CMD 0x00000000
384#define EFUSEAR_REG_MASK 0x03ff
385#define EFUSEAR_REG_SHIFT 8
386#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200387};
388
françois romieuc0e45c12011-01-03 15:08:04 +0000389enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800390 LED_FREQ = 0x1a,
391 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000392 ERIDR = 0x70,
393 ERIAR = 0x74,
394#define ERIAR_FLAG 0x80000000
395#define ERIAR_WRITE_CMD 0x80000000
396#define ERIAR_READ_CMD 0x00000000
397#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000398#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800399#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
400#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
401#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
402#define ERIAR_MASK_SHIFT 12
403#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
404#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
405#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000406 EPHY_RXER_NUM = 0x7c,
407 OCPDR = 0xb0, /* OCP GPHY access */
408#define OCPDR_WRITE_CMD 0x80000000
409#define OCPDR_READ_CMD 0x00000000
410#define OCPDR_REG_MASK 0x7f
411#define OCPDR_GPHY_REG_SHIFT 16
412#define OCPDR_DATA_MASK 0xffff
413 OCPAR = 0xb4,
414#define OCPAR_FLAG 0x80000000
415#define OCPAR_GPHY_WRITE_CMD 0x8000f060
416#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000417 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
418 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200419#define TXPLA_RST (1 << 29)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800420#define PWM_EN (1 << 22)
françois romieuc0e45c12011-01-03 15:08:04 +0000421};
422
Francois Romieu07d3f512007-02-21 22:40:46 +0100423enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100425 SYSErr = 0x8000,
426 PCSTimeout = 0x4000,
427 SWInt = 0x0100,
428 TxDescUnavail = 0x0080,
429 RxFIFOOver = 0x0040,
430 LinkChg = 0x0020,
431 RxOverflow = 0x0010,
432 TxErr = 0x0008,
433 TxOK = 0x0004,
434 RxErr = 0x0002,
435 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400438 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200439 RxFOVF = (1 << 23),
440 RxRWT = (1 << 22),
441 RxRES = (1 << 21),
442 RxRUNT = (1 << 20),
443 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800446 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100447 CmdReset = 0x10,
448 CmdRxEnb = 0x08,
449 CmdTxEnb = 0x04,
450 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Francois Romieu275391a2007-02-23 23:50:28 +0100452 /* TXPoll register p.5 */
453 HPQ = 0x80, /* Poll cmd on the high prio queue */
454 NPQ = 0x40, /* Poll cmd on the low prio queue */
455 FSWInt = 0x01, /* Forced software interrupt */
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100458 Cfg9346_Lock = 0x00,
459 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100462 AcceptErr = 0x20,
463 AcceptRunt = 0x10,
464 AcceptBroadcast = 0x08,
465 AcceptMulticast = 0x04,
466 AcceptMyPhys = 0x02,
467 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200468#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /* TxConfigBits */
471 TxInterFrameGapShift = 24,
472 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
473
Francois Romieu5d06a992006-02-23 00:47:58 +0100474 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200475 LEDS1 = (1 << 7),
476 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200477 Speed_down = (1 << 4),
478 MEMMAP = (1 << 3),
479 IOMAP = (1 << 2),
480 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100481 PMEnable = (1 << 0), /* Power Management Enable */
482
Francois Romieu6dccd162007-02-13 23:38:05 +0100483 /* Config2 register p. 25 */
françois romieu2ca6cf02011-12-15 08:37:43 +0000484 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100485 PCI_Clock_66MHz = 0x01,
486 PCI_Clock_33MHz = 0x00,
487
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100488 /* Config3 register p.25 */
489 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
490 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200491 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200492 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100493
Francois Romieud58d46b2011-05-03 16:38:29 +0200494 /* Config4 register */
495 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
496
Francois Romieu5d06a992006-02-23 00:47:58 +0100497 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100498 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
499 MWF = (1 << 5), /* Accept Multicast wakeup frame */
500 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200501 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100502 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100503 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /* TBICSR p.28 */
506 TBIReset = 0x80000000,
507 TBILoopback = 0x40000000,
508 TBINwEnable = 0x20000000,
509 TBINwRestart = 0x10000000,
510 TBILinkOk = 0x02000000,
511 TBINwComplete = 0x01000000,
512
513 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200514 EnableBist = (1 << 15), // 8168 8101
515 Mac_dbgo_oe = (1 << 14), // 8168 8101
516 Normal_mode = (1 << 13), // unused
517 Force_half_dup = (1 << 12), // 8168 8101
518 Force_rxflow_en = (1 << 11), // 8168 8101
519 Force_txflow_en = (1 << 10), // 8168 8101
520 Cxpl_dbg_sel = (1 << 9), // 8168 8101
521 ASF = (1 << 8), // 8168 8101
522 PktCntrDisable = (1 << 7), // 8168 8101
523 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 RxVlan = (1 << 6),
525 RxChkSum = (1 << 5),
526 PCIDAC = (1 << 4),
527 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100528 INTT_0 = 0x0000, // 8168
529 INTT_1 = 0x0001, // 8168
530 INTT_2 = 0x0002, // 8168
531 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100534 TBI_Enable = 0x80,
535 TxFlowCtrl = 0x40,
536 RxFlowCtrl = 0x20,
537 _1000bpsF = 0x10,
538 _100bps = 0x08,
539 _10bps = 0x04,
540 LinkStatus = 0x02,
541 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100544 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200545
546 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100547 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548};
549
Francois Romieu2b7b4312011-04-18 22:53:24 -0700550enum rtl_desc_bit {
551 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
553 RingEnd = (1 << 30), /* End of descriptor ring */
554 FirstFrag = (1 << 29), /* First segment of a packet */
555 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700556};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Francois Romieu2b7b4312011-04-18 22:53:24 -0700558/* Generic case. */
559enum rtl_tx_desc_bit {
560 /* First doubleword. */
561 TD_LSO = (1 << 27), /* Large Send Offload */
562#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Francois Romieu2b7b4312011-04-18 22:53:24 -0700564 /* Second doubleword. */
565 TxVlanTag = (1 << 17), /* Add VLAN tag */
566};
567
568/* 8169, 8168b and 810x except 8102e. */
569enum rtl_tx_desc_bit_0 {
570 /* First doubleword. */
571#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
572 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
573 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
574 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
575};
576
577/* 8102e, 8168c and beyond. */
578enum rtl_tx_desc_bit_1 {
579 /* Second doubleword. */
580#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
581 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
582 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
583 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
584};
585
586static const struct rtl_tx_desc_info {
587 struct {
588 u32 udp;
589 u32 tcp;
590 } checksum;
591 u16 mss_shift;
592 u16 opts_offset;
593} tx_desc_info [] = {
594 [RTL_TD_0] = {
595 .checksum = {
596 .udp = TD0_IP_CS | TD0_UDP_CS,
597 .tcp = TD0_IP_CS | TD0_TCP_CS
598 },
599 .mss_shift = TD0_MSS_SHIFT,
600 .opts_offset = 0
601 },
602 [RTL_TD_1] = {
603 .checksum = {
604 .udp = TD1_IP_CS | TD1_UDP_CS,
605 .tcp = TD1_IP_CS | TD1_TCP_CS
606 },
607 .mss_shift = TD1_MSS_SHIFT,
608 .opts_offset = 1
609 }
610};
611
612enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* Rx private */
614 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
615 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
616
617#define RxProtoUDP (PID1)
618#define RxProtoTCP (PID0)
619#define RxProtoIP (PID1 | PID0)
620#define RxProtoMask RxProtoIP
621
622 IPFail = (1 << 16), /* IP checksum failed */
623 UDPFail = (1 << 15), /* UDP/IP checksum failed */
624 TCPFail = (1 << 14), /* TCP/IP checksum failed */
625 RxVlanTag = (1 << 16), /* VLAN tag available */
626};
627
628#define RsvdMask 0x3fffc000
629
630struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200631 __le32 opts1;
632 __le32 opts2;
633 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634};
635
636struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200637 __le32 opts1;
638 __le32 opts2;
639 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640};
641
642struct ring_info {
643 struct sk_buff *skb;
644 u32 len;
645 u8 __pad[sizeof(void *) - sizeof(u32)];
646};
647
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200648enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200649 RTL_FEATURE_WOL = (1 << 0),
650 RTL_FEATURE_MSI = (1 << 1),
651 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200652};
653
Ivan Vecera355423d2009-02-06 21:49:57 -0800654struct rtl8169_counters {
655 __le64 tx_packets;
656 __le64 rx_packets;
657 __le64 tx_errors;
658 __le32 rx_errors;
659 __le16 rx_missed;
660 __le16 align_errors;
661 __le32 tx_one_collision;
662 __le32 tx_multi_collision;
663 __le64 rx_unicast;
664 __le64 rx_broadcast;
665 __le32 rx_multicast;
666 __le16 tx_aborted;
667 __le16 tx_underun;
668};
669
Francois Romieuda78dbf2012-01-26 14:18:23 +0100670enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100671 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100672 RTL_FLAG_TASK_SLOW_PENDING,
673 RTL_FLAG_TASK_RESET_PENDING,
674 RTL_FLAG_TASK_PHY_PENDING,
675 RTL_FLAG_MAX
676};
677
Junchang Wang8027aa22012-03-04 23:30:32 +0100678struct rtl8169_stats {
679 u64 packets;
680 u64 bytes;
681 struct u64_stats_sync syncp;
682};
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684struct rtl8169_private {
685 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200686 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000687 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700688 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200689 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700690 u16 txd_version;
691 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
693 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
694 u32 dirty_rx;
695 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100696 struct rtl8169_stats rx_stats;
697 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
699 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
700 dma_addr_t TxPhyAddr;
701 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000702 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 struct timer_list timer;
705 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100706
707 u16 event_slow;
françois romieuc0e45c12011-01-03 15:08:04 +0000708
709 struct mdio_ops {
710 void (*write)(void __iomem *, int, int);
711 int (*read)(void __iomem *, int);
712 } mdio_ops;
713
françois romieu065c27c2011-01-03 15:08:12 +0000714 struct pll_power_ops {
715 void (*down)(struct rtl8169_private *);
716 void (*up)(struct rtl8169_private *);
717 } pll_power_ops;
718
Francois Romieud58d46b2011-05-03 16:38:29 +0200719 struct jumbo_ops {
720 void (*enable)(struct rtl8169_private *);
721 void (*disable)(struct rtl8169_private *);
722 } jumbo_ops;
723
Oliver Neukum54405cd2011-01-06 21:55:13 +0100724 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200725 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000726 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100727 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000728 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800730 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100731
732 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100733 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
734 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100735 struct work_struct work;
736 } wk;
737
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200738 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200739
740 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800741 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000742 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400743 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000744
Francois Romieub6ffd972011-06-17 17:00:05 +0200745 struct rtl_fw {
746 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200747
748#define RTL_VER_SIZE 32
749
750 char version[RTL_VER_SIZE];
751
752 struct rtl_fw_phy_action {
753 __le32 *code;
754 size_t size;
755 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200756 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300757#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758};
759
Ralf Baechle979b6c12005-06-13 14:30:40 -0700760MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700763MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200764module_param_named(debug, debug.msg_enable, int, 0);
765MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766MODULE_LICENSE("GPL");
767MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000768MODULE_FIRMWARE(FIRMWARE_8168D_1);
769MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000770MODULE_FIRMWARE(FIRMWARE_8168E_1);
771MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400772MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800773MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800774MODULE_FIRMWARE(FIRMWARE_8168F_1);
775MODULE_FIRMWARE(FIRMWARE_8168F_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
David Howells7d12e782006-10-05 14:55:46 +0100777static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Francois Romieuda78dbf2012-01-26 14:18:23 +0100779static void rtl_lock_work(struct rtl8169_private *tp)
780{
781 mutex_lock(&tp->wk.mutex);
782}
783
784static void rtl_unlock_work(struct rtl8169_private *tp)
785{
786 mutex_unlock(&tp->wk.mutex);
787}
788
Francois Romieud58d46b2011-05-03 16:38:29 +0200789static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
790{
791 int cap = pci_pcie_cap(pdev);
792
793 if (cap) {
794 u16 ctl;
795
796 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
797 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
798 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
799 }
800}
801
françois romieub646d902011-01-03 15:08:21 +0000802static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
803{
804 void __iomem *ioaddr = tp->mmio_addr;
805 int i;
806
807 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
808 for (i = 0; i < 20; i++) {
809 udelay(100);
810 if (RTL_R32(OCPAR) & OCPAR_FLAG)
811 break;
812 }
813 return RTL_R32(OCPDR);
814}
815
816static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
817{
818 void __iomem *ioaddr = tp->mmio_addr;
819 int i;
820
821 RTL_W32(OCPDR, data);
822 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
823 for (i = 0; i < 20; i++) {
824 udelay(100);
825 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
826 break;
827 }
828}
829
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800830static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000831{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800832 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000833 int i;
834
835 RTL_W8(ERIDR, cmd);
836 RTL_W32(ERIAR, 0x800010e8);
837 msleep(2);
838 for (i = 0; i < 5; i++) {
839 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200840 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000841 break;
842 }
843
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800844 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000845}
846
847#define OOB_CMD_RESET 0x00
848#define OOB_CMD_DRIVER_START 0x05
849#define OOB_CMD_DRIVER_STOP 0x06
850
Francois Romieucecb5fd2011-04-01 10:21:07 +0200851static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
852{
853 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
854}
855
françois romieub646d902011-01-03 15:08:21 +0000856static void rtl8168_driver_start(struct rtl8169_private *tp)
857{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200858 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000859 int i;
860
861 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
862
Francois Romieucecb5fd2011-04-01 10:21:07 +0200863 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000864
françois romieub646d902011-01-03 15:08:21 +0000865 for (i = 0; i < 10; i++) {
866 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000867 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000868 break;
869 }
870}
871
872static void rtl8168_driver_stop(struct rtl8169_private *tp)
873{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200874 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000875 int i;
876
877 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
878
Francois Romieucecb5fd2011-04-01 10:21:07 +0200879 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000880
françois romieub646d902011-01-03 15:08:21 +0000881 for (i = 0; i < 10; i++) {
882 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000883 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000884 break;
885 }
886}
887
hayeswang4804b3b2011-03-21 01:50:29 +0000888static int r8168dp_check_dash(struct rtl8169_private *tp)
889{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200890 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000891
Francois Romieucecb5fd2011-04-01 10:21:07 +0200892 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000893}
françois romieub646d902011-01-03 15:08:21 +0000894
françois romieu4da19632011-01-03 15:07:55 +0000895static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
897 int i;
898
Francois Romieua6baf3a2007-11-08 23:23:21 +0100899 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Francois Romieu23714082006-01-29 00:49:09 +0100901 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100902 /*
903 * Check if the RTL8169 has completed writing to the specified
904 * MII register.
905 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200906 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 break;
Francois Romieu23714082006-01-29 00:49:09 +0100908 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700910 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700911 * According to hardware specs a 20us delay is required after write
912 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700913 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700914 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
françois romieu4da19632011-01-03 15:07:55 +0000917static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919 int i, value = -1;
920
Francois Romieua6baf3a2007-11-08 23:23:21 +0100921 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Francois Romieu23714082006-01-29 00:49:09 +0100923 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100924 /*
925 * Check if the RTL8169 has completed retrieving data from
926 * the specified MII register.
927 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100929 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 break;
931 }
Francois Romieu23714082006-01-29 00:49:09 +0100932 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700934 /*
935 * According to hardware specs a 20us delay is required after read
936 * complete indication, but before sending next command.
937 */
938 udelay(20);
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return value;
941}
942
françois romieuc0e45c12011-01-03 15:08:04 +0000943static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
944{
945 int i;
946
947 RTL_W32(OCPDR, data |
948 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
949 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
950 RTL_W32(EPHY_RXER_NUM, 0);
951
952 for (i = 0; i < 100; i++) {
953 mdelay(1);
954 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
955 break;
956 }
957}
958
959static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
960{
961 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
962 (value & OCPDR_DATA_MASK));
963}
964
965static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
966{
967 int i;
968
969 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
970
971 mdelay(1);
972 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
973 RTL_W32(EPHY_RXER_NUM, 0);
974
975 for (i = 0; i < 100; i++) {
976 mdelay(1);
977 if (RTL_R32(OCPAR) & OCPAR_FLAG)
978 break;
979 }
980
981 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
982}
983
françois romieue6de30d2011-01-03 15:08:37 +0000984#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
985
986static void r8168dp_2_mdio_start(void __iomem *ioaddr)
987{
988 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
989}
990
991static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
992{
993 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
994}
995
996static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
997{
998 r8168dp_2_mdio_start(ioaddr);
999
1000 r8169_mdio_write(ioaddr, reg_addr, value);
1001
1002 r8168dp_2_mdio_stop(ioaddr);
1003}
1004
1005static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
1006{
1007 int value;
1008
1009 r8168dp_2_mdio_start(ioaddr);
1010
1011 value = r8169_mdio_read(ioaddr, reg_addr);
1012
1013 r8168dp_2_mdio_stop(ioaddr);
1014
1015 return value;
1016}
1017
françois romieu4da19632011-01-03 15:07:55 +00001018static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001019{
françois romieuc0e45c12011-01-03 15:08:04 +00001020 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001021}
1022
françois romieu4da19632011-01-03 15:07:55 +00001023static int rtl_readphy(struct rtl8169_private *tp, int location)
1024{
françois romieuc0e45c12011-01-03 15:08:04 +00001025 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +00001026}
1027
1028static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1029{
1030 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1031}
1032
1033static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001034{
1035 int val;
1036
françois romieu4da19632011-01-03 15:07:55 +00001037 val = rtl_readphy(tp, reg_addr);
1038 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001039}
1040
Francois Romieuccdffb92008-07-26 14:26:06 +02001041static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1042 int val)
1043{
1044 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001045
françois romieu4da19632011-01-03 15:07:55 +00001046 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001047}
1048
1049static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1050{
1051 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001052
françois romieu4da19632011-01-03 15:07:55 +00001053 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001054}
1055
Francois Romieudacf8152008-08-02 20:44:13 +02001056static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
1057{
1058 unsigned int i;
1059
1060 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1061 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1062
1063 for (i = 0; i < 100; i++) {
1064 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1065 break;
1066 udelay(10);
1067 }
1068}
1069
1070static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1071{
1072 u16 value = 0xffff;
1073 unsigned int i;
1074
1075 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1076
1077 for (i = 0; i < 100; i++) {
1078 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1079 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1080 break;
1081 }
1082 udelay(10);
1083 }
1084
1085 return value;
1086}
1087
1088static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1089{
1090 unsigned int i;
1091
1092 RTL_W32(CSIDR, value);
1093 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1094 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1095
1096 for (i = 0; i < 100; i++) {
1097 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1098 break;
1099 udelay(10);
1100 }
1101}
1102
1103static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1104{
1105 u32 value = ~0x00;
1106 unsigned int i;
1107
1108 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1109 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1110
1111 for (i = 0; i < 100; i++) {
1112 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1113 value = RTL_R32(CSIDR);
1114 break;
1115 }
1116 udelay(10);
1117 }
1118
1119 return value;
1120}
1121
Hayes Wang133ac402011-07-06 15:58:05 +08001122static
1123void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1124{
1125 unsigned int i;
1126
1127 BUG_ON((addr & 3) || (mask == 0));
1128 RTL_W32(ERIDR, val);
1129 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1130
1131 for (i = 0; i < 100; i++) {
1132 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1133 break;
1134 udelay(100);
1135 }
1136}
1137
1138static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1139{
1140 u32 value = ~0x00;
1141 unsigned int i;
1142
1143 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1144
1145 for (i = 0; i < 100; i++) {
1146 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1147 value = RTL_R32(ERIDR);
1148 break;
1149 }
1150 udelay(100);
1151 }
1152
1153 return value;
1154}
1155
1156static void
1157rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1158{
1159 u32 val;
1160
1161 val = rtl_eri_read(ioaddr, addr, type);
1162 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1163}
1164
françois romieuc28aa382011-08-02 03:53:43 +00001165struct exgmac_reg {
1166 u16 addr;
1167 u16 mask;
1168 u32 val;
1169};
1170
1171static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1172 const struct exgmac_reg *r, int len)
1173{
1174 while (len-- > 0) {
1175 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1176 r++;
1177 }
1178}
1179
françois romieudaf9df62009-10-07 12:44:20 +00001180static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1181{
1182 u8 value = 0xff;
1183 unsigned int i;
1184
1185 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1186
1187 for (i = 0; i < 300; i++) {
1188 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1189 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1190 break;
1191 }
1192 udelay(100);
1193 }
1194
1195 return value;
1196}
1197
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001198static u16 rtl_get_events(struct rtl8169_private *tp)
1199{
1200 void __iomem *ioaddr = tp->mmio_addr;
1201
1202 return RTL_R16(IntrStatus);
1203}
1204
1205static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1206{
1207 void __iomem *ioaddr = tp->mmio_addr;
1208
1209 RTL_W16(IntrStatus, bits);
1210 mmiowb();
1211}
1212
1213static void rtl_irq_disable(struct rtl8169_private *tp)
1214{
1215 void __iomem *ioaddr = tp->mmio_addr;
1216
1217 RTL_W16(IntrMask, 0);
1218 mmiowb();
1219}
1220
Francois Romieu3e990ff2012-01-26 12:50:01 +01001221static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1222{
1223 void __iomem *ioaddr = tp->mmio_addr;
1224
1225 RTL_W16(IntrMask, bits);
1226}
1227
Francois Romieuda78dbf2012-01-26 14:18:23 +01001228#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1229#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1230#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1231
1232static void rtl_irq_enable_all(struct rtl8169_private *tp)
1233{
1234 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1235}
1236
françois romieu811fd302011-12-04 20:30:45 +00001237static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
françois romieu811fd302011-12-04 20:30:45 +00001239 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001241 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001242 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001243 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
françois romieu4da19632011-01-03 15:07:55 +00001246static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
françois romieu4da19632011-01-03 15:07:55 +00001248 void __iomem *ioaddr = tp->mmio_addr;
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return RTL_R32(TBICSR) & TBIReset;
1251}
1252
françois romieu4da19632011-01-03 15:07:55 +00001253static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
françois romieu4da19632011-01-03 15:07:55 +00001255 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256}
1257
1258static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1259{
1260 return RTL_R32(TBICSR) & TBILinkOk;
1261}
1262
1263static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1264{
1265 return RTL_R8(PHYstatus) & LinkStatus;
1266}
1267
françois romieu4da19632011-01-03 15:07:55 +00001268static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
françois romieu4da19632011-01-03 15:07:55 +00001270 void __iomem *ioaddr = tp->mmio_addr;
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1273}
1274
françois romieu4da19632011-01-03 15:07:55 +00001275static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 unsigned int val;
1278
françois romieu4da19632011-01-03 15:07:55 +00001279 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1280 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
Hayes Wang70090422011-07-06 15:58:06 +08001283static void rtl_link_chg_patch(struct rtl8169_private *tp)
1284{
1285 void __iomem *ioaddr = tp->mmio_addr;
1286 struct net_device *dev = tp->dev;
1287
1288 if (!netif_running(dev))
1289 return;
1290
1291 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1292 if (RTL_R8(PHYstatus) & _1000bpsF) {
1293 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1294 0x00000011, ERIAR_EXGMAC);
1295 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1296 0x00000005, ERIAR_EXGMAC);
1297 } else if (RTL_R8(PHYstatus) & _100bps) {
1298 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1299 0x0000001f, ERIAR_EXGMAC);
1300 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1301 0x00000005, ERIAR_EXGMAC);
1302 } else {
1303 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1304 0x0000001f, ERIAR_EXGMAC);
1305 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1306 0x0000003f, ERIAR_EXGMAC);
1307 }
1308 /* Reset packet filter */
1309 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1310 ERIAR_EXGMAC);
1311 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1312 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001313 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1314 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1315 if (RTL_R8(PHYstatus) & _1000bpsF) {
1316 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1317 0x00000011, ERIAR_EXGMAC);
1318 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1319 0x00000005, ERIAR_EXGMAC);
1320 } else {
1321 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1322 0x0000001f, ERIAR_EXGMAC);
1323 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1324 0x0000003f, ERIAR_EXGMAC);
1325 }
Hayes Wang70090422011-07-06 15:58:06 +08001326 }
1327}
1328
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001329static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001330 struct rtl8169_private *tp,
1331 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001334 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001335 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001336 if (pm)
1337 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001339 if (net_ratelimit())
1340 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001341 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001343 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001344 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001345 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
1348
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001349static void rtl8169_check_link_status(struct net_device *dev,
1350 struct rtl8169_private *tp,
1351 void __iomem *ioaddr)
1352{
1353 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1354}
1355
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001356#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1357
1358static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1359{
1360 void __iomem *ioaddr = tp->mmio_addr;
1361 u8 options;
1362 u32 wolopts = 0;
1363
1364 options = RTL_R8(Config1);
1365 if (!(options & PMEnable))
1366 return 0;
1367
1368 options = RTL_R8(Config3);
1369 if (options & LinkUp)
1370 wolopts |= WAKE_PHY;
1371 if (options & MagicPacket)
1372 wolopts |= WAKE_MAGIC;
1373
1374 options = RTL_R8(Config5);
1375 if (options & UWF)
1376 wolopts |= WAKE_UCAST;
1377 if (options & BWF)
1378 wolopts |= WAKE_BCAST;
1379 if (options & MWF)
1380 wolopts |= WAKE_MCAST;
1381
1382 return wolopts;
1383}
1384
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001385static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1386{
1387 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001388
Francois Romieuda78dbf2012-01-26 14:18:23 +01001389 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001390
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001391 wol->supported = WAKE_ANY;
1392 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001393
Francois Romieuda78dbf2012-01-26 14:18:23 +01001394 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001395}
1396
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001397static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001398{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001399 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001400 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001401 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001402 u32 opt;
1403 u16 reg;
1404 u8 mask;
1405 } cfg[] = {
1406 { WAKE_ANY, Config1, PMEnable },
1407 { WAKE_PHY, Config3, LinkUp },
1408 { WAKE_MAGIC, Config3, MagicPacket },
1409 { WAKE_UCAST, Config5, UWF },
1410 { WAKE_BCAST, Config5, BWF },
1411 { WAKE_MCAST, Config5, MWF },
1412 { WAKE_ANY, Config5, LanWake }
1413 };
1414
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001415 RTL_W8(Cfg9346, Cfg9346_Unlock);
1416
1417 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1418 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001419 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001420 options |= cfg[i].mask;
1421 RTL_W8(cfg[i].reg, options);
1422 }
1423
1424 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001425}
1426
1427static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1428{
1429 struct rtl8169_private *tp = netdev_priv(dev);
1430
Francois Romieuda78dbf2012-01-26 14:18:23 +01001431 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001432
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001433 if (wol->wolopts)
1434 tp->features |= RTL_FEATURE_WOL;
1435 else
1436 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001437 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001438
1439 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001440
françois romieuea809072010-11-08 13:23:58 +00001441 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1442
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001443 return 0;
1444}
1445
Francois Romieu31bd2042011-04-26 18:58:59 +02001446static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1447{
Francois Romieu85bffe62011-04-27 08:22:39 +02001448 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001449}
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451static void rtl8169_get_drvinfo(struct net_device *dev,
1452 struct ethtool_drvinfo *info)
1453{
1454 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001455 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Rick Jones68aad782011-11-07 13:29:27 +00001457 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1458 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1459 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001460 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001461 if (!IS_ERR_OR_NULL(rtl_fw))
1462 strlcpy(info->fw_version, rtl_fw->version,
1463 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
1466static int rtl8169_get_regs_len(struct net_device *dev)
1467{
1468 return R8169_REGS_SIZE;
1469}
1470
1471static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001472 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
1474 struct rtl8169_private *tp = netdev_priv(dev);
1475 void __iomem *ioaddr = tp->mmio_addr;
1476 int ret = 0;
1477 u32 reg;
1478
1479 reg = RTL_R32(TBICSR);
1480 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1481 (duplex == DUPLEX_FULL)) {
1482 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1483 } else if (autoneg == AUTONEG_ENABLE)
1484 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1485 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001486 netif_warn(tp, link, dev,
1487 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 ret = -EOPNOTSUPP;
1489 }
1490
1491 return ret;
1492}
1493
1494static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001495 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
1497 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001498 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001499 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Hayes Wang716b50a2011-02-22 17:26:18 +08001501 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001504 int auto_nego;
1505
françois romieu4da19632011-01-03 15:07:55 +00001506 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001507 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1508 ADVERTISE_100HALF | ADVERTISE_100FULL);
1509
1510 if (adv & ADVERTISED_10baseT_Half)
1511 auto_nego |= ADVERTISE_10HALF;
1512 if (adv & ADVERTISED_10baseT_Full)
1513 auto_nego |= ADVERTISE_10FULL;
1514 if (adv & ADVERTISED_100baseT_Half)
1515 auto_nego |= ADVERTISE_100HALF;
1516 if (adv & ADVERTISED_100baseT_Full)
1517 auto_nego |= ADVERTISE_100FULL;
1518
françois romieu3577aa12009-05-19 10:46:48 +00001519 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1520
françois romieu4da19632011-01-03 15:07:55 +00001521 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001522 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1523
1524 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001525 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001526 if (adv & ADVERTISED_1000baseT_Half)
1527 giga_ctrl |= ADVERTISE_1000HALF;
1528 if (adv & ADVERTISED_1000baseT_Full)
1529 giga_ctrl |= ADVERTISE_1000FULL;
1530 } else if (adv & (ADVERTISED_1000baseT_Half |
1531 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001532 netif_info(tp, link, dev,
1533 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001534 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
françois romieu3577aa12009-05-19 10:46:48 +00001537 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001538
françois romieu4da19632011-01-03 15:07:55 +00001539 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1540 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001541 } else {
1542 giga_ctrl = 0;
1543
1544 if (speed == SPEED_10)
1545 bmcr = 0;
1546 else if (speed == SPEED_100)
1547 bmcr = BMCR_SPEED100;
1548 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001549 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001550
1551 if (duplex == DUPLEX_FULL)
1552 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001553 }
1554
françois romieu4da19632011-01-03 15:07:55 +00001555 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001556
Francois Romieucecb5fd2011-04-01 10:21:07 +02001557 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1558 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001559 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001560 rtl_writephy(tp, 0x17, 0x2138);
1561 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001562 } else {
françois romieu4da19632011-01-03 15:07:55 +00001563 rtl_writephy(tp, 0x17, 0x2108);
1564 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001565 }
1566 }
1567
Oliver Neukum54405cd2011-01-06 21:55:13 +01001568 rc = 0;
1569out:
1570 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
1572
1573static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001574 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
1576 struct rtl8169_private *tp = netdev_priv(dev);
1577 int ret;
1578
Oliver Neukum54405cd2011-01-06 21:55:13 +01001579 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001580 if (ret < 0)
1581 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Francois Romieu4876cc12011-03-11 21:07:11 +01001583 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1584 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001586 }
1587out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 return ret;
1589}
1590
1591static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1592{
1593 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 int ret;
1595
Francois Romieu4876cc12011-03-11 21:07:11 +01001596 del_timer_sync(&tp->timer);
1597
Francois Romieuda78dbf2012-01-26 14:18:23 +01001598 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001599 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001600 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001601 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return ret;
1604}
1605
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001606static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1607 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
Francois Romieud58d46b2011-05-03 16:38:29 +02001609 struct rtl8169_private *tp = netdev_priv(dev);
1610
Francois Romieu2b7b4312011-04-18 22:53:24 -07001611 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001612 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Francois Romieud58d46b2011-05-03 16:38:29 +02001614 if (dev->mtu > JUMBO_1K &&
1615 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1616 features &= ~NETIF_F_IP_CSUM;
1617
Michał Mirosław350fb322011-04-08 06:35:56 +00001618 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
Francois Romieuda78dbf2012-01-26 14:18:23 +01001621static void __rtl8169_set_features(struct net_device *dev,
1622 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
1624 struct rtl8169_private *tp = netdev_priv(dev);
Ben Greear6bbe0212012-02-10 15:04:33 +00001625 netdev_features_t changed = features ^ dev->features;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001626 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Ben Greear6bbe0212012-02-10 15:04:33 +00001628 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)))
1629 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Ben Greear6bbe0212012-02-10 15:04:33 +00001631 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)) {
1632 if (features & NETIF_F_RXCSUM)
1633 tp->cp_cmd |= RxChkSum;
1634 else
1635 tp->cp_cmd &= ~RxChkSum;
Michał Mirosław350fb322011-04-08 06:35:56 +00001636
Ben Greear6bbe0212012-02-10 15:04:33 +00001637 if (dev->features & NETIF_F_HW_VLAN_RX)
1638 tp->cp_cmd |= RxVlan;
1639 else
1640 tp->cp_cmd &= ~RxVlan;
1641
1642 RTL_W16(CPlusCmd, tp->cp_cmd);
1643 RTL_R16(CPlusCmd);
1644 }
1645 if (changed & NETIF_F_RXALL) {
1646 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1647 if (features & NETIF_F_RXALL)
1648 tmp |= (AcceptErr | AcceptRunt);
1649 RTL_W32(RxConfig, tmp);
1650 }
Francois Romieuda78dbf2012-01-26 14:18:23 +01001651}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Francois Romieuda78dbf2012-01-26 14:18:23 +01001653static int rtl8169_set_features(struct net_device *dev,
1654 netdev_features_t features)
1655{
1656 struct rtl8169_private *tp = netdev_priv(dev);
1657
1658 rtl_lock_work(tp);
1659 __rtl8169_set_features(dev, features);
1660 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 return 0;
1663}
1664
Francois Romieuda78dbf2012-01-26 14:18:23 +01001665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1667 struct sk_buff *skb)
1668{
Jesse Grosseab6d182010-10-20 13:56:03 +00001669 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1671}
1672
Francois Romieu7a8fc772011-03-01 17:18:33 +01001673static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
1675 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Francois Romieu7a8fc772011-03-01 17:18:33 +01001677 if (opts2 & RxVlanTag)
1678 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
Francois Romieuccdffb92008-07-26 14:26:06 +02001683static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
1685 struct rtl8169_private *tp = netdev_priv(dev);
1686 void __iomem *ioaddr = tp->mmio_addr;
1687 u32 status;
1688
1689 cmd->supported =
1690 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1691 cmd->port = PORT_FIBRE;
1692 cmd->transceiver = XCVR_INTERNAL;
1693
1694 status = RTL_R32(TBICSR);
1695 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1696 cmd->autoneg = !!(status & TBINwEnable);
1697
David Decotigny70739492011-04-27 18:32:40 +00001698 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001700
1701 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
1703
Francois Romieuccdffb92008-07-26 14:26:06 +02001704static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
1706 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Francois Romieuccdffb92008-07-26 14:26:06 +02001708 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709}
1710
1711static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1712{
1713 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001714 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Francois Romieuda78dbf2012-01-26 14:18:23 +01001716 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02001717 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001718 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Francois Romieuccdffb92008-07-26 14:26:06 +02001720 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721}
1722
1723static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1724 void *p)
1725{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001726 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Francois Romieu5b0384f2006-08-16 16:00:01 +02001728 if (regs->len > R8169_REGS_SIZE)
1729 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Francois Romieuda78dbf2012-01-26 14:18:23 +01001731 rtl_lock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001732 memcpy_fromio(p, tp->mmio_addr, regs->len);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001733 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734}
1735
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001736static u32 rtl8169_get_msglevel(struct net_device *dev)
1737{
1738 struct rtl8169_private *tp = netdev_priv(dev);
1739
1740 return tp->msg_enable;
1741}
1742
1743static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1744{
1745 struct rtl8169_private *tp = netdev_priv(dev);
1746
1747 tp->msg_enable = value;
1748}
1749
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001750static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1751 "tx_packets",
1752 "rx_packets",
1753 "tx_errors",
1754 "rx_errors",
1755 "rx_missed",
1756 "align_errors",
1757 "tx_single_collisions",
1758 "tx_multi_collisions",
1759 "unicast",
1760 "broadcast",
1761 "multicast",
1762 "tx_aborted",
1763 "tx_underrun",
1764};
1765
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001766static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001767{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001768 switch (sset) {
1769 case ETH_SS_STATS:
1770 return ARRAY_SIZE(rtl8169_gstrings);
1771 default:
1772 return -EOPNOTSUPP;
1773 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001774}
1775
Ivan Vecera355423d2009-02-06 21:49:57 -08001776static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001777{
1778 struct rtl8169_private *tp = netdev_priv(dev);
1779 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001780 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001781 struct rtl8169_counters *counters;
1782 dma_addr_t paddr;
1783 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001784 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001785
Ivan Vecera355423d2009-02-06 21:49:57 -08001786 /*
1787 * Some chips are unable to dump tally counters when the receiver
1788 * is disabled.
1789 */
1790 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1791 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001792
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001793 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001794 if (!counters)
1795 return;
1796
1797 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001798 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001799 RTL_W32(CounterAddrLow, cmd);
1800 RTL_W32(CounterAddrLow, cmd | CounterDump);
1801
Ivan Vecera355423d2009-02-06 21:49:57 -08001802 while (wait--) {
1803 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001804 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001805 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001806 }
1807 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001808 }
1809
1810 RTL_W32(CounterAddrLow, 0);
1811 RTL_W32(CounterAddrHigh, 0);
1812
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001813 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001814}
1815
Ivan Vecera355423d2009-02-06 21:49:57 -08001816static void rtl8169_get_ethtool_stats(struct net_device *dev,
1817 struct ethtool_stats *stats, u64 *data)
1818{
1819 struct rtl8169_private *tp = netdev_priv(dev);
1820
1821 ASSERT_RTNL();
1822
1823 rtl8169_update_counters(dev);
1824
1825 data[0] = le64_to_cpu(tp->counters.tx_packets);
1826 data[1] = le64_to_cpu(tp->counters.rx_packets);
1827 data[2] = le64_to_cpu(tp->counters.tx_errors);
1828 data[3] = le32_to_cpu(tp->counters.rx_errors);
1829 data[4] = le16_to_cpu(tp->counters.rx_missed);
1830 data[5] = le16_to_cpu(tp->counters.align_errors);
1831 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1832 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1833 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1834 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1835 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1836 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1837 data[12] = le16_to_cpu(tp->counters.tx_underun);
1838}
1839
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001840static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1841{
1842 switch(stringset) {
1843 case ETH_SS_STATS:
1844 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1845 break;
1846 }
1847}
1848
Jeff Garzik7282d492006-09-13 14:30:00 -04001849static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 .get_drvinfo = rtl8169_get_drvinfo,
1851 .get_regs_len = rtl8169_get_regs_len,
1852 .get_link = ethtool_op_get_link,
1853 .get_settings = rtl8169_get_settings,
1854 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001855 .get_msglevel = rtl8169_get_msglevel,
1856 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001858 .get_wol = rtl8169_get_wol,
1859 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001860 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001861 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001862 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863};
1864
Francois Romieu07d3f512007-02-21 22:40:46 +01001865static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001866 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
Francois Romieu5d320a22011-05-08 17:47:36 +02001868 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001869 /*
1870 * The driver currently handles the 8168Bf and the 8168Be identically
1871 * but they can be identified more specifically through the test below
1872 * if needed:
1873 *
1874 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001875 *
1876 * Same thing for the 8101Eb and the 8101Ec:
1877 *
1878 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001879 */
Francois Romieu37441002011-06-17 22:58:54 +02001880 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001882 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 int mac_version;
1884 } mac_info[] = {
Hayes Wangc2218922011-09-06 16:55:18 +08001885 /* 8168F family. */
1886 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1887 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1888
hayeswang01dc7fe2011-03-21 01:50:28 +00001889 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001890 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001891 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1892 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1893 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1894
Francois Romieu5b538df2008-07-20 16:22:45 +02001895 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001896 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1897 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001898 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001899
françois romieue6de30d2011-01-03 15:08:37 +00001900 /* 8168DP family. */
1901 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1902 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001903 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001904
Francois Romieuef808d52008-06-29 13:10:54 +02001905 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001906 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001907 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001908 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001909 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001910 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1911 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001912 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001913 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001914 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001915
1916 /* 8168B family. */
1917 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1918 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1919 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1920 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1921
1922 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001923 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001924 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1925 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1926 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001927 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1928 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1929 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1930 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1931 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1932 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001933 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001934 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001935 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001936 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1937 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001938 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1939 /* FIXME: where did these entries come from ? -- FR */
1940 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1941 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1942
1943 /* 8110 family. */
1944 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1945 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1946 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1947 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1948 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1949 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1950
Jean Delvaref21b75e2009-05-26 20:54:48 -07001951 /* Catch-all */
1952 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001953 };
1954 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 u32 reg;
1956
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001957 reg = RTL_R32(TxConfig);
1958 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 p++;
1960 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001961
1962 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1963 netif_notice(tp, probe, dev,
1964 "unknown MAC, using family default\n");
1965 tp->mac_version = default_version;
1966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
1969static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1970{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001971 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
Francois Romieu867763c2007-08-17 18:21:58 +02001974struct phy_reg {
1975 u16 reg;
1976 u16 val;
1977};
1978
françois romieu4da19632011-01-03 15:07:55 +00001979static void rtl_writephy_batch(struct rtl8169_private *tp,
1980 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001981{
1982 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001983 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001984 regs++;
1985 }
1986}
1987
françois romieubca03d52011-01-03 15:07:31 +00001988#define PHY_READ 0x00000000
1989#define PHY_DATA_OR 0x10000000
1990#define PHY_DATA_AND 0x20000000
1991#define PHY_BJMPN 0x30000000
1992#define PHY_READ_EFUSE 0x40000000
1993#define PHY_READ_MAC_BYTE 0x50000000
1994#define PHY_WRITE_MAC_BYTE 0x60000000
1995#define PHY_CLEAR_READCOUNT 0x70000000
1996#define PHY_WRITE 0x80000000
1997#define PHY_READCOUNT_EQ_SKIP 0x90000000
1998#define PHY_COMP_EQ_SKIPN 0xa0000000
1999#define PHY_COMP_NEQ_SKIPN 0xb0000000
2000#define PHY_WRITE_PREVIOUS 0xc0000000
2001#define PHY_SKIPN 0xd0000000
2002#define PHY_DELAY_MS 0xe0000000
2003#define PHY_WRITE_ERI_WORD 0xf0000000
2004
Hayes Wang960aee62011-06-18 11:37:48 +02002005struct fw_info {
2006 u32 magic;
2007 char version[RTL_VER_SIZE];
2008 __le32 fw_start;
2009 __le32 fw_len;
2010 u8 chksum;
2011} __packed;
2012
Francois Romieu1c361ef2011-06-17 17:16:24 +02002013#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2014
2015static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002016{
Francois Romieub6ffd972011-06-17 17:00:05 +02002017 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002018 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002019 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2020 char *version = rtl_fw->version;
2021 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002022
Francois Romieu1c361ef2011-06-17 17:16:24 +02002023 if (fw->size < FW_OPCODE_SIZE)
2024 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002025
2026 if (!fw_info->magic) {
2027 size_t i, size, start;
2028 u8 checksum = 0;
2029
2030 if (fw->size < sizeof(*fw_info))
2031 goto out;
2032
2033 for (i = 0; i < fw->size; i++)
2034 checksum += fw->data[i];
2035 if (checksum != 0)
2036 goto out;
2037
2038 start = le32_to_cpu(fw_info->fw_start);
2039 if (start > fw->size)
2040 goto out;
2041
2042 size = le32_to_cpu(fw_info->fw_len);
2043 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2044 goto out;
2045
2046 memcpy(version, fw_info->version, RTL_VER_SIZE);
2047
2048 pa->code = (__le32 *)(fw->data + start);
2049 pa->size = size;
2050 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002051 if (fw->size % FW_OPCODE_SIZE)
2052 goto out;
2053
2054 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2055
2056 pa->code = (__le32 *)fw->data;
2057 pa->size = fw->size / FW_OPCODE_SIZE;
2058 }
2059 version[RTL_VER_SIZE - 1] = 0;
2060
2061 rc = true;
2062out:
2063 return rc;
2064}
2065
Francois Romieufd112f22011-06-18 00:10:29 +02002066static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2067 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002068{
Francois Romieufd112f22011-06-18 00:10:29 +02002069 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002070 size_t index;
2071
Francois Romieu1c361ef2011-06-17 17:16:24 +02002072 for (index = 0; index < pa->size; index++) {
2073 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002074 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002075
hayeswang42b82dc2011-01-10 02:07:25 +00002076 switch(action & 0xf0000000) {
2077 case PHY_READ:
2078 case PHY_DATA_OR:
2079 case PHY_DATA_AND:
2080 case PHY_READ_EFUSE:
2081 case PHY_CLEAR_READCOUNT:
2082 case PHY_WRITE:
2083 case PHY_WRITE_PREVIOUS:
2084 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002085 break;
2086
hayeswang42b82dc2011-01-10 02:07:25 +00002087 case PHY_BJMPN:
2088 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002089 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002090 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002091 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002092 }
2093 break;
2094 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002095 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002096 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002097 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002098 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002099 }
2100 break;
2101 case PHY_COMP_EQ_SKIPN:
2102 case PHY_COMP_NEQ_SKIPN:
2103 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002104 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002105 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002106 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002107 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002108 }
2109 break;
2110
2111 case PHY_READ_MAC_BYTE:
2112 case PHY_WRITE_MAC_BYTE:
2113 case PHY_WRITE_ERI_WORD:
2114 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002115 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002116 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002117 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002118 }
2119 }
Francois Romieufd112f22011-06-18 00:10:29 +02002120 rc = true;
2121out:
2122 return rc;
2123}
françois romieubca03d52011-01-03 15:07:31 +00002124
Francois Romieufd112f22011-06-18 00:10:29 +02002125static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2126{
2127 struct net_device *dev = tp->dev;
2128 int rc = -EINVAL;
2129
2130 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2131 netif_err(tp, ifup, dev, "invalid firwmare\n");
2132 goto out;
2133 }
2134
2135 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2136 rc = 0;
2137out:
2138 return rc;
2139}
2140
2141static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2142{
2143 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2144 u32 predata, count;
2145 size_t index;
2146
2147 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002148
Francois Romieu1c361ef2011-06-17 17:16:24 +02002149 for (index = 0; index < pa->size; ) {
2150 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002151 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002152 u32 regno = (action & 0x0fff0000) >> 16;
2153
2154 if (!action)
2155 break;
françois romieubca03d52011-01-03 15:07:31 +00002156
2157 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002158 case PHY_READ:
2159 predata = rtl_readphy(tp, regno);
2160 count++;
2161 index++;
françois romieubca03d52011-01-03 15:07:31 +00002162 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002163 case PHY_DATA_OR:
2164 predata |= data;
2165 index++;
2166 break;
2167 case PHY_DATA_AND:
2168 predata &= data;
2169 index++;
2170 break;
2171 case PHY_BJMPN:
2172 index -= regno;
2173 break;
2174 case PHY_READ_EFUSE:
2175 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2176 index++;
2177 break;
2178 case PHY_CLEAR_READCOUNT:
2179 count = 0;
2180 index++;
2181 break;
2182 case PHY_WRITE:
2183 rtl_writephy(tp, regno, data);
2184 index++;
2185 break;
2186 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002187 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002188 break;
2189 case PHY_COMP_EQ_SKIPN:
2190 if (predata == data)
2191 index += regno;
2192 index++;
2193 break;
2194 case PHY_COMP_NEQ_SKIPN:
2195 if (predata != data)
2196 index += regno;
2197 index++;
2198 break;
2199 case PHY_WRITE_PREVIOUS:
2200 rtl_writephy(tp, regno, predata);
2201 index++;
2202 break;
2203 case PHY_SKIPN:
2204 index += regno + 1;
2205 break;
2206 case PHY_DELAY_MS:
2207 mdelay(data);
2208 index++;
2209 break;
2210
2211 case PHY_READ_MAC_BYTE:
2212 case PHY_WRITE_MAC_BYTE:
2213 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002214 default:
2215 BUG();
2216 }
2217 }
2218}
2219
françois romieuf1e02ed2011-01-13 13:07:53 +00002220static void rtl_release_firmware(struct rtl8169_private *tp)
2221{
Francois Romieub6ffd972011-06-17 17:00:05 +02002222 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2223 release_firmware(tp->rtl_fw->fw);
2224 kfree(tp->rtl_fw);
2225 }
2226 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002227}
2228
François Romieu953a12c2011-04-24 17:38:48 +02002229static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002230{
Francois Romieub6ffd972011-06-17 17:00:05 +02002231 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002232
2233 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002234 if (!IS_ERR_OR_NULL(rtl_fw))
2235 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002236}
2237
2238static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2239{
2240 if (rtl_readphy(tp, reg) != val)
2241 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2242 else
2243 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002244}
2245
françois romieu4da19632011-01-03 15:07:55 +00002246static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002248 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002249 { 0x1f, 0x0001 },
2250 { 0x06, 0x006e },
2251 { 0x08, 0x0708 },
2252 { 0x15, 0x4000 },
2253 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
françois romieu0b9b5712009-08-10 19:44:56 +00002255 { 0x1f, 0x0001 },
2256 { 0x03, 0x00a1 },
2257 { 0x02, 0x0008 },
2258 { 0x01, 0x0120 },
2259 { 0x00, 0x1000 },
2260 { 0x04, 0x0800 },
2261 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
françois romieu0b9b5712009-08-10 19:44:56 +00002263 { 0x03, 0xff41 },
2264 { 0x02, 0xdf60 },
2265 { 0x01, 0x0140 },
2266 { 0x00, 0x0077 },
2267 { 0x04, 0x7800 },
2268 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
françois romieu0b9b5712009-08-10 19:44:56 +00002270 { 0x03, 0x802f },
2271 { 0x02, 0x4f02 },
2272 { 0x01, 0x0409 },
2273 { 0x00, 0xf0f9 },
2274 { 0x04, 0x9800 },
2275 { 0x04, 0x9000 },
2276
2277 { 0x03, 0xdf01 },
2278 { 0x02, 0xdf20 },
2279 { 0x01, 0xff95 },
2280 { 0x00, 0xba00 },
2281 { 0x04, 0xa800 },
2282 { 0x04, 0xa000 },
2283
2284 { 0x03, 0xff41 },
2285 { 0x02, 0xdf20 },
2286 { 0x01, 0x0140 },
2287 { 0x00, 0x00bb },
2288 { 0x04, 0xb800 },
2289 { 0x04, 0xb000 },
2290
2291 { 0x03, 0xdf41 },
2292 { 0x02, 0xdc60 },
2293 { 0x01, 0x6340 },
2294 { 0x00, 0x007d },
2295 { 0x04, 0xd800 },
2296 { 0x04, 0xd000 },
2297
2298 { 0x03, 0xdf01 },
2299 { 0x02, 0xdf20 },
2300 { 0x01, 0x100a },
2301 { 0x00, 0xa0ff },
2302 { 0x04, 0xf800 },
2303 { 0x04, 0xf000 },
2304
2305 { 0x1f, 0x0000 },
2306 { 0x0b, 0x0000 },
2307 { 0x00, 0x9200 }
2308 };
2309
françois romieu4da19632011-01-03 15:07:55 +00002310 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311}
2312
françois romieu4da19632011-01-03 15:07:55 +00002313static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002314{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002315 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002316 { 0x1f, 0x0002 },
2317 { 0x01, 0x90d0 },
2318 { 0x1f, 0x0000 }
2319 };
2320
françois romieu4da19632011-01-03 15:07:55 +00002321 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002322}
2323
françois romieu4da19632011-01-03 15:07:55 +00002324static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002325{
2326 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002327
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002328 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2329 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002330 return;
2331
françois romieu4da19632011-01-03 15:07:55 +00002332 rtl_writephy(tp, 0x1f, 0x0001);
2333 rtl_writephy(tp, 0x10, 0xf01b);
2334 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002335}
2336
françois romieu4da19632011-01-03 15:07:55 +00002337static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002338{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002339 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002340 { 0x1f, 0x0001 },
2341 { 0x04, 0x0000 },
2342 { 0x03, 0x00a1 },
2343 { 0x02, 0x0008 },
2344 { 0x01, 0x0120 },
2345 { 0x00, 0x1000 },
2346 { 0x04, 0x0800 },
2347 { 0x04, 0x9000 },
2348 { 0x03, 0x802f },
2349 { 0x02, 0x4f02 },
2350 { 0x01, 0x0409 },
2351 { 0x00, 0xf099 },
2352 { 0x04, 0x9800 },
2353 { 0x04, 0xa000 },
2354 { 0x03, 0xdf01 },
2355 { 0x02, 0xdf20 },
2356 { 0x01, 0xff95 },
2357 { 0x00, 0xba00 },
2358 { 0x04, 0xa800 },
2359 { 0x04, 0xf000 },
2360 { 0x03, 0xdf01 },
2361 { 0x02, 0xdf20 },
2362 { 0x01, 0x101a },
2363 { 0x00, 0xa0ff },
2364 { 0x04, 0xf800 },
2365 { 0x04, 0x0000 },
2366 { 0x1f, 0x0000 },
2367
2368 { 0x1f, 0x0001 },
2369 { 0x10, 0xf41b },
2370 { 0x14, 0xfb54 },
2371 { 0x18, 0xf5c7 },
2372 { 0x1f, 0x0000 },
2373
2374 { 0x1f, 0x0001 },
2375 { 0x17, 0x0cc0 },
2376 { 0x1f, 0x0000 }
2377 };
2378
françois romieu4da19632011-01-03 15:07:55 +00002379 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002380
françois romieu4da19632011-01-03 15:07:55 +00002381 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002382}
2383
françois romieu4da19632011-01-03 15:07:55 +00002384static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002385{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002386 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002387 { 0x1f, 0x0001 },
2388 { 0x04, 0x0000 },
2389 { 0x03, 0x00a1 },
2390 { 0x02, 0x0008 },
2391 { 0x01, 0x0120 },
2392 { 0x00, 0x1000 },
2393 { 0x04, 0x0800 },
2394 { 0x04, 0x9000 },
2395 { 0x03, 0x802f },
2396 { 0x02, 0x4f02 },
2397 { 0x01, 0x0409 },
2398 { 0x00, 0xf099 },
2399 { 0x04, 0x9800 },
2400 { 0x04, 0xa000 },
2401 { 0x03, 0xdf01 },
2402 { 0x02, 0xdf20 },
2403 { 0x01, 0xff95 },
2404 { 0x00, 0xba00 },
2405 { 0x04, 0xa800 },
2406 { 0x04, 0xf000 },
2407 { 0x03, 0xdf01 },
2408 { 0x02, 0xdf20 },
2409 { 0x01, 0x101a },
2410 { 0x00, 0xa0ff },
2411 { 0x04, 0xf800 },
2412 { 0x04, 0x0000 },
2413 { 0x1f, 0x0000 },
2414
2415 { 0x1f, 0x0001 },
2416 { 0x0b, 0x8480 },
2417 { 0x1f, 0x0000 },
2418
2419 { 0x1f, 0x0001 },
2420 { 0x18, 0x67c7 },
2421 { 0x04, 0x2000 },
2422 { 0x03, 0x002f },
2423 { 0x02, 0x4360 },
2424 { 0x01, 0x0109 },
2425 { 0x00, 0x3022 },
2426 { 0x04, 0x2800 },
2427 { 0x1f, 0x0000 },
2428
2429 { 0x1f, 0x0001 },
2430 { 0x17, 0x0cc0 },
2431 { 0x1f, 0x0000 }
2432 };
2433
françois romieu4da19632011-01-03 15:07:55 +00002434 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002435}
2436
françois romieu4da19632011-01-03 15:07:55 +00002437static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002438{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002439 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002440 { 0x10, 0xf41b },
2441 { 0x1f, 0x0000 }
2442 };
2443
françois romieu4da19632011-01-03 15:07:55 +00002444 rtl_writephy(tp, 0x1f, 0x0001);
2445 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002446
françois romieu4da19632011-01-03 15:07:55 +00002447 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002448}
2449
françois romieu4da19632011-01-03 15:07:55 +00002450static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002451{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002452 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002453 { 0x1f, 0x0001 },
2454 { 0x10, 0xf41b },
2455 { 0x1f, 0x0000 }
2456 };
2457
françois romieu4da19632011-01-03 15:07:55 +00002458 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002459}
2460
françois romieu4da19632011-01-03 15:07:55 +00002461static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002462{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002463 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002464 { 0x1f, 0x0000 },
2465 { 0x1d, 0x0f00 },
2466 { 0x1f, 0x0002 },
2467 { 0x0c, 0x1ec8 },
2468 { 0x1f, 0x0000 }
2469 };
2470
françois romieu4da19632011-01-03 15:07:55 +00002471 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002472}
2473
françois romieu4da19632011-01-03 15:07:55 +00002474static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002475{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002476 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002477 { 0x1f, 0x0001 },
2478 { 0x1d, 0x3d98 },
2479 { 0x1f, 0x0000 }
2480 };
2481
françois romieu4da19632011-01-03 15:07:55 +00002482 rtl_writephy(tp, 0x1f, 0x0000);
2483 rtl_patchphy(tp, 0x14, 1 << 5);
2484 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002485
françois romieu4da19632011-01-03 15:07:55 +00002486 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002487}
2488
françois romieu4da19632011-01-03 15:07:55 +00002489static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002490{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002491 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002492 { 0x1f, 0x0001 },
2493 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002494 { 0x1f, 0x0002 },
2495 { 0x00, 0x88d4 },
2496 { 0x01, 0x82b1 },
2497 { 0x03, 0x7002 },
2498 { 0x08, 0x9e30 },
2499 { 0x09, 0x01f0 },
2500 { 0x0a, 0x5500 },
2501 { 0x0c, 0x00c8 },
2502 { 0x1f, 0x0003 },
2503 { 0x12, 0xc096 },
2504 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002505 { 0x1f, 0x0000 },
2506 { 0x1f, 0x0000 },
2507 { 0x09, 0x2000 },
2508 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002509 };
2510
françois romieu4da19632011-01-03 15:07:55 +00002511 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002512
françois romieu4da19632011-01-03 15:07:55 +00002513 rtl_patchphy(tp, 0x14, 1 << 5);
2514 rtl_patchphy(tp, 0x0d, 1 << 5);
2515 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002516}
2517
françois romieu4da19632011-01-03 15:07:55 +00002518static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002519{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002520 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002521 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002522 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002523 { 0x03, 0x802f },
2524 { 0x02, 0x4f02 },
2525 { 0x01, 0x0409 },
2526 { 0x00, 0xf099 },
2527 { 0x04, 0x9800 },
2528 { 0x04, 0x9000 },
2529 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002530 { 0x1f, 0x0002 },
2531 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002532 { 0x06, 0x0761 },
2533 { 0x1f, 0x0003 },
2534 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002535 { 0x1f, 0x0000 }
2536 };
2537
françois romieu4da19632011-01-03 15:07:55 +00002538 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002539
françois romieu4da19632011-01-03 15:07:55 +00002540 rtl_patchphy(tp, 0x16, 1 << 0);
2541 rtl_patchphy(tp, 0x14, 1 << 5);
2542 rtl_patchphy(tp, 0x0d, 1 << 5);
2543 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002544}
2545
françois romieu4da19632011-01-03 15:07:55 +00002546static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002547{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002548 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002549 { 0x1f, 0x0001 },
2550 { 0x12, 0x2300 },
2551 { 0x1d, 0x3d98 },
2552 { 0x1f, 0x0002 },
2553 { 0x0c, 0x7eb8 },
2554 { 0x06, 0x5461 },
2555 { 0x1f, 0x0003 },
2556 { 0x16, 0x0f0a },
2557 { 0x1f, 0x0000 }
2558 };
2559
françois romieu4da19632011-01-03 15:07:55 +00002560 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002561
françois romieu4da19632011-01-03 15:07:55 +00002562 rtl_patchphy(tp, 0x16, 1 << 0);
2563 rtl_patchphy(tp, 0x14, 1 << 5);
2564 rtl_patchphy(tp, 0x0d, 1 << 5);
2565 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002566}
2567
françois romieu4da19632011-01-03 15:07:55 +00002568static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002569{
françois romieu4da19632011-01-03 15:07:55 +00002570 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002571}
2572
françois romieubca03d52011-01-03 15:07:31 +00002573static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002574{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002575 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002576 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002577 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002578 { 0x06, 0x4064 },
2579 { 0x07, 0x2863 },
2580 { 0x08, 0x059c },
2581 { 0x09, 0x26b4 },
2582 { 0x0a, 0x6a19 },
2583 { 0x0b, 0xdcc8 },
2584 { 0x10, 0xf06d },
2585 { 0x14, 0x7f68 },
2586 { 0x18, 0x7fd9 },
2587 { 0x1c, 0xf0ff },
2588 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002589 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002590 { 0x12, 0xf49f },
2591 { 0x13, 0x070b },
2592 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002593 { 0x14, 0x94c0 },
2594
2595 /*
2596 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002597 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002598 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002599 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002600 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002601 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002602 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002603 { 0x06, 0x5561 },
2604
2605 /*
2606 * Can not link to 1Gbps with bad cable
2607 * Decrease SNR threshold form 21.07dB to 19.04dB
2608 */
2609 { 0x1f, 0x0001 },
2610 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002611
2612 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002613 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002614 };
françois romieubca03d52011-01-03 15:07:31 +00002615 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002616
françois romieu4da19632011-01-03 15:07:55 +00002617 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002618
françois romieubca03d52011-01-03 15:07:31 +00002619 /*
2620 * Rx Error Issue
2621 * Fine Tune Switching regulator parameter
2622 */
françois romieu4da19632011-01-03 15:07:55 +00002623 rtl_writephy(tp, 0x1f, 0x0002);
2624 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2625 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002626
françois romieudaf9df62009-10-07 12:44:20 +00002627 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002628 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002629 { 0x1f, 0x0002 },
2630 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002631 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002632 { 0x05, 0x8330 },
2633 { 0x06, 0x669a },
2634 { 0x1f, 0x0002 }
2635 };
2636 int val;
2637
françois romieu4da19632011-01-03 15:07:55 +00002638 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002639
françois romieu4da19632011-01-03 15:07:55 +00002640 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002641
2642 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002643 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002644 0x0065, 0x0066, 0x0067, 0x0068,
2645 0x0069, 0x006a, 0x006b, 0x006c
2646 };
2647 int i;
2648
françois romieu4da19632011-01-03 15:07:55 +00002649 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002650
2651 val &= 0xff00;
2652 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002653 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002654 }
2655 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002656 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002657 { 0x1f, 0x0002 },
2658 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002659 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002660 { 0x05, 0x8330 },
2661 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002662 };
2663
françois romieu4da19632011-01-03 15:07:55 +00002664 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002665 }
2666
françois romieubca03d52011-01-03 15:07:31 +00002667 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002668 rtl_writephy(tp, 0x1f, 0x0002);
2669 rtl_patchphy(tp, 0x0d, 0x0300);
2670 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002671
françois romieubca03d52011-01-03 15:07:31 +00002672 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002673 rtl_writephy(tp, 0x1f, 0x0002);
2674 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2675 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002676
françois romieu4da19632011-01-03 15:07:55 +00002677 rtl_writephy(tp, 0x1f, 0x0005);
2678 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002679
2680 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002681
françois romieu4da19632011-01-03 15:07:55 +00002682 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002683}
2684
françois romieubca03d52011-01-03 15:07:31 +00002685static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002686{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002687 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002688 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002689 { 0x1f, 0x0001 },
2690 { 0x06, 0x4064 },
2691 { 0x07, 0x2863 },
2692 { 0x08, 0x059c },
2693 { 0x09, 0x26b4 },
2694 { 0x0a, 0x6a19 },
2695 { 0x0b, 0xdcc8 },
2696 { 0x10, 0xf06d },
2697 { 0x14, 0x7f68 },
2698 { 0x18, 0x7fd9 },
2699 { 0x1c, 0xf0ff },
2700 { 0x1d, 0x3d9c },
2701 { 0x1f, 0x0003 },
2702 { 0x12, 0xf49f },
2703 { 0x13, 0x070b },
2704 { 0x1a, 0x05ad },
2705 { 0x14, 0x94c0 },
2706
françois romieubca03d52011-01-03 15:07:31 +00002707 /*
2708 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002709 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002710 */
françois romieudaf9df62009-10-07 12:44:20 +00002711 { 0x1f, 0x0002 },
2712 { 0x06, 0x5561 },
2713 { 0x1f, 0x0005 },
2714 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002715 { 0x06, 0x5561 },
2716
2717 /*
2718 * Can not link to 1Gbps with bad cable
2719 * Decrease SNR threshold form 21.07dB to 19.04dB
2720 */
2721 { 0x1f, 0x0001 },
2722 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002723
2724 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002725 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002726 };
françois romieubca03d52011-01-03 15:07:31 +00002727 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002728
françois romieu4da19632011-01-03 15:07:55 +00002729 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002730
2731 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002732 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002733 { 0x1f, 0x0002 },
2734 { 0x05, 0x669a },
2735 { 0x1f, 0x0005 },
2736 { 0x05, 0x8330 },
2737 { 0x06, 0x669a },
2738
2739 { 0x1f, 0x0002 }
2740 };
2741 int val;
2742
françois romieu4da19632011-01-03 15:07:55 +00002743 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002744
françois romieu4da19632011-01-03 15:07:55 +00002745 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002746 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002747 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002748 0x0065, 0x0066, 0x0067, 0x0068,
2749 0x0069, 0x006a, 0x006b, 0x006c
2750 };
2751 int i;
2752
françois romieu4da19632011-01-03 15:07:55 +00002753 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002754
2755 val &= 0xff00;
2756 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002757 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002758 }
2759 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002760 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002761 { 0x1f, 0x0002 },
2762 { 0x05, 0x2642 },
2763 { 0x1f, 0x0005 },
2764 { 0x05, 0x8330 },
2765 { 0x06, 0x2642 }
2766 };
2767
françois romieu4da19632011-01-03 15:07:55 +00002768 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002769 }
2770
françois romieubca03d52011-01-03 15:07:31 +00002771 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002772 rtl_writephy(tp, 0x1f, 0x0002);
2773 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2774 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002775
françois romieubca03d52011-01-03 15:07:31 +00002776 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002777 rtl_writephy(tp, 0x1f, 0x0002);
2778 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002779
françois romieu4da19632011-01-03 15:07:55 +00002780 rtl_writephy(tp, 0x1f, 0x0005);
2781 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002782
2783 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002784
françois romieu4da19632011-01-03 15:07:55 +00002785 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002786}
2787
françois romieu4da19632011-01-03 15:07:55 +00002788static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002789{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002790 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002791 { 0x1f, 0x0002 },
2792 { 0x10, 0x0008 },
2793 { 0x0d, 0x006c },
2794
2795 { 0x1f, 0x0000 },
2796 { 0x0d, 0xf880 },
2797
2798 { 0x1f, 0x0001 },
2799 { 0x17, 0x0cc0 },
2800
2801 { 0x1f, 0x0001 },
2802 { 0x0b, 0xa4d8 },
2803 { 0x09, 0x281c },
2804 { 0x07, 0x2883 },
2805 { 0x0a, 0x6b35 },
2806 { 0x1d, 0x3da4 },
2807 { 0x1c, 0xeffd },
2808 { 0x14, 0x7f52 },
2809 { 0x18, 0x7fc6 },
2810 { 0x08, 0x0601 },
2811 { 0x06, 0x4063 },
2812 { 0x10, 0xf074 },
2813 { 0x1f, 0x0003 },
2814 { 0x13, 0x0789 },
2815 { 0x12, 0xf4bd },
2816 { 0x1a, 0x04fd },
2817 { 0x14, 0x84b0 },
2818 { 0x1f, 0x0000 },
2819 { 0x00, 0x9200 },
2820
2821 { 0x1f, 0x0005 },
2822 { 0x01, 0x0340 },
2823 { 0x1f, 0x0001 },
2824 { 0x04, 0x4000 },
2825 { 0x03, 0x1d21 },
2826 { 0x02, 0x0c32 },
2827 { 0x01, 0x0200 },
2828 { 0x00, 0x5554 },
2829 { 0x04, 0x4800 },
2830 { 0x04, 0x4000 },
2831 { 0x04, 0xf000 },
2832 { 0x03, 0xdf01 },
2833 { 0x02, 0xdf20 },
2834 { 0x01, 0x101a },
2835 { 0x00, 0xa0ff },
2836 { 0x04, 0xf800 },
2837 { 0x04, 0xf000 },
2838 { 0x1f, 0x0000 },
2839
2840 { 0x1f, 0x0007 },
2841 { 0x1e, 0x0023 },
2842 { 0x16, 0x0000 },
2843 { 0x1f, 0x0000 }
2844 };
2845
françois romieu4da19632011-01-03 15:07:55 +00002846 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002847}
2848
françois romieue6de30d2011-01-03 15:08:37 +00002849static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2850{
2851 static const struct phy_reg phy_reg_init[] = {
2852 { 0x1f, 0x0001 },
2853 { 0x17, 0x0cc0 },
2854
2855 { 0x1f, 0x0007 },
2856 { 0x1e, 0x002d },
2857 { 0x18, 0x0040 },
2858 { 0x1f, 0x0000 }
2859 };
2860
2861 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2862 rtl_patchphy(tp, 0x0d, 1 << 5);
2863}
2864
Hayes Wang70090422011-07-06 15:58:06 +08002865static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002866{
2867 static const struct phy_reg phy_reg_init[] = {
2868 /* Enable Delay cap */
2869 { 0x1f, 0x0005 },
2870 { 0x05, 0x8b80 },
2871 { 0x06, 0xc896 },
2872 { 0x1f, 0x0000 },
2873
2874 /* Channel estimation fine tune */
2875 { 0x1f, 0x0001 },
2876 { 0x0b, 0x6c20 },
2877 { 0x07, 0x2872 },
2878 { 0x1c, 0xefff },
2879 { 0x1f, 0x0003 },
2880 { 0x14, 0x6420 },
2881 { 0x1f, 0x0000 },
2882
2883 /* Update PFM & 10M TX idle timer */
2884 { 0x1f, 0x0007 },
2885 { 0x1e, 0x002f },
2886 { 0x15, 0x1919 },
2887 { 0x1f, 0x0000 },
2888
2889 { 0x1f, 0x0007 },
2890 { 0x1e, 0x00ac },
2891 { 0x18, 0x0006 },
2892 { 0x1f, 0x0000 }
2893 };
2894
Francois Romieu15ecd032011-04-27 13:52:22 -07002895 rtl_apply_firmware(tp);
2896
hayeswang01dc7fe2011-03-21 01:50:28 +00002897 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2898
2899 /* DCO enable for 10M IDLE Power */
2900 rtl_writephy(tp, 0x1f, 0x0007);
2901 rtl_writephy(tp, 0x1e, 0x0023);
2902 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2903 rtl_writephy(tp, 0x1f, 0x0000);
2904
2905 /* For impedance matching */
2906 rtl_writephy(tp, 0x1f, 0x0002);
2907 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002908 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002909
2910 /* PHY auto speed down */
2911 rtl_writephy(tp, 0x1f, 0x0007);
2912 rtl_writephy(tp, 0x1e, 0x002d);
2913 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2914 rtl_writephy(tp, 0x1f, 0x0000);
2915 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2916
2917 rtl_writephy(tp, 0x1f, 0x0005);
2918 rtl_writephy(tp, 0x05, 0x8b86);
2919 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2920 rtl_writephy(tp, 0x1f, 0x0000);
2921
2922 rtl_writephy(tp, 0x1f, 0x0005);
2923 rtl_writephy(tp, 0x05, 0x8b85);
2924 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2925 rtl_writephy(tp, 0x1f, 0x0007);
2926 rtl_writephy(tp, 0x1e, 0x0020);
2927 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2928 rtl_writephy(tp, 0x1f, 0x0006);
2929 rtl_writephy(tp, 0x00, 0x5a00);
2930 rtl_writephy(tp, 0x1f, 0x0000);
2931 rtl_writephy(tp, 0x0d, 0x0007);
2932 rtl_writephy(tp, 0x0e, 0x003c);
2933 rtl_writephy(tp, 0x0d, 0x4007);
2934 rtl_writephy(tp, 0x0e, 0x0000);
2935 rtl_writephy(tp, 0x0d, 0x0000);
2936}
2937
Hayes Wang70090422011-07-06 15:58:06 +08002938static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2939{
2940 static const struct phy_reg phy_reg_init[] = {
2941 /* Enable Delay cap */
2942 { 0x1f, 0x0004 },
2943 { 0x1f, 0x0007 },
2944 { 0x1e, 0x00ac },
2945 { 0x18, 0x0006 },
2946 { 0x1f, 0x0002 },
2947 { 0x1f, 0x0000 },
2948 { 0x1f, 0x0000 },
2949
2950 /* Channel estimation fine tune */
2951 { 0x1f, 0x0003 },
2952 { 0x09, 0xa20f },
2953 { 0x1f, 0x0000 },
2954 { 0x1f, 0x0000 },
2955
2956 /* Green Setting */
2957 { 0x1f, 0x0005 },
2958 { 0x05, 0x8b5b },
2959 { 0x06, 0x9222 },
2960 { 0x05, 0x8b6d },
2961 { 0x06, 0x8000 },
2962 { 0x05, 0x8b76 },
2963 { 0x06, 0x8000 },
2964 { 0x1f, 0x0000 }
2965 };
2966
2967 rtl_apply_firmware(tp);
2968
2969 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2970
2971 /* For 4-corner performance improve */
2972 rtl_writephy(tp, 0x1f, 0x0005);
2973 rtl_writephy(tp, 0x05, 0x8b80);
2974 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2975 rtl_writephy(tp, 0x1f, 0x0000);
2976
2977 /* PHY auto speed down */
2978 rtl_writephy(tp, 0x1f, 0x0004);
2979 rtl_writephy(tp, 0x1f, 0x0007);
2980 rtl_writephy(tp, 0x1e, 0x002d);
2981 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2982 rtl_writephy(tp, 0x1f, 0x0002);
2983 rtl_writephy(tp, 0x1f, 0x0000);
2984 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2985
2986 /* improve 10M EEE waveform */
2987 rtl_writephy(tp, 0x1f, 0x0005);
2988 rtl_writephy(tp, 0x05, 0x8b86);
2989 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2990 rtl_writephy(tp, 0x1f, 0x0000);
2991
2992 /* Improve 2-pair detection performance */
2993 rtl_writephy(tp, 0x1f, 0x0005);
2994 rtl_writephy(tp, 0x05, 0x8b85);
2995 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
2996 rtl_writephy(tp, 0x1f, 0x0000);
2997
2998 /* EEE setting */
2999 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
3000 ERIAR_EXGMAC);
3001 rtl_writephy(tp, 0x1f, 0x0005);
3002 rtl_writephy(tp, 0x05, 0x8b85);
3003 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3004 rtl_writephy(tp, 0x1f, 0x0004);
3005 rtl_writephy(tp, 0x1f, 0x0007);
3006 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04003007 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003008 rtl_writephy(tp, 0x1f, 0x0002);
3009 rtl_writephy(tp, 0x1f, 0x0000);
3010 rtl_writephy(tp, 0x0d, 0x0007);
3011 rtl_writephy(tp, 0x0e, 0x003c);
3012 rtl_writephy(tp, 0x0d, 0x4007);
3013 rtl_writephy(tp, 0x0e, 0x0000);
3014 rtl_writephy(tp, 0x0d, 0x0000);
3015
3016 /* Green feature */
3017 rtl_writephy(tp, 0x1f, 0x0003);
3018 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3019 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3020 rtl_writephy(tp, 0x1f, 0x0000);
3021}
3022
Hayes Wangc2218922011-09-06 16:55:18 +08003023static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3024{
3025 static const struct phy_reg phy_reg_init[] = {
3026 /* Channel estimation fine tune */
3027 { 0x1f, 0x0003 },
3028 { 0x09, 0xa20f },
3029 { 0x1f, 0x0000 },
3030
3031 /* Modify green table for giga & fnet */
3032 { 0x1f, 0x0005 },
3033 { 0x05, 0x8b55 },
3034 { 0x06, 0x0000 },
3035 { 0x05, 0x8b5e },
3036 { 0x06, 0x0000 },
3037 { 0x05, 0x8b67 },
3038 { 0x06, 0x0000 },
3039 { 0x05, 0x8b70 },
3040 { 0x06, 0x0000 },
3041 { 0x1f, 0x0000 },
3042 { 0x1f, 0x0007 },
3043 { 0x1e, 0x0078 },
3044 { 0x17, 0x0000 },
3045 { 0x19, 0x00fb },
3046 { 0x1f, 0x0000 },
3047
3048 /* Modify green table for 10M */
3049 { 0x1f, 0x0005 },
3050 { 0x05, 0x8b79 },
3051 { 0x06, 0xaa00 },
3052 { 0x1f, 0x0000 },
3053
3054 /* Disable hiimpedance detection (RTCT) */
3055 { 0x1f, 0x0003 },
3056 { 0x01, 0x328a },
3057 { 0x1f, 0x0000 }
3058 };
3059
3060 rtl_apply_firmware(tp);
3061
3062 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3063
3064 /* For 4-corner performance improve */
3065 rtl_writephy(tp, 0x1f, 0x0005);
3066 rtl_writephy(tp, 0x05, 0x8b80);
3067 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3068 rtl_writephy(tp, 0x1f, 0x0000);
3069
3070 /* PHY auto speed down */
3071 rtl_writephy(tp, 0x1f, 0x0007);
3072 rtl_writephy(tp, 0x1e, 0x002d);
3073 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3074 rtl_writephy(tp, 0x1f, 0x0000);
3075 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3076
3077 /* Improve 10M EEE waveform */
3078 rtl_writephy(tp, 0x1f, 0x0005);
3079 rtl_writephy(tp, 0x05, 0x8b86);
3080 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3081 rtl_writephy(tp, 0x1f, 0x0000);
3082
3083 /* Improve 2-pair detection performance */
3084 rtl_writephy(tp, 0x1f, 0x0005);
3085 rtl_writephy(tp, 0x05, 0x8b85);
3086 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3087 rtl_writephy(tp, 0x1f, 0x0000);
3088}
3089
3090static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3091{
3092 rtl_apply_firmware(tp);
3093
3094 /* For 4-corner performance improve */
3095 rtl_writephy(tp, 0x1f, 0x0005);
3096 rtl_writephy(tp, 0x05, 0x8b80);
3097 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3098 rtl_writephy(tp, 0x1f, 0x0000);
3099
3100 /* PHY auto speed down */
3101 rtl_writephy(tp, 0x1f, 0x0007);
3102 rtl_writephy(tp, 0x1e, 0x002d);
3103 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3104 rtl_writephy(tp, 0x1f, 0x0000);
3105 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3106
3107 /* Improve 10M EEE waveform */
3108 rtl_writephy(tp, 0x1f, 0x0005);
3109 rtl_writephy(tp, 0x05, 0x8b86);
3110 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3111 rtl_writephy(tp, 0x1f, 0x0000);
3112}
3113
françois romieu4da19632011-01-03 15:07:55 +00003114static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003115{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003116 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003117 { 0x1f, 0x0003 },
3118 { 0x08, 0x441d },
3119 { 0x01, 0x9100 },
3120 { 0x1f, 0x0000 }
3121 };
3122
françois romieu4da19632011-01-03 15:07:55 +00003123 rtl_writephy(tp, 0x1f, 0x0000);
3124 rtl_patchphy(tp, 0x11, 1 << 12);
3125 rtl_patchphy(tp, 0x19, 1 << 13);
3126 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003127
françois romieu4da19632011-01-03 15:07:55 +00003128 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003129}
3130
Hayes Wang5a5e4442011-02-22 17:26:21 +08003131static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3132{
3133 static const struct phy_reg phy_reg_init[] = {
3134 { 0x1f, 0x0005 },
3135 { 0x1a, 0x0000 },
3136 { 0x1f, 0x0000 },
3137
3138 { 0x1f, 0x0004 },
3139 { 0x1c, 0x0000 },
3140 { 0x1f, 0x0000 },
3141
3142 { 0x1f, 0x0001 },
3143 { 0x15, 0x7701 },
3144 { 0x1f, 0x0000 }
3145 };
3146
3147 /* Disable ALDPS before ram code */
3148 rtl_writephy(tp, 0x1f, 0x0000);
3149 rtl_writephy(tp, 0x18, 0x0310);
3150 msleep(100);
3151
François Romieu953a12c2011-04-24 17:38:48 +02003152 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003153
3154 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3155}
3156
Francois Romieu5615d9f2007-08-17 17:50:46 +02003157static void rtl_hw_phy_config(struct net_device *dev)
3158{
3159 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003160
3161 rtl8169_print_mac_version(tp);
3162
3163 switch (tp->mac_version) {
3164 case RTL_GIGA_MAC_VER_01:
3165 break;
3166 case RTL_GIGA_MAC_VER_02:
3167 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003168 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003169 break;
3170 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003171 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003172 break;
françois romieu2e9558562009-08-10 19:44:19 +00003173 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003174 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003175 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003176 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003177 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003178 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003179 case RTL_GIGA_MAC_VER_07:
3180 case RTL_GIGA_MAC_VER_08:
3181 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003182 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003183 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003184 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003185 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003186 break;
3187 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003188 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003189 break;
3190 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003191 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003192 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003193 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003194 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003195 break;
3196 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003197 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003198 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003199 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003200 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003201 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003202 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003203 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003204 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003205 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003206 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003207 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003208 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003209 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003210 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003211 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003212 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003213 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003214 break;
3215 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003216 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003217 break;
3218 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003219 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003220 break;
françois romieue6de30d2011-01-03 15:08:37 +00003221 case RTL_GIGA_MAC_VER_28:
3222 rtl8168d_4_hw_phy_config(tp);
3223 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003224 case RTL_GIGA_MAC_VER_29:
3225 case RTL_GIGA_MAC_VER_30:
3226 rtl8105e_hw_phy_config(tp);
3227 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003228 case RTL_GIGA_MAC_VER_31:
3229 /* None. */
3230 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003231 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003232 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003233 rtl8168e_1_hw_phy_config(tp);
3234 break;
3235 case RTL_GIGA_MAC_VER_34:
3236 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003237 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003238 case RTL_GIGA_MAC_VER_35:
3239 rtl8168f_1_hw_phy_config(tp);
3240 break;
3241 case RTL_GIGA_MAC_VER_36:
3242 rtl8168f_2_hw_phy_config(tp);
3243 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003244
Francois Romieu5615d9f2007-08-17 17:50:46 +02003245 default:
3246 break;
3247 }
3248}
3249
Francois Romieuda78dbf2012-01-26 14:18:23 +01003250static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 struct timer_list *timer = &tp->timer;
3253 void __iomem *ioaddr = tp->mmio_addr;
3254 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3255
Francois Romieubcf0bf92006-07-26 23:14:13 +02003256 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
françois romieu4da19632011-01-03 15:07:55 +00003258 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003259 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 * A busy loop could burn quite a few cycles on nowadays CPU.
3261 * Let's delay the execution of the timer for a few ticks.
3262 */
3263 timeout = HZ/10;
3264 goto out_mod_timer;
3265 }
3266
3267 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01003268 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Francois Romieuda78dbf2012-01-26 14:18:23 +01003270 netif_warn(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
françois romieu4da19632011-01-03 15:07:55 +00003272 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
3274out_mod_timer:
3275 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003276}
3277
3278static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3279{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003280 if (!test_and_set_bit(flag, tp->wk.flags))
3281 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003282}
3283
3284static void rtl8169_phy_timer(unsigned long __opaque)
3285{
3286 struct net_device *dev = (struct net_device *)__opaque;
3287 struct rtl8169_private *tp = netdev_priv(dev);
3288
Francois Romieu98ddf982012-01-31 10:47:34 +01003289 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290}
3291
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292#ifdef CONFIG_NET_POLL_CONTROLLER
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293static void rtl8169_netpoll(struct net_device *dev)
3294{
3295 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296
Francois Romieuda78dbf2012-01-26 14:18:23 +01003297 rtl8169_interrupt(tp->pci_dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298}
3299#endif
3300
3301static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3302 void __iomem *ioaddr)
3303{
3304 iounmap(ioaddr);
3305 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003306 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 pci_disable_device(pdev);
3308 free_netdev(dev);
3309}
3310
Francois Romieubf793292006-11-01 00:53:05 +01003311static void rtl8169_phy_reset(struct net_device *dev,
3312 struct rtl8169_private *tp)
3313{
Francois Romieu07d3f512007-02-21 22:40:46 +01003314 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003315
françois romieu4da19632011-01-03 15:07:55 +00003316 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003317 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003318 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003319 return;
3320 msleep(1);
3321 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003322 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003323}
3324
David S. Miller8decf862011-09-22 03:23:13 -04003325static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3326{
3327 void __iomem *ioaddr = tp->mmio_addr;
3328
3329 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3330 (RTL_R8(PHYstatus) & TBI_Enable);
3331}
3332
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003333static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003335 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003336
Francois Romieu5615d9f2007-08-17 17:50:46 +02003337 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003338
Marcus Sundberg773328942008-07-10 21:28:08 +02003339 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3340 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3341 RTL_W8(0x82, 0x01);
3342 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003343
Francois Romieu6dccd162007-02-13 23:38:05 +01003344 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3345
3346 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3347 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003348
Francois Romieubcf0bf92006-07-26 23:14:13 +02003349 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003350 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3351 RTL_W8(0x82, 0x01);
3352 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003353 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003354 }
3355
Francois Romieubf793292006-11-01 00:53:05 +01003356 rtl8169_phy_reset(dev, tp);
3357
Oliver Neukum54405cd2011-01-06 21:55:13 +01003358 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003359 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3360 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3361 (tp->mii.supports_gmii ?
3362 ADVERTISED_1000baseT_Half |
3363 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003364
David S. Miller8decf862011-09-22 03:23:13 -04003365 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003366 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003367}
3368
Francois Romieu773d2022007-01-31 23:47:43 +01003369static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3370{
3371 void __iomem *ioaddr = tp->mmio_addr;
3372 u32 high;
3373 u32 low;
3374
3375 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3376 high = addr[4] | (addr[5] << 8);
3377
Francois Romieuda78dbf2012-01-26 14:18:23 +01003378 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003379
3380 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2bf2010-04-26 11:42:58 +00003381
Francois Romieu773d2022007-01-31 23:47:43 +01003382 RTL_W32(MAC4, high);
françois romieu908ba2bf2010-04-26 11:42:58 +00003383 RTL_R32(MAC4);
3384
Francois Romieu78f1cd02010-03-27 19:35:46 -07003385 RTL_W32(MAC0, low);
françois romieu908ba2bf2010-04-26 11:42:58 +00003386 RTL_R32(MAC0);
3387
françois romieuc28aa382011-08-02 03:53:43 +00003388 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3389 const struct exgmac_reg e[] = {
3390 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3391 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3392 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3393 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3394 low >> 16 },
3395 };
3396
3397 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3398 }
3399
Francois Romieu773d2022007-01-31 23:47:43 +01003400 RTL_W8(Cfg9346, Cfg9346_Lock);
3401
Francois Romieuda78dbf2012-01-26 14:18:23 +01003402 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003403}
3404
3405static int rtl_set_mac_address(struct net_device *dev, void *p)
3406{
3407 struct rtl8169_private *tp = netdev_priv(dev);
3408 struct sockaddr *addr = p;
3409
3410 if (!is_valid_ether_addr(addr->sa_data))
3411 return -EADDRNOTAVAIL;
3412
3413 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3414
3415 rtl_rar_set(tp, dev->dev_addr);
3416
3417 return 0;
3418}
3419
Francois Romieu5f787a12006-08-17 13:02:36 +02003420static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3421{
3422 struct rtl8169_private *tp = netdev_priv(dev);
3423 struct mii_ioctl_data *data = if_mii(ifr);
3424
Francois Romieu8b4ab282008-11-19 22:05:25 -08003425 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3426}
Francois Romieu5f787a12006-08-17 13:02:36 +02003427
Francois Romieucecb5fd2011-04-01 10:21:07 +02003428static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3429 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003430{
Francois Romieu5f787a12006-08-17 13:02:36 +02003431 switch (cmd) {
3432 case SIOCGMIIPHY:
3433 data->phy_id = 32; /* Internal PHY */
3434 return 0;
3435
3436 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003437 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003438 return 0;
3439
3440 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003441 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003442 return 0;
3443 }
3444 return -EOPNOTSUPP;
3445}
3446
Francois Romieu8b4ab282008-11-19 22:05:25 -08003447static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3448{
3449 return -EOPNOTSUPP;
3450}
3451
Francois Romieu0e485152007-02-20 00:00:26 +01003452static const struct rtl_cfg_info {
3453 void (*hw_start)(struct net_device *);
3454 unsigned int region;
3455 unsigned int align;
Francois Romieuda78dbf2012-01-26 14:18:23 +01003456 u16 event_slow;
Francois Romieuccdffb92008-07-26 14:26:06 +02003457 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07003458 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01003459} rtl_cfg_infos [] = {
3460 [RTL_CFG_0] = {
3461 .hw_start = rtl_hw_start_8169,
3462 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01003463 .align = 0,
Francois Romieuda78dbf2012-01-26 14:18:23 +01003464 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003465 .features = RTL_FEATURE_GMII,
3466 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01003467 },
3468 [RTL_CFG_1] = {
3469 .hw_start = rtl_hw_start_8168,
3470 .region = 2,
3471 .align = 8,
Francois Romieuda78dbf2012-01-26 14:18:23 +01003472 .event_slow = SYSErr | LinkChg | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003473 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3474 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01003475 },
3476 [RTL_CFG_2] = {
3477 .hw_start = rtl_hw_start_8101,
3478 .region = 2,
3479 .align = 8,
Francois Romieuda78dbf2012-01-26 14:18:23 +01003480 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
3481 PCSTimeout,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003482 .features = RTL_FEATURE_MSI,
3483 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01003484 }
3485};
3486
Francois Romieufbac58f2007-10-04 22:51:38 +02003487/* Cfg9346_Unlock assumed. */
françois romieu2ca6cf02011-12-15 08:37:43 +00003488static unsigned rtl_try_msi(struct rtl8169_private *tp,
Francois Romieufbac58f2007-10-04 22:51:38 +02003489 const struct rtl_cfg_info *cfg)
3490{
françois romieu2ca6cf02011-12-15 08:37:43 +00003491 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieufbac58f2007-10-04 22:51:38 +02003492 unsigned msi = 0;
3493 u8 cfg2;
3494
3495 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02003496 if (cfg->features & RTL_FEATURE_MSI) {
françois romieu2ca6cf02011-12-15 08:37:43 +00003497 if (pci_enable_msi(tp->pci_dev)) {
3498 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
Francois Romieufbac58f2007-10-04 22:51:38 +02003499 } else {
3500 cfg2 |= MSIEnable;
3501 msi = RTL_FEATURE_MSI;
3502 }
3503 }
françois romieu2ca6cf02011-12-15 08:37:43 +00003504 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3505 RTL_W8(Config2, cfg2);
Francois Romieufbac58f2007-10-04 22:51:38 +02003506 return msi;
3507}
3508
3509static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3510{
3511 if (tp->features & RTL_FEATURE_MSI) {
3512 pci_disable_msi(pdev);
3513 tp->features &= ~RTL_FEATURE_MSI;
3514 }
3515}
3516
françois romieuc0e45c12011-01-03 15:08:04 +00003517static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3518{
3519 struct mdio_ops *ops = &tp->mdio_ops;
3520
3521 switch (tp->mac_version) {
3522 case RTL_GIGA_MAC_VER_27:
3523 ops->write = r8168dp_1_mdio_write;
3524 ops->read = r8168dp_1_mdio_read;
3525 break;
françois romieue6de30d2011-01-03 15:08:37 +00003526 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003527 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003528 ops->write = r8168dp_2_mdio_write;
3529 ops->read = r8168dp_2_mdio_read;
3530 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003531 default:
3532 ops->write = r8169_mdio_write;
3533 ops->read = r8169_mdio_read;
3534 break;
3535 }
3536}
3537
David S. Miller1805b2f2011-10-24 18:18:09 -04003538static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3539{
3540 void __iomem *ioaddr = tp->mmio_addr;
3541
3542 switch (tp->mac_version) {
3543 case RTL_GIGA_MAC_VER_29:
3544 case RTL_GIGA_MAC_VER_30:
3545 case RTL_GIGA_MAC_VER_32:
3546 case RTL_GIGA_MAC_VER_33:
3547 case RTL_GIGA_MAC_VER_34:
3548 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3549 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3550 break;
3551 default:
3552 break;
3553 }
3554}
3555
3556static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3557{
3558 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3559 return false;
3560
3561 rtl_writephy(tp, 0x1f, 0x0000);
3562 rtl_writephy(tp, MII_BMCR, 0x0000);
3563
3564 rtl_wol_suspend_quirk(tp);
3565
3566 return true;
3567}
3568
françois romieu065c27c2011-01-03 15:08:12 +00003569static void r810x_phy_power_down(struct rtl8169_private *tp)
3570{
3571 rtl_writephy(tp, 0x1f, 0x0000);
3572 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3573}
3574
3575static void r810x_phy_power_up(struct rtl8169_private *tp)
3576{
3577 rtl_writephy(tp, 0x1f, 0x0000);
3578 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3579}
3580
3581static void r810x_pll_power_down(struct rtl8169_private *tp)
3582{
David S. Miller1805b2f2011-10-24 18:18:09 -04003583 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003584 return;
françois romieu065c27c2011-01-03 15:08:12 +00003585
3586 r810x_phy_power_down(tp);
3587}
3588
3589static void r810x_pll_power_up(struct rtl8169_private *tp)
3590{
3591 r810x_phy_power_up(tp);
3592}
3593
3594static void r8168_phy_power_up(struct rtl8169_private *tp)
3595{
3596 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003597 switch (tp->mac_version) {
3598 case RTL_GIGA_MAC_VER_11:
3599 case RTL_GIGA_MAC_VER_12:
3600 case RTL_GIGA_MAC_VER_17:
3601 case RTL_GIGA_MAC_VER_18:
3602 case RTL_GIGA_MAC_VER_19:
3603 case RTL_GIGA_MAC_VER_20:
3604 case RTL_GIGA_MAC_VER_21:
3605 case RTL_GIGA_MAC_VER_22:
3606 case RTL_GIGA_MAC_VER_23:
3607 case RTL_GIGA_MAC_VER_24:
3608 case RTL_GIGA_MAC_VER_25:
3609 case RTL_GIGA_MAC_VER_26:
3610 case RTL_GIGA_MAC_VER_27:
3611 case RTL_GIGA_MAC_VER_28:
3612 case RTL_GIGA_MAC_VER_31:
3613 rtl_writephy(tp, 0x0e, 0x0000);
3614 break;
3615 default:
3616 break;
3617 }
françois romieu065c27c2011-01-03 15:08:12 +00003618 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3619}
3620
3621static void r8168_phy_power_down(struct rtl8169_private *tp)
3622{
3623 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003624 switch (tp->mac_version) {
3625 case RTL_GIGA_MAC_VER_32:
3626 case RTL_GIGA_MAC_VER_33:
3627 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3628 break;
3629
3630 case RTL_GIGA_MAC_VER_11:
3631 case RTL_GIGA_MAC_VER_12:
3632 case RTL_GIGA_MAC_VER_17:
3633 case RTL_GIGA_MAC_VER_18:
3634 case RTL_GIGA_MAC_VER_19:
3635 case RTL_GIGA_MAC_VER_20:
3636 case RTL_GIGA_MAC_VER_21:
3637 case RTL_GIGA_MAC_VER_22:
3638 case RTL_GIGA_MAC_VER_23:
3639 case RTL_GIGA_MAC_VER_24:
3640 case RTL_GIGA_MAC_VER_25:
3641 case RTL_GIGA_MAC_VER_26:
3642 case RTL_GIGA_MAC_VER_27:
3643 case RTL_GIGA_MAC_VER_28:
3644 case RTL_GIGA_MAC_VER_31:
3645 rtl_writephy(tp, 0x0e, 0x0200);
3646 default:
3647 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3648 break;
3649 }
françois romieu065c27c2011-01-03 15:08:12 +00003650}
3651
3652static void r8168_pll_power_down(struct rtl8169_private *tp)
3653{
3654 void __iomem *ioaddr = tp->mmio_addr;
3655
Francois Romieucecb5fd2011-04-01 10:21:07 +02003656 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3657 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3658 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003659 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003660 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003661 }
françois romieu065c27c2011-01-03 15:08:12 +00003662
Francois Romieucecb5fd2011-04-01 10:21:07 +02003663 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3664 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003665 (RTL_R16(CPlusCmd) & ASF)) {
3666 return;
3667 }
3668
hayeswang01dc7fe2011-03-21 01:50:28 +00003669 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3670 tp->mac_version == RTL_GIGA_MAC_VER_33)
3671 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3672
David S. Miller1805b2f2011-10-24 18:18:09 -04003673 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003674 return;
françois romieu065c27c2011-01-03 15:08:12 +00003675
3676 r8168_phy_power_down(tp);
3677
3678 switch (tp->mac_version) {
3679 case RTL_GIGA_MAC_VER_25:
3680 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003681 case RTL_GIGA_MAC_VER_27:
3682 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003683 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003684 case RTL_GIGA_MAC_VER_32:
3685 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003686 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3687 break;
3688 }
3689}
3690
3691static void r8168_pll_power_up(struct rtl8169_private *tp)
3692{
3693 void __iomem *ioaddr = tp->mmio_addr;
3694
Francois Romieucecb5fd2011-04-01 10:21:07 +02003695 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3696 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3697 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003698 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003699 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003700 }
françois romieu065c27c2011-01-03 15:08:12 +00003701
3702 switch (tp->mac_version) {
3703 case RTL_GIGA_MAC_VER_25:
3704 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003705 case RTL_GIGA_MAC_VER_27:
3706 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003707 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003708 case RTL_GIGA_MAC_VER_32:
3709 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003710 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3711 break;
3712 }
3713
3714 r8168_phy_power_up(tp);
3715}
3716
Francois Romieud58d46b2011-05-03 16:38:29 +02003717static void rtl_generic_op(struct rtl8169_private *tp,
3718 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00003719{
3720 if (op)
3721 op(tp);
3722}
3723
3724static void rtl_pll_power_down(struct rtl8169_private *tp)
3725{
Francois Romieud58d46b2011-05-03 16:38:29 +02003726 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00003727}
3728
3729static void rtl_pll_power_up(struct rtl8169_private *tp)
3730{
Francois Romieud58d46b2011-05-03 16:38:29 +02003731 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00003732}
3733
3734static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3735{
3736 struct pll_power_ops *ops = &tp->pll_power_ops;
3737
3738 switch (tp->mac_version) {
3739 case RTL_GIGA_MAC_VER_07:
3740 case RTL_GIGA_MAC_VER_08:
3741 case RTL_GIGA_MAC_VER_09:
3742 case RTL_GIGA_MAC_VER_10:
3743 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003744 case RTL_GIGA_MAC_VER_29:
3745 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003746 ops->down = r810x_pll_power_down;
3747 ops->up = r810x_pll_power_up;
3748 break;
3749
3750 case RTL_GIGA_MAC_VER_11:
3751 case RTL_GIGA_MAC_VER_12:
3752 case RTL_GIGA_MAC_VER_17:
3753 case RTL_GIGA_MAC_VER_18:
3754 case RTL_GIGA_MAC_VER_19:
3755 case RTL_GIGA_MAC_VER_20:
3756 case RTL_GIGA_MAC_VER_21:
3757 case RTL_GIGA_MAC_VER_22:
3758 case RTL_GIGA_MAC_VER_23:
3759 case RTL_GIGA_MAC_VER_24:
3760 case RTL_GIGA_MAC_VER_25:
3761 case RTL_GIGA_MAC_VER_26:
3762 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003763 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003764 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003765 case RTL_GIGA_MAC_VER_32:
3766 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003767 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08003768 case RTL_GIGA_MAC_VER_35:
3769 case RTL_GIGA_MAC_VER_36:
françois romieu065c27c2011-01-03 15:08:12 +00003770 ops->down = r8168_pll_power_down;
3771 ops->up = r8168_pll_power_up;
3772 break;
3773
3774 default:
3775 ops->down = NULL;
3776 ops->up = NULL;
3777 break;
3778 }
3779}
3780
Hayes Wange542a222011-07-06 15:58:04 +08003781static void rtl_init_rxcfg(struct rtl8169_private *tp)
3782{
3783 void __iomem *ioaddr = tp->mmio_addr;
3784
3785 switch (tp->mac_version) {
3786 case RTL_GIGA_MAC_VER_01:
3787 case RTL_GIGA_MAC_VER_02:
3788 case RTL_GIGA_MAC_VER_03:
3789 case RTL_GIGA_MAC_VER_04:
3790 case RTL_GIGA_MAC_VER_05:
3791 case RTL_GIGA_MAC_VER_06:
3792 case RTL_GIGA_MAC_VER_10:
3793 case RTL_GIGA_MAC_VER_11:
3794 case RTL_GIGA_MAC_VER_12:
3795 case RTL_GIGA_MAC_VER_13:
3796 case RTL_GIGA_MAC_VER_14:
3797 case RTL_GIGA_MAC_VER_15:
3798 case RTL_GIGA_MAC_VER_16:
3799 case RTL_GIGA_MAC_VER_17:
3800 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3801 break;
3802 case RTL_GIGA_MAC_VER_18:
3803 case RTL_GIGA_MAC_VER_19:
3804 case RTL_GIGA_MAC_VER_20:
3805 case RTL_GIGA_MAC_VER_21:
3806 case RTL_GIGA_MAC_VER_22:
3807 case RTL_GIGA_MAC_VER_23:
3808 case RTL_GIGA_MAC_VER_24:
3809 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3810 break;
3811 default:
3812 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3813 break;
3814 }
3815}
3816
Hayes Wang92fc43b2011-07-06 15:58:03 +08003817static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3818{
3819 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3820}
3821
Francois Romieud58d46b2011-05-03 16:38:29 +02003822static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3823{
françois romieu9c5028e2012-03-02 04:43:14 +00003824 void __iomem *ioaddr = tp->mmio_addr;
3825
3826 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003827 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00003828 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003829}
3830
3831static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3832{
françois romieu9c5028e2012-03-02 04:43:14 +00003833 void __iomem *ioaddr = tp->mmio_addr;
3834
3835 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003836 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00003837 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003838}
3839
3840static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3841{
3842 void __iomem *ioaddr = tp->mmio_addr;
3843
3844 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3845 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3846 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3847}
3848
3849static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3850{
3851 void __iomem *ioaddr = tp->mmio_addr;
3852
3853 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3854 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3855 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3856}
3857
3858static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3859{
3860 void __iomem *ioaddr = tp->mmio_addr;
3861
3862 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3863}
3864
3865static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3866{
3867 void __iomem *ioaddr = tp->mmio_addr;
3868
3869 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3870}
3871
3872static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3873{
3874 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003875
3876 RTL_W8(MaxTxPacketSize, 0x3f);
3877 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3878 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003879 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003880}
3881
3882static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3883{
3884 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003885
3886 RTL_W8(MaxTxPacketSize, 0x0c);
3887 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3888 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003889 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003890}
3891
3892static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3893{
3894 rtl_tx_performance_tweak(tp->pci_dev,
3895 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3896}
3897
3898static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3899{
3900 rtl_tx_performance_tweak(tp->pci_dev,
3901 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3902}
3903
3904static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3905{
3906 void __iomem *ioaddr = tp->mmio_addr;
3907
3908 r8168b_0_hw_jumbo_enable(tp);
3909
3910 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
3911}
3912
3913static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3914{
3915 void __iomem *ioaddr = tp->mmio_addr;
3916
3917 r8168b_0_hw_jumbo_disable(tp);
3918
3919 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3920}
3921
3922static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
3923{
3924 struct jumbo_ops *ops = &tp->jumbo_ops;
3925
3926 switch (tp->mac_version) {
3927 case RTL_GIGA_MAC_VER_11:
3928 ops->disable = r8168b_0_hw_jumbo_disable;
3929 ops->enable = r8168b_0_hw_jumbo_enable;
3930 break;
3931 case RTL_GIGA_MAC_VER_12:
3932 case RTL_GIGA_MAC_VER_17:
3933 ops->disable = r8168b_1_hw_jumbo_disable;
3934 ops->enable = r8168b_1_hw_jumbo_enable;
3935 break;
3936 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
3937 case RTL_GIGA_MAC_VER_19:
3938 case RTL_GIGA_MAC_VER_20:
3939 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
3940 case RTL_GIGA_MAC_VER_22:
3941 case RTL_GIGA_MAC_VER_23:
3942 case RTL_GIGA_MAC_VER_24:
3943 case RTL_GIGA_MAC_VER_25:
3944 case RTL_GIGA_MAC_VER_26:
3945 ops->disable = r8168c_hw_jumbo_disable;
3946 ops->enable = r8168c_hw_jumbo_enable;
3947 break;
3948 case RTL_GIGA_MAC_VER_27:
3949 case RTL_GIGA_MAC_VER_28:
3950 ops->disable = r8168dp_hw_jumbo_disable;
3951 ops->enable = r8168dp_hw_jumbo_enable;
3952 break;
3953 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
3954 case RTL_GIGA_MAC_VER_32:
3955 case RTL_GIGA_MAC_VER_33:
3956 case RTL_GIGA_MAC_VER_34:
3957 ops->disable = r8168e_hw_jumbo_disable;
3958 ops->enable = r8168e_hw_jumbo_enable;
3959 break;
3960
3961 /*
3962 * No action needed for jumbo frames with 8169.
3963 * No jumbo for 810x at all.
3964 */
3965 default:
3966 ops->disable = NULL;
3967 ops->enable = NULL;
3968 break;
3969 }
3970}
3971
Francois Romieu6f43adc2011-04-29 15:05:51 +02003972static void rtl_hw_reset(struct rtl8169_private *tp)
3973{
3974 void __iomem *ioaddr = tp->mmio_addr;
3975 int i;
3976
3977 /* Soft reset the chip. */
3978 RTL_W8(ChipCmd, CmdReset);
3979
3980 /* Check that the chip has finished the reset. */
3981 for (i = 0; i < 100; i++) {
3982 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3983 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003984 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003985 }
3986}
3987
Francois Romieub6ffd972011-06-17 17:00:05 +02003988static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
3989{
3990 struct rtl_fw *rtl_fw;
3991 const char *name;
3992 int rc = -ENOMEM;
3993
3994 name = rtl_lookup_firmware_name(tp);
3995 if (!name)
3996 goto out_no_firmware;
3997
3998 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
3999 if (!rtl_fw)
4000 goto err_warn;
4001
4002 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4003 if (rc < 0)
4004 goto err_free;
4005
Francois Romieufd112f22011-06-18 00:10:29 +02004006 rc = rtl_check_firmware(tp, rtl_fw);
4007 if (rc < 0)
4008 goto err_release_firmware;
4009
Francois Romieub6ffd972011-06-17 17:00:05 +02004010 tp->rtl_fw = rtl_fw;
4011out:
4012 return;
4013
Francois Romieufd112f22011-06-18 00:10:29 +02004014err_release_firmware:
4015 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004016err_free:
4017 kfree(rtl_fw);
4018err_warn:
4019 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4020 name, rc);
4021out_no_firmware:
4022 tp->rtl_fw = NULL;
4023 goto out;
4024}
4025
François Romieu953a12c2011-04-24 17:38:48 +02004026static void rtl_request_firmware(struct rtl8169_private *tp)
4027{
Francois Romieub6ffd972011-06-17 17:00:05 +02004028 if (IS_ERR(tp->rtl_fw))
4029 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004030}
4031
Hayes Wang92fc43b2011-07-06 15:58:03 +08004032static void rtl_rx_close(struct rtl8169_private *tp)
4033{
4034 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004035
Francois Romieu1687b562011-07-19 17:21:29 +02004036 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004037}
4038
françois romieue6de30d2011-01-03 15:08:37 +00004039static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040{
françois romieue6de30d2011-01-03 15:08:37 +00004041 void __iomem *ioaddr = tp->mmio_addr;
4042
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004044 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045
Hayes Wang92fc43b2011-07-06 15:58:03 +08004046 rtl_rx_close(tp);
4047
Hayes Wang5d2e1952011-02-22 17:26:22 +08004048 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004049 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4050 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00004051 while (RTL_R8(TxPoll) & NPQ)
4052 udelay(20);
Hayes Wangc2218922011-09-06 16:55:18 +08004053 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4054 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
4055 tp->mac_version == RTL_GIGA_MAC_VER_36) {
David S. Miller8decf862011-09-22 03:23:13 -04004056 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08004057 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4058 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004059 } else {
4060 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4061 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004062 }
4063
Hayes Wang92fc43b2011-07-06 15:58:03 +08004064 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065}
4066
Francois Romieu7f796d832007-06-11 23:04:41 +02004067static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004068{
4069 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004070
4071 /* Set DMA burst size and Interframe Gap Time */
4072 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4073 (InterFrameGap << TxInterFrameGapShift));
4074}
4075
Francois Romieu07ce4062007-02-23 23:36:39 +01004076static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077{
4078 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
Francois Romieu07ce4062007-02-23 23:36:39 +01004080 tp->hw_start(dev);
4081
Francois Romieuda78dbf2012-01-26 14:18:23 +01004082 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004083}
4084
Francois Romieu7f796d832007-06-11 23:04:41 +02004085static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4086 void __iomem *ioaddr)
4087{
4088 /*
4089 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4090 * register to be written before TxDescAddrLow to work.
4091 * Switching from MMIO to I/O access fixes the issue as well.
4092 */
4093 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004094 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004095 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004096 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004097}
4098
4099static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4100{
4101 u16 cmd;
4102
4103 cmd = RTL_R16(CPlusCmd);
4104 RTL_W16(CPlusCmd, cmd);
4105 return cmd;
4106}
4107
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004108static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004109{
4110 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004111 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004112}
4113
Francois Romieu6dccd162007-02-13 23:38:05 +01004114static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4115{
Francois Romieu37441002011-06-17 22:58:54 +02004116 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004117 u32 mac_version;
4118 u32 clk;
4119 u32 val;
4120 } cfg2_info [] = {
4121 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4122 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4123 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4124 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004125 };
4126 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004127 unsigned int i;
4128 u32 clk;
4129
4130 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004131 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004132 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4133 RTL_W32(0x7c, p->val);
4134 break;
4135 }
4136 }
4137}
4138
Francois Romieue6b763e2012-03-08 09:35:39 +01004139static void rtl_set_rx_mode(struct net_device *dev)
4140{
4141 struct rtl8169_private *tp = netdev_priv(dev);
4142 void __iomem *ioaddr = tp->mmio_addr;
4143 u32 mc_filter[2]; /* Multicast hash filter */
4144 int rx_mode;
4145 u32 tmp = 0;
4146
4147 if (dev->flags & IFF_PROMISC) {
4148 /* Unconditionally log net taps. */
4149 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4150 rx_mode =
4151 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4152 AcceptAllPhys;
4153 mc_filter[1] = mc_filter[0] = 0xffffffff;
4154 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4155 (dev->flags & IFF_ALLMULTI)) {
4156 /* Too many to filter perfectly -- accept all multicasts. */
4157 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4158 mc_filter[1] = mc_filter[0] = 0xffffffff;
4159 } else {
4160 struct netdev_hw_addr *ha;
4161
4162 rx_mode = AcceptBroadcast | AcceptMyPhys;
4163 mc_filter[1] = mc_filter[0] = 0;
4164 netdev_for_each_mc_addr(ha, dev) {
4165 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4166 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4167 rx_mode |= AcceptMulticast;
4168 }
4169 }
4170
4171 if (dev->features & NETIF_F_RXALL)
4172 rx_mode |= (AcceptErr | AcceptRunt);
4173
4174 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4175
4176 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4177 u32 data = mc_filter[0];
4178
4179 mc_filter[0] = swab32(mc_filter[1]);
4180 mc_filter[1] = swab32(data);
4181 }
4182
4183 RTL_W32(MAR0 + 4, mc_filter[1]);
4184 RTL_W32(MAR0 + 0, mc_filter[0]);
4185
4186 RTL_W32(RxConfig, tmp);
4187}
4188
Francois Romieu07ce4062007-02-23 23:36:39 +01004189static void rtl_hw_start_8169(struct net_device *dev)
4190{
4191 struct rtl8169_private *tp = netdev_priv(dev);
4192 void __iomem *ioaddr = tp->mmio_addr;
4193 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004194
Francois Romieu9cb427b2006-11-02 00:10:16 +01004195 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4196 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4197 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4198 }
4199
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004201 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4202 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4203 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4204 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004205 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4206
Hayes Wange542a222011-07-06 15:58:04 +08004207 rtl_init_rxcfg(tp);
4208
françois romieuf0298f82011-01-03 15:07:42 +00004209 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004211 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212
Francois Romieucecb5fd2011-04-01 10:21:07 +02004213 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4214 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4215 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4216 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004217 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Francois Romieu7f796d832007-06-11 23:04:41 +02004219 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004220
Francois Romieucecb5fd2011-04-01 10:21:07 +02004221 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4222 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004223 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004225 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 }
4227
Francois Romieubcf0bf92006-07-26 23:14:13 +02004228 RTL_W16(CPlusCmd, tp->cp_cmd);
4229
Francois Romieu6dccd162007-02-13 23:38:05 +01004230 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4231
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 /*
4233 * Undocumented corner. Supposedly:
4234 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4235 */
4236 RTL_W16(IntrMitigate, 0x0000);
4237
Francois Romieu7f796d832007-06-11 23:04:41 +02004238 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004239
Francois Romieucecb5fd2011-04-01 10:21:07 +02004240 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4241 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4242 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4243 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004244 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4245 rtl_set_rx_tx_config_registers(tp);
4246 }
4247
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004249
4250 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4251 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252
4253 RTL_W32(RxMissed, 0);
4254
Francois Romieu07ce4062007-02-23 23:36:39 +01004255 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
4257 /* no early-rx interrupts */
4258 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004259}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
françois romieu650e8d52011-01-03 15:08:29 +00004261static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004262{
4263 u32 csi;
4264
4265 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00004266 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4267}
4268
françois romieue6de30d2011-01-03 15:08:37 +00004269static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4270{
4271 rtl_csi_access_enable(ioaddr, 0x17000000);
4272}
4273
françois romieu650e8d52011-01-03 15:08:29 +00004274static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4275{
4276 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02004277}
4278
4279struct ephy_info {
4280 unsigned int offset;
4281 u16 mask;
4282 u16 bits;
4283};
4284
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004285static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004286{
4287 u16 w;
4288
4289 while (len-- > 0) {
4290 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4291 rtl_ephy_write(ioaddr, e->offset, w);
4292 e++;
4293 }
4294}
4295
Francois Romieub726e492008-06-28 12:22:59 +02004296static void rtl_disable_clock_request(struct pci_dev *pdev)
4297{
Jon Masone44daad2011-06-27 07:46:31 +00004298 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004299
4300 if (cap) {
4301 u16 ctl;
4302
4303 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4304 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4305 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4306 }
4307}
4308
françois romieue6de30d2011-01-03 15:08:37 +00004309static void rtl_enable_clock_request(struct pci_dev *pdev)
4310{
Jon Masone44daad2011-06-27 07:46:31 +00004311 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004312
4313 if (cap) {
4314 u16 ctl;
4315
4316 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4317 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4318 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4319 }
4320}
4321
Francois Romieub726e492008-06-28 12:22:59 +02004322#define R8168_CPCMD_QUIRK_MASK (\
4323 EnableBist | \
4324 Mac_dbgo_oe | \
4325 Force_half_dup | \
4326 Force_rxflow_en | \
4327 Force_txflow_en | \
4328 Cxpl_dbg_sel | \
4329 ASF | \
4330 PktCntrDisable | \
4331 Mac_dbgo_sel)
4332
Francois Romieu219a1e92008-06-28 11:58:39 +02004333static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4334{
Francois Romieub726e492008-06-28 12:22:59 +02004335 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4336
4337 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4338
Francois Romieu2e68ae42008-06-28 12:00:55 +02004339 rtl_tx_performance_tweak(pdev,
4340 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004341}
4342
4343static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4344{
4345 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004346
françois romieuf0298f82011-01-03 15:07:42 +00004347 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004348
4349 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004350}
4351
4352static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4353{
Francois Romieub726e492008-06-28 12:22:59 +02004354 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4355
4356 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4357
Francois Romieu219a1e92008-06-28 11:58:39 +02004358 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004359
4360 rtl_disable_clock_request(pdev);
4361
4362 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004363}
4364
Francois Romieuef3386f2008-06-29 12:24:30 +02004365static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004366{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004367 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004368 { 0x01, 0, 0x0001 },
4369 { 0x02, 0x0800, 0x1000 },
4370 { 0x03, 0, 0x0042 },
4371 { 0x06, 0x0080, 0x0000 },
4372 { 0x07, 0, 0x2000 }
4373 };
4374
françois romieu650e8d52011-01-03 15:08:29 +00004375 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004376
4377 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4378
Francois Romieu219a1e92008-06-28 11:58:39 +02004379 __rtl_hw_start_8168cp(ioaddr, pdev);
4380}
4381
Francois Romieuef3386f2008-06-29 12:24:30 +02004382static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4383{
françois romieu650e8d52011-01-03 15:08:29 +00004384 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004385
4386 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4387
4388 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4389
4390 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4391}
4392
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004393static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4394{
françois romieu650e8d52011-01-03 15:08:29 +00004395 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004396
4397 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4398
4399 /* Magic. */
4400 RTL_W8(DBG_REG, 0x20);
4401
françois romieuf0298f82011-01-03 15:07:42 +00004402 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004403
4404 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4405
4406 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4407}
4408
Francois Romieu219a1e92008-06-28 11:58:39 +02004409static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4410{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004411 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004412 { 0x02, 0x0800, 0x1000 },
4413 { 0x03, 0, 0x0002 },
4414 { 0x06, 0x0080, 0x0000 }
4415 };
4416
françois romieu650e8d52011-01-03 15:08:29 +00004417 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004418
4419 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4420
4421 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4422
Francois Romieu219a1e92008-06-28 11:58:39 +02004423 __rtl_hw_start_8168cp(ioaddr, pdev);
4424}
4425
4426static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4427{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004428 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004429 { 0x01, 0, 0x0001 },
4430 { 0x03, 0x0400, 0x0220 }
4431 };
4432
françois romieu650e8d52011-01-03 15:08:29 +00004433 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004434
4435 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4436
Francois Romieu219a1e92008-06-28 11:58:39 +02004437 __rtl_hw_start_8168cp(ioaddr, pdev);
4438}
4439
Francois Romieu197ff762008-06-28 13:16:02 +02004440static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4441{
4442 rtl_hw_start_8168c_2(ioaddr, pdev);
4443}
4444
Francois Romieu6fb07052008-06-29 11:54:28 +02004445static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4446{
françois romieu650e8d52011-01-03 15:08:29 +00004447 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004448
4449 __rtl_hw_start_8168cp(ioaddr, pdev);
4450}
4451
Francois Romieu5b538df2008-07-20 16:22:45 +02004452static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4453{
françois romieu650e8d52011-01-03 15:08:29 +00004454 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004455
4456 rtl_disable_clock_request(pdev);
4457
françois romieuf0298f82011-01-03 15:07:42 +00004458 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004459
4460 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4461
4462 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4463}
4464
hayeswang4804b3b2011-03-21 01:50:29 +00004465static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4466{
4467 rtl_csi_access_enable_1(ioaddr);
4468
4469 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4470
4471 RTL_W8(MaxTxPacketSize, TxPacketMax);
4472
4473 rtl_disable_clock_request(pdev);
4474}
4475
françois romieue6de30d2011-01-03 15:08:37 +00004476static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4477{
4478 static const struct ephy_info e_info_8168d_4[] = {
4479 { 0x0b, ~0, 0x48 },
4480 { 0x19, 0x20, 0x50 },
4481 { 0x0c, ~0, 0x20 }
4482 };
4483 int i;
4484
4485 rtl_csi_access_enable_1(ioaddr);
4486
4487 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4488
4489 RTL_W8(MaxTxPacketSize, TxPacketMax);
4490
4491 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4492 const struct ephy_info *e = e_info_8168d_4 + i;
4493 u16 w;
4494
4495 w = rtl_ephy_read(ioaddr, e->offset);
4496 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4497 }
4498
4499 rtl_enable_clock_request(pdev);
4500}
4501
Hayes Wang70090422011-07-06 15:58:06 +08004502static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
hayeswang01dc7fe2011-03-21 01:50:28 +00004503{
Hayes Wang70090422011-07-06 15:58:06 +08004504 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004505 { 0x00, 0x0200, 0x0100 },
4506 { 0x00, 0x0000, 0x0004 },
4507 { 0x06, 0x0002, 0x0001 },
4508 { 0x06, 0x0000, 0x0030 },
4509 { 0x07, 0x0000, 0x2000 },
4510 { 0x00, 0x0000, 0x0020 },
4511 { 0x03, 0x5800, 0x2000 },
4512 { 0x03, 0x0000, 0x0001 },
4513 { 0x01, 0x0800, 0x1000 },
4514 { 0x07, 0x0000, 0x4000 },
4515 { 0x1e, 0x0000, 0x2000 },
4516 { 0x19, 0xffff, 0xfe6c },
4517 { 0x0a, 0x0000, 0x0040 }
4518 };
4519
4520 rtl_csi_access_enable_2(ioaddr);
4521
Hayes Wang70090422011-07-06 15:58:06 +08004522 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004523
4524 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4525
4526 RTL_W8(MaxTxPacketSize, TxPacketMax);
4527
4528 rtl_disable_clock_request(pdev);
4529
4530 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004531 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4532 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004533
Francois Romieucecb5fd2011-04-01 10:21:07 +02004534 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004535}
4536
Hayes Wang70090422011-07-06 15:58:06 +08004537static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4538{
4539 static const struct ephy_info e_info_8168e_2[] = {
4540 { 0x09, 0x0000, 0x0080 },
4541 { 0x19, 0x0000, 0x0224 }
4542 };
4543
4544 rtl_csi_access_enable_1(ioaddr);
4545
4546 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4547
4548 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4549
4550 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4551 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4552 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4553 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4554 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4555 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4556 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4557 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4558 ERIAR_EXGMAC);
4559
Hayes Wang3090bd92011-09-06 16:55:15 +08004560 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004561
4562 rtl_disable_clock_request(pdev);
4563
4564 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4565 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4566
4567 /* Adjust EEE LED frequency */
4568 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4569
4570 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4571 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4572 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4573}
4574
Hayes Wangc2218922011-09-06 16:55:18 +08004575static void rtl_hw_start_8168f_1(void __iomem *ioaddr, struct pci_dev *pdev)
4576{
4577 static const struct ephy_info e_info_8168f_1[] = {
4578 { 0x06, 0x00c0, 0x0020 },
4579 { 0x08, 0x0001, 0x0002 },
4580 { 0x09, 0x0000, 0x0080 },
4581 { 0x19, 0x0000, 0x0224 }
4582 };
4583
4584 rtl_csi_access_enable_1(ioaddr);
4585
4586 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4587
4588 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4589
4590 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4591 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4592 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4593 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4594 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4595 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4596 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4597 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4598 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4599 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
4600 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4601 ERIAR_EXGMAC);
4602
4603 RTL_W8(MaxTxPacketSize, EarlySize);
4604
4605 rtl_disable_clock_request(pdev);
4606
4607 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4608 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4609
4610 /* Adjust EEE LED frequency */
4611 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4612
4613 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4614 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4615 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4616}
4617
Francois Romieu07ce4062007-02-23 23:36:39 +01004618static void rtl_hw_start_8168(struct net_device *dev)
4619{
Francois Romieu2dd99532007-06-11 23:22:52 +02004620 struct rtl8169_private *tp = netdev_priv(dev);
4621 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004622 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004623
4624 RTL_W8(Cfg9346, Cfg9346_Unlock);
4625
françois romieuf0298f82011-01-03 15:07:42 +00004626 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004627
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004628 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004629
Francois Romieu0e485152007-02-20 00:00:26 +01004630 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004631
4632 RTL_W16(CPlusCmd, tp->cp_cmd);
4633
Francois Romieu0e485152007-02-20 00:00:26 +01004634 RTL_W16(IntrMitigate, 0x5151);
4635
4636 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00004637 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01004638 tp->event_slow |= RxFIFOOver | PCSTimeout;
4639 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01004640 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004641
4642 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4643
Francois Romieub8363902008-06-01 12:31:57 +02004644 rtl_set_rx_mode(dev);
4645
4646 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4647 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004648
4649 RTL_R8(IntrMask);
4650
Francois Romieu219a1e92008-06-28 11:58:39 +02004651 switch (tp->mac_version) {
4652 case RTL_GIGA_MAC_VER_11:
4653 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004654 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004655
4656 case RTL_GIGA_MAC_VER_12:
4657 case RTL_GIGA_MAC_VER_17:
4658 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004659 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004660
4661 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004662 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004663 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004664
4665 case RTL_GIGA_MAC_VER_19:
4666 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004667 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004668
4669 case RTL_GIGA_MAC_VER_20:
4670 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004671 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004672
Francois Romieu197ff762008-06-28 13:16:02 +02004673 case RTL_GIGA_MAC_VER_21:
4674 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004675 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004676
Francois Romieu6fb07052008-06-29 11:54:28 +02004677 case RTL_GIGA_MAC_VER_22:
4678 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004679 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004680
Francois Romieuef3386f2008-06-29 12:24:30 +02004681 case RTL_GIGA_MAC_VER_23:
4682 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004683 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004684
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004685 case RTL_GIGA_MAC_VER_24:
4686 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004687 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004688
Francois Romieu5b538df2008-07-20 16:22:45 +02004689 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004690 case RTL_GIGA_MAC_VER_26:
4691 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004692 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004693 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004694
françois romieue6de30d2011-01-03 15:08:37 +00004695 case RTL_GIGA_MAC_VER_28:
4696 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004697 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004698
hayeswang4804b3b2011-03-21 01:50:29 +00004699 case RTL_GIGA_MAC_VER_31:
4700 rtl_hw_start_8168dp(ioaddr, pdev);
4701 break;
4702
hayeswang01dc7fe2011-03-21 01:50:28 +00004703 case RTL_GIGA_MAC_VER_32:
4704 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004705 rtl_hw_start_8168e_1(ioaddr, pdev);
4706 break;
4707 case RTL_GIGA_MAC_VER_34:
4708 rtl_hw_start_8168e_2(ioaddr, pdev);
hayeswang01dc7fe2011-03-21 01:50:28 +00004709 break;
françois romieue6de30d2011-01-03 15:08:37 +00004710
Hayes Wangc2218922011-09-06 16:55:18 +08004711 case RTL_GIGA_MAC_VER_35:
4712 case RTL_GIGA_MAC_VER_36:
4713 rtl_hw_start_8168f_1(ioaddr, pdev);
4714 break;
4715
Francois Romieu219a1e92008-06-28 11:58:39 +02004716 default:
4717 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4718 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004719 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004720 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004721
Francois Romieu0e485152007-02-20 00:00:26 +01004722 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4723
Francois Romieub8363902008-06-01 12:31:57 +02004724 RTL_W8(Cfg9346, Cfg9346_Lock);
4725
Francois Romieu2dd99532007-06-11 23:22:52 +02004726 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004727}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728
Francois Romieu2857ffb2008-08-02 21:08:49 +02004729#define R810X_CPCMD_QUIRK_MASK (\
4730 EnableBist | \
4731 Mac_dbgo_oe | \
4732 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004733 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004734 Force_txflow_en | \
4735 Cxpl_dbg_sel | \
4736 ASF | \
4737 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004738 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004739
4740static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4741{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004742 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004743 { 0x01, 0, 0x6e65 },
4744 { 0x02, 0, 0x091f },
4745 { 0x03, 0, 0xc2f9 },
4746 { 0x06, 0, 0xafb5 },
4747 { 0x07, 0, 0x0e00 },
4748 { 0x19, 0, 0xec80 },
4749 { 0x01, 0, 0x2e65 },
4750 { 0x01, 0, 0x6e65 }
4751 };
4752 u8 cfg1;
4753
françois romieu650e8d52011-01-03 15:08:29 +00004754 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004755
4756 RTL_W8(DBG_REG, FIX_NAK_1);
4757
4758 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4759
4760 RTL_W8(Config1,
4761 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4762 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4763
4764 cfg1 = RTL_R8(Config1);
4765 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4766 RTL_W8(Config1, cfg1 & ~LEDS0);
4767
Francois Romieu2857ffb2008-08-02 21:08:49 +02004768 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4769}
4770
4771static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4772{
françois romieu650e8d52011-01-03 15:08:29 +00004773 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004774
4775 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4776
4777 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4778 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004779}
4780
4781static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4782{
4783 rtl_hw_start_8102e_2(ioaddr, pdev);
4784
4785 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4786}
4787
Hayes Wang5a5e4442011-02-22 17:26:21 +08004788static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4789{
4790 static const struct ephy_info e_info_8105e_1[] = {
4791 { 0x07, 0, 0x4000 },
4792 { 0x19, 0, 0x0200 },
4793 { 0x19, 0, 0x0020 },
4794 { 0x1e, 0, 0x2000 },
4795 { 0x03, 0, 0x0001 },
4796 { 0x19, 0, 0x0100 },
4797 { 0x19, 0, 0x0004 },
4798 { 0x0a, 0, 0x0020 }
4799 };
4800
Francois Romieucecb5fd2011-04-01 10:21:07 +02004801 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004802 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4803
Francois Romieucecb5fd2011-04-01 10:21:07 +02004804 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004805 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4806
4807 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08004808 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004809
4810 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4811}
4812
4813static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4814{
4815 rtl_hw_start_8105e_1(ioaddr, pdev);
4816 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4817}
4818
Francois Romieu07ce4062007-02-23 23:36:39 +01004819static void rtl_hw_start_8101(struct net_device *dev)
4820{
Francois Romieucdf1a602007-06-11 23:29:50 +02004821 struct rtl8169_private *tp = netdev_priv(dev);
4822 void __iomem *ioaddr = tp->mmio_addr;
4823 struct pci_dev *pdev = tp->pci_dev;
4824
Francois Romieuda78dbf2012-01-26 14:18:23 +01004825 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
4826 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00004827
Francois Romieucecb5fd2011-04-01 10:21:07 +02004828 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4829 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00004830 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02004831
4832 if (cap) {
4833 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4834 PCI_EXP_DEVCTL_NOSNOOP_EN);
4835 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004836 }
4837
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004838 RTL_W8(Cfg9346, Cfg9346_Unlock);
4839
Francois Romieu2857ffb2008-08-02 21:08:49 +02004840 switch (tp->mac_version) {
4841 case RTL_GIGA_MAC_VER_07:
4842 rtl_hw_start_8102e_1(ioaddr, pdev);
4843 break;
4844
4845 case RTL_GIGA_MAC_VER_08:
4846 rtl_hw_start_8102e_3(ioaddr, pdev);
4847 break;
4848
4849 case RTL_GIGA_MAC_VER_09:
4850 rtl_hw_start_8102e_2(ioaddr, pdev);
4851 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004852
4853 case RTL_GIGA_MAC_VER_29:
4854 rtl_hw_start_8105e_1(ioaddr, pdev);
4855 break;
4856 case RTL_GIGA_MAC_VER_30:
4857 rtl_hw_start_8105e_2(ioaddr, pdev);
4858 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004859 }
4860
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004861 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004862
françois romieuf0298f82011-01-03 15:07:42 +00004863 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004864
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004865 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004866
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004867 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004868 RTL_W16(CPlusCmd, tp->cp_cmd);
4869
4870 RTL_W16(IntrMitigate, 0x0000);
4871
4872 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4873
4874 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4875 rtl_set_rx_tx_config_registers(tp);
4876
Francois Romieucdf1a602007-06-11 23:29:50 +02004877 RTL_R8(IntrMask);
4878
Francois Romieucdf1a602007-06-11 23:29:50 +02004879 rtl_set_rx_mode(dev);
4880
4881 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882}
4883
4884static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4885{
Francois Romieud58d46b2011-05-03 16:38:29 +02004886 struct rtl8169_private *tp = netdev_priv(dev);
4887
4888 if (new_mtu < ETH_ZLEN ||
4889 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 return -EINVAL;
4891
Francois Romieud58d46b2011-05-03 16:38:29 +02004892 if (new_mtu > ETH_DATA_LEN)
4893 rtl_hw_jumbo_enable(tp);
4894 else
4895 rtl_hw_jumbo_disable(tp);
4896
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004898 netdev_update_features(dev);
4899
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004900 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901}
4902
4903static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4904{
Al Viro95e09182007-12-22 18:55:39 +00004905 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4907}
4908
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004909static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4910 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004912 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004913 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004914
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004915 kfree(*data_buff);
4916 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 rtl8169_make_unusable_by_asic(desc);
4918}
4919
4920static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4921{
4922 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4923
4924 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4925}
4926
4927static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4928 u32 rx_buf_sz)
4929{
4930 desc->addr = cpu_to_le64(mapping);
4931 wmb();
4932 rtl8169_mark_to_asic(desc, rx_buf_sz);
4933}
4934
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004935static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004937 return (void *)ALIGN((long)data, 16);
4938}
4939
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004940static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4941 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004942{
4943 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004945 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004946 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004947 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004949 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4950 if (!data)
4951 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004952
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004953 if (rtl8169_align(data) != data) {
4954 kfree(data);
4955 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4956 if (!data)
4957 return NULL;
4958 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004959
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004960 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004961 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004962 if (unlikely(dma_mapping_error(d, mapping))) {
4963 if (net_ratelimit())
4964 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004965 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967
4968 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004969 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004970
4971err_out:
4972 kfree(data);
4973 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974}
4975
4976static void rtl8169_rx_clear(struct rtl8169_private *tp)
4977{
Francois Romieu07d3f512007-02-21 22:40:46 +01004978 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979
4980 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004981 if (tp->Rx_databuff[i]) {
4982 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 tp->RxDescArray + i);
4984 }
4985 }
4986}
4987
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004988static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004990 desc->opts1 |= cpu_to_le32(RingEnd);
4991}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004992
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004993static int rtl8169_rx_fill(struct rtl8169_private *tp)
4994{
4995 unsigned int i;
4996
4997 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004998 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004999
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005000 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005002
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005003 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005004 if (!data) {
5005 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005006 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005007 }
5008 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005011 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5012 return 0;
5013
5014err_out:
5015 rtl8169_rx_clear(tp);
5016 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017}
5018
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019static int rtl8169_init_ring(struct net_device *dev)
5020{
5021 struct rtl8169_private *tp = netdev_priv(dev);
5022
5023 rtl8169_init_ring_indexes(tp);
5024
5025 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005026 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005028 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029}
5030
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005031static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 struct TxDesc *desc)
5033{
5034 unsigned int len = tx_skb->len;
5035
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005036 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5037
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 desc->opts1 = 0x00;
5039 desc->opts2 = 0x00;
5040 desc->addr = 0x00;
5041 tx_skb->len = 0;
5042}
5043
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005044static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5045 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046{
5047 unsigned int i;
5048
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005049 for (i = 0; i < n; i++) {
5050 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 struct ring_info *tx_skb = tp->tx_skb + entry;
5052 unsigned int len = tx_skb->len;
5053
5054 if (len) {
5055 struct sk_buff *skb = tx_skb->skb;
5056
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005057 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 tp->TxDescArray + entry);
5059 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005060 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 dev_kfree_skb(skb);
5062 tx_skb->skb = NULL;
5063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 }
5065 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005066}
5067
5068static void rtl8169_tx_clear(struct rtl8169_private *tp)
5069{
5070 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 tp->cur_tx = tp->dirty_tx = 0;
Igor Maravic036dafa2012-03-05 00:01:25 +01005072 netdev_reset_queue(tp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073}
5074
Francois Romieu4422bcd2012-01-26 11:23:32 +01005075static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076{
David Howellsc4028952006-11-22 14:57:56 +00005077 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005078 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079
Francois Romieuda78dbf2012-01-26 14:18:23 +01005080 napi_disable(&tp->napi);
5081 netif_stop_queue(dev);
5082 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083
françois romieuc7c2c392011-12-04 20:30:52 +00005084 rtl8169_hw_reset(tp);
5085
Francois Romieu56de4142011-03-15 17:29:31 +01005086 for (i = 0; i < NUM_RX_DESC; i++)
5087 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5088
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005090 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091
Francois Romieuda78dbf2012-01-26 14:18:23 +01005092 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01005093 rtl_hw_start(dev);
5094 netif_wake_queue(dev);
5095 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096}
5097
5098static void rtl8169_tx_timeout(struct net_device *dev)
5099{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005100 struct rtl8169_private *tp = netdev_priv(dev);
5101
5102 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103}
5104
5105static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005106 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107{
5108 struct skb_shared_info *info = skb_shinfo(skb);
5109 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005110 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005111 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112
5113 entry = tp->cur_tx;
5114 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005115 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 dma_addr_t mapping;
5117 u32 status, len;
5118 void *addr;
5119
5120 entry = (entry + 1) % NUM_TX_DESC;
5121
5122 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005123 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005124 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005125 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005126 if (unlikely(dma_mapping_error(d, mapping))) {
5127 if (net_ratelimit())
5128 netif_err(tp, drv, tp->dev,
5129 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005130 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132
Francois Romieucecb5fd2011-04-01 10:21:07 +02005133 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005134 status = opts[0] | len |
5135 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
5137 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005138 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 txd->addr = cpu_to_le64(mapping);
5140
5141 tp->tx_skb[entry].len = len;
5142 }
5143
5144 if (cur_frag) {
5145 tp->tx_skb[entry].skb = skb;
5146 txd->opts1 |= cpu_to_le32(LastFrag);
5147 }
5148
5149 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005150
5151err_out:
5152 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5153 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154}
5155
Francois Romieu2b7b4312011-04-18 22:53:24 -07005156static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5157 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005159 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005160 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005161 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162
Francois Romieu2b7b4312011-04-18 22:53:24 -07005163 if (mss) {
5164 opts[0] |= TD_LSO;
5165 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5166 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005167 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168
5169 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005170 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005172 opts[offset] |= info->checksum.udp;
5173 else
5174 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176}
5177
Stephen Hemminger613573252009-08-31 19:50:58 +00005178static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5179 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180{
5181 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005182 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 struct TxDesc *txd = tp->TxDescArray + entry;
5184 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005185 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 dma_addr_t mapping;
5187 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005188 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005189 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005190
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005192 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005193 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 }
5195
5196 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005197 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005199 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005200 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005201 if (unlikely(dma_mapping_error(d, mapping))) {
5202 if (net_ratelimit())
5203 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005204 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206
5207 tp->tx_skb[entry].len = len;
5208 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209
Francois Romieu2b7b4312011-04-18 22:53:24 -07005210 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5211 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005212
Francois Romieu2b7b4312011-04-18 22:53:24 -07005213 rtl8169_tso_csum(tp, skb, opts);
5214
5215 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005216 if (frags < 0)
5217 goto err_dma_1;
5218 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005219 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005220 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005221 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005222 tp->tx_skb[entry].skb = skb;
5223 }
5224
Francois Romieu2b7b4312011-04-18 22:53:24 -07005225 txd->opts2 = cpu_to_le32(opts[1]);
5226
Igor Maravic036dafa2012-03-05 00:01:25 +01005227 netdev_sent_queue(dev, skb->len);
5228
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 wmb();
5230
Francois Romieucecb5fd2011-04-01 10:21:07 +02005231 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005232 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 txd->opts1 = cpu_to_le32(status);
5234
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 tp->cur_tx += frags + 1;
5236
David Dillow4c020a92010-03-03 16:33:10 +00005237 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238
Francois Romieucecb5fd2011-04-01 10:21:07 +02005239 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240
Francois Romieuda78dbf2012-01-26 14:18:23 +01005241 mmiowb();
5242
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01005244 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5245 * not miss a ring update when it notices a stopped queue.
5246 */
5247 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01005249 /* Sync with rtl_tx:
5250 * - publish queue status and cur_tx ring index (write barrier)
5251 * - refresh dirty_tx ring index (read barrier).
5252 * May the current thread have a pessimistic view of the ring
5253 * status and forget to wake up queue, a racing rtl_tx thread
5254 * can't.
5255 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005256 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
5258 netif_wake_queue(dev);
5259 }
5260
Stephen Hemminger613573252009-08-31 19:50:58 +00005261 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005263err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005264 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005265err_dma_0:
5266 dev_kfree_skb(skb);
5267 dev->stats.tx_dropped++;
5268 return NETDEV_TX_OK;
5269
5270err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005272 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005273 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274}
5275
5276static void rtl8169_pcierr_interrupt(struct net_device *dev)
5277{
5278 struct rtl8169_private *tp = netdev_priv(dev);
5279 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 u16 pci_status, pci_cmd;
5281
5282 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5283 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5284
Joe Perchesbf82c182010-02-09 11:49:50 +00005285 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5286 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287
5288 /*
5289 * The recovery sequence below admits a very elaborated explanation:
5290 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005291 * - I did not see what else could be done;
5292 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 *
5294 * Feel free to adjust to your needs.
5295 */
Francois Romieua27993f2006-12-18 00:04:19 +01005296 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005297 pci_cmd &= ~PCI_COMMAND_PARITY;
5298 else
5299 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5300
5301 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302
5303 pci_write_config_word(pdev, PCI_STATUS,
5304 pci_status & (PCI_STATUS_DETECTED_PARITY |
5305 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5306 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5307
5308 /* The infamous DAC f*ckup only happens at boot time */
5309 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005310 void __iomem *ioaddr = tp->mmio_addr;
5311
Joe Perchesbf82c182010-02-09 11:49:50 +00005312 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 tp->cp_cmd &= ~PCIDAC;
5314 RTL_W16(CPlusCmd, tp->cp_cmd);
5315 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 }
5317
françois romieue6de30d2011-01-03 15:08:37 +00005318 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005319
Francois Romieu98ddf982012-01-31 10:47:34 +01005320 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321}
5322
Igor Maravic036dafa2012-03-05 00:01:25 +01005323struct rtl_txc {
5324 int packets;
5325 int bytes;
5326};
5327
Francois Romieuda78dbf2012-01-26 14:18:23 +01005328static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329{
Igor Maravic036dafa2012-03-05 00:01:25 +01005330 struct rtl8169_stats *tx_stats = &tp->tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 unsigned int dirty_tx, tx_left;
Igor Maravic036dafa2012-03-05 00:01:25 +01005332 struct rtl_txc txc = { 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 dirty_tx = tp->dirty_tx;
5335 smp_rmb();
5336 tx_left = tp->cur_tx - dirty_tx;
5337
5338 while (tx_left > 0) {
5339 unsigned int entry = dirty_tx % NUM_TX_DESC;
5340 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 u32 status;
5342
5343 rmb();
5344 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5345 if (status & DescOwn)
5346 break;
5347
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005348 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5349 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 if (status & LastFrag) {
Igor Maravic036dafa2012-03-05 00:01:25 +01005351 struct sk_buff *skb = tx_skb->skb;
5352
5353 txc.packets++;
5354 txc.bytes += skb->len;
5355 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 tx_skb->skb = NULL;
5357 }
5358 dirty_tx++;
5359 tx_left--;
5360 }
5361
Igor Maravic036dafa2012-03-05 00:01:25 +01005362 u64_stats_update_begin(&tx_stats->syncp);
5363 tx_stats->packets += txc.packets;
5364 tx_stats->bytes += txc.bytes;
5365 u64_stats_update_end(&tx_stats->syncp);
5366
5367 netdev_completed_queue(dev, txc.packets, txc.bytes);
5368
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 if (tp->dirty_tx != dirty_tx) {
5370 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005371 /* Sync with rtl8169_start_xmit:
5372 * - publish dirty_tx ring index (write barrier)
5373 * - refresh cur_tx ring index and queue status (read barrier)
5374 * May the current thread miss the stopped queue condition,
5375 * a racing xmit thread can only have a right view of the
5376 * ring status.
5377 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005378 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 if (netif_queue_stopped(dev) &&
5380 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5381 netif_wake_queue(dev);
5382 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005383 /*
5384 * 8168 hack: TxPoll requests are lost when the Tx packets are
5385 * too close. Let's kick an extra TxPoll request when a burst
5386 * of start_xmit activity is detected (if it is not detected,
5387 * it is slow enough). -- FR
5388 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005389 if (tp->cur_tx != dirty_tx) {
5390 void __iomem *ioaddr = tp->mmio_addr;
5391
Francois Romieud78ae2d2007-08-26 20:08:19 +02005392 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 }
5395}
5396
Francois Romieu126fa4b2005-05-12 20:09:17 -04005397static inline int rtl8169_fragmented_frame(u32 status)
5398{
5399 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5400}
5401
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005402static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 u32 status = opts1 & RxProtoMask;
5405
5406 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005407 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 skb->ip_summed = CHECKSUM_UNNECESSARY;
5409 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005410 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411}
5412
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005413static struct sk_buff *rtl8169_try_rx_copy(void *data,
5414 struct rtl8169_private *tp,
5415 int pkt_size,
5416 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005418 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005419 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005421 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005422 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005423 prefetch(data);
5424 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5425 if (skb)
5426 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005427 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5428
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005429 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430}
5431
Francois Romieuda78dbf2012-01-26 14:18:23 +01005432static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433{
5434 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005435 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 cur_rx = tp->cur_rx;
5438 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005439 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005441 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005443 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 u32 status;
5445
5446 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005447 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448
5449 if (status & DescOwn)
5450 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005451 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005452 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5453 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005454 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005456 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005458 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005459 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005460 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005461 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005462 }
Ben Greear6bbe0212012-02-10 15:04:33 +00005463 if ((status & (RxRUNT | RxCRC)) &&
5464 !(status & (RxRWT | RxFOVF)) &&
5465 (dev->features & NETIF_F_RXALL))
5466 goto process_pkt;
5467
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005468 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005470 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00005471 dma_addr_t addr;
5472 int pkt_size;
5473
5474process_pkt:
5475 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00005476 if (likely(!(dev->features & NETIF_F_RXFCS)))
5477 pkt_size = (status & 0x00003fff) - 4;
5478 else
5479 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480
Francois Romieu126fa4b2005-05-12 20:09:17 -04005481 /*
5482 * The driver does not support incoming fragmented
5483 * frames. They are seen as a symptom of over-mtu
5484 * sized frames.
5485 */
5486 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005487 dev->stats.rx_dropped++;
5488 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005489 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005490 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005491 }
5492
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005493 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5494 tp, pkt_size, addr);
5495 rtl8169_mark_to_asic(desc, rx_buf_sz);
5496 if (!skb) {
5497 dev->stats.rx_dropped++;
5498 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 }
5500
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005501 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 skb_put(skb, pkt_size);
5503 skb->protocol = eth_type_trans(skb, dev);
5504
Francois Romieu7a8fc772011-03-01 17:18:33 +01005505 rtl8169_rx_vlan_tag(desc, skb);
5506
Francois Romieu56de4142011-03-15 17:29:31 +01005507 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508
Junchang Wang8027aa22012-03-04 23:30:32 +01005509 u64_stats_update_begin(&tp->rx_stats.syncp);
5510 tp->rx_stats.packets++;
5511 tp->rx_stats.bytes += pkt_size;
5512 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005514
5515 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005516 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005517 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5518 desc->opts2 = 0;
5519 cur_rx++;
5520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 }
5522
5523 count = cur_rx - tp->cur_rx;
5524 tp->cur_rx = cur_rx;
5525
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005526 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527
5528 return count;
5529}
5530
Francois Romieu07d3f512007-02-21 22:40:46 +01005531static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532{
Francois Romieu07d3f512007-02-21 22:40:46 +01005533 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005536 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005538 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005539 if (status && status != 0xffff) {
5540 status &= RTL_EVENT_NAPI | tp->event_slow;
5541 if (status) {
5542 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00005543
Francois Romieuda78dbf2012-01-26 14:18:23 +01005544 rtl_irq_disable(tp);
5545 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 return IRQ_RETVAL(handled);
5549}
5550
Francois Romieuda78dbf2012-01-26 14:18:23 +01005551/*
5552 * Workqueue context.
5553 */
5554static void rtl_slow_event_work(struct rtl8169_private *tp)
5555{
5556 struct net_device *dev = tp->dev;
5557 u16 status;
5558
5559 status = rtl_get_events(tp) & tp->event_slow;
5560 rtl_ack_events(tp, status);
5561
5562 if (unlikely(status & RxFIFOOver)) {
5563 switch (tp->mac_version) {
5564 /* Work around for rx fifo overflow */
5565 case RTL_GIGA_MAC_VER_11:
5566 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01005567 /* XXX - Hack alert. See rtl_task(). */
5568 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005569 default:
5570 break;
5571 }
5572 }
5573
5574 if (unlikely(status & SYSErr))
5575 rtl8169_pcierr_interrupt(dev);
5576
5577 if (status & LinkChg)
5578 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
5579
5580 napi_disable(&tp->napi);
5581 rtl_irq_disable(tp);
5582
5583 napi_enable(&tp->napi);
5584 napi_schedule(&tp->napi);
5585}
5586
Francois Romieu4422bcd2012-01-26 11:23:32 +01005587static void rtl_task(struct work_struct *work)
5588{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005589 static const struct {
5590 int bitnr;
5591 void (*action)(struct rtl8169_private *);
5592 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01005593 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005594 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
5595 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
5596 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
5597 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01005598 struct rtl8169_private *tp =
5599 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005600 struct net_device *dev = tp->dev;
5601 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01005602
Francois Romieuda78dbf2012-01-26 14:18:23 +01005603 rtl_lock_work(tp);
5604
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005605 if (!netif_running(dev) ||
5606 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01005607 goto out_unlock;
5608
5609 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
5610 bool pending;
5611
Francois Romieuda78dbf2012-01-26 14:18:23 +01005612 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005613 if (pending)
5614 rtl_work[i].action(tp);
5615 }
5616
5617out_unlock:
5618 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01005619}
5620
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005621static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005623 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5624 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005625 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
5626 int work_done= 0;
5627 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628
Francois Romieuda78dbf2012-01-26 14:18:23 +01005629 status = rtl_get_events(tp);
5630 rtl_ack_events(tp, status & ~tp->event_slow);
5631
5632 if (status & RTL_EVENT_NAPI_RX)
5633 work_done = rtl_rx(dev, tp, (u32) budget);
5634
5635 if (status & RTL_EVENT_NAPI_TX)
5636 rtl_tx(dev, tp);
5637
5638 if (status & tp->event_slow) {
5639 enable_mask &= ~tp->event_slow;
5640
5641 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
5642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005644 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005645 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005646
Francois Romieuda78dbf2012-01-26 14:18:23 +01005647 rtl_irq_enable(tp, enable_mask);
5648 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 }
5650
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005651 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653
Francois Romieu523a6092008-09-10 22:28:56 +02005654static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5655{
5656 struct rtl8169_private *tp = netdev_priv(dev);
5657
5658 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5659 return;
5660
5661 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5662 RTL_W32(RxMissed, 0);
5663}
5664
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665static void rtl8169_down(struct net_device *dev)
5666{
5667 struct rtl8169_private *tp = netdev_priv(dev);
5668 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669
Francois Romieu4876cc12011-03-11 21:07:11 +01005670 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005672 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005673 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674
Hayes Wang92fc43b2011-07-06 15:58:03 +08005675 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005676 /*
5677 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01005678 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
5679 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005680 */
Francois Romieu523a6092008-09-10 22:28:56 +02005681 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005684 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 rtl8169_tx_clear(tp);
5687
5688 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005689
5690 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691}
5692
5693static int rtl8169_close(struct net_device *dev)
5694{
5695 struct rtl8169_private *tp = netdev_priv(dev);
5696 struct pci_dev *pdev = tp->pci_dev;
5697
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005698 pm_runtime_get_sync(&pdev->dev);
5699
Francois Romieucecb5fd2011-04-01 10:21:07 +02005700 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005701 rtl8169_update_counters(dev);
5702
Francois Romieuda78dbf2012-01-26 14:18:23 +01005703 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005704 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005705
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005707 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708
5709 free_irq(dev->irq, dev);
5710
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005711 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5712 tp->RxPhyAddr);
5713 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5714 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 tp->TxDescArray = NULL;
5716 tp->RxDescArray = NULL;
5717
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005718 pm_runtime_put_sync(&pdev->dev);
5719
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 return 0;
5721}
5722
Francois Romieudf43ac72012-03-08 09:48:40 +01005723static int rtl_open(struct net_device *dev)
5724{
5725 struct rtl8169_private *tp = netdev_priv(dev);
5726 void __iomem *ioaddr = tp->mmio_addr;
5727 struct pci_dev *pdev = tp->pci_dev;
5728 int retval = -ENOMEM;
5729
5730 pm_runtime_get_sync(&pdev->dev);
5731
5732 /*
5733 * Rx and Tx desscriptors needs 256 bytes alignment.
5734 * dma_alloc_coherent provides more.
5735 */
5736 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
5737 &tp->TxPhyAddr, GFP_KERNEL);
5738 if (!tp->TxDescArray)
5739 goto err_pm_runtime_put;
5740
5741 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
5742 &tp->RxPhyAddr, GFP_KERNEL);
5743 if (!tp->RxDescArray)
5744 goto err_free_tx_0;
5745
5746 retval = rtl8169_init_ring(dev);
5747 if (retval < 0)
5748 goto err_free_rx_1;
5749
5750 INIT_WORK(&tp->wk.work, rtl_task);
5751
5752 smp_mb();
5753
5754 rtl_request_firmware(tp);
5755
5756 retval = request_irq(dev->irq, rtl8169_interrupt,
5757 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
5758 dev->name, dev);
5759 if (retval < 0)
5760 goto err_release_fw_2;
5761
5762 rtl_lock_work(tp);
5763
5764 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
5765
5766 napi_enable(&tp->napi);
5767
5768 rtl8169_init_phy(dev, tp);
5769
5770 __rtl8169_set_features(dev, dev->features);
5771
5772 rtl_pll_power_up(tp);
5773
5774 rtl_hw_start(dev);
5775
5776 netif_start_queue(dev);
5777
5778 rtl_unlock_work(tp);
5779
5780 tp->saved_wolopts = 0;
5781 pm_runtime_put_noidle(&pdev->dev);
5782
5783 rtl8169_check_link_status(dev, tp, ioaddr);
5784out:
5785 return retval;
5786
5787err_release_fw_2:
5788 rtl_release_firmware(tp);
5789 rtl8169_rx_clear(tp);
5790err_free_rx_1:
5791 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5792 tp->RxPhyAddr);
5793 tp->RxDescArray = NULL;
5794err_free_tx_0:
5795 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5796 tp->TxPhyAddr);
5797 tp->TxDescArray = NULL;
5798err_pm_runtime_put:
5799 pm_runtime_put_noidle(&pdev->dev);
5800 goto out;
5801}
5802
Junchang Wang8027aa22012-03-04 23:30:32 +01005803static struct rtnl_link_stats64 *
5804rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805{
5806 struct rtl8169_private *tp = netdev_priv(dev);
5807 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01005808 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809
Francois Romieuda78dbf2012-01-26 14:18:23 +01005810 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02005811 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02005812
Junchang Wang8027aa22012-03-04 23:30:32 +01005813 do {
5814 start = u64_stats_fetch_begin_bh(&tp->rx_stats.syncp);
5815 stats->rx_packets = tp->rx_stats.packets;
5816 stats->rx_bytes = tp->rx_stats.bytes;
5817 } while (u64_stats_fetch_retry_bh(&tp->rx_stats.syncp, start));
5818
5819
5820 do {
5821 start = u64_stats_fetch_begin_bh(&tp->tx_stats.syncp);
5822 stats->tx_packets = tp->tx_stats.packets;
5823 stats->tx_bytes = tp->tx_stats.bytes;
5824 } while (u64_stats_fetch_retry_bh(&tp->tx_stats.syncp, start));
5825
5826 stats->rx_dropped = dev->stats.rx_dropped;
5827 stats->tx_dropped = dev->stats.tx_dropped;
5828 stats->rx_length_errors = dev->stats.rx_length_errors;
5829 stats->rx_errors = dev->stats.rx_errors;
5830 stats->rx_crc_errors = dev->stats.rx_crc_errors;
5831 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
5832 stats->rx_missed_errors = dev->stats.rx_missed_errors;
5833
5834 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835}
5836
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005837static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005838{
françois romieu065c27c2011-01-03 15:08:12 +00005839 struct rtl8169_private *tp = netdev_priv(dev);
5840
Francois Romieu5d06a992006-02-23 00:47:58 +01005841 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005842 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005843
5844 netif_device_detach(dev);
5845 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005846
5847 rtl_lock_work(tp);
5848 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005849 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005850 rtl_unlock_work(tp);
5851
5852 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005853}
Francois Romieu5d06a992006-02-23 00:47:58 +01005854
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005855#ifdef CONFIG_PM
5856
5857static int rtl8169_suspend(struct device *device)
5858{
5859 struct pci_dev *pdev = to_pci_dev(device);
5860 struct net_device *dev = pci_get_drvdata(pdev);
5861
5862 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005863
Francois Romieu5d06a992006-02-23 00:47:58 +01005864 return 0;
5865}
5866
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005867static void __rtl8169_resume(struct net_device *dev)
5868{
françois romieu065c27c2011-01-03 15:08:12 +00005869 struct rtl8169_private *tp = netdev_priv(dev);
5870
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005871 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005872
5873 rtl_pll_power_up(tp);
5874
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005875 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005876
Francois Romieu98ddf982012-01-31 10:47:34 +01005877 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005878}
5879
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005880static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005881{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005882 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005883 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005884 struct rtl8169_private *tp = netdev_priv(dev);
5885
5886 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005887
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005888 if (netif_running(dev))
5889 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005890
Francois Romieu5d06a992006-02-23 00:47:58 +01005891 return 0;
5892}
5893
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005894static int rtl8169_runtime_suspend(struct device *device)
5895{
5896 struct pci_dev *pdev = to_pci_dev(device);
5897 struct net_device *dev = pci_get_drvdata(pdev);
5898 struct rtl8169_private *tp = netdev_priv(dev);
5899
5900 if (!tp->TxDescArray)
5901 return 0;
5902
Francois Romieuda78dbf2012-01-26 14:18:23 +01005903 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005904 tp->saved_wolopts = __rtl8169_get_wol(tp);
5905 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005906 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005907
5908 rtl8169_net_suspend(dev);
5909
5910 return 0;
5911}
5912
5913static int rtl8169_runtime_resume(struct device *device)
5914{
5915 struct pci_dev *pdev = to_pci_dev(device);
5916 struct net_device *dev = pci_get_drvdata(pdev);
5917 struct rtl8169_private *tp = netdev_priv(dev);
5918
5919 if (!tp->TxDescArray)
5920 return 0;
5921
Francois Romieuda78dbf2012-01-26 14:18:23 +01005922 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005923 __rtl8169_set_wol(tp, tp->saved_wolopts);
5924 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005925 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005926
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005927 rtl8169_init_phy(dev, tp);
5928
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005929 __rtl8169_resume(dev);
5930
5931 return 0;
5932}
5933
5934static int rtl8169_runtime_idle(struct device *device)
5935{
5936 struct pci_dev *pdev = to_pci_dev(device);
5937 struct net_device *dev = pci_get_drvdata(pdev);
5938 struct rtl8169_private *tp = netdev_priv(dev);
5939
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005940 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005941}
5942
Alexey Dobriyan47145212009-12-14 18:00:08 -08005943static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005944 .suspend = rtl8169_suspend,
5945 .resume = rtl8169_resume,
5946 .freeze = rtl8169_suspend,
5947 .thaw = rtl8169_resume,
5948 .poweroff = rtl8169_suspend,
5949 .restore = rtl8169_resume,
5950 .runtime_suspend = rtl8169_runtime_suspend,
5951 .runtime_resume = rtl8169_runtime_resume,
5952 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005953};
5954
5955#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5956
5957#else /* !CONFIG_PM */
5958
5959#define RTL8169_PM_OPS NULL
5960
5961#endif /* !CONFIG_PM */
5962
David S. Miller1805b2f2011-10-24 18:18:09 -04005963static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
5964{
5965 void __iomem *ioaddr = tp->mmio_addr;
5966
5967 /* WoL fails with 8168b when the receiver is disabled. */
5968 switch (tp->mac_version) {
5969 case RTL_GIGA_MAC_VER_11:
5970 case RTL_GIGA_MAC_VER_12:
5971 case RTL_GIGA_MAC_VER_17:
5972 pci_clear_master(tp->pci_dev);
5973
5974 RTL_W8(ChipCmd, CmdRxEnb);
5975 /* PCI commit */
5976 RTL_R8(ChipCmd);
5977 break;
5978 default:
5979 break;
5980 }
5981}
5982
Francois Romieu1765f952008-09-13 17:21:40 +02005983static void rtl_shutdown(struct pci_dev *pdev)
5984{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005985 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005986 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00005987 struct device *d = &pdev->dev;
5988
5989 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02005990
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005991 rtl8169_net_suspend(dev);
5992
Francois Romieucecb5fd2011-04-01 10:21:07 +02005993 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08005994 rtl_rar_set(tp, dev->perm_addr);
5995
Hayes Wang92fc43b2011-07-06 15:58:03 +08005996 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00005997
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005998 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04005999 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6000 rtl_wol_suspend_quirk(tp);
6001 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006002 }
6003
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006004 pci_wake_from_d3(pdev, true);
6005 pci_set_power_state(pdev, PCI_D3hot);
6006 }
françois romieu2a15cd22012-03-06 01:14:12 +00006007
6008 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006009}
Francois Romieu5d06a992006-02-23 00:47:58 +01006010
Francois Romieue27566e2012-03-08 09:54:01 +01006011static void __devexit rtl_remove_one(struct pci_dev *pdev)
6012{
6013 struct net_device *dev = pci_get_drvdata(pdev);
6014 struct rtl8169_private *tp = netdev_priv(dev);
6015
6016 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6017 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6018 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6019 rtl8168_driver_stop(tp);
6020 }
6021
6022 cancel_work_sync(&tp->wk.work);
6023
6024 unregister_netdev(dev);
6025
6026 rtl_release_firmware(tp);
6027
6028 if (pci_dev_run_wake(pdev))
6029 pm_runtime_get_noresume(&pdev->dev);
6030
6031 /* restore original MAC address */
6032 rtl_rar_set(tp, dev->perm_addr);
6033
6034 rtl_disable_msi(pdev, tp);
6035 rtl8169_release_board(pdev, dev, tp->mmio_addr);
6036 pci_set_drvdata(pdev, NULL);
6037}
6038
Francois Romieufa9c3852012-03-08 10:01:50 +01006039static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01006040 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01006041 .ndo_stop = rtl8169_close,
6042 .ndo_get_stats64 = rtl8169_get_stats64,
6043 .ndo_start_xmit = rtl8169_start_xmit,
6044 .ndo_tx_timeout = rtl8169_tx_timeout,
6045 .ndo_validate_addr = eth_validate_addr,
6046 .ndo_change_mtu = rtl8169_change_mtu,
6047 .ndo_fix_features = rtl8169_fix_features,
6048 .ndo_set_features = rtl8169_set_features,
6049 .ndo_set_mac_address = rtl_set_mac_address,
6050 .ndo_do_ioctl = rtl8169_ioctl,
6051 .ndo_set_rx_mode = rtl_set_rx_mode,
6052#ifdef CONFIG_NET_POLL_CONTROLLER
6053 .ndo_poll_controller = rtl8169_netpoll,
6054#endif
6055
6056};
6057
Francois Romieu3b6cf252012-03-08 09:59:04 +01006058static int __devinit
6059rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6060{
6061 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
6062 const unsigned int region = cfg->region;
6063 struct rtl8169_private *tp;
6064 struct mii_if_info *mii;
6065 struct net_device *dev;
6066 void __iomem *ioaddr;
6067 int chipset, i;
6068 int rc;
6069
6070 if (netif_msg_drv(&debug)) {
6071 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
6072 MODULENAME, RTL8169_VERSION);
6073 }
6074
6075 dev = alloc_etherdev(sizeof (*tp));
6076 if (!dev) {
6077 rc = -ENOMEM;
6078 goto out;
6079 }
6080
6081 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01006082 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006083 tp = netdev_priv(dev);
6084 tp->dev = dev;
6085 tp->pci_dev = pdev;
6086 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
6087
6088 mii = &tp->mii;
6089 mii->dev = dev;
6090 mii->mdio_read = rtl_mdio_read;
6091 mii->mdio_write = rtl_mdio_write;
6092 mii->phy_id_mask = 0x1f;
6093 mii->reg_num_mask = 0x1f;
6094 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
6095
6096 /* disable ASPM completely as that cause random device stop working
6097 * problems as well as full system hangs for some PCIe devices users */
6098 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6099 PCIE_LINK_STATE_CLKPM);
6100
6101 /* enable device (incl. PCI PM wakeup and hotplug setup) */
6102 rc = pci_enable_device(pdev);
6103 if (rc < 0) {
6104 netif_err(tp, probe, dev, "enable failure\n");
6105 goto err_out_free_dev_1;
6106 }
6107
6108 if (pci_set_mwi(pdev) < 0)
6109 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
6110
6111 /* make sure PCI base addr 1 is MMIO */
6112 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
6113 netif_err(tp, probe, dev,
6114 "region #%d not an MMIO resource, aborting\n",
6115 region);
6116 rc = -ENODEV;
6117 goto err_out_mwi_2;
6118 }
6119
6120 /* check for weird/broken PCI region reporting */
6121 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
6122 netif_err(tp, probe, dev,
6123 "Invalid PCI region size(s), aborting\n");
6124 rc = -ENODEV;
6125 goto err_out_mwi_2;
6126 }
6127
6128 rc = pci_request_regions(pdev, MODULENAME);
6129 if (rc < 0) {
6130 netif_err(tp, probe, dev, "could not request regions\n");
6131 goto err_out_mwi_2;
6132 }
6133
6134 tp->cp_cmd = RxChkSum;
6135
6136 if ((sizeof(dma_addr_t) > 4) &&
6137 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
6138 tp->cp_cmd |= PCIDAC;
6139 dev->features |= NETIF_F_HIGHDMA;
6140 } else {
6141 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6142 if (rc < 0) {
6143 netif_err(tp, probe, dev, "DMA configuration failed\n");
6144 goto err_out_free_res_3;
6145 }
6146 }
6147
6148 /* ioremap MMIO region */
6149 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
6150 if (!ioaddr) {
6151 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
6152 rc = -EIO;
6153 goto err_out_free_res_3;
6154 }
6155 tp->mmio_addr = ioaddr;
6156
6157 if (!pci_is_pcie(pdev))
6158 netif_info(tp, probe, dev, "not PCI Express\n");
6159
6160 /* Identify chip attached to board */
6161 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
6162
6163 rtl_init_rxcfg(tp);
6164
6165 rtl_irq_disable(tp);
6166
6167 rtl_hw_reset(tp);
6168
6169 rtl_ack_events(tp, 0xffff);
6170
6171 pci_set_master(pdev);
6172
6173 /*
6174 * Pretend we are using VLANs; This bypasses a nasty bug where
6175 * Interrupts stop flowing on high load on 8110SCd controllers.
6176 */
6177 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6178 tp->cp_cmd |= RxVlan;
6179
6180 rtl_init_mdio_ops(tp);
6181 rtl_init_pll_power_ops(tp);
6182 rtl_init_jumbo_ops(tp);
6183
6184 rtl8169_print_mac_version(tp);
6185
6186 chipset = tp->mac_version;
6187 tp->txd_version = rtl_chip_infos[chipset].txd_version;
6188
6189 RTL_W8(Cfg9346, Cfg9346_Unlock);
6190 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
6191 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
6192 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
6193 tp->features |= RTL_FEATURE_WOL;
6194 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
6195 tp->features |= RTL_FEATURE_WOL;
6196 tp->features |= rtl_try_msi(tp, cfg);
6197 RTL_W8(Cfg9346, Cfg9346_Lock);
6198
6199 if (rtl_tbi_enabled(tp)) {
6200 tp->set_speed = rtl8169_set_speed_tbi;
6201 tp->get_settings = rtl8169_gset_tbi;
6202 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
6203 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
6204 tp->link_ok = rtl8169_tbi_link_ok;
6205 tp->do_ioctl = rtl_tbi_ioctl;
6206 } else {
6207 tp->set_speed = rtl8169_set_speed_xmii;
6208 tp->get_settings = rtl8169_gset_xmii;
6209 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
6210 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
6211 tp->link_ok = rtl8169_xmii_link_ok;
6212 tp->do_ioctl = rtl_xmii_ioctl;
6213 }
6214
6215 mutex_init(&tp->wk.mutex);
6216
6217 /* Get MAC address */
6218 for (i = 0; i < ETH_ALEN; i++)
6219 dev->dev_addr[i] = RTL_R8(MAC0 + i);
6220 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
6221
6222 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
6223 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
6224 dev->irq = pdev->irq;
6225 dev->base_addr = (unsigned long) ioaddr;
6226
6227 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
6228
6229 /* don't enable SG, IP_CSUM and TSO by default - it might not work
6230 * properly for all devices */
6231 dev->features |= NETIF_F_RXCSUM |
6232 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6233
6234 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6235 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6236 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6237 NETIF_F_HIGHDMA;
6238
6239 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6240 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
6241 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
6242
6243 dev->hw_features |= NETIF_F_RXALL;
6244 dev->hw_features |= NETIF_F_RXFCS;
6245
6246 tp->hw_start = cfg->hw_start;
6247 tp->event_slow = cfg->event_slow;
6248
6249 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
6250 ~(RxBOVF | RxFOVF) : ~0;
6251
6252 init_timer(&tp->timer);
6253 tp->timer.data = (unsigned long) dev;
6254 tp->timer.function = rtl8169_phy_timer;
6255
6256 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
6257
6258 rc = register_netdev(dev);
6259 if (rc < 0)
6260 goto err_out_msi_4;
6261
6262 pci_set_drvdata(pdev, dev);
6263
6264 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
6265 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
6266 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
6267 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
6268 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
6269 "tx checksumming: %s]\n",
6270 rtl_chip_infos[chipset].jumbo_max,
6271 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
6272 }
6273
6274 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6275 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6276 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6277 rtl8168_driver_start(tp);
6278 }
6279
6280 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
6281
6282 if (pci_dev_run_wake(pdev))
6283 pm_runtime_put_noidle(&pdev->dev);
6284
6285 netif_carrier_off(dev);
6286
6287out:
6288 return rc;
6289
6290err_out_msi_4:
6291 rtl_disable_msi(pdev, tp);
6292 iounmap(ioaddr);
6293err_out_free_res_3:
6294 pci_release_regions(pdev);
6295err_out_mwi_2:
6296 pci_clear_mwi(pdev);
6297 pci_disable_device(pdev);
6298err_out_free_dev_1:
6299 free_netdev(dev);
6300 goto out;
6301}
6302
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303static struct pci_driver rtl8169_pci_driver = {
6304 .name = MODULENAME,
6305 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01006306 .probe = rtl_init_one,
Francois Romieue27566e2012-03-08 09:54:01 +01006307 .remove = __devexit_p(rtl_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02006308 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006309 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310};
6311
Francois Romieu07d3f512007-02-21 22:40:46 +01006312static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313{
Jeff Garzik29917622006-08-19 17:48:59 -04006314 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315}
6316
Francois Romieu07d3f512007-02-21 22:40:46 +01006317static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318{
6319 pci_unregister_driver(&rtl8169_pci_driver);
6320}
6321
6322module_init(rtl8169_init_module);
6323module_exit(rtl8169_cleanup_module);