blob: eb6fdeff5adf2ab20d96a53f32701416f09a31e8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Francois Romieu07d3f512007-02-21 22:40:46 +01002 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3 *
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
7 *
8 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/delay.h>
17#include <linux/ethtool.h>
18#include <linux/mii.h>
19#include <linux/if_vlan.h>
20#include <linux/crc32.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/tcp.h>
24#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000025#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000027#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000028#include <linux/firmware.h>
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +000029#include <linux/pci-aspm.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040030#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <asm/io.h>
33#include <asm/irq.h>
34
Francois Romieu865c6522008-05-11 14:51:00 +020035#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define MODULENAME "r8169"
37#define PFX MODULENAME ": "
38
françois romieubca03d52011-01-03 15:07:31 +000039#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
40#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000041#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
42#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080043#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080044#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
45#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080046#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
Hayes Wang7e18dca2012-03-30 14:33:02 +080047#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
Hayes Wangb3d7b2f2012-03-30 14:48:06 +080048#define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
Hayes Wang5598bfe2012-07-02 17:23:21 +080049#define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#ifdef RTL8169_DEBUG
52#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020053 if (!(expr)) { \
54 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070055 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020056 }
Joe Perches06fa7352007-10-18 21:15:00 +020057#define dprintk(fmt, args...) \
58 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#else
60#define assert(expr) do {} while (0)
61#define dprintk(fmt, args...) do {} while (0)
62#endif /* RTL8169_DEBUG */
63
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020064#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d92005-09-30 16:54:02 -070065 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020066
Julien Ducourthial477206a2012-05-09 00:00:06 +020067#define TX_SLOTS_AVAIL(tp) \
68 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
69
70/* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
71#define TX_FRAGS_READY_FOR(tp,nr_frags) \
72 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
75 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050076static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Francois Romieu9c14cea2008-07-05 00:21:15 +020078#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
81#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
82
83#define R8169_REGS_SIZE 256
84#define R8169_NAPI_WEIGHT 64
85#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
86#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
87#define RX_BUF_SIZE 1536 /* Rx Buffer size */
88#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
89#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
90
91#define RTL8169_TX_TIMEOUT (6*HZ)
92#define RTL8169_PHY_TIMEOUT (10*HZ)
93
françois romieuea8dbdd2009-03-15 01:10:50 +000094#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
95#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020096#define RTL_EEPROM_SIG_ADDR 0x0000
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/* write/read MMIO register */
99#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
100#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
101#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
102#define RTL_R8(reg) readb (ioaddr + (reg))
103#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +0000104#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200107 RTL_GIGA_MAC_VER_01 = 0,
108 RTL_GIGA_MAC_VER_02,
109 RTL_GIGA_MAC_VER_03,
110 RTL_GIGA_MAC_VER_04,
111 RTL_GIGA_MAC_VER_05,
112 RTL_GIGA_MAC_VER_06,
113 RTL_GIGA_MAC_VER_07,
114 RTL_GIGA_MAC_VER_08,
115 RTL_GIGA_MAC_VER_09,
116 RTL_GIGA_MAC_VER_10,
117 RTL_GIGA_MAC_VER_11,
118 RTL_GIGA_MAC_VER_12,
119 RTL_GIGA_MAC_VER_13,
120 RTL_GIGA_MAC_VER_14,
121 RTL_GIGA_MAC_VER_15,
122 RTL_GIGA_MAC_VER_16,
123 RTL_GIGA_MAC_VER_17,
124 RTL_GIGA_MAC_VER_18,
125 RTL_GIGA_MAC_VER_19,
126 RTL_GIGA_MAC_VER_20,
127 RTL_GIGA_MAC_VER_21,
128 RTL_GIGA_MAC_VER_22,
129 RTL_GIGA_MAC_VER_23,
130 RTL_GIGA_MAC_VER_24,
131 RTL_GIGA_MAC_VER_25,
132 RTL_GIGA_MAC_VER_26,
133 RTL_GIGA_MAC_VER_27,
134 RTL_GIGA_MAC_VER_28,
135 RTL_GIGA_MAC_VER_29,
136 RTL_GIGA_MAC_VER_30,
137 RTL_GIGA_MAC_VER_31,
138 RTL_GIGA_MAC_VER_32,
139 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800140 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800141 RTL_GIGA_MAC_VER_35,
142 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800143 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800144 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800145 RTL_GIGA_MAC_VER_39,
Francois Romieu85bffe62011-04-27 08:22:39 +0200146 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147};
148
Francois Romieu2b7b4312011-04-18 22:53:24 -0700149enum rtl_tx_desc_version {
150 RTL_TD_0 = 0,
151 RTL_TD_1 = 1,
152};
153
Francois Romieud58d46b2011-05-03 16:38:29 +0200154#define JUMBO_1K ETH_DATA_LEN
155#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
156#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
157#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
158#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
159
160#define _R(NAME,TD,FW,SZ,B) { \
161 .name = NAME, \
162 .txd_version = TD, \
163 .fw_name = FW, \
164 .jumbo_max = SZ, \
165 .jumbo_tx_csum = B \
166}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800168static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700170 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200171 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200172 u16 jumbo_max;
173 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200174} rtl_chip_infos[] = {
175 /* PCI devices. */
176 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200177 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200178 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200179 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200180 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200181 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200182 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200183 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200184 [RTL_GIGA_MAC_VER_05] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200185 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200186 [RTL_GIGA_MAC_VER_06] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200187 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200188 /* PCI-E devices. */
189 [RTL_GIGA_MAC_VER_07] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200190 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200191 [RTL_GIGA_MAC_VER_08] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200192 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200193 [RTL_GIGA_MAC_VER_09] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200194 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200195 [RTL_GIGA_MAC_VER_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200196 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200197 [RTL_GIGA_MAC_VER_11] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200198 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200199 [RTL_GIGA_MAC_VER_12] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200200 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200201 [RTL_GIGA_MAC_VER_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200202 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200203 [RTL_GIGA_MAC_VER_14] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200204 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200205 [RTL_GIGA_MAC_VER_15] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200206 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200207 [RTL_GIGA_MAC_VER_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200208 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200209 [RTL_GIGA_MAC_VER_17] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200210 _R("RTL8168b/8111b", RTL_TD_1, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200211 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200212 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200213 [RTL_GIGA_MAC_VER_19] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200214 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200215 [RTL_GIGA_MAC_VER_20] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200216 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200217 [RTL_GIGA_MAC_VER_21] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200218 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200219 [RTL_GIGA_MAC_VER_22] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200220 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200221 [RTL_GIGA_MAC_VER_23] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200222 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200223 [RTL_GIGA_MAC_VER_24] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200224 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200225 [RTL_GIGA_MAC_VER_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200226 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
227 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200228 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200229 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
230 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200231 [RTL_GIGA_MAC_VER_27] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200232 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200233 [RTL_GIGA_MAC_VER_28] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200234 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200235 [RTL_GIGA_MAC_VER_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200236 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
237 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200238 [RTL_GIGA_MAC_VER_30] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200239 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
240 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200241 [RTL_GIGA_MAC_VER_31] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200242 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200243 [RTL_GIGA_MAC_VER_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200244 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
245 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200246 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200247 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
248 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800249 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200250 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
251 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800252 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200253 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
254 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800255 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200256 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
257 JUMBO_9K, false),
Hayes Wang7e18dca2012-03-30 14:33:02 +0800258 [RTL_GIGA_MAC_VER_37] =
259 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
260 JUMBO_1K, true),
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800261 [RTL_GIGA_MAC_VER_38] =
262 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
263 JUMBO_9K, false),
Hayes Wang5598bfe2012-07-02 17:23:21 +0800264 [RTL_GIGA_MAC_VER_39] =
265 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1,
266 JUMBO_1K, true),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267};
268#undef _R
269
Francois Romieubcf0bf92006-07-26 23:14:13 +0200270enum cfg_version {
271 RTL_CFG_0 = 0x00,
272 RTL_CFG_1,
273 RTL_CFG_2
274};
275
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000276static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200277 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200278 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200279 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100280 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200281 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
282 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000283 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200284 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200285 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
286 { PCI_VENDOR_ID_LINKSYS, 0x1032,
287 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100288 { 0x0001, 0x8168,
289 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 {0,},
291};
292
293MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
294
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000295static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700296static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200297static struct {
298 u32 msg_enable;
299} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Francois Romieu07d3f512007-02-21 22:40:46 +0100301enum rtl_registers {
302 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100303 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100304 MAR0 = 8, /* Multicast filter. */
305 CounterAddrLow = 0x10,
306 CounterAddrHigh = 0x14,
307 TxDescStartAddrLow = 0x20,
308 TxDescStartAddrHigh = 0x24,
309 TxHDescStartAddrLow = 0x28,
310 TxHDescStartAddrHigh = 0x2c,
311 FLASH = 0x30,
312 ERSR = 0x36,
313 ChipCmd = 0x37,
314 TxPoll = 0x38,
315 IntrMask = 0x3c,
316 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700317
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800318 TxConfig = 0x40,
319#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
320#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
321
322 RxConfig = 0x44,
323#define RX128_INT_EN (1 << 15) /* 8111c and later */
324#define RX_MULTI_EN (1 << 14) /* 8111c only */
325#define RXCFG_FIFO_SHIFT 13
326 /* No threshold before first PCI xfer */
327#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
328#define RXCFG_DMA_SHIFT 8
329 /* Unlimited maximum PCI burst. */
330#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700331
Francois Romieu07d3f512007-02-21 22:40:46 +0100332 RxMissed = 0x4c,
333 Cfg9346 = 0x50,
334 Config0 = 0x51,
335 Config1 = 0x52,
336 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200337#define PME_SIGNAL (1 << 5) /* 8168c and later */
338
Francois Romieu07d3f512007-02-21 22:40:46 +0100339 Config3 = 0x54,
340 Config4 = 0x55,
341 Config5 = 0x56,
342 MultiIntr = 0x5c,
343 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100344 PHYstatus = 0x6c,
345 RxMaxSize = 0xda,
346 CPlusCmd = 0xe0,
347 IntrMitigate = 0xe2,
348 RxDescAddrLow = 0xe4,
349 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000350 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
351
352#define NoEarlyTx 0x3f /* Max value : no early transmit. */
353
354 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
355
356#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800357#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000358
Francois Romieu07d3f512007-02-21 22:40:46 +0100359 FuncEvent = 0xf0,
360 FuncEventMask = 0xf4,
361 FuncPresetState = 0xf8,
362 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363};
364
Francois Romieuf162a5d2008-06-01 22:37:49 +0200365enum rtl8110_registers {
366 TBICSR = 0x64,
367 TBI_ANAR = 0x68,
368 TBI_LPAR = 0x6a,
369};
370
371enum rtl8168_8101_registers {
372 CSIDR = 0x64,
373 CSIAR = 0x68,
374#define CSIAR_FLAG 0x80000000
375#define CSIAR_WRITE_CMD 0x80000000
376#define CSIAR_BYTE_ENABLE 0x0f
377#define CSIAR_BYTE_ENABLE_SHIFT 12
378#define CSIAR_ADDR_MASK 0x0fff
Hayes Wang7e18dca2012-03-30 14:33:02 +0800379#define CSIAR_FUNC_CARD 0x00000000
380#define CSIAR_FUNC_SDIO 0x00010000
381#define CSIAR_FUNC_NIC 0x00020000
françois romieu065c27c2011-01-03 15:08:12 +0000382 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200383 EPHYAR = 0x80,
384#define EPHYAR_FLAG 0x80000000
385#define EPHYAR_WRITE_CMD 0x80000000
386#define EPHYAR_REG_MASK 0x1f
387#define EPHYAR_REG_SHIFT 16
388#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800389 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800390#define PFM_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200391 DBG_REG = 0xd1,
392#define FIX_NAK_1 (1 << 4)
393#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800394 TWSI = 0xd2,
395 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800396#define NOW_IS_OOB (1 << 7)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800397#define EN_NDP (1 << 3)
398#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000399 EFUSEAR = 0xdc,
400#define EFUSEAR_FLAG 0x80000000
401#define EFUSEAR_WRITE_CMD 0x80000000
402#define EFUSEAR_READ_CMD 0x00000000
403#define EFUSEAR_REG_MASK 0x03ff
404#define EFUSEAR_REG_SHIFT 8
405#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200406};
407
françois romieuc0e45c12011-01-03 15:08:04 +0000408enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800409 LED_FREQ = 0x1a,
410 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000411 ERIDR = 0x70,
412 ERIAR = 0x74,
413#define ERIAR_FLAG 0x80000000
414#define ERIAR_WRITE_CMD 0x80000000
415#define ERIAR_READ_CMD 0x00000000
416#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000417#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800418#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
419#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
420#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
421#define ERIAR_MASK_SHIFT 12
422#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
423#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
424#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000425 EPHY_RXER_NUM = 0x7c,
426 OCPDR = 0xb0, /* OCP GPHY access */
427#define OCPDR_WRITE_CMD 0x80000000
428#define OCPDR_READ_CMD 0x00000000
429#define OCPDR_REG_MASK 0x7f
430#define OCPDR_GPHY_REG_SHIFT 16
431#define OCPDR_DATA_MASK 0xffff
432 OCPAR = 0xb4,
433#define OCPAR_FLAG 0x80000000
434#define OCPAR_GPHY_WRITE_CMD 0x8000f060
435#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000436 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
437 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200438#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800439#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800440#define PWM_EN (1 << 22)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800441#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000442};
443
Francois Romieu07d3f512007-02-21 22:40:46 +0100444enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100446 SYSErr = 0x8000,
447 PCSTimeout = 0x4000,
448 SWInt = 0x0100,
449 TxDescUnavail = 0x0080,
450 RxFIFOOver = 0x0040,
451 LinkChg = 0x0020,
452 RxOverflow = 0x0010,
453 TxErr = 0x0008,
454 TxOK = 0x0004,
455 RxErr = 0x0002,
456 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400459 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200460 RxFOVF = (1 << 23),
461 RxRWT = (1 << 22),
462 RxRES = (1 << 21),
463 RxRUNT = (1 << 20),
464 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800467 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100468 CmdReset = 0x10,
469 CmdRxEnb = 0x08,
470 CmdTxEnb = 0x04,
471 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Francois Romieu275391a2007-02-23 23:50:28 +0100473 /* TXPoll register p.5 */
474 HPQ = 0x80, /* Poll cmd on the high prio queue */
475 NPQ = 0x40, /* Poll cmd on the low prio queue */
476 FSWInt = 0x01, /* Forced software interrupt */
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100479 Cfg9346_Lock = 0x00,
480 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100483 AcceptErr = 0x20,
484 AcceptRunt = 0x10,
485 AcceptBroadcast = 0x08,
486 AcceptMulticast = 0x04,
487 AcceptMyPhys = 0x02,
488 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200489#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /* TxConfigBits */
492 TxInterFrameGapShift = 24,
493 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
494
Francois Romieu5d06a992006-02-23 00:47:58 +0100495 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200496 LEDS1 = (1 << 7),
497 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200498 Speed_down = (1 << 4),
499 MEMMAP = (1 << 3),
500 IOMAP = (1 << 2),
501 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100502 PMEnable = (1 << 0), /* Power Management Enable */
503
Francois Romieu6dccd162007-02-13 23:38:05 +0100504 /* Config2 register p. 25 */
françois romieu2ca6cf02011-12-15 08:37:43 +0000505 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100506 PCI_Clock_66MHz = 0x01,
507 PCI_Clock_33MHz = 0x00,
508
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100509 /* Config3 register p.25 */
510 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
511 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200512 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200513 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100514
Francois Romieud58d46b2011-05-03 16:38:29 +0200515 /* Config4 register */
516 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
517
Francois Romieu5d06a992006-02-23 00:47:58 +0100518 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100519 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
520 MWF = (1 << 5), /* Accept Multicast wakeup frame */
521 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200522 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100523 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100524 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* TBICSR p.28 */
527 TBIReset = 0x80000000,
528 TBILoopback = 0x40000000,
529 TBINwEnable = 0x20000000,
530 TBINwRestart = 0x10000000,
531 TBILinkOk = 0x02000000,
532 TBINwComplete = 0x01000000,
533
534 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200535 EnableBist = (1 << 15), // 8168 8101
536 Mac_dbgo_oe = (1 << 14), // 8168 8101
537 Normal_mode = (1 << 13), // unused
538 Force_half_dup = (1 << 12), // 8168 8101
539 Force_rxflow_en = (1 << 11), // 8168 8101
540 Force_txflow_en = (1 << 10), // 8168 8101
541 Cxpl_dbg_sel = (1 << 9), // 8168 8101
542 ASF = (1 << 8), // 8168 8101
543 PktCntrDisable = (1 << 7), // 8168 8101
544 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 RxVlan = (1 << 6),
546 RxChkSum = (1 << 5),
547 PCIDAC = (1 << 4),
548 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100549 INTT_0 = 0x0000, // 8168
550 INTT_1 = 0x0001, // 8168
551 INTT_2 = 0x0002, // 8168
552 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100555 TBI_Enable = 0x80,
556 TxFlowCtrl = 0x40,
557 RxFlowCtrl = 0x20,
558 _1000bpsF = 0x10,
559 _100bps = 0x08,
560 _10bps = 0x04,
561 LinkStatus = 0x02,
562 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100565 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200566
567 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100568 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569};
570
Francois Romieu2b7b4312011-04-18 22:53:24 -0700571enum rtl_desc_bit {
572 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
574 RingEnd = (1 << 30), /* End of descriptor ring */
575 FirstFrag = (1 << 29), /* First segment of a packet */
576 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700577};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Francois Romieu2b7b4312011-04-18 22:53:24 -0700579/* Generic case. */
580enum rtl_tx_desc_bit {
581 /* First doubleword. */
582 TD_LSO = (1 << 27), /* Large Send Offload */
583#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Francois Romieu2b7b4312011-04-18 22:53:24 -0700585 /* Second doubleword. */
586 TxVlanTag = (1 << 17), /* Add VLAN tag */
587};
588
589/* 8169, 8168b and 810x except 8102e. */
590enum rtl_tx_desc_bit_0 {
591 /* First doubleword. */
592#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
593 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
594 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
595 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
596};
597
598/* 8102e, 8168c and beyond. */
599enum rtl_tx_desc_bit_1 {
600 /* Second doubleword. */
601#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
602 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
603 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
604 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
605};
606
607static const struct rtl_tx_desc_info {
608 struct {
609 u32 udp;
610 u32 tcp;
611 } checksum;
612 u16 mss_shift;
613 u16 opts_offset;
614} tx_desc_info [] = {
615 [RTL_TD_0] = {
616 .checksum = {
617 .udp = TD0_IP_CS | TD0_UDP_CS,
618 .tcp = TD0_IP_CS | TD0_TCP_CS
619 },
620 .mss_shift = TD0_MSS_SHIFT,
621 .opts_offset = 0
622 },
623 [RTL_TD_1] = {
624 .checksum = {
625 .udp = TD1_IP_CS | TD1_UDP_CS,
626 .tcp = TD1_IP_CS | TD1_TCP_CS
627 },
628 .mss_shift = TD1_MSS_SHIFT,
629 .opts_offset = 1
630 }
631};
632
633enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /* Rx private */
635 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
636 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
637
638#define RxProtoUDP (PID1)
639#define RxProtoTCP (PID0)
640#define RxProtoIP (PID1 | PID0)
641#define RxProtoMask RxProtoIP
642
643 IPFail = (1 << 16), /* IP checksum failed */
644 UDPFail = (1 << 15), /* UDP/IP checksum failed */
645 TCPFail = (1 << 14), /* TCP/IP checksum failed */
646 RxVlanTag = (1 << 16), /* VLAN tag available */
647};
648
649#define RsvdMask 0x3fffc000
650
651struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200652 __le32 opts1;
653 __le32 opts2;
654 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655};
656
657struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200658 __le32 opts1;
659 __le32 opts2;
660 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661};
662
663struct ring_info {
664 struct sk_buff *skb;
665 u32 len;
666 u8 __pad[sizeof(void *) - sizeof(u32)];
667};
668
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200669enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200670 RTL_FEATURE_WOL = (1 << 0),
671 RTL_FEATURE_MSI = (1 << 1),
672 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200673};
674
Ivan Vecera355423d2009-02-06 21:49:57 -0800675struct rtl8169_counters {
676 __le64 tx_packets;
677 __le64 rx_packets;
678 __le64 tx_errors;
679 __le32 rx_errors;
680 __le16 rx_missed;
681 __le16 align_errors;
682 __le32 tx_one_collision;
683 __le32 tx_multi_collision;
684 __le64 rx_unicast;
685 __le64 rx_broadcast;
686 __le32 rx_multicast;
687 __le16 tx_aborted;
688 __le16 tx_underun;
689};
690
Francois Romieuda78dbf2012-01-26 14:18:23 +0100691enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100692 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100693 RTL_FLAG_TASK_SLOW_PENDING,
694 RTL_FLAG_TASK_RESET_PENDING,
695 RTL_FLAG_TASK_PHY_PENDING,
696 RTL_FLAG_MAX
697};
698
Junchang Wang8027aa22012-03-04 23:30:32 +0100699struct rtl8169_stats {
700 u64 packets;
701 u64 bytes;
702 struct u64_stats_sync syncp;
703};
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705struct rtl8169_private {
706 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200707 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000708 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700709 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200710 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700711 u16 txd_version;
712 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
714 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
715 u32 dirty_rx;
716 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100717 struct rtl8169_stats rx_stats;
718 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
720 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
721 dma_addr_t TxPhyAddr;
722 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000723 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 struct timer_list timer;
726 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100727
728 u16 event_slow;
françois romieuc0e45c12011-01-03 15:08:04 +0000729
730 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200731 void (*write)(struct rtl8169_private *, int, int);
732 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000733 } mdio_ops;
734
françois romieu065c27c2011-01-03 15:08:12 +0000735 struct pll_power_ops {
736 void (*down)(struct rtl8169_private *);
737 void (*up)(struct rtl8169_private *);
738 } pll_power_ops;
739
Francois Romieud58d46b2011-05-03 16:38:29 +0200740 struct jumbo_ops {
741 void (*enable)(struct rtl8169_private *);
742 void (*disable)(struct rtl8169_private *);
743 } jumbo_ops;
744
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800745 struct csi_ops {
Francois Romieu52989f02012-07-06 13:37:00 +0200746 void (*write)(struct rtl8169_private *, int, int);
747 u32 (*read)(struct rtl8169_private *, int);
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800748 } csi_ops;
749
Oliver Neukum54405cd2011-01-06 21:55:13 +0100750 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200751 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000752 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100753 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000754 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800756 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100757
758 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100759 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
760 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100761 struct work_struct work;
762 } wk;
763
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200764 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200765
766 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800767 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000768 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400769 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000770
Francois Romieub6ffd972011-06-17 17:00:05 +0200771 struct rtl_fw {
772 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200773
774#define RTL_VER_SIZE 32
775
776 char version[RTL_VER_SIZE];
777
778 struct rtl_fw_phy_action {
779 __le32 *code;
780 size_t size;
781 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200782 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300783#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784};
785
Ralf Baechle979b6c12005-06-13 14:30:40 -0700786MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700789MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200790module_param_named(debug, debug.msg_enable, int, 0);
791MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792MODULE_LICENSE("GPL");
793MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000794MODULE_FIRMWARE(FIRMWARE_8168D_1);
795MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000796MODULE_FIRMWARE(FIRMWARE_8168E_1);
797MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400798MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800799MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800800MODULE_FIRMWARE(FIRMWARE_8168F_1);
801MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800802MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800803MODULE_FIRMWARE(FIRMWARE_8411_1);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800804MODULE_FIRMWARE(FIRMWARE_8106E_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Francois Romieuda78dbf2012-01-26 14:18:23 +0100806static void rtl_lock_work(struct rtl8169_private *tp)
807{
808 mutex_lock(&tp->wk.mutex);
809}
810
811static void rtl_unlock_work(struct rtl8169_private *tp)
812{
813 mutex_unlock(&tp->wk.mutex);
814}
815
Francois Romieud58d46b2011-05-03 16:38:29 +0200816static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
817{
818 int cap = pci_pcie_cap(pdev);
819
820 if (cap) {
821 u16 ctl;
822
823 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
824 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
825 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
826 }
827}
828
Francois Romieuffc46952012-07-06 14:19:23 +0200829struct rtl_cond {
830 bool (*check)(struct rtl8169_private *);
831 const char *msg;
832};
833
834static void rtl_udelay(unsigned int d)
835{
836 udelay(d);
837}
838
839static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
840 void (*delay)(unsigned int), unsigned int d, int n,
841 bool high)
842{
843 int i;
844
845 for (i = 0; i < n; i++) {
846 delay(d);
847 if (c->check(tp) == high)
848 return true;
849 }
850 netif_err(tp, drv, tp->dev, c->msg);
851 return false;
852}
853
854static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
855 const struct rtl_cond *c,
856 unsigned int d, int n)
857{
858 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
859}
860
861static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
862 const struct rtl_cond *c,
863 unsigned int d, int n)
864{
865 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
866}
867
868static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
869 const struct rtl_cond *c,
870 unsigned int d, int n)
871{
872 return rtl_loop_wait(tp, c, msleep, d, n, true);
873}
874
875static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
876 const struct rtl_cond *c,
877 unsigned int d, int n)
878{
879 return rtl_loop_wait(tp, c, msleep, d, n, false);
880}
881
882#define DECLARE_RTL_COND(name) \
883static bool name ## _check(struct rtl8169_private *); \
884 \
885static const struct rtl_cond name = { \
886 .check = name ## _check, \
887 .msg = #name \
888}; \
889 \
890static bool name ## _check(struct rtl8169_private *tp)
891
892DECLARE_RTL_COND(rtl_ocpar_cond)
893{
894 void __iomem *ioaddr = tp->mmio_addr;
895
896 return RTL_R32(OCPAR) & OCPAR_FLAG;
897}
898
françois romieub646d902011-01-03 15:08:21 +0000899static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
900{
901 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000902
903 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Francois Romieuffc46952012-07-06 14:19:23 +0200904
905 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
906 RTL_R32(OCPDR) : ~0;
françois romieub646d902011-01-03 15:08:21 +0000907}
908
909static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
910{
911 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000912
913 RTL_W32(OCPDR, data);
914 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Francois Romieuffc46952012-07-06 14:19:23 +0200915
916 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
917}
918
919DECLARE_RTL_COND(rtl_eriar_cond)
920{
921 void __iomem *ioaddr = tp->mmio_addr;
922
923 return RTL_R32(ERIAR) & ERIAR_FLAG;
françois romieub646d902011-01-03 15:08:21 +0000924}
925
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800926static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000927{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800928 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000929
930 RTL_W8(ERIDR, cmd);
931 RTL_W32(ERIAR, 0x800010e8);
932 msleep(2);
Francois Romieuffc46952012-07-06 14:19:23 +0200933
934 if (!rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 5))
935 return;
françois romieub646d902011-01-03 15:08:21 +0000936
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800937 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000938}
939
940#define OOB_CMD_RESET 0x00
941#define OOB_CMD_DRIVER_START 0x05
942#define OOB_CMD_DRIVER_STOP 0x06
943
Francois Romieucecb5fd2011-04-01 10:21:07 +0200944static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
945{
946 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
947}
948
Francois Romieuffc46952012-07-06 14:19:23 +0200949DECLARE_RTL_COND(rtl_ocp_read_cond)
françois romieub646d902011-01-03 15:08:21 +0000950{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200951 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000952
Francois Romieucecb5fd2011-04-01 10:21:07 +0200953 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000954
Francois Romieuffc46952012-07-06 14:19:23 +0200955 return ocp_read(tp, 0x0f, reg) & 0x00000800;
956}
957
958static void rtl8168_driver_start(struct rtl8169_private *tp)
959{
960 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
961
962 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
françois romieub646d902011-01-03 15:08:21 +0000963}
964
965static void rtl8168_driver_stop(struct rtl8169_private *tp)
966{
françois romieub646d902011-01-03 15:08:21 +0000967 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
968
Francois Romieuffc46952012-07-06 14:19:23 +0200969 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
françois romieub646d902011-01-03 15:08:21 +0000970}
971
hayeswang4804b3b2011-03-21 01:50:29 +0000972static int r8168dp_check_dash(struct rtl8169_private *tp)
973{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200974 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000975
Francois Romieucecb5fd2011-04-01 10:21:07 +0200976 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000977}
françois romieub646d902011-01-03 15:08:21 +0000978
Francois Romieuffc46952012-07-06 14:19:23 +0200979DECLARE_RTL_COND(rtl_phyar_cond)
980{
981 void __iomem *ioaddr = tp->mmio_addr;
982
983 return RTL_R32(PHYAR) & 0x80000000;
984}
985
Francois Romieu24192212012-07-06 20:19:42 +0200986static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Francois Romieu24192212012-07-06 20:19:42 +0200988 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Francois Romieu24192212012-07-06 20:19:42 +0200990 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Francois Romieuffc46952012-07-06 14:19:23 +0200992 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -0700993 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700994 * According to hardware specs a 20us delay is required after write
995 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700996 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700997 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
Francois Romieu24192212012-07-06 20:19:42 +02001000static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Francois Romieu24192212012-07-06 20:19:42 +02001002 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieuffc46952012-07-06 14:19:23 +02001003 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Francois Romieu24192212012-07-06 20:19:42 +02001005 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Francois Romieuffc46952012-07-06 14:19:23 +02001007 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1008 RTL_R32(PHYAR) & 0xffff : ~0;
1009
Timo Teräs81a95f02010-06-09 17:31:48 -07001010 /*
1011 * According to hardware specs a 20us delay is required after read
1012 * complete indication, but before sending next command.
1013 */
1014 udelay(20);
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return value;
1017}
1018
Francois Romieu24192212012-07-06 20:19:42 +02001019static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001020{
Francois Romieu24192212012-07-06 20:19:42 +02001021 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001022
Francois Romieu24192212012-07-06 20:19:42 +02001023 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
françois romieuc0e45c12011-01-03 15:08:04 +00001024 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
1025 RTL_W32(EPHY_RXER_NUM, 0);
1026
Francois Romieuffc46952012-07-06 14:19:23 +02001027 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001028}
1029
Francois Romieu24192212012-07-06 20:19:42 +02001030static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001031{
Francois Romieu24192212012-07-06 20:19:42 +02001032 r8168dp_1_mdio_access(tp, reg,
1033 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001034}
1035
Francois Romieu24192212012-07-06 20:19:42 +02001036static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001037{
Francois Romieu24192212012-07-06 20:19:42 +02001038 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001039
Francois Romieu24192212012-07-06 20:19:42 +02001040 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001041
1042 mdelay(1);
1043 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1044 RTL_W32(EPHY_RXER_NUM, 0);
1045
Francois Romieuffc46952012-07-06 14:19:23 +02001046 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1047 RTL_R32(OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001048}
1049
françois romieue6de30d2011-01-03 15:08:37 +00001050#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1051
1052static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1053{
1054 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1055}
1056
1057static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1058{
1059 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1060}
1061
Francois Romieu24192212012-07-06 20:19:42 +02001062static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001063{
Francois Romieu24192212012-07-06 20:19:42 +02001064 void __iomem *ioaddr = tp->mmio_addr;
1065
françois romieue6de30d2011-01-03 15:08:37 +00001066 r8168dp_2_mdio_start(ioaddr);
1067
Francois Romieu24192212012-07-06 20:19:42 +02001068 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001069
1070 r8168dp_2_mdio_stop(ioaddr);
1071}
1072
Francois Romieu24192212012-07-06 20:19:42 +02001073static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001074{
Francois Romieu24192212012-07-06 20:19:42 +02001075 void __iomem *ioaddr = tp->mmio_addr;
françois romieue6de30d2011-01-03 15:08:37 +00001076 int value;
1077
1078 r8168dp_2_mdio_start(ioaddr);
1079
Francois Romieu24192212012-07-06 20:19:42 +02001080 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001081
1082 r8168dp_2_mdio_stop(ioaddr);
1083
1084 return value;
1085}
1086
françois romieu4da19632011-01-03 15:07:55 +00001087static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001088{
Francois Romieu24192212012-07-06 20:19:42 +02001089 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001090}
1091
françois romieu4da19632011-01-03 15:07:55 +00001092static int rtl_readphy(struct rtl8169_private *tp, int location)
1093{
Francois Romieu24192212012-07-06 20:19:42 +02001094 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001095}
1096
1097static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1098{
1099 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1100}
1101
1102static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001103{
1104 int val;
1105
françois romieu4da19632011-01-03 15:07:55 +00001106 val = rtl_readphy(tp, reg_addr);
1107 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001108}
1109
Francois Romieuccdffb92008-07-26 14:26:06 +02001110static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1111 int val)
1112{
1113 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001114
françois romieu4da19632011-01-03 15:07:55 +00001115 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001116}
1117
1118static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1119{
1120 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001121
françois romieu4da19632011-01-03 15:07:55 +00001122 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001123}
1124
Francois Romieuffc46952012-07-06 14:19:23 +02001125DECLARE_RTL_COND(rtl_ephyar_cond)
1126{
1127 void __iomem *ioaddr = tp->mmio_addr;
1128
1129 return RTL_R32(EPHYAR) & EPHYAR_FLAG;
1130}
1131
Francois Romieufdf6fc02012-07-06 22:40:38 +02001132static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001133{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001134 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001135
1136 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1137 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1138
Francois Romieuffc46952012-07-06 14:19:23 +02001139 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1140
1141 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001142}
1143
Francois Romieufdf6fc02012-07-06 22:40:38 +02001144static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001145{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001146 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001147
1148 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1149
Francois Romieuffc46952012-07-06 14:19:23 +02001150 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1151 RTL_R32(EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001152}
1153
Francois Romieufdf6fc02012-07-06 22:40:38 +02001154static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1155 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001156{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001157 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001158
1159 BUG_ON((addr & 3) || (mask == 0));
1160 RTL_W32(ERIDR, val);
1161 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1162
Francois Romieuffc46952012-07-06 14:19:23 +02001163 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001164}
1165
Francois Romieufdf6fc02012-07-06 22:40:38 +02001166static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001167{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001168 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001169
1170 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1171
Francois Romieuffc46952012-07-06 14:19:23 +02001172 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1173 RTL_R32(ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001174}
1175
Francois Romieufdf6fc02012-07-06 22:40:38 +02001176static void rtl_w1w0_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
1177 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001178{
1179 u32 val;
1180
Francois Romieufdf6fc02012-07-06 22:40:38 +02001181 val = rtl_eri_read(tp, addr, type);
1182 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001183}
1184
françois romieuc28aa382011-08-02 03:53:43 +00001185struct exgmac_reg {
1186 u16 addr;
1187 u16 mask;
1188 u32 val;
1189};
1190
Francois Romieufdf6fc02012-07-06 22:40:38 +02001191static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001192 const struct exgmac_reg *r, int len)
1193{
1194 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001195 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001196 r++;
1197 }
1198}
1199
Francois Romieuffc46952012-07-06 14:19:23 +02001200DECLARE_RTL_COND(rtl_efusear_cond)
1201{
1202 void __iomem *ioaddr = tp->mmio_addr;
1203
1204 return RTL_R32(EFUSEAR) & EFUSEAR_FLAG;
1205}
1206
Francois Romieufdf6fc02012-07-06 22:40:38 +02001207static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001208{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001209 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00001210
1211 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1212
Francois Romieuffc46952012-07-06 14:19:23 +02001213 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1214 RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001215}
1216
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001217static u16 rtl_get_events(struct rtl8169_private *tp)
1218{
1219 void __iomem *ioaddr = tp->mmio_addr;
1220
1221 return RTL_R16(IntrStatus);
1222}
1223
1224static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1225{
1226 void __iomem *ioaddr = tp->mmio_addr;
1227
1228 RTL_W16(IntrStatus, bits);
1229 mmiowb();
1230}
1231
1232static void rtl_irq_disable(struct rtl8169_private *tp)
1233{
1234 void __iomem *ioaddr = tp->mmio_addr;
1235
1236 RTL_W16(IntrMask, 0);
1237 mmiowb();
1238}
1239
Francois Romieu3e990ff2012-01-26 12:50:01 +01001240static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1241{
1242 void __iomem *ioaddr = tp->mmio_addr;
1243
1244 RTL_W16(IntrMask, bits);
1245}
1246
Francois Romieuda78dbf2012-01-26 14:18:23 +01001247#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1248#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1249#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1250
1251static void rtl_irq_enable_all(struct rtl8169_private *tp)
1252{
1253 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1254}
1255
françois romieu811fd302011-12-04 20:30:45 +00001256static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
françois romieu811fd302011-12-04 20:30:45 +00001258 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001260 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001261 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001262 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
françois romieu4da19632011-01-03 15:07:55 +00001265static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
françois romieu4da19632011-01-03 15:07:55 +00001267 void __iomem *ioaddr = tp->mmio_addr;
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return RTL_R32(TBICSR) & TBIReset;
1270}
1271
françois romieu4da19632011-01-03 15:07:55 +00001272static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
françois romieu4da19632011-01-03 15:07:55 +00001274 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
1277static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1278{
1279 return RTL_R32(TBICSR) & TBILinkOk;
1280}
1281
1282static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1283{
1284 return RTL_R8(PHYstatus) & LinkStatus;
1285}
1286
françois romieu4da19632011-01-03 15:07:55 +00001287static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
françois romieu4da19632011-01-03 15:07:55 +00001289 void __iomem *ioaddr = tp->mmio_addr;
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1292}
1293
françois romieu4da19632011-01-03 15:07:55 +00001294static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
1296 unsigned int val;
1297
françois romieu4da19632011-01-03 15:07:55 +00001298 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1299 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
Hayes Wang70090422011-07-06 15:58:06 +08001302static void rtl_link_chg_patch(struct rtl8169_private *tp)
1303{
1304 void __iomem *ioaddr = tp->mmio_addr;
1305 struct net_device *dev = tp->dev;
1306
1307 if (!netif_running(dev))
1308 return;
1309
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001310 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1311 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Hayes Wang70090422011-07-06 15:58:06 +08001312 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001313 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1314 ERIAR_EXGMAC);
1315 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1316 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001317 } else if (RTL_R8(PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001318 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1319 ERIAR_EXGMAC);
1320 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1321 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001322 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001323 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1324 ERIAR_EXGMAC);
1325 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1326 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001327 }
1328 /* Reset packet filter */
Francois Romieufdf6fc02012-07-06 22:40:38 +02001329 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001330 ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02001331 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001332 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001333 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1334 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1335 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001336 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1337 ERIAR_EXGMAC);
1338 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1339 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001340 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001341 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1342 ERIAR_EXGMAC);
1343 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1344 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001345 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001346 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1347 if (RTL_R8(PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001348 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1349 ERIAR_EXGMAC);
1350 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1351 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001352 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001353 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1354 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001355 }
Hayes Wang70090422011-07-06 15:58:06 +08001356 }
1357}
1358
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001359static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001360 struct rtl8169_private *tp,
1361 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001364 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001365 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001366 if (pm)
1367 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001369 if (net_ratelimit())
1370 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001371 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001373 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001374 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001375 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001379static void rtl8169_check_link_status(struct net_device *dev,
1380 struct rtl8169_private *tp,
1381 void __iomem *ioaddr)
1382{
1383 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1384}
1385
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001386#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1387
1388static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1389{
1390 void __iomem *ioaddr = tp->mmio_addr;
1391 u8 options;
1392 u32 wolopts = 0;
1393
1394 options = RTL_R8(Config1);
1395 if (!(options & PMEnable))
1396 return 0;
1397
1398 options = RTL_R8(Config3);
1399 if (options & LinkUp)
1400 wolopts |= WAKE_PHY;
1401 if (options & MagicPacket)
1402 wolopts |= WAKE_MAGIC;
1403
1404 options = RTL_R8(Config5);
1405 if (options & UWF)
1406 wolopts |= WAKE_UCAST;
1407 if (options & BWF)
1408 wolopts |= WAKE_BCAST;
1409 if (options & MWF)
1410 wolopts |= WAKE_MCAST;
1411
1412 return wolopts;
1413}
1414
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001415static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1416{
1417 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001418
Francois Romieuda78dbf2012-01-26 14:18:23 +01001419 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001420
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001421 wol->supported = WAKE_ANY;
1422 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001423
Francois Romieuda78dbf2012-01-26 14:18:23 +01001424 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001425}
1426
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001427static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001428{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001429 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001430 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001431 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001432 u32 opt;
1433 u16 reg;
1434 u8 mask;
1435 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001436 { WAKE_PHY, Config3, LinkUp },
1437 { WAKE_MAGIC, Config3, MagicPacket },
1438 { WAKE_UCAST, Config5, UWF },
1439 { WAKE_BCAST, Config5, BWF },
1440 { WAKE_MCAST, Config5, MWF },
1441 { WAKE_ANY, Config5, LanWake }
1442 };
Francois Romieu851e6022012-04-17 11:10:11 +02001443 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001444
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001445 RTL_W8(Cfg9346, Cfg9346_Unlock);
1446
1447 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
Francois Romieu851e6022012-04-17 11:10:11 +02001448 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001449 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001450 options |= cfg[i].mask;
1451 RTL_W8(cfg[i].reg, options);
1452 }
1453
Francois Romieu851e6022012-04-17 11:10:11 +02001454 switch (tp->mac_version) {
1455 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1456 options = RTL_R8(Config1) & ~PMEnable;
1457 if (wolopts)
1458 options |= PMEnable;
1459 RTL_W8(Config1, options);
1460 break;
1461 default:
Francois Romieud387b422012-04-17 11:12:01 +02001462 options = RTL_R8(Config2) & ~PME_SIGNAL;
1463 if (wolopts)
1464 options |= PME_SIGNAL;
1465 RTL_W8(Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001466 break;
1467 }
1468
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001469 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001470}
1471
1472static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1473{
1474 struct rtl8169_private *tp = netdev_priv(dev);
1475
Francois Romieuda78dbf2012-01-26 14:18:23 +01001476 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001477
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001478 if (wol->wolopts)
1479 tp->features |= RTL_FEATURE_WOL;
1480 else
1481 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001482 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001483
1484 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001485
françois romieuea809072010-11-08 13:23:58 +00001486 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1487
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001488 return 0;
1489}
1490
Francois Romieu31bd2042011-04-26 18:58:59 +02001491static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1492{
Francois Romieu85bffe62011-04-27 08:22:39 +02001493 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001494}
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496static void rtl8169_get_drvinfo(struct net_device *dev,
1497 struct ethtool_drvinfo *info)
1498{
1499 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001500 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Rick Jones68aad782011-11-07 13:29:27 +00001502 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1503 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1504 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001505 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001506 if (!IS_ERR_OR_NULL(rtl_fw))
1507 strlcpy(info->fw_version, rtl_fw->version,
1508 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
1510
1511static int rtl8169_get_regs_len(struct net_device *dev)
1512{
1513 return R8169_REGS_SIZE;
1514}
1515
1516static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001517 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
1519 struct rtl8169_private *tp = netdev_priv(dev);
1520 void __iomem *ioaddr = tp->mmio_addr;
1521 int ret = 0;
1522 u32 reg;
1523
1524 reg = RTL_R32(TBICSR);
1525 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1526 (duplex == DUPLEX_FULL)) {
1527 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1528 } else if (autoneg == AUTONEG_ENABLE)
1529 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1530 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001531 netif_warn(tp, link, dev,
1532 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 ret = -EOPNOTSUPP;
1534 }
1535
1536 return ret;
1537}
1538
1539static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001540 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
1542 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001543 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001544 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Hayes Wang716b50a2011-02-22 17:26:18 +08001546 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001549 int auto_nego;
1550
françois romieu4da19632011-01-03 15:07:55 +00001551 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001552 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1553 ADVERTISE_100HALF | ADVERTISE_100FULL);
1554
1555 if (adv & ADVERTISED_10baseT_Half)
1556 auto_nego |= ADVERTISE_10HALF;
1557 if (adv & ADVERTISED_10baseT_Full)
1558 auto_nego |= ADVERTISE_10FULL;
1559 if (adv & ADVERTISED_100baseT_Half)
1560 auto_nego |= ADVERTISE_100HALF;
1561 if (adv & ADVERTISED_100baseT_Full)
1562 auto_nego |= ADVERTISE_100FULL;
1563
françois romieu3577aa12009-05-19 10:46:48 +00001564 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1565
françois romieu4da19632011-01-03 15:07:55 +00001566 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001567 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1568
1569 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001570 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001571 if (adv & ADVERTISED_1000baseT_Half)
1572 giga_ctrl |= ADVERTISE_1000HALF;
1573 if (adv & ADVERTISED_1000baseT_Full)
1574 giga_ctrl |= ADVERTISE_1000FULL;
1575 } else if (adv & (ADVERTISED_1000baseT_Half |
1576 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001577 netif_info(tp, link, dev,
1578 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001579 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
françois romieu3577aa12009-05-19 10:46:48 +00001582 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001583
françois romieu4da19632011-01-03 15:07:55 +00001584 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1585 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001586 } else {
1587 giga_ctrl = 0;
1588
1589 if (speed == SPEED_10)
1590 bmcr = 0;
1591 else if (speed == SPEED_100)
1592 bmcr = BMCR_SPEED100;
1593 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001594 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001595
1596 if (duplex == DUPLEX_FULL)
1597 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001598 }
1599
françois romieu4da19632011-01-03 15:07:55 +00001600 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001601
Francois Romieucecb5fd2011-04-01 10:21:07 +02001602 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1603 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001604 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001605 rtl_writephy(tp, 0x17, 0x2138);
1606 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001607 } else {
françois romieu4da19632011-01-03 15:07:55 +00001608 rtl_writephy(tp, 0x17, 0x2108);
1609 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001610 }
1611 }
1612
Oliver Neukum54405cd2011-01-06 21:55:13 +01001613 rc = 0;
1614out:
1615 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
1617
1618static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001619 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
1621 struct rtl8169_private *tp = netdev_priv(dev);
1622 int ret;
1623
Oliver Neukum54405cd2011-01-06 21:55:13 +01001624 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001625 if (ret < 0)
1626 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Francois Romieu4876cc12011-03-11 21:07:11 +01001628 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1629 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001631 }
1632out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 return ret;
1634}
1635
1636static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1637{
1638 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 int ret;
1640
Francois Romieu4876cc12011-03-11 21:07:11 +01001641 del_timer_sync(&tp->timer);
1642
Francois Romieuda78dbf2012-01-26 14:18:23 +01001643 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001644 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001645 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001646 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 return ret;
1649}
1650
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001651static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1652 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
Francois Romieud58d46b2011-05-03 16:38:29 +02001654 struct rtl8169_private *tp = netdev_priv(dev);
1655
Francois Romieu2b7b4312011-04-18 22:53:24 -07001656 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001657 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Francois Romieud58d46b2011-05-03 16:38:29 +02001659 if (dev->mtu > JUMBO_1K &&
1660 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1661 features &= ~NETIF_F_IP_CSUM;
1662
Michał Mirosław350fb322011-04-08 06:35:56 +00001663 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
Francois Romieuda78dbf2012-01-26 14:18:23 +01001666static void __rtl8169_set_features(struct net_device *dev,
1667 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
1669 struct rtl8169_private *tp = netdev_priv(dev);
Ben Greear6bbe0212012-02-10 15:04:33 +00001670 netdev_features_t changed = features ^ dev->features;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001671 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Ben Greear6bbe0212012-02-10 15:04:33 +00001673 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)))
1674 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Ben Greear6bbe0212012-02-10 15:04:33 +00001676 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)) {
1677 if (features & NETIF_F_RXCSUM)
1678 tp->cp_cmd |= RxChkSum;
1679 else
1680 tp->cp_cmd &= ~RxChkSum;
Michał Mirosław350fb322011-04-08 06:35:56 +00001681
Ben Greear6bbe0212012-02-10 15:04:33 +00001682 if (dev->features & NETIF_F_HW_VLAN_RX)
1683 tp->cp_cmd |= RxVlan;
1684 else
1685 tp->cp_cmd &= ~RxVlan;
1686
1687 RTL_W16(CPlusCmd, tp->cp_cmd);
1688 RTL_R16(CPlusCmd);
1689 }
1690 if (changed & NETIF_F_RXALL) {
1691 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1692 if (features & NETIF_F_RXALL)
1693 tmp |= (AcceptErr | AcceptRunt);
1694 RTL_W32(RxConfig, tmp);
1695 }
Francois Romieuda78dbf2012-01-26 14:18:23 +01001696}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Francois Romieuda78dbf2012-01-26 14:18:23 +01001698static int rtl8169_set_features(struct net_device *dev,
1699 netdev_features_t features)
1700{
1701 struct rtl8169_private *tp = netdev_priv(dev);
1702
1703 rtl_lock_work(tp);
1704 __rtl8169_set_features(dev, features);
1705 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 return 0;
1708}
1709
Francois Romieuda78dbf2012-01-26 14:18:23 +01001710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1712 struct sk_buff *skb)
1713{
Jesse Grosseab6d182010-10-20 13:56:03 +00001714 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1716}
1717
Francois Romieu7a8fc772011-03-01 17:18:33 +01001718static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
1720 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Francois Romieu7a8fc772011-03-01 17:18:33 +01001722 if (opts2 & RxVlanTag)
1723 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
Francois Romieuccdffb92008-07-26 14:26:06 +02001728static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
1730 struct rtl8169_private *tp = netdev_priv(dev);
1731 void __iomem *ioaddr = tp->mmio_addr;
1732 u32 status;
1733
1734 cmd->supported =
1735 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1736 cmd->port = PORT_FIBRE;
1737 cmd->transceiver = XCVR_INTERNAL;
1738
1739 status = RTL_R32(TBICSR);
1740 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1741 cmd->autoneg = !!(status & TBINwEnable);
1742
David Decotigny70739492011-04-27 18:32:40 +00001743 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001745
1746 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}
1748
Francois Romieuccdffb92008-07-26 14:26:06 +02001749static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
1751 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Francois Romieuccdffb92008-07-26 14:26:06 +02001753 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
1756static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1757{
1758 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001759 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Francois Romieuda78dbf2012-01-26 14:18:23 +01001761 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02001762 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001763 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Francois Romieuccdffb92008-07-26 14:26:06 +02001765 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766}
1767
1768static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1769 void *p)
1770{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001771 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Francois Romieu5b0384f2006-08-16 16:00:01 +02001773 if (regs->len > R8169_REGS_SIZE)
1774 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Francois Romieuda78dbf2012-01-26 14:18:23 +01001776 rtl_lock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001777 memcpy_fromio(p, tp->mmio_addr, regs->len);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001778 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779}
1780
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001781static u32 rtl8169_get_msglevel(struct net_device *dev)
1782{
1783 struct rtl8169_private *tp = netdev_priv(dev);
1784
1785 return tp->msg_enable;
1786}
1787
1788static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1789{
1790 struct rtl8169_private *tp = netdev_priv(dev);
1791
1792 tp->msg_enable = value;
1793}
1794
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001795static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1796 "tx_packets",
1797 "rx_packets",
1798 "tx_errors",
1799 "rx_errors",
1800 "rx_missed",
1801 "align_errors",
1802 "tx_single_collisions",
1803 "tx_multi_collisions",
1804 "unicast",
1805 "broadcast",
1806 "multicast",
1807 "tx_aborted",
1808 "tx_underrun",
1809};
1810
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001811static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001812{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001813 switch (sset) {
1814 case ETH_SS_STATS:
1815 return ARRAY_SIZE(rtl8169_gstrings);
1816 default:
1817 return -EOPNOTSUPP;
1818 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001819}
1820
Francois Romieuffc46952012-07-06 14:19:23 +02001821DECLARE_RTL_COND(rtl_counters_cond)
1822{
1823 void __iomem *ioaddr = tp->mmio_addr;
1824
1825 return RTL_R32(CounterAddrLow) & CounterDump;
1826}
1827
Ivan Vecera355423d2009-02-06 21:49:57 -08001828static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001829{
1830 struct rtl8169_private *tp = netdev_priv(dev);
1831 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001832 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001833 struct rtl8169_counters *counters;
1834 dma_addr_t paddr;
1835 u32 cmd;
1836
Ivan Vecera355423d2009-02-06 21:49:57 -08001837 /*
1838 * Some chips are unable to dump tally counters when the receiver
1839 * is disabled.
1840 */
1841 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1842 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001843
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001844 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001845 if (!counters)
1846 return;
1847
1848 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001849 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001850 RTL_W32(CounterAddrLow, cmd);
1851 RTL_W32(CounterAddrLow, cmd | CounterDump);
1852
Francois Romieuffc46952012-07-06 14:19:23 +02001853 if (rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000))
1854 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001855
1856 RTL_W32(CounterAddrLow, 0);
1857 RTL_W32(CounterAddrHigh, 0);
1858
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001859 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001860}
1861
Ivan Vecera355423d2009-02-06 21:49:57 -08001862static void rtl8169_get_ethtool_stats(struct net_device *dev,
1863 struct ethtool_stats *stats, u64 *data)
1864{
1865 struct rtl8169_private *tp = netdev_priv(dev);
1866
1867 ASSERT_RTNL();
1868
1869 rtl8169_update_counters(dev);
1870
1871 data[0] = le64_to_cpu(tp->counters.tx_packets);
1872 data[1] = le64_to_cpu(tp->counters.rx_packets);
1873 data[2] = le64_to_cpu(tp->counters.tx_errors);
1874 data[3] = le32_to_cpu(tp->counters.rx_errors);
1875 data[4] = le16_to_cpu(tp->counters.rx_missed);
1876 data[5] = le16_to_cpu(tp->counters.align_errors);
1877 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1878 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1879 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1880 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1881 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1882 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1883 data[12] = le16_to_cpu(tp->counters.tx_underun);
1884}
1885
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001886static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1887{
1888 switch(stringset) {
1889 case ETH_SS_STATS:
1890 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1891 break;
1892 }
1893}
1894
Jeff Garzik7282d492006-09-13 14:30:00 -04001895static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 .get_drvinfo = rtl8169_get_drvinfo,
1897 .get_regs_len = rtl8169_get_regs_len,
1898 .get_link = ethtool_op_get_link,
1899 .get_settings = rtl8169_get_settings,
1900 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001901 .get_msglevel = rtl8169_get_msglevel,
1902 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001904 .get_wol = rtl8169_get_wol,
1905 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001906 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001907 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001908 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00001909 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910};
1911
Francois Romieu07d3f512007-02-21 22:40:46 +01001912static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001913 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
Francois Romieu5d320a22011-05-08 17:47:36 +02001915 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001916 /*
1917 * The driver currently handles the 8168Bf and the 8168Be identically
1918 * but they can be identified more specifically through the test below
1919 * if needed:
1920 *
1921 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001922 *
1923 * Same thing for the 8101Eb and the 8101Ec:
1924 *
1925 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001926 */
Francois Romieu37441002011-06-17 22:58:54 +02001927 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001929 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 int mac_version;
1931 } mac_info[] = {
Hayes Wangc2218922011-09-06 16:55:18 +08001932 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001933 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08001934 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1935 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1936
hayeswang01dc7fe2011-03-21 01:50:28 +00001937 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001938 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001939 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1940 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1941 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1942
Francois Romieu5b538df2008-07-20 16:22:45 +02001943 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001944 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1945 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001946 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001947
françois romieue6de30d2011-01-03 15:08:37 +00001948 /* 8168DP family. */
1949 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1950 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001951 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001952
Francois Romieuef808d52008-06-29 13:10:54 +02001953 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001954 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001955 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001956 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001957 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001958 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1959 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001960 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001961 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001962 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001963
1964 /* 8168B family. */
1965 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1966 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1967 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1968 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1969
1970 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08001971 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
1972 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08001973 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00001974 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001975 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1976 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1977 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001978 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1979 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1980 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1981 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1982 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1983 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001984 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001985 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001986 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001987 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1988 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001989 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1990 /* FIXME: where did these entries come from ? -- FR */
1991 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1992 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1993
1994 /* 8110 family. */
1995 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1996 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1997 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1998 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1999 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2000 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2001
Jean Delvaref21b75e2009-05-26 20:54:48 -07002002 /* Catch-all */
2003 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002004 };
2005 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 u32 reg;
2007
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002008 reg = RTL_R32(TxConfig);
2009 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 p++;
2011 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002012
2013 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2014 netif_notice(tp, probe, dev,
2015 "unknown MAC, using family default\n");
2016 tp->mac_version = default_version;
2017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018}
2019
2020static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2021{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002022 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
2024
Francois Romieu867763c2007-08-17 18:21:58 +02002025struct phy_reg {
2026 u16 reg;
2027 u16 val;
2028};
2029
françois romieu4da19632011-01-03 15:07:55 +00002030static void rtl_writephy_batch(struct rtl8169_private *tp,
2031 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002032{
2033 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002034 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002035 regs++;
2036 }
2037}
2038
françois romieubca03d52011-01-03 15:07:31 +00002039#define PHY_READ 0x00000000
2040#define PHY_DATA_OR 0x10000000
2041#define PHY_DATA_AND 0x20000000
2042#define PHY_BJMPN 0x30000000
2043#define PHY_READ_EFUSE 0x40000000
2044#define PHY_READ_MAC_BYTE 0x50000000
2045#define PHY_WRITE_MAC_BYTE 0x60000000
2046#define PHY_CLEAR_READCOUNT 0x70000000
2047#define PHY_WRITE 0x80000000
2048#define PHY_READCOUNT_EQ_SKIP 0x90000000
2049#define PHY_COMP_EQ_SKIPN 0xa0000000
2050#define PHY_COMP_NEQ_SKIPN 0xb0000000
2051#define PHY_WRITE_PREVIOUS 0xc0000000
2052#define PHY_SKIPN 0xd0000000
2053#define PHY_DELAY_MS 0xe0000000
2054#define PHY_WRITE_ERI_WORD 0xf0000000
2055
Hayes Wang960aee62011-06-18 11:37:48 +02002056struct fw_info {
2057 u32 magic;
2058 char version[RTL_VER_SIZE];
2059 __le32 fw_start;
2060 __le32 fw_len;
2061 u8 chksum;
2062} __packed;
2063
Francois Romieu1c361ef2011-06-17 17:16:24 +02002064#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2065
2066static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002067{
Francois Romieub6ffd972011-06-17 17:00:05 +02002068 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002069 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002070 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2071 char *version = rtl_fw->version;
2072 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002073
Francois Romieu1c361ef2011-06-17 17:16:24 +02002074 if (fw->size < FW_OPCODE_SIZE)
2075 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002076
2077 if (!fw_info->magic) {
2078 size_t i, size, start;
2079 u8 checksum = 0;
2080
2081 if (fw->size < sizeof(*fw_info))
2082 goto out;
2083
2084 for (i = 0; i < fw->size; i++)
2085 checksum += fw->data[i];
2086 if (checksum != 0)
2087 goto out;
2088
2089 start = le32_to_cpu(fw_info->fw_start);
2090 if (start > fw->size)
2091 goto out;
2092
2093 size = le32_to_cpu(fw_info->fw_len);
2094 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2095 goto out;
2096
2097 memcpy(version, fw_info->version, RTL_VER_SIZE);
2098
2099 pa->code = (__le32 *)(fw->data + start);
2100 pa->size = size;
2101 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002102 if (fw->size % FW_OPCODE_SIZE)
2103 goto out;
2104
2105 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2106
2107 pa->code = (__le32 *)fw->data;
2108 pa->size = fw->size / FW_OPCODE_SIZE;
2109 }
2110 version[RTL_VER_SIZE - 1] = 0;
2111
2112 rc = true;
2113out:
2114 return rc;
2115}
2116
Francois Romieufd112f22011-06-18 00:10:29 +02002117static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2118 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002119{
Francois Romieufd112f22011-06-18 00:10:29 +02002120 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002121 size_t index;
2122
Francois Romieu1c361ef2011-06-17 17:16:24 +02002123 for (index = 0; index < pa->size; index++) {
2124 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002125 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002126
hayeswang42b82dc2011-01-10 02:07:25 +00002127 switch(action & 0xf0000000) {
2128 case PHY_READ:
2129 case PHY_DATA_OR:
2130 case PHY_DATA_AND:
2131 case PHY_READ_EFUSE:
2132 case PHY_CLEAR_READCOUNT:
2133 case PHY_WRITE:
2134 case PHY_WRITE_PREVIOUS:
2135 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002136 break;
2137
hayeswang42b82dc2011-01-10 02:07:25 +00002138 case PHY_BJMPN:
2139 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002140 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002141 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002142 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002143 }
2144 break;
2145 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002146 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002147 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002148 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002149 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002150 }
2151 break;
2152 case PHY_COMP_EQ_SKIPN:
2153 case PHY_COMP_NEQ_SKIPN:
2154 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002155 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002156 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002157 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002158 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002159 }
2160 break;
2161
2162 case PHY_READ_MAC_BYTE:
2163 case PHY_WRITE_MAC_BYTE:
2164 case PHY_WRITE_ERI_WORD:
2165 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002166 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002167 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002168 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002169 }
2170 }
Francois Romieufd112f22011-06-18 00:10:29 +02002171 rc = true;
2172out:
2173 return rc;
2174}
françois romieubca03d52011-01-03 15:07:31 +00002175
Francois Romieufd112f22011-06-18 00:10:29 +02002176static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2177{
2178 struct net_device *dev = tp->dev;
2179 int rc = -EINVAL;
2180
2181 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2182 netif_err(tp, ifup, dev, "invalid firwmare\n");
2183 goto out;
2184 }
2185
2186 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2187 rc = 0;
2188out:
2189 return rc;
2190}
2191
2192static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2193{
2194 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2195 u32 predata, count;
2196 size_t index;
2197
2198 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002199
Francois Romieu1c361ef2011-06-17 17:16:24 +02002200 for (index = 0; index < pa->size; ) {
2201 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002202 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002203 u32 regno = (action & 0x0fff0000) >> 16;
2204
2205 if (!action)
2206 break;
françois romieubca03d52011-01-03 15:07:31 +00002207
2208 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002209 case PHY_READ:
2210 predata = rtl_readphy(tp, regno);
2211 count++;
2212 index++;
françois romieubca03d52011-01-03 15:07:31 +00002213 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002214 case PHY_DATA_OR:
2215 predata |= data;
2216 index++;
2217 break;
2218 case PHY_DATA_AND:
2219 predata &= data;
2220 index++;
2221 break;
2222 case PHY_BJMPN:
2223 index -= regno;
2224 break;
2225 case PHY_READ_EFUSE:
Francois Romieufdf6fc02012-07-06 22:40:38 +02002226 predata = rtl8168d_efuse_read(tp, regno);
hayeswang42b82dc2011-01-10 02:07:25 +00002227 index++;
2228 break;
2229 case PHY_CLEAR_READCOUNT:
2230 count = 0;
2231 index++;
2232 break;
2233 case PHY_WRITE:
2234 rtl_writephy(tp, regno, data);
2235 index++;
2236 break;
2237 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002238 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002239 break;
2240 case PHY_COMP_EQ_SKIPN:
2241 if (predata == data)
2242 index += regno;
2243 index++;
2244 break;
2245 case PHY_COMP_NEQ_SKIPN:
2246 if (predata != data)
2247 index += regno;
2248 index++;
2249 break;
2250 case PHY_WRITE_PREVIOUS:
2251 rtl_writephy(tp, regno, predata);
2252 index++;
2253 break;
2254 case PHY_SKIPN:
2255 index += regno + 1;
2256 break;
2257 case PHY_DELAY_MS:
2258 mdelay(data);
2259 index++;
2260 break;
2261
2262 case PHY_READ_MAC_BYTE:
2263 case PHY_WRITE_MAC_BYTE:
2264 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002265 default:
2266 BUG();
2267 }
2268 }
2269}
2270
françois romieuf1e02ed2011-01-13 13:07:53 +00002271static void rtl_release_firmware(struct rtl8169_private *tp)
2272{
Francois Romieub6ffd972011-06-17 17:00:05 +02002273 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2274 release_firmware(tp->rtl_fw->fw);
2275 kfree(tp->rtl_fw);
2276 }
2277 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002278}
2279
François Romieu953a12c2011-04-24 17:38:48 +02002280static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002281{
Francois Romieub6ffd972011-06-17 17:00:05 +02002282 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002283
2284 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002285 if (!IS_ERR_OR_NULL(rtl_fw))
2286 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002287}
2288
2289static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2290{
2291 if (rtl_readphy(tp, reg) != val)
2292 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2293 else
2294 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002295}
2296
françois romieu4da19632011-01-03 15:07:55 +00002297static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002299 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002300 { 0x1f, 0x0001 },
2301 { 0x06, 0x006e },
2302 { 0x08, 0x0708 },
2303 { 0x15, 0x4000 },
2304 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
françois romieu0b9b5712009-08-10 19:44:56 +00002306 { 0x1f, 0x0001 },
2307 { 0x03, 0x00a1 },
2308 { 0x02, 0x0008 },
2309 { 0x01, 0x0120 },
2310 { 0x00, 0x1000 },
2311 { 0x04, 0x0800 },
2312 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
françois romieu0b9b5712009-08-10 19:44:56 +00002314 { 0x03, 0xff41 },
2315 { 0x02, 0xdf60 },
2316 { 0x01, 0x0140 },
2317 { 0x00, 0x0077 },
2318 { 0x04, 0x7800 },
2319 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
françois romieu0b9b5712009-08-10 19:44:56 +00002321 { 0x03, 0x802f },
2322 { 0x02, 0x4f02 },
2323 { 0x01, 0x0409 },
2324 { 0x00, 0xf0f9 },
2325 { 0x04, 0x9800 },
2326 { 0x04, 0x9000 },
2327
2328 { 0x03, 0xdf01 },
2329 { 0x02, 0xdf20 },
2330 { 0x01, 0xff95 },
2331 { 0x00, 0xba00 },
2332 { 0x04, 0xa800 },
2333 { 0x04, 0xa000 },
2334
2335 { 0x03, 0xff41 },
2336 { 0x02, 0xdf20 },
2337 { 0x01, 0x0140 },
2338 { 0x00, 0x00bb },
2339 { 0x04, 0xb800 },
2340 { 0x04, 0xb000 },
2341
2342 { 0x03, 0xdf41 },
2343 { 0x02, 0xdc60 },
2344 { 0x01, 0x6340 },
2345 { 0x00, 0x007d },
2346 { 0x04, 0xd800 },
2347 { 0x04, 0xd000 },
2348
2349 { 0x03, 0xdf01 },
2350 { 0x02, 0xdf20 },
2351 { 0x01, 0x100a },
2352 { 0x00, 0xa0ff },
2353 { 0x04, 0xf800 },
2354 { 0x04, 0xf000 },
2355
2356 { 0x1f, 0x0000 },
2357 { 0x0b, 0x0000 },
2358 { 0x00, 0x9200 }
2359 };
2360
françois romieu4da19632011-01-03 15:07:55 +00002361 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362}
2363
françois romieu4da19632011-01-03 15:07:55 +00002364static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002365{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002366 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002367 { 0x1f, 0x0002 },
2368 { 0x01, 0x90d0 },
2369 { 0x1f, 0x0000 }
2370 };
2371
françois romieu4da19632011-01-03 15:07:55 +00002372 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002373}
2374
françois romieu4da19632011-01-03 15:07:55 +00002375static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002376{
2377 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002378
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002379 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2380 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002381 return;
2382
françois romieu4da19632011-01-03 15:07:55 +00002383 rtl_writephy(tp, 0x1f, 0x0001);
2384 rtl_writephy(tp, 0x10, 0xf01b);
2385 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002386}
2387
françois romieu4da19632011-01-03 15:07:55 +00002388static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002389{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002390 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002391 { 0x1f, 0x0001 },
2392 { 0x04, 0x0000 },
2393 { 0x03, 0x00a1 },
2394 { 0x02, 0x0008 },
2395 { 0x01, 0x0120 },
2396 { 0x00, 0x1000 },
2397 { 0x04, 0x0800 },
2398 { 0x04, 0x9000 },
2399 { 0x03, 0x802f },
2400 { 0x02, 0x4f02 },
2401 { 0x01, 0x0409 },
2402 { 0x00, 0xf099 },
2403 { 0x04, 0x9800 },
2404 { 0x04, 0xa000 },
2405 { 0x03, 0xdf01 },
2406 { 0x02, 0xdf20 },
2407 { 0x01, 0xff95 },
2408 { 0x00, 0xba00 },
2409 { 0x04, 0xa800 },
2410 { 0x04, 0xf000 },
2411 { 0x03, 0xdf01 },
2412 { 0x02, 0xdf20 },
2413 { 0x01, 0x101a },
2414 { 0x00, 0xa0ff },
2415 { 0x04, 0xf800 },
2416 { 0x04, 0x0000 },
2417 { 0x1f, 0x0000 },
2418
2419 { 0x1f, 0x0001 },
2420 { 0x10, 0xf41b },
2421 { 0x14, 0xfb54 },
2422 { 0x18, 0xf5c7 },
2423 { 0x1f, 0x0000 },
2424
2425 { 0x1f, 0x0001 },
2426 { 0x17, 0x0cc0 },
2427 { 0x1f, 0x0000 }
2428 };
2429
françois romieu4da19632011-01-03 15:07:55 +00002430 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002431
françois romieu4da19632011-01-03 15:07:55 +00002432 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002433}
2434
françois romieu4da19632011-01-03 15:07:55 +00002435static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002436{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002437 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002438 { 0x1f, 0x0001 },
2439 { 0x04, 0x0000 },
2440 { 0x03, 0x00a1 },
2441 { 0x02, 0x0008 },
2442 { 0x01, 0x0120 },
2443 { 0x00, 0x1000 },
2444 { 0x04, 0x0800 },
2445 { 0x04, 0x9000 },
2446 { 0x03, 0x802f },
2447 { 0x02, 0x4f02 },
2448 { 0x01, 0x0409 },
2449 { 0x00, 0xf099 },
2450 { 0x04, 0x9800 },
2451 { 0x04, 0xa000 },
2452 { 0x03, 0xdf01 },
2453 { 0x02, 0xdf20 },
2454 { 0x01, 0xff95 },
2455 { 0x00, 0xba00 },
2456 { 0x04, 0xa800 },
2457 { 0x04, 0xf000 },
2458 { 0x03, 0xdf01 },
2459 { 0x02, 0xdf20 },
2460 { 0x01, 0x101a },
2461 { 0x00, 0xa0ff },
2462 { 0x04, 0xf800 },
2463 { 0x04, 0x0000 },
2464 { 0x1f, 0x0000 },
2465
2466 { 0x1f, 0x0001 },
2467 { 0x0b, 0x8480 },
2468 { 0x1f, 0x0000 },
2469
2470 { 0x1f, 0x0001 },
2471 { 0x18, 0x67c7 },
2472 { 0x04, 0x2000 },
2473 { 0x03, 0x002f },
2474 { 0x02, 0x4360 },
2475 { 0x01, 0x0109 },
2476 { 0x00, 0x3022 },
2477 { 0x04, 0x2800 },
2478 { 0x1f, 0x0000 },
2479
2480 { 0x1f, 0x0001 },
2481 { 0x17, 0x0cc0 },
2482 { 0x1f, 0x0000 }
2483 };
2484
françois romieu4da19632011-01-03 15:07:55 +00002485 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002486}
2487
françois romieu4da19632011-01-03 15:07:55 +00002488static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002489{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002490 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002491 { 0x10, 0xf41b },
2492 { 0x1f, 0x0000 }
2493 };
2494
françois romieu4da19632011-01-03 15:07:55 +00002495 rtl_writephy(tp, 0x1f, 0x0001);
2496 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002497
françois romieu4da19632011-01-03 15:07:55 +00002498 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002499}
2500
françois romieu4da19632011-01-03 15:07:55 +00002501static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002502{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002503 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002504 { 0x1f, 0x0001 },
2505 { 0x10, 0xf41b },
2506 { 0x1f, 0x0000 }
2507 };
2508
françois romieu4da19632011-01-03 15:07:55 +00002509 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002510}
2511
françois romieu4da19632011-01-03 15:07:55 +00002512static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002513{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002514 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002515 { 0x1f, 0x0000 },
2516 { 0x1d, 0x0f00 },
2517 { 0x1f, 0x0002 },
2518 { 0x0c, 0x1ec8 },
2519 { 0x1f, 0x0000 }
2520 };
2521
françois romieu4da19632011-01-03 15:07:55 +00002522 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002523}
2524
françois romieu4da19632011-01-03 15:07:55 +00002525static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002526{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002527 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002528 { 0x1f, 0x0001 },
2529 { 0x1d, 0x3d98 },
2530 { 0x1f, 0x0000 }
2531 };
2532
françois romieu4da19632011-01-03 15:07:55 +00002533 rtl_writephy(tp, 0x1f, 0x0000);
2534 rtl_patchphy(tp, 0x14, 1 << 5);
2535 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002536
françois romieu4da19632011-01-03 15:07:55 +00002537 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002538}
2539
françois romieu4da19632011-01-03 15:07:55 +00002540static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002541{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002542 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002543 { 0x1f, 0x0001 },
2544 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002545 { 0x1f, 0x0002 },
2546 { 0x00, 0x88d4 },
2547 { 0x01, 0x82b1 },
2548 { 0x03, 0x7002 },
2549 { 0x08, 0x9e30 },
2550 { 0x09, 0x01f0 },
2551 { 0x0a, 0x5500 },
2552 { 0x0c, 0x00c8 },
2553 { 0x1f, 0x0003 },
2554 { 0x12, 0xc096 },
2555 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002556 { 0x1f, 0x0000 },
2557 { 0x1f, 0x0000 },
2558 { 0x09, 0x2000 },
2559 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002560 };
2561
françois romieu4da19632011-01-03 15:07:55 +00002562 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002563
françois romieu4da19632011-01-03 15:07:55 +00002564 rtl_patchphy(tp, 0x14, 1 << 5);
2565 rtl_patchphy(tp, 0x0d, 1 << 5);
2566 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002567}
2568
françois romieu4da19632011-01-03 15:07:55 +00002569static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002570{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002571 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002572 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002573 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002574 { 0x03, 0x802f },
2575 { 0x02, 0x4f02 },
2576 { 0x01, 0x0409 },
2577 { 0x00, 0xf099 },
2578 { 0x04, 0x9800 },
2579 { 0x04, 0x9000 },
2580 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002581 { 0x1f, 0x0002 },
2582 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002583 { 0x06, 0x0761 },
2584 { 0x1f, 0x0003 },
2585 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002586 { 0x1f, 0x0000 }
2587 };
2588
françois romieu4da19632011-01-03 15:07:55 +00002589 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002590
françois romieu4da19632011-01-03 15:07:55 +00002591 rtl_patchphy(tp, 0x16, 1 << 0);
2592 rtl_patchphy(tp, 0x14, 1 << 5);
2593 rtl_patchphy(tp, 0x0d, 1 << 5);
2594 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002595}
2596
françois romieu4da19632011-01-03 15:07:55 +00002597static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002598{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002599 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002600 { 0x1f, 0x0001 },
2601 { 0x12, 0x2300 },
2602 { 0x1d, 0x3d98 },
2603 { 0x1f, 0x0002 },
2604 { 0x0c, 0x7eb8 },
2605 { 0x06, 0x5461 },
2606 { 0x1f, 0x0003 },
2607 { 0x16, 0x0f0a },
2608 { 0x1f, 0x0000 }
2609 };
2610
françois romieu4da19632011-01-03 15:07:55 +00002611 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002612
françois romieu4da19632011-01-03 15:07:55 +00002613 rtl_patchphy(tp, 0x16, 1 << 0);
2614 rtl_patchphy(tp, 0x14, 1 << 5);
2615 rtl_patchphy(tp, 0x0d, 1 << 5);
2616 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002617}
2618
françois romieu4da19632011-01-03 15:07:55 +00002619static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002620{
françois romieu4da19632011-01-03 15:07:55 +00002621 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002622}
2623
françois romieubca03d52011-01-03 15:07:31 +00002624static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002625{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002626 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002627 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002628 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002629 { 0x06, 0x4064 },
2630 { 0x07, 0x2863 },
2631 { 0x08, 0x059c },
2632 { 0x09, 0x26b4 },
2633 { 0x0a, 0x6a19 },
2634 { 0x0b, 0xdcc8 },
2635 { 0x10, 0xf06d },
2636 { 0x14, 0x7f68 },
2637 { 0x18, 0x7fd9 },
2638 { 0x1c, 0xf0ff },
2639 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002640 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002641 { 0x12, 0xf49f },
2642 { 0x13, 0x070b },
2643 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002644 { 0x14, 0x94c0 },
2645
2646 /*
2647 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002648 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002649 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002650 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002651 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002652 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002653 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002654 { 0x06, 0x5561 },
2655
2656 /*
2657 * Can not link to 1Gbps with bad cable
2658 * Decrease SNR threshold form 21.07dB to 19.04dB
2659 */
2660 { 0x1f, 0x0001 },
2661 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002662
2663 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002664 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002665 };
2666
françois romieu4da19632011-01-03 15:07:55 +00002667 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002668
françois romieubca03d52011-01-03 15:07:31 +00002669 /*
2670 * Rx Error Issue
2671 * Fine Tune Switching regulator parameter
2672 */
françois romieu4da19632011-01-03 15:07:55 +00002673 rtl_writephy(tp, 0x1f, 0x0002);
2674 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2675 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002676
Francois Romieufdf6fc02012-07-06 22:40:38 +02002677 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002678 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002679 { 0x1f, 0x0002 },
2680 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002681 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002682 { 0x05, 0x8330 },
2683 { 0x06, 0x669a },
2684 { 0x1f, 0x0002 }
2685 };
2686 int val;
2687
françois romieu4da19632011-01-03 15:07:55 +00002688 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002689
françois romieu4da19632011-01-03 15:07:55 +00002690 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002691
2692 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002693 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002694 0x0065, 0x0066, 0x0067, 0x0068,
2695 0x0069, 0x006a, 0x006b, 0x006c
2696 };
2697 int i;
2698
françois romieu4da19632011-01-03 15:07:55 +00002699 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002700
2701 val &= 0xff00;
2702 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002703 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002704 }
2705 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002706 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002707 { 0x1f, 0x0002 },
2708 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002709 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002710 { 0x05, 0x8330 },
2711 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002712 };
2713
françois romieu4da19632011-01-03 15:07:55 +00002714 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002715 }
2716
françois romieubca03d52011-01-03 15:07:31 +00002717 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002718 rtl_writephy(tp, 0x1f, 0x0002);
2719 rtl_patchphy(tp, 0x0d, 0x0300);
2720 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002721
françois romieubca03d52011-01-03 15:07:31 +00002722 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002723 rtl_writephy(tp, 0x1f, 0x0002);
2724 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2725 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002726
françois romieu4da19632011-01-03 15:07:55 +00002727 rtl_writephy(tp, 0x1f, 0x0005);
2728 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002729
2730 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002731
françois romieu4da19632011-01-03 15:07:55 +00002732 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002733}
2734
françois romieubca03d52011-01-03 15:07:31 +00002735static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002736{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002737 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002738 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002739 { 0x1f, 0x0001 },
2740 { 0x06, 0x4064 },
2741 { 0x07, 0x2863 },
2742 { 0x08, 0x059c },
2743 { 0x09, 0x26b4 },
2744 { 0x0a, 0x6a19 },
2745 { 0x0b, 0xdcc8 },
2746 { 0x10, 0xf06d },
2747 { 0x14, 0x7f68 },
2748 { 0x18, 0x7fd9 },
2749 { 0x1c, 0xf0ff },
2750 { 0x1d, 0x3d9c },
2751 { 0x1f, 0x0003 },
2752 { 0x12, 0xf49f },
2753 { 0x13, 0x070b },
2754 { 0x1a, 0x05ad },
2755 { 0x14, 0x94c0 },
2756
françois romieubca03d52011-01-03 15:07:31 +00002757 /*
2758 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002759 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002760 */
françois romieudaf9df62009-10-07 12:44:20 +00002761 { 0x1f, 0x0002 },
2762 { 0x06, 0x5561 },
2763 { 0x1f, 0x0005 },
2764 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002765 { 0x06, 0x5561 },
2766
2767 /*
2768 * Can not link to 1Gbps with bad cable
2769 * Decrease SNR threshold form 21.07dB to 19.04dB
2770 */
2771 { 0x1f, 0x0001 },
2772 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002773
2774 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002775 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002776 };
2777
françois romieu4da19632011-01-03 15:07:55 +00002778 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002779
Francois Romieufdf6fc02012-07-06 22:40:38 +02002780 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002781 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002782 { 0x1f, 0x0002 },
2783 { 0x05, 0x669a },
2784 { 0x1f, 0x0005 },
2785 { 0x05, 0x8330 },
2786 { 0x06, 0x669a },
2787
2788 { 0x1f, 0x0002 }
2789 };
2790 int val;
2791
françois romieu4da19632011-01-03 15:07:55 +00002792 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002793
françois romieu4da19632011-01-03 15:07:55 +00002794 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002795 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002796 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002797 0x0065, 0x0066, 0x0067, 0x0068,
2798 0x0069, 0x006a, 0x006b, 0x006c
2799 };
2800 int i;
2801
françois romieu4da19632011-01-03 15:07:55 +00002802 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002803
2804 val &= 0xff00;
2805 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002806 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002807 }
2808 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002809 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002810 { 0x1f, 0x0002 },
2811 { 0x05, 0x2642 },
2812 { 0x1f, 0x0005 },
2813 { 0x05, 0x8330 },
2814 { 0x06, 0x2642 }
2815 };
2816
françois romieu4da19632011-01-03 15:07:55 +00002817 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002818 }
2819
françois romieubca03d52011-01-03 15:07:31 +00002820 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002821 rtl_writephy(tp, 0x1f, 0x0002);
2822 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2823 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002824
françois romieubca03d52011-01-03 15:07:31 +00002825 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002826 rtl_writephy(tp, 0x1f, 0x0002);
2827 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002828
françois romieu4da19632011-01-03 15:07:55 +00002829 rtl_writephy(tp, 0x1f, 0x0005);
2830 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002831
2832 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002833
françois romieu4da19632011-01-03 15:07:55 +00002834 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002835}
2836
françois romieu4da19632011-01-03 15:07:55 +00002837static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002838{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002839 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002840 { 0x1f, 0x0002 },
2841 { 0x10, 0x0008 },
2842 { 0x0d, 0x006c },
2843
2844 { 0x1f, 0x0000 },
2845 { 0x0d, 0xf880 },
2846
2847 { 0x1f, 0x0001 },
2848 { 0x17, 0x0cc0 },
2849
2850 { 0x1f, 0x0001 },
2851 { 0x0b, 0xa4d8 },
2852 { 0x09, 0x281c },
2853 { 0x07, 0x2883 },
2854 { 0x0a, 0x6b35 },
2855 { 0x1d, 0x3da4 },
2856 { 0x1c, 0xeffd },
2857 { 0x14, 0x7f52 },
2858 { 0x18, 0x7fc6 },
2859 { 0x08, 0x0601 },
2860 { 0x06, 0x4063 },
2861 { 0x10, 0xf074 },
2862 { 0x1f, 0x0003 },
2863 { 0x13, 0x0789 },
2864 { 0x12, 0xf4bd },
2865 { 0x1a, 0x04fd },
2866 { 0x14, 0x84b0 },
2867 { 0x1f, 0x0000 },
2868 { 0x00, 0x9200 },
2869
2870 { 0x1f, 0x0005 },
2871 { 0x01, 0x0340 },
2872 { 0x1f, 0x0001 },
2873 { 0x04, 0x4000 },
2874 { 0x03, 0x1d21 },
2875 { 0x02, 0x0c32 },
2876 { 0x01, 0x0200 },
2877 { 0x00, 0x5554 },
2878 { 0x04, 0x4800 },
2879 { 0x04, 0x4000 },
2880 { 0x04, 0xf000 },
2881 { 0x03, 0xdf01 },
2882 { 0x02, 0xdf20 },
2883 { 0x01, 0x101a },
2884 { 0x00, 0xa0ff },
2885 { 0x04, 0xf800 },
2886 { 0x04, 0xf000 },
2887 { 0x1f, 0x0000 },
2888
2889 { 0x1f, 0x0007 },
2890 { 0x1e, 0x0023 },
2891 { 0x16, 0x0000 },
2892 { 0x1f, 0x0000 }
2893 };
2894
françois romieu4da19632011-01-03 15:07:55 +00002895 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002896}
2897
françois romieue6de30d2011-01-03 15:08:37 +00002898static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2899{
2900 static const struct phy_reg phy_reg_init[] = {
2901 { 0x1f, 0x0001 },
2902 { 0x17, 0x0cc0 },
2903
2904 { 0x1f, 0x0007 },
2905 { 0x1e, 0x002d },
2906 { 0x18, 0x0040 },
2907 { 0x1f, 0x0000 }
2908 };
2909
2910 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2911 rtl_patchphy(tp, 0x0d, 1 << 5);
2912}
2913
Hayes Wang70090422011-07-06 15:58:06 +08002914static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002915{
2916 static const struct phy_reg phy_reg_init[] = {
2917 /* Enable Delay cap */
2918 { 0x1f, 0x0005 },
2919 { 0x05, 0x8b80 },
2920 { 0x06, 0xc896 },
2921 { 0x1f, 0x0000 },
2922
2923 /* Channel estimation fine tune */
2924 { 0x1f, 0x0001 },
2925 { 0x0b, 0x6c20 },
2926 { 0x07, 0x2872 },
2927 { 0x1c, 0xefff },
2928 { 0x1f, 0x0003 },
2929 { 0x14, 0x6420 },
2930 { 0x1f, 0x0000 },
2931
2932 /* Update PFM & 10M TX idle timer */
2933 { 0x1f, 0x0007 },
2934 { 0x1e, 0x002f },
2935 { 0x15, 0x1919 },
2936 { 0x1f, 0x0000 },
2937
2938 { 0x1f, 0x0007 },
2939 { 0x1e, 0x00ac },
2940 { 0x18, 0x0006 },
2941 { 0x1f, 0x0000 }
2942 };
2943
Francois Romieu15ecd032011-04-27 13:52:22 -07002944 rtl_apply_firmware(tp);
2945
hayeswang01dc7fe2011-03-21 01:50:28 +00002946 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2947
2948 /* DCO enable for 10M IDLE Power */
2949 rtl_writephy(tp, 0x1f, 0x0007);
2950 rtl_writephy(tp, 0x1e, 0x0023);
2951 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2952 rtl_writephy(tp, 0x1f, 0x0000);
2953
2954 /* For impedance matching */
2955 rtl_writephy(tp, 0x1f, 0x0002);
2956 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002957 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002958
2959 /* PHY auto speed down */
2960 rtl_writephy(tp, 0x1f, 0x0007);
2961 rtl_writephy(tp, 0x1e, 0x002d);
2962 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2963 rtl_writephy(tp, 0x1f, 0x0000);
2964 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2965
2966 rtl_writephy(tp, 0x1f, 0x0005);
2967 rtl_writephy(tp, 0x05, 0x8b86);
2968 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2969 rtl_writephy(tp, 0x1f, 0x0000);
2970
2971 rtl_writephy(tp, 0x1f, 0x0005);
2972 rtl_writephy(tp, 0x05, 0x8b85);
2973 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2974 rtl_writephy(tp, 0x1f, 0x0007);
2975 rtl_writephy(tp, 0x1e, 0x0020);
2976 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2977 rtl_writephy(tp, 0x1f, 0x0006);
2978 rtl_writephy(tp, 0x00, 0x5a00);
2979 rtl_writephy(tp, 0x1f, 0x0000);
2980 rtl_writephy(tp, 0x0d, 0x0007);
2981 rtl_writephy(tp, 0x0e, 0x003c);
2982 rtl_writephy(tp, 0x0d, 0x4007);
2983 rtl_writephy(tp, 0x0e, 0x0000);
2984 rtl_writephy(tp, 0x0d, 0x0000);
2985}
2986
Hayes Wang70090422011-07-06 15:58:06 +08002987static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2988{
2989 static const struct phy_reg phy_reg_init[] = {
2990 /* Enable Delay cap */
2991 { 0x1f, 0x0004 },
2992 { 0x1f, 0x0007 },
2993 { 0x1e, 0x00ac },
2994 { 0x18, 0x0006 },
2995 { 0x1f, 0x0002 },
2996 { 0x1f, 0x0000 },
2997 { 0x1f, 0x0000 },
2998
2999 /* Channel estimation fine tune */
3000 { 0x1f, 0x0003 },
3001 { 0x09, 0xa20f },
3002 { 0x1f, 0x0000 },
3003 { 0x1f, 0x0000 },
3004
3005 /* Green Setting */
3006 { 0x1f, 0x0005 },
3007 { 0x05, 0x8b5b },
3008 { 0x06, 0x9222 },
3009 { 0x05, 0x8b6d },
3010 { 0x06, 0x8000 },
3011 { 0x05, 0x8b76 },
3012 { 0x06, 0x8000 },
3013 { 0x1f, 0x0000 }
3014 };
3015
3016 rtl_apply_firmware(tp);
3017
3018 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3019
3020 /* For 4-corner performance improve */
3021 rtl_writephy(tp, 0x1f, 0x0005);
3022 rtl_writephy(tp, 0x05, 0x8b80);
3023 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
3024 rtl_writephy(tp, 0x1f, 0x0000);
3025
3026 /* PHY auto speed down */
3027 rtl_writephy(tp, 0x1f, 0x0004);
3028 rtl_writephy(tp, 0x1f, 0x0007);
3029 rtl_writephy(tp, 0x1e, 0x002d);
3030 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3031 rtl_writephy(tp, 0x1f, 0x0002);
3032 rtl_writephy(tp, 0x1f, 0x0000);
3033 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3034
3035 /* improve 10M EEE waveform */
3036 rtl_writephy(tp, 0x1f, 0x0005);
3037 rtl_writephy(tp, 0x05, 0x8b86);
3038 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3039 rtl_writephy(tp, 0x1f, 0x0000);
3040
3041 /* Improve 2-pair detection performance */
3042 rtl_writephy(tp, 0x1f, 0x0005);
3043 rtl_writephy(tp, 0x05, 0x8b85);
3044 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3045 rtl_writephy(tp, 0x1f, 0x0000);
3046
3047 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003048 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003049 rtl_writephy(tp, 0x1f, 0x0005);
3050 rtl_writephy(tp, 0x05, 0x8b85);
3051 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3052 rtl_writephy(tp, 0x1f, 0x0004);
3053 rtl_writephy(tp, 0x1f, 0x0007);
3054 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04003055 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003056 rtl_writephy(tp, 0x1f, 0x0002);
3057 rtl_writephy(tp, 0x1f, 0x0000);
3058 rtl_writephy(tp, 0x0d, 0x0007);
3059 rtl_writephy(tp, 0x0e, 0x003c);
3060 rtl_writephy(tp, 0x0d, 0x4007);
3061 rtl_writephy(tp, 0x0e, 0x0000);
3062 rtl_writephy(tp, 0x0d, 0x0000);
3063
3064 /* Green feature */
3065 rtl_writephy(tp, 0x1f, 0x0003);
3066 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3067 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3068 rtl_writephy(tp, 0x1f, 0x0000);
3069}
3070
Hayes Wang5f886e02012-03-30 14:33:03 +08003071static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3072{
3073 /* For 4-corner performance improve */
3074 rtl_writephy(tp, 0x1f, 0x0005);
3075 rtl_writephy(tp, 0x05, 0x8b80);
3076 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3077 rtl_writephy(tp, 0x1f, 0x0000);
3078
3079 /* PHY auto speed down */
3080 rtl_writephy(tp, 0x1f, 0x0007);
3081 rtl_writephy(tp, 0x1e, 0x002d);
3082 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3083 rtl_writephy(tp, 0x1f, 0x0000);
3084 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3085
3086 /* Improve 10M EEE waveform */
3087 rtl_writephy(tp, 0x1f, 0x0005);
3088 rtl_writephy(tp, 0x05, 0x8b86);
3089 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3090 rtl_writephy(tp, 0x1f, 0x0000);
3091}
3092
Hayes Wangc2218922011-09-06 16:55:18 +08003093static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3094{
3095 static const struct phy_reg phy_reg_init[] = {
3096 /* Channel estimation fine tune */
3097 { 0x1f, 0x0003 },
3098 { 0x09, 0xa20f },
3099 { 0x1f, 0x0000 },
3100
3101 /* Modify green table for giga & fnet */
3102 { 0x1f, 0x0005 },
3103 { 0x05, 0x8b55 },
3104 { 0x06, 0x0000 },
3105 { 0x05, 0x8b5e },
3106 { 0x06, 0x0000 },
3107 { 0x05, 0x8b67 },
3108 { 0x06, 0x0000 },
3109 { 0x05, 0x8b70 },
3110 { 0x06, 0x0000 },
3111 { 0x1f, 0x0000 },
3112 { 0x1f, 0x0007 },
3113 { 0x1e, 0x0078 },
3114 { 0x17, 0x0000 },
3115 { 0x19, 0x00fb },
3116 { 0x1f, 0x0000 },
3117
3118 /* Modify green table for 10M */
3119 { 0x1f, 0x0005 },
3120 { 0x05, 0x8b79 },
3121 { 0x06, 0xaa00 },
3122 { 0x1f, 0x0000 },
3123
3124 /* Disable hiimpedance detection (RTCT) */
3125 { 0x1f, 0x0003 },
3126 { 0x01, 0x328a },
3127 { 0x1f, 0x0000 }
3128 };
3129
3130 rtl_apply_firmware(tp);
3131
3132 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3133
Hayes Wang5f886e02012-03-30 14:33:03 +08003134 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003135
3136 /* Improve 2-pair detection performance */
3137 rtl_writephy(tp, 0x1f, 0x0005);
3138 rtl_writephy(tp, 0x05, 0x8b85);
3139 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3140 rtl_writephy(tp, 0x1f, 0x0000);
3141}
3142
3143static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3144{
3145 rtl_apply_firmware(tp);
3146
Hayes Wang5f886e02012-03-30 14:33:03 +08003147 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003148}
3149
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003150static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3151{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003152 static const struct phy_reg phy_reg_init[] = {
3153 /* Channel estimation fine tune */
3154 { 0x1f, 0x0003 },
3155 { 0x09, 0xa20f },
3156 { 0x1f, 0x0000 },
3157
3158 /* Modify green table for giga & fnet */
3159 { 0x1f, 0x0005 },
3160 { 0x05, 0x8b55 },
3161 { 0x06, 0x0000 },
3162 { 0x05, 0x8b5e },
3163 { 0x06, 0x0000 },
3164 { 0x05, 0x8b67 },
3165 { 0x06, 0x0000 },
3166 { 0x05, 0x8b70 },
3167 { 0x06, 0x0000 },
3168 { 0x1f, 0x0000 },
3169 { 0x1f, 0x0007 },
3170 { 0x1e, 0x0078 },
3171 { 0x17, 0x0000 },
3172 { 0x19, 0x00aa },
3173 { 0x1f, 0x0000 },
3174
3175 /* Modify green table for 10M */
3176 { 0x1f, 0x0005 },
3177 { 0x05, 0x8b79 },
3178 { 0x06, 0xaa00 },
3179 { 0x1f, 0x0000 },
3180
3181 /* Disable hiimpedance detection (RTCT) */
3182 { 0x1f, 0x0003 },
3183 { 0x01, 0x328a },
3184 { 0x1f, 0x0000 }
3185 };
3186
3187
3188 rtl_apply_firmware(tp);
3189
3190 rtl8168f_hw_phy_config(tp);
3191
3192 /* Improve 2-pair detection performance */
3193 rtl_writephy(tp, 0x1f, 0x0005);
3194 rtl_writephy(tp, 0x05, 0x8b85);
3195 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3196 rtl_writephy(tp, 0x1f, 0x0000);
3197
3198 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3199
3200 /* Modify green table for giga */
3201 rtl_writephy(tp, 0x1f, 0x0005);
3202 rtl_writephy(tp, 0x05, 0x8b54);
3203 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3204 rtl_writephy(tp, 0x05, 0x8b5d);
3205 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3206 rtl_writephy(tp, 0x05, 0x8a7c);
3207 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3208 rtl_writephy(tp, 0x05, 0x8a7f);
3209 rtl_w1w0_phy(tp, 0x06, 0x0100, 0x0000);
3210 rtl_writephy(tp, 0x05, 0x8a82);
3211 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3212 rtl_writephy(tp, 0x05, 0x8a85);
3213 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3214 rtl_writephy(tp, 0x05, 0x8a88);
3215 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3216 rtl_writephy(tp, 0x1f, 0x0000);
3217
3218 /* uc same-seed solution */
3219 rtl_writephy(tp, 0x1f, 0x0005);
3220 rtl_writephy(tp, 0x05, 0x8b85);
3221 rtl_w1w0_phy(tp, 0x06, 0x8000, 0x0000);
3222 rtl_writephy(tp, 0x1f, 0x0000);
3223
3224 /* eee setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003225 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003226 rtl_writephy(tp, 0x1f, 0x0005);
3227 rtl_writephy(tp, 0x05, 0x8b85);
3228 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3229 rtl_writephy(tp, 0x1f, 0x0004);
3230 rtl_writephy(tp, 0x1f, 0x0007);
3231 rtl_writephy(tp, 0x1e, 0x0020);
3232 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
3233 rtl_writephy(tp, 0x1f, 0x0000);
3234 rtl_writephy(tp, 0x0d, 0x0007);
3235 rtl_writephy(tp, 0x0e, 0x003c);
3236 rtl_writephy(tp, 0x0d, 0x4007);
3237 rtl_writephy(tp, 0x0e, 0x0000);
3238 rtl_writephy(tp, 0x0d, 0x0000);
3239
3240 /* Green feature */
3241 rtl_writephy(tp, 0x1f, 0x0003);
3242 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3243 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3244 rtl_writephy(tp, 0x1f, 0x0000);
3245}
3246
françois romieu4da19632011-01-03 15:07:55 +00003247static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003248{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003249 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003250 { 0x1f, 0x0003 },
3251 { 0x08, 0x441d },
3252 { 0x01, 0x9100 },
3253 { 0x1f, 0x0000 }
3254 };
3255
françois romieu4da19632011-01-03 15:07:55 +00003256 rtl_writephy(tp, 0x1f, 0x0000);
3257 rtl_patchphy(tp, 0x11, 1 << 12);
3258 rtl_patchphy(tp, 0x19, 1 << 13);
3259 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003260
françois romieu4da19632011-01-03 15:07:55 +00003261 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003262}
3263
Hayes Wang5a5e4442011-02-22 17:26:21 +08003264static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3265{
3266 static const struct phy_reg phy_reg_init[] = {
3267 { 0x1f, 0x0005 },
3268 { 0x1a, 0x0000 },
3269 { 0x1f, 0x0000 },
3270
3271 { 0x1f, 0x0004 },
3272 { 0x1c, 0x0000 },
3273 { 0x1f, 0x0000 },
3274
3275 { 0x1f, 0x0001 },
3276 { 0x15, 0x7701 },
3277 { 0x1f, 0x0000 }
3278 };
3279
3280 /* Disable ALDPS before ram code */
3281 rtl_writephy(tp, 0x1f, 0x0000);
3282 rtl_writephy(tp, 0x18, 0x0310);
3283 msleep(100);
3284
François Romieu953a12c2011-04-24 17:38:48 +02003285 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003286
3287 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3288}
3289
Hayes Wang7e18dca2012-03-30 14:33:02 +08003290static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3291{
Hayes Wang7e18dca2012-03-30 14:33:02 +08003292 /* Disable ALDPS before setting firmware */
3293 rtl_writephy(tp, 0x1f, 0x0000);
3294 rtl_writephy(tp, 0x18, 0x0310);
3295 msleep(20);
3296
3297 rtl_apply_firmware(tp);
3298
3299 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003300 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003301 rtl_writephy(tp, 0x1f, 0x0004);
3302 rtl_writephy(tp, 0x10, 0x401f);
3303 rtl_writephy(tp, 0x19, 0x7030);
3304 rtl_writephy(tp, 0x1f, 0x0000);
3305}
3306
Hayes Wang5598bfe2012-07-02 17:23:21 +08003307static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3308{
Hayes Wang5598bfe2012-07-02 17:23:21 +08003309 static const struct phy_reg phy_reg_init[] = {
3310 { 0x1f, 0x0004 },
3311 { 0x10, 0xc07f },
3312 { 0x19, 0x7030 },
3313 { 0x1f, 0x0000 }
3314 };
3315
3316 /* Disable ALDPS before ram code */
3317 rtl_writephy(tp, 0x1f, 0x0000);
3318 rtl_writephy(tp, 0x18, 0x0310);
3319 msleep(100);
3320
3321 rtl_apply_firmware(tp);
3322
Francois Romieufdf6fc02012-07-06 22:40:38 +02003323 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003324 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3325
Francois Romieufdf6fc02012-07-06 22:40:38 +02003326 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003327}
3328
Francois Romieu5615d9f2007-08-17 17:50:46 +02003329static void rtl_hw_phy_config(struct net_device *dev)
3330{
3331 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003332
3333 rtl8169_print_mac_version(tp);
3334
3335 switch (tp->mac_version) {
3336 case RTL_GIGA_MAC_VER_01:
3337 break;
3338 case RTL_GIGA_MAC_VER_02:
3339 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003340 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003341 break;
3342 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003343 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003344 break;
françois romieu2e9558562009-08-10 19:44:19 +00003345 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003346 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003347 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003348 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003349 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003350 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003351 case RTL_GIGA_MAC_VER_07:
3352 case RTL_GIGA_MAC_VER_08:
3353 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003354 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003355 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003356 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003357 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003358 break;
3359 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003360 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003361 break;
3362 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003363 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003364 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003365 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003366 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003367 break;
3368 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003369 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003370 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003371 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003372 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003373 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003374 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003375 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003376 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003377 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003378 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003379 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003380 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003381 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003382 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003383 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003384 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003385 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003386 break;
3387 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003388 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003389 break;
3390 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003391 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003392 break;
françois romieue6de30d2011-01-03 15:08:37 +00003393 case RTL_GIGA_MAC_VER_28:
3394 rtl8168d_4_hw_phy_config(tp);
3395 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003396 case RTL_GIGA_MAC_VER_29:
3397 case RTL_GIGA_MAC_VER_30:
3398 rtl8105e_hw_phy_config(tp);
3399 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003400 case RTL_GIGA_MAC_VER_31:
3401 /* None. */
3402 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003403 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003404 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003405 rtl8168e_1_hw_phy_config(tp);
3406 break;
3407 case RTL_GIGA_MAC_VER_34:
3408 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003409 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003410 case RTL_GIGA_MAC_VER_35:
3411 rtl8168f_1_hw_phy_config(tp);
3412 break;
3413 case RTL_GIGA_MAC_VER_36:
3414 rtl8168f_2_hw_phy_config(tp);
3415 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003416
Hayes Wang7e18dca2012-03-30 14:33:02 +08003417 case RTL_GIGA_MAC_VER_37:
3418 rtl8402_hw_phy_config(tp);
3419 break;
3420
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003421 case RTL_GIGA_MAC_VER_38:
3422 rtl8411_hw_phy_config(tp);
3423 break;
3424
Hayes Wang5598bfe2012-07-02 17:23:21 +08003425 case RTL_GIGA_MAC_VER_39:
3426 rtl8106e_hw_phy_config(tp);
3427 break;
3428
Francois Romieu5615d9f2007-08-17 17:50:46 +02003429 default:
3430 break;
3431 }
3432}
3433
Francois Romieuda78dbf2012-01-26 14:18:23 +01003434static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 struct timer_list *timer = &tp->timer;
3437 void __iomem *ioaddr = tp->mmio_addr;
3438 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3439
Francois Romieubcf0bf92006-07-26 23:14:13 +02003440 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
françois romieu4da19632011-01-03 15:07:55 +00003442 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003443 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 * A busy loop could burn quite a few cycles on nowadays CPU.
3445 * Let's delay the execution of the timer for a few ticks.
3446 */
3447 timeout = HZ/10;
3448 goto out_mod_timer;
3449 }
3450
3451 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01003452 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
Francois Romieuda78dbf2012-01-26 14:18:23 +01003454 netif_warn(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
françois romieu4da19632011-01-03 15:07:55 +00003456 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
3458out_mod_timer:
3459 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003460}
3461
3462static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3463{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003464 if (!test_and_set_bit(flag, tp->wk.flags))
3465 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003466}
3467
3468static void rtl8169_phy_timer(unsigned long __opaque)
3469{
3470 struct net_device *dev = (struct net_device *)__opaque;
3471 struct rtl8169_private *tp = netdev_priv(dev);
3472
Francois Romieu98ddf982012-01-31 10:47:34 +01003473 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474}
3475
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3477 void __iomem *ioaddr)
3478{
3479 iounmap(ioaddr);
3480 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003481 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 pci_disable_device(pdev);
3483 free_netdev(dev);
3484}
3485
Francois Romieuffc46952012-07-06 14:19:23 +02003486DECLARE_RTL_COND(rtl_phy_reset_cond)
3487{
3488 return tp->phy_reset_pending(tp);
3489}
3490
Francois Romieubf793292006-11-01 00:53:05 +01003491static void rtl8169_phy_reset(struct net_device *dev,
3492 struct rtl8169_private *tp)
3493{
françois romieu4da19632011-01-03 15:07:55 +00003494 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02003495 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01003496}
3497
David S. Miller8decf862011-09-22 03:23:13 -04003498static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3499{
3500 void __iomem *ioaddr = tp->mmio_addr;
3501
3502 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3503 (RTL_R8(PHYstatus) & TBI_Enable);
3504}
3505
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003506static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003508 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003509
Francois Romieu5615d9f2007-08-17 17:50:46 +02003510 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003511
Marcus Sundberg773328942008-07-10 21:28:08 +02003512 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3513 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3514 RTL_W8(0x82, 0x01);
3515 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003516
Francois Romieu6dccd162007-02-13 23:38:05 +01003517 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3518
3519 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3520 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003521
Francois Romieubcf0bf92006-07-26 23:14:13 +02003522 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003523 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3524 RTL_W8(0x82, 0x01);
3525 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003526 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003527 }
3528
Francois Romieubf793292006-11-01 00:53:05 +01003529 rtl8169_phy_reset(dev, tp);
3530
Oliver Neukum54405cd2011-01-06 21:55:13 +01003531 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003532 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3533 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3534 (tp->mii.supports_gmii ?
3535 ADVERTISED_1000baseT_Half |
3536 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003537
David S. Miller8decf862011-09-22 03:23:13 -04003538 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003539 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003540}
3541
Francois Romieu773d2022007-01-31 23:47:43 +01003542static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3543{
3544 void __iomem *ioaddr = tp->mmio_addr;
3545 u32 high;
3546 u32 low;
3547
3548 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3549 high = addr[4] | (addr[5] << 8);
3550
Francois Romieuda78dbf2012-01-26 14:18:23 +01003551 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003552
3553 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2bf2010-04-26 11:42:58 +00003554
Francois Romieu773d2022007-01-31 23:47:43 +01003555 RTL_W32(MAC4, high);
françois romieu908ba2bf2010-04-26 11:42:58 +00003556 RTL_R32(MAC4);
3557
Francois Romieu78f1cd02010-03-27 19:35:46 -07003558 RTL_W32(MAC0, low);
françois romieu908ba2bf2010-04-26 11:42:58 +00003559 RTL_R32(MAC0);
3560
françois romieuc28aa382011-08-02 03:53:43 +00003561 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3562 const struct exgmac_reg e[] = {
3563 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3564 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3565 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3566 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3567 low >> 16 },
3568 };
3569
Francois Romieufdf6fc02012-07-06 22:40:38 +02003570 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
françois romieuc28aa382011-08-02 03:53:43 +00003571 }
3572
Francois Romieu773d2022007-01-31 23:47:43 +01003573 RTL_W8(Cfg9346, Cfg9346_Lock);
3574
Francois Romieuda78dbf2012-01-26 14:18:23 +01003575 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003576}
3577
3578static int rtl_set_mac_address(struct net_device *dev, void *p)
3579{
3580 struct rtl8169_private *tp = netdev_priv(dev);
3581 struct sockaddr *addr = p;
3582
3583 if (!is_valid_ether_addr(addr->sa_data))
3584 return -EADDRNOTAVAIL;
3585
3586 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3587
3588 rtl_rar_set(tp, dev->dev_addr);
3589
3590 return 0;
3591}
3592
Francois Romieu5f787a12006-08-17 13:02:36 +02003593static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3594{
3595 struct rtl8169_private *tp = netdev_priv(dev);
3596 struct mii_ioctl_data *data = if_mii(ifr);
3597
Francois Romieu8b4ab282008-11-19 22:05:25 -08003598 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3599}
Francois Romieu5f787a12006-08-17 13:02:36 +02003600
Francois Romieucecb5fd2011-04-01 10:21:07 +02003601static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3602 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003603{
Francois Romieu5f787a12006-08-17 13:02:36 +02003604 switch (cmd) {
3605 case SIOCGMIIPHY:
3606 data->phy_id = 32; /* Internal PHY */
3607 return 0;
3608
3609 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003610 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003611 return 0;
3612
3613 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003614 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003615 return 0;
3616 }
3617 return -EOPNOTSUPP;
3618}
3619
Francois Romieu8b4ab282008-11-19 22:05:25 -08003620static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3621{
3622 return -EOPNOTSUPP;
3623}
3624
Francois Romieufbac58f2007-10-04 22:51:38 +02003625static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3626{
3627 if (tp->features & RTL_FEATURE_MSI) {
3628 pci_disable_msi(pdev);
3629 tp->features &= ~RTL_FEATURE_MSI;
3630 }
3631}
3632
françois romieuc0e45c12011-01-03 15:08:04 +00003633static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3634{
3635 struct mdio_ops *ops = &tp->mdio_ops;
3636
3637 switch (tp->mac_version) {
3638 case RTL_GIGA_MAC_VER_27:
3639 ops->write = r8168dp_1_mdio_write;
3640 ops->read = r8168dp_1_mdio_read;
3641 break;
françois romieue6de30d2011-01-03 15:08:37 +00003642 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003643 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003644 ops->write = r8168dp_2_mdio_write;
3645 ops->read = r8168dp_2_mdio_read;
3646 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003647 default:
3648 ops->write = r8169_mdio_write;
3649 ops->read = r8169_mdio_read;
3650 break;
3651 }
3652}
3653
David S. Miller1805b2f2011-10-24 18:18:09 -04003654static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3655{
3656 void __iomem *ioaddr = tp->mmio_addr;
3657
3658 switch (tp->mac_version) {
3659 case RTL_GIGA_MAC_VER_29:
3660 case RTL_GIGA_MAC_VER_30:
3661 case RTL_GIGA_MAC_VER_32:
3662 case RTL_GIGA_MAC_VER_33:
3663 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08003664 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003665 case RTL_GIGA_MAC_VER_38:
Hayes Wang5598bfe2012-07-02 17:23:21 +08003666 case RTL_GIGA_MAC_VER_39:
David S. Miller1805b2f2011-10-24 18:18:09 -04003667 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3668 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3669 break;
3670 default:
3671 break;
3672 }
3673}
3674
3675static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3676{
3677 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3678 return false;
3679
3680 rtl_writephy(tp, 0x1f, 0x0000);
3681 rtl_writephy(tp, MII_BMCR, 0x0000);
3682
3683 rtl_wol_suspend_quirk(tp);
3684
3685 return true;
3686}
3687
françois romieu065c27c2011-01-03 15:08:12 +00003688static void r810x_phy_power_down(struct rtl8169_private *tp)
3689{
3690 rtl_writephy(tp, 0x1f, 0x0000);
3691 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3692}
3693
3694static void r810x_phy_power_up(struct rtl8169_private *tp)
3695{
3696 rtl_writephy(tp, 0x1f, 0x0000);
3697 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3698}
3699
3700static void r810x_pll_power_down(struct rtl8169_private *tp)
3701{
Hayes Wang00042992012-03-30 14:33:00 +08003702 void __iomem *ioaddr = tp->mmio_addr;
3703
David S. Miller1805b2f2011-10-24 18:18:09 -04003704 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003705 return;
françois romieu065c27c2011-01-03 15:08:12 +00003706
3707 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003708
3709 switch (tp->mac_version) {
3710 case RTL_GIGA_MAC_VER_07:
3711 case RTL_GIGA_MAC_VER_08:
3712 case RTL_GIGA_MAC_VER_09:
3713 case RTL_GIGA_MAC_VER_10:
3714 case RTL_GIGA_MAC_VER_13:
3715 case RTL_GIGA_MAC_VER_16:
3716 break;
3717 default:
3718 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3719 break;
3720 }
françois romieu065c27c2011-01-03 15:08:12 +00003721}
3722
3723static void r810x_pll_power_up(struct rtl8169_private *tp)
3724{
Hayes Wang00042992012-03-30 14:33:00 +08003725 void __iomem *ioaddr = tp->mmio_addr;
3726
françois romieu065c27c2011-01-03 15:08:12 +00003727 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003728
3729 switch (tp->mac_version) {
3730 case RTL_GIGA_MAC_VER_07:
3731 case RTL_GIGA_MAC_VER_08:
3732 case RTL_GIGA_MAC_VER_09:
3733 case RTL_GIGA_MAC_VER_10:
3734 case RTL_GIGA_MAC_VER_13:
3735 case RTL_GIGA_MAC_VER_16:
3736 break;
3737 default:
3738 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3739 break;
3740 }
françois romieu065c27c2011-01-03 15:08:12 +00003741}
3742
3743static void r8168_phy_power_up(struct rtl8169_private *tp)
3744{
3745 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003746 switch (tp->mac_version) {
3747 case RTL_GIGA_MAC_VER_11:
3748 case RTL_GIGA_MAC_VER_12:
3749 case RTL_GIGA_MAC_VER_17:
3750 case RTL_GIGA_MAC_VER_18:
3751 case RTL_GIGA_MAC_VER_19:
3752 case RTL_GIGA_MAC_VER_20:
3753 case RTL_GIGA_MAC_VER_21:
3754 case RTL_GIGA_MAC_VER_22:
3755 case RTL_GIGA_MAC_VER_23:
3756 case RTL_GIGA_MAC_VER_24:
3757 case RTL_GIGA_MAC_VER_25:
3758 case RTL_GIGA_MAC_VER_26:
3759 case RTL_GIGA_MAC_VER_27:
3760 case RTL_GIGA_MAC_VER_28:
3761 case RTL_GIGA_MAC_VER_31:
3762 rtl_writephy(tp, 0x0e, 0x0000);
3763 break;
3764 default:
3765 break;
3766 }
françois romieu065c27c2011-01-03 15:08:12 +00003767 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3768}
3769
3770static void r8168_phy_power_down(struct rtl8169_private *tp)
3771{
3772 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003773 switch (tp->mac_version) {
3774 case RTL_GIGA_MAC_VER_32:
3775 case RTL_GIGA_MAC_VER_33:
3776 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3777 break;
3778
3779 case RTL_GIGA_MAC_VER_11:
3780 case RTL_GIGA_MAC_VER_12:
3781 case RTL_GIGA_MAC_VER_17:
3782 case RTL_GIGA_MAC_VER_18:
3783 case RTL_GIGA_MAC_VER_19:
3784 case RTL_GIGA_MAC_VER_20:
3785 case RTL_GIGA_MAC_VER_21:
3786 case RTL_GIGA_MAC_VER_22:
3787 case RTL_GIGA_MAC_VER_23:
3788 case RTL_GIGA_MAC_VER_24:
3789 case RTL_GIGA_MAC_VER_25:
3790 case RTL_GIGA_MAC_VER_26:
3791 case RTL_GIGA_MAC_VER_27:
3792 case RTL_GIGA_MAC_VER_28:
3793 case RTL_GIGA_MAC_VER_31:
3794 rtl_writephy(tp, 0x0e, 0x0200);
3795 default:
3796 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3797 break;
3798 }
françois romieu065c27c2011-01-03 15:08:12 +00003799}
3800
3801static void r8168_pll_power_down(struct rtl8169_private *tp)
3802{
3803 void __iomem *ioaddr = tp->mmio_addr;
3804
Francois Romieucecb5fd2011-04-01 10:21:07 +02003805 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3806 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3807 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003808 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003809 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003810 }
françois romieu065c27c2011-01-03 15:08:12 +00003811
Francois Romieucecb5fd2011-04-01 10:21:07 +02003812 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3813 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003814 (RTL_R16(CPlusCmd) & ASF)) {
3815 return;
3816 }
3817
hayeswang01dc7fe2011-03-21 01:50:28 +00003818 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3819 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02003820 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00003821
David S. Miller1805b2f2011-10-24 18:18:09 -04003822 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003823 return;
françois romieu065c27c2011-01-03 15:08:12 +00003824
3825 r8168_phy_power_down(tp);
3826
3827 switch (tp->mac_version) {
3828 case RTL_GIGA_MAC_VER_25:
3829 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003830 case RTL_GIGA_MAC_VER_27:
3831 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003832 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003833 case RTL_GIGA_MAC_VER_32:
3834 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003835 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3836 break;
3837 }
3838}
3839
3840static void r8168_pll_power_up(struct rtl8169_private *tp)
3841{
3842 void __iomem *ioaddr = tp->mmio_addr;
3843
françois romieu065c27c2011-01-03 15:08:12 +00003844 switch (tp->mac_version) {
3845 case RTL_GIGA_MAC_VER_25:
3846 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003847 case RTL_GIGA_MAC_VER_27:
3848 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003849 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003850 case RTL_GIGA_MAC_VER_32:
3851 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003852 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3853 break;
3854 }
3855
3856 r8168_phy_power_up(tp);
3857}
3858
Francois Romieud58d46b2011-05-03 16:38:29 +02003859static void rtl_generic_op(struct rtl8169_private *tp,
3860 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00003861{
3862 if (op)
3863 op(tp);
3864}
3865
3866static void rtl_pll_power_down(struct rtl8169_private *tp)
3867{
Francois Romieud58d46b2011-05-03 16:38:29 +02003868 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00003869}
3870
3871static void rtl_pll_power_up(struct rtl8169_private *tp)
3872{
Francois Romieud58d46b2011-05-03 16:38:29 +02003873 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00003874}
3875
3876static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3877{
3878 struct pll_power_ops *ops = &tp->pll_power_ops;
3879
3880 switch (tp->mac_version) {
3881 case RTL_GIGA_MAC_VER_07:
3882 case RTL_GIGA_MAC_VER_08:
3883 case RTL_GIGA_MAC_VER_09:
3884 case RTL_GIGA_MAC_VER_10:
3885 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003886 case RTL_GIGA_MAC_VER_29:
3887 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08003888 case RTL_GIGA_MAC_VER_37:
Hayes Wang5598bfe2012-07-02 17:23:21 +08003889 case RTL_GIGA_MAC_VER_39:
françois romieu065c27c2011-01-03 15:08:12 +00003890 ops->down = r810x_pll_power_down;
3891 ops->up = r810x_pll_power_up;
3892 break;
3893
3894 case RTL_GIGA_MAC_VER_11:
3895 case RTL_GIGA_MAC_VER_12:
3896 case RTL_GIGA_MAC_VER_17:
3897 case RTL_GIGA_MAC_VER_18:
3898 case RTL_GIGA_MAC_VER_19:
3899 case RTL_GIGA_MAC_VER_20:
3900 case RTL_GIGA_MAC_VER_21:
3901 case RTL_GIGA_MAC_VER_22:
3902 case RTL_GIGA_MAC_VER_23:
3903 case RTL_GIGA_MAC_VER_24:
3904 case RTL_GIGA_MAC_VER_25:
3905 case RTL_GIGA_MAC_VER_26:
3906 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003907 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003908 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003909 case RTL_GIGA_MAC_VER_32:
3910 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003911 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08003912 case RTL_GIGA_MAC_VER_35:
3913 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003914 case RTL_GIGA_MAC_VER_38:
françois romieu065c27c2011-01-03 15:08:12 +00003915 ops->down = r8168_pll_power_down;
3916 ops->up = r8168_pll_power_up;
3917 break;
3918
3919 default:
3920 ops->down = NULL;
3921 ops->up = NULL;
3922 break;
3923 }
3924}
3925
Hayes Wange542a222011-07-06 15:58:04 +08003926static void rtl_init_rxcfg(struct rtl8169_private *tp)
3927{
3928 void __iomem *ioaddr = tp->mmio_addr;
3929
3930 switch (tp->mac_version) {
3931 case RTL_GIGA_MAC_VER_01:
3932 case RTL_GIGA_MAC_VER_02:
3933 case RTL_GIGA_MAC_VER_03:
3934 case RTL_GIGA_MAC_VER_04:
3935 case RTL_GIGA_MAC_VER_05:
3936 case RTL_GIGA_MAC_VER_06:
3937 case RTL_GIGA_MAC_VER_10:
3938 case RTL_GIGA_MAC_VER_11:
3939 case RTL_GIGA_MAC_VER_12:
3940 case RTL_GIGA_MAC_VER_13:
3941 case RTL_GIGA_MAC_VER_14:
3942 case RTL_GIGA_MAC_VER_15:
3943 case RTL_GIGA_MAC_VER_16:
3944 case RTL_GIGA_MAC_VER_17:
3945 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3946 break;
3947 case RTL_GIGA_MAC_VER_18:
3948 case RTL_GIGA_MAC_VER_19:
3949 case RTL_GIGA_MAC_VER_20:
3950 case RTL_GIGA_MAC_VER_21:
3951 case RTL_GIGA_MAC_VER_22:
3952 case RTL_GIGA_MAC_VER_23:
3953 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00003954 case RTL_GIGA_MAC_VER_34:
Hayes Wange542a222011-07-06 15:58:04 +08003955 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3956 break;
3957 default:
3958 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3959 break;
3960 }
3961}
3962
Hayes Wang92fc43b2011-07-06 15:58:03 +08003963static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3964{
3965 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3966}
3967
Francois Romieud58d46b2011-05-03 16:38:29 +02003968static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3969{
françois romieu9c5028e2012-03-02 04:43:14 +00003970 void __iomem *ioaddr = tp->mmio_addr;
3971
3972 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003973 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00003974 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003975}
3976
3977static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3978{
françois romieu9c5028e2012-03-02 04:43:14 +00003979 void __iomem *ioaddr = tp->mmio_addr;
3980
3981 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003982 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00003983 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003984}
3985
3986static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3987{
3988 void __iomem *ioaddr = tp->mmio_addr;
3989
3990 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3991 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3992 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3993}
3994
3995static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3996{
3997 void __iomem *ioaddr = tp->mmio_addr;
3998
3999 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4000 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
4001 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
4002}
4003
4004static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4005{
4006 void __iomem *ioaddr = tp->mmio_addr;
4007
4008 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4009}
4010
4011static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4012{
4013 void __iomem *ioaddr = tp->mmio_addr;
4014
4015 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4016}
4017
4018static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4019{
4020 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02004021
4022 RTL_W8(MaxTxPacketSize, 0x3f);
4023 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4024 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01004025 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02004026}
4027
4028static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4029{
4030 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02004031
4032 RTL_W8(MaxTxPacketSize, 0x0c);
4033 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4034 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01004035 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02004036}
4037
4038static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4039{
4040 rtl_tx_performance_tweak(tp->pci_dev,
4041 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4042}
4043
4044static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4045{
4046 rtl_tx_performance_tweak(tp->pci_dev,
4047 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4048}
4049
4050static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4051{
4052 void __iomem *ioaddr = tp->mmio_addr;
4053
4054 r8168b_0_hw_jumbo_enable(tp);
4055
4056 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
4057}
4058
4059static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4060{
4061 void __iomem *ioaddr = tp->mmio_addr;
4062
4063 r8168b_0_hw_jumbo_disable(tp);
4064
4065 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4066}
4067
4068static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
4069{
4070 struct jumbo_ops *ops = &tp->jumbo_ops;
4071
4072 switch (tp->mac_version) {
4073 case RTL_GIGA_MAC_VER_11:
4074 ops->disable = r8168b_0_hw_jumbo_disable;
4075 ops->enable = r8168b_0_hw_jumbo_enable;
4076 break;
4077 case RTL_GIGA_MAC_VER_12:
4078 case RTL_GIGA_MAC_VER_17:
4079 ops->disable = r8168b_1_hw_jumbo_disable;
4080 ops->enable = r8168b_1_hw_jumbo_enable;
4081 break;
4082 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4083 case RTL_GIGA_MAC_VER_19:
4084 case RTL_GIGA_MAC_VER_20:
4085 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4086 case RTL_GIGA_MAC_VER_22:
4087 case RTL_GIGA_MAC_VER_23:
4088 case RTL_GIGA_MAC_VER_24:
4089 case RTL_GIGA_MAC_VER_25:
4090 case RTL_GIGA_MAC_VER_26:
4091 ops->disable = r8168c_hw_jumbo_disable;
4092 ops->enable = r8168c_hw_jumbo_enable;
4093 break;
4094 case RTL_GIGA_MAC_VER_27:
4095 case RTL_GIGA_MAC_VER_28:
4096 ops->disable = r8168dp_hw_jumbo_disable;
4097 ops->enable = r8168dp_hw_jumbo_enable;
4098 break;
4099 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4100 case RTL_GIGA_MAC_VER_32:
4101 case RTL_GIGA_MAC_VER_33:
4102 case RTL_GIGA_MAC_VER_34:
4103 ops->disable = r8168e_hw_jumbo_disable;
4104 ops->enable = r8168e_hw_jumbo_enable;
4105 break;
4106
4107 /*
4108 * No action needed for jumbo frames with 8169.
4109 * No jumbo for 810x at all.
4110 */
4111 default:
4112 ops->disable = NULL;
4113 ops->enable = NULL;
4114 break;
4115 }
4116}
4117
Francois Romieuffc46952012-07-06 14:19:23 +02004118DECLARE_RTL_COND(rtl_chipcmd_cond)
4119{
4120 void __iomem *ioaddr = tp->mmio_addr;
4121
4122 return RTL_R8(ChipCmd) & CmdReset;
4123}
4124
Francois Romieu6f43adc2011-04-29 15:05:51 +02004125static void rtl_hw_reset(struct rtl8169_private *tp)
4126{
4127 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu6f43adc2011-04-29 15:05:51 +02004128
Francois Romieu6f43adc2011-04-29 15:05:51 +02004129 RTL_W8(ChipCmd, CmdReset);
4130
Francois Romieuffc46952012-07-06 14:19:23 +02004131 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004132}
4133
Francois Romieub6ffd972011-06-17 17:00:05 +02004134static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4135{
4136 struct rtl_fw *rtl_fw;
4137 const char *name;
4138 int rc = -ENOMEM;
4139
4140 name = rtl_lookup_firmware_name(tp);
4141 if (!name)
4142 goto out_no_firmware;
4143
4144 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4145 if (!rtl_fw)
4146 goto err_warn;
4147
4148 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4149 if (rc < 0)
4150 goto err_free;
4151
Francois Romieufd112f22011-06-18 00:10:29 +02004152 rc = rtl_check_firmware(tp, rtl_fw);
4153 if (rc < 0)
4154 goto err_release_firmware;
4155
Francois Romieub6ffd972011-06-17 17:00:05 +02004156 tp->rtl_fw = rtl_fw;
4157out:
4158 return;
4159
Francois Romieufd112f22011-06-18 00:10:29 +02004160err_release_firmware:
4161 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004162err_free:
4163 kfree(rtl_fw);
4164err_warn:
4165 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4166 name, rc);
4167out_no_firmware:
4168 tp->rtl_fw = NULL;
4169 goto out;
4170}
4171
François Romieu953a12c2011-04-24 17:38:48 +02004172static void rtl_request_firmware(struct rtl8169_private *tp)
4173{
Francois Romieub6ffd972011-06-17 17:00:05 +02004174 if (IS_ERR(tp->rtl_fw))
4175 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004176}
4177
Hayes Wang92fc43b2011-07-06 15:58:03 +08004178static void rtl_rx_close(struct rtl8169_private *tp)
4179{
4180 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004181
Francois Romieu1687b562011-07-19 17:21:29 +02004182 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004183}
4184
Francois Romieuffc46952012-07-06 14:19:23 +02004185DECLARE_RTL_COND(rtl_npq_cond)
4186{
4187 void __iomem *ioaddr = tp->mmio_addr;
4188
4189 return RTL_R8(TxPoll) & NPQ;
4190}
4191
4192DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4193{
4194 void __iomem *ioaddr = tp->mmio_addr;
4195
4196 return RTL_R32(TxConfig) & TXCFG_EMPTY;
4197}
4198
françois romieue6de30d2011-01-03 15:08:37 +00004199static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200{
françois romieue6de30d2011-01-03 15:08:37 +00004201 void __iomem *ioaddr = tp->mmio_addr;
4202
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004204 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205
Hayes Wang92fc43b2011-07-06 15:58:03 +08004206 rtl_rx_close(tp);
4207
Hayes Wang5d2e1952011-02-22 17:26:22 +08004208 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004209 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4210 tp->mac_version == RTL_GIGA_MAC_VER_31) {
Francois Romieuffc46952012-07-06 14:19:23 +02004211 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Hayes Wangc2218922011-09-06 16:55:18 +08004212 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4213 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
Hayes Wang7e18dca2012-03-30 14:33:02 +08004214 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004215 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
4216 tp->mac_version == RTL_GIGA_MAC_VER_38) {
David S. Miller8decf862011-09-22 03:23:13 -04004217 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02004218 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004219 } else {
4220 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4221 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004222 }
4223
Hayes Wang92fc43b2011-07-06 15:58:03 +08004224 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225}
4226
Francois Romieu7f796d832007-06-11 23:04:41 +02004227static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004228{
4229 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004230
4231 /* Set DMA burst size and Interframe Gap Time */
4232 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4233 (InterFrameGap << TxInterFrameGapShift));
4234}
4235
Francois Romieu07ce4062007-02-23 23:36:39 +01004236static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237{
4238 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239
Francois Romieu07ce4062007-02-23 23:36:39 +01004240 tp->hw_start(dev);
4241
Francois Romieuda78dbf2012-01-26 14:18:23 +01004242 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004243}
4244
Francois Romieu7f796d832007-06-11 23:04:41 +02004245static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4246 void __iomem *ioaddr)
4247{
4248 /*
4249 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4250 * register to be written before TxDescAddrLow to work.
4251 * Switching from MMIO to I/O access fixes the issue as well.
4252 */
4253 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004254 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004255 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004256 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004257}
4258
4259static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4260{
4261 u16 cmd;
4262
4263 cmd = RTL_R16(CPlusCmd);
4264 RTL_W16(CPlusCmd, cmd);
4265 return cmd;
4266}
4267
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004268static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004269{
4270 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004271 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004272}
4273
Francois Romieu6dccd162007-02-13 23:38:05 +01004274static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4275{
Francois Romieu37441002011-06-17 22:58:54 +02004276 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004277 u32 mac_version;
4278 u32 clk;
4279 u32 val;
4280 } cfg2_info [] = {
4281 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4282 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4283 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4284 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004285 };
4286 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004287 unsigned int i;
4288 u32 clk;
4289
4290 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004291 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004292 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4293 RTL_W32(0x7c, p->val);
4294 break;
4295 }
4296 }
4297}
4298
Francois Romieue6b763e2012-03-08 09:35:39 +01004299static void rtl_set_rx_mode(struct net_device *dev)
4300{
4301 struct rtl8169_private *tp = netdev_priv(dev);
4302 void __iomem *ioaddr = tp->mmio_addr;
4303 u32 mc_filter[2]; /* Multicast hash filter */
4304 int rx_mode;
4305 u32 tmp = 0;
4306
4307 if (dev->flags & IFF_PROMISC) {
4308 /* Unconditionally log net taps. */
4309 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4310 rx_mode =
4311 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4312 AcceptAllPhys;
4313 mc_filter[1] = mc_filter[0] = 0xffffffff;
4314 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4315 (dev->flags & IFF_ALLMULTI)) {
4316 /* Too many to filter perfectly -- accept all multicasts. */
4317 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4318 mc_filter[1] = mc_filter[0] = 0xffffffff;
4319 } else {
4320 struct netdev_hw_addr *ha;
4321
4322 rx_mode = AcceptBroadcast | AcceptMyPhys;
4323 mc_filter[1] = mc_filter[0] = 0;
4324 netdev_for_each_mc_addr(ha, dev) {
4325 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4326 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4327 rx_mode |= AcceptMulticast;
4328 }
4329 }
4330
4331 if (dev->features & NETIF_F_RXALL)
4332 rx_mode |= (AcceptErr | AcceptRunt);
4333
4334 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4335
4336 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4337 u32 data = mc_filter[0];
4338
4339 mc_filter[0] = swab32(mc_filter[1]);
4340 mc_filter[1] = swab32(data);
4341 }
4342
4343 RTL_W32(MAR0 + 4, mc_filter[1]);
4344 RTL_W32(MAR0 + 0, mc_filter[0]);
4345
4346 RTL_W32(RxConfig, tmp);
4347}
4348
Francois Romieu07ce4062007-02-23 23:36:39 +01004349static void rtl_hw_start_8169(struct net_device *dev)
4350{
4351 struct rtl8169_private *tp = netdev_priv(dev);
4352 void __iomem *ioaddr = tp->mmio_addr;
4353 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004354
Francois Romieu9cb427b2006-11-02 00:10:16 +01004355 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4356 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4357 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4358 }
4359
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004361 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4362 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4363 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4364 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004365 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4366
Hayes Wange542a222011-07-06 15:58:04 +08004367 rtl_init_rxcfg(tp);
4368
françois romieuf0298f82011-01-03 15:07:42 +00004369 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004371 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372
Francois Romieucecb5fd2011-04-01 10:21:07 +02004373 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4374 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4375 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4376 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004377 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378
Francois Romieu7f796d832007-06-11 23:04:41 +02004379 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004380
Francois Romieucecb5fd2011-04-01 10:21:07 +02004381 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4382 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004383 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004385 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 }
4387
Francois Romieubcf0bf92006-07-26 23:14:13 +02004388 RTL_W16(CPlusCmd, tp->cp_cmd);
4389
Francois Romieu6dccd162007-02-13 23:38:05 +01004390 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4391
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 /*
4393 * Undocumented corner. Supposedly:
4394 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4395 */
4396 RTL_W16(IntrMitigate, 0x0000);
4397
Francois Romieu7f796d832007-06-11 23:04:41 +02004398 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004399
Francois Romieucecb5fd2011-04-01 10:21:07 +02004400 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4401 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4402 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4403 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004404 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4405 rtl_set_rx_tx_config_registers(tp);
4406 }
4407
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004409
4410 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4411 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412
4413 RTL_W32(RxMissed, 0);
4414
Francois Romieu07ce4062007-02-23 23:36:39 +01004415 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416
4417 /* no early-rx interrupts */
4418 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004419}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004421static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
4422{
4423 if (tp->csi_ops.write)
Francois Romieu52989f02012-07-06 13:37:00 +02004424 tp->csi_ops.write(tp, addr, value);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004425}
4426
4427static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
4428{
Francois Romieu52989f02012-07-06 13:37:00 +02004429 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004430}
4431
4432static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004433{
4434 u32 csi;
4435
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004436 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4437 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00004438}
4439
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004440static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004441{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004442 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00004443}
4444
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004445static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00004446{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004447 rtl_csi_access_enable(tp, 0x27000000);
4448}
4449
Francois Romieuffc46952012-07-06 14:19:23 +02004450DECLARE_RTL_COND(rtl_csiar_cond)
4451{
4452 void __iomem *ioaddr = tp->mmio_addr;
4453
4454 return RTL_R32(CSIAR) & CSIAR_FLAG;
4455}
4456
Francois Romieu52989f02012-07-06 13:37:00 +02004457static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004458{
Francois Romieu52989f02012-07-06 13:37:00 +02004459 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004460
4461 RTL_W32(CSIDR, value);
4462 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4463 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4464
Francois Romieuffc46952012-07-06 14:19:23 +02004465 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004466}
4467
Francois Romieu52989f02012-07-06 13:37:00 +02004468static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004469{
Francois Romieu52989f02012-07-06 13:37:00 +02004470 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004471
4472 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
4473 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4474
Francois Romieuffc46952012-07-06 14:19:23 +02004475 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4476 RTL_R32(CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004477}
4478
Francois Romieu52989f02012-07-06 13:37:00 +02004479static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wang7e18dca2012-03-30 14:33:02 +08004480{
Francois Romieu52989f02012-07-06 13:37:00 +02004481 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004482
4483 RTL_W32(CSIDR, value);
4484 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4485 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
4486 CSIAR_FUNC_NIC);
4487
Francois Romieuffc46952012-07-06 14:19:23 +02004488 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004489}
4490
Francois Romieu52989f02012-07-06 13:37:00 +02004491static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wang7e18dca2012-03-30 14:33:02 +08004492{
Francois Romieu52989f02012-07-06 13:37:00 +02004493 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004494
4495 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
4496 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4497
Francois Romieuffc46952012-07-06 14:19:23 +02004498 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4499 RTL_R32(CSIDR) : ~0;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004500}
4501
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004502static void __devinit rtl_init_csi_ops(struct rtl8169_private *tp)
4503{
4504 struct csi_ops *ops = &tp->csi_ops;
4505
4506 switch (tp->mac_version) {
4507 case RTL_GIGA_MAC_VER_01:
4508 case RTL_GIGA_MAC_VER_02:
4509 case RTL_GIGA_MAC_VER_03:
4510 case RTL_GIGA_MAC_VER_04:
4511 case RTL_GIGA_MAC_VER_05:
4512 case RTL_GIGA_MAC_VER_06:
4513 case RTL_GIGA_MAC_VER_10:
4514 case RTL_GIGA_MAC_VER_11:
4515 case RTL_GIGA_MAC_VER_12:
4516 case RTL_GIGA_MAC_VER_13:
4517 case RTL_GIGA_MAC_VER_14:
4518 case RTL_GIGA_MAC_VER_15:
4519 case RTL_GIGA_MAC_VER_16:
4520 case RTL_GIGA_MAC_VER_17:
4521 ops->write = NULL;
4522 ops->read = NULL;
4523 break;
4524
Hayes Wang7e18dca2012-03-30 14:33:02 +08004525 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004526 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004527 ops->write = r8402_csi_write;
4528 ops->read = r8402_csi_read;
4529 break;
4530
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004531 default:
4532 ops->write = r8169_csi_write;
4533 ops->read = r8169_csi_read;
4534 break;
4535 }
Francois Romieudacf8152008-08-02 20:44:13 +02004536}
4537
4538struct ephy_info {
4539 unsigned int offset;
4540 u16 mask;
4541 u16 bits;
4542};
4543
Francois Romieufdf6fc02012-07-06 22:40:38 +02004544static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
4545 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004546{
4547 u16 w;
4548
4549 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02004550 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4551 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02004552 e++;
4553 }
4554}
4555
Francois Romieub726e492008-06-28 12:22:59 +02004556static void rtl_disable_clock_request(struct pci_dev *pdev)
4557{
Jon Masone44daad2011-06-27 07:46:31 +00004558 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004559
4560 if (cap) {
4561 u16 ctl;
4562
4563 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4564 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4565 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4566 }
4567}
4568
françois romieue6de30d2011-01-03 15:08:37 +00004569static void rtl_enable_clock_request(struct pci_dev *pdev)
4570{
Jon Masone44daad2011-06-27 07:46:31 +00004571 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004572
4573 if (cap) {
4574 u16 ctl;
4575
4576 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4577 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4578 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4579 }
4580}
4581
Francois Romieub726e492008-06-28 12:22:59 +02004582#define R8168_CPCMD_QUIRK_MASK (\
4583 EnableBist | \
4584 Mac_dbgo_oe | \
4585 Force_half_dup | \
4586 Force_rxflow_en | \
4587 Force_txflow_en | \
4588 Cxpl_dbg_sel | \
4589 ASF | \
4590 PktCntrDisable | \
4591 Mac_dbgo_sel)
4592
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004593static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004594{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004595 void __iomem *ioaddr = tp->mmio_addr;
4596 struct pci_dev *pdev = tp->pci_dev;
4597
Francois Romieub726e492008-06-28 12:22:59 +02004598 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4599
4600 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4601
Francois Romieu2e68ae42008-06-28 12:00:55 +02004602 rtl_tx_performance_tweak(pdev,
4603 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004604}
4605
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004606static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004607{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004608 void __iomem *ioaddr = tp->mmio_addr;
4609
4610 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004611
françois romieuf0298f82011-01-03 15:07:42 +00004612 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004613
4614 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004615}
4616
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004617static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004618{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004619 void __iomem *ioaddr = tp->mmio_addr;
4620 struct pci_dev *pdev = tp->pci_dev;
4621
Francois Romieub726e492008-06-28 12:22:59 +02004622 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4623
4624 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4625
Francois Romieu219a1e92008-06-28 11:58:39 +02004626 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004627
4628 rtl_disable_clock_request(pdev);
4629
4630 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004631}
4632
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004633static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004634{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004635 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004636 { 0x01, 0, 0x0001 },
4637 { 0x02, 0x0800, 0x1000 },
4638 { 0x03, 0, 0x0042 },
4639 { 0x06, 0x0080, 0x0000 },
4640 { 0x07, 0, 0x2000 }
4641 };
4642
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004643 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004644
Francois Romieufdf6fc02012-07-06 22:40:38 +02004645 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02004646
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004647 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004648}
4649
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004650static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02004651{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004652 void __iomem *ioaddr = tp->mmio_addr;
4653 struct pci_dev *pdev = tp->pci_dev;
4654
4655 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004656
4657 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4658
4659 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4660
4661 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4662}
4663
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004664static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004665{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004666 void __iomem *ioaddr = tp->mmio_addr;
4667 struct pci_dev *pdev = tp->pci_dev;
4668
4669 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004670
4671 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4672
4673 /* Magic. */
4674 RTL_W8(DBG_REG, 0x20);
4675
françois romieuf0298f82011-01-03 15:07:42 +00004676 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004677
4678 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4679
4680 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4681}
4682
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004683static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004684{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004685 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004686 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004687 { 0x02, 0x0800, 0x1000 },
4688 { 0x03, 0, 0x0002 },
4689 { 0x06, 0x0080, 0x0000 }
4690 };
4691
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004692 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004693
4694 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4695
Francois Romieufdf6fc02012-07-06 22:40:38 +02004696 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02004697
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004698 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004699}
4700
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004701static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004702{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004703 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004704 { 0x01, 0, 0x0001 },
4705 { 0x03, 0x0400, 0x0220 }
4706 };
4707
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004708 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004709
Francois Romieufdf6fc02012-07-06 22:40:38 +02004710 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02004711
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004712 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004713}
4714
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004715static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02004716{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004717 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004718}
4719
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004720static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02004721{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004722 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004723
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004724 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004725}
4726
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004727static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02004728{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004729 void __iomem *ioaddr = tp->mmio_addr;
4730 struct pci_dev *pdev = tp->pci_dev;
4731
4732 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004733
4734 rtl_disable_clock_request(pdev);
4735
françois romieuf0298f82011-01-03 15:07:42 +00004736 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004737
4738 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4739
4740 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4741}
4742
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004743static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00004744{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004745 void __iomem *ioaddr = tp->mmio_addr;
4746 struct pci_dev *pdev = tp->pci_dev;
4747
4748 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004749
4750 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4751
4752 RTL_W8(MaxTxPacketSize, TxPacketMax);
4753
4754 rtl_disable_clock_request(pdev);
4755}
4756
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004757static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004758{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004759 void __iomem *ioaddr = tp->mmio_addr;
4760 struct pci_dev *pdev = tp->pci_dev;
françois romieue6de30d2011-01-03 15:08:37 +00004761 static const struct ephy_info e_info_8168d_4[] = {
4762 { 0x0b, ~0, 0x48 },
4763 { 0x19, 0x20, 0x50 },
4764 { 0x0c, ~0, 0x20 }
4765 };
4766 int i;
4767
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004768 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004769
4770 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4771
4772 RTL_W8(MaxTxPacketSize, TxPacketMax);
4773
4774 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4775 const struct ephy_info *e = e_info_8168d_4 + i;
4776 u16 w;
4777
Francois Romieufdf6fc02012-07-06 22:40:38 +02004778 w = rtl_ephy_read(tp, e->offset);
4779 rtl_ephy_write(tp, 0x03, (w & e->mask) | e->bits);
françois romieue6de30d2011-01-03 15:08:37 +00004780 }
4781
4782 rtl_enable_clock_request(pdev);
4783}
4784
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004785static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00004786{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004787 void __iomem *ioaddr = tp->mmio_addr;
4788 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08004789 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004790 { 0x00, 0x0200, 0x0100 },
4791 { 0x00, 0x0000, 0x0004 },
4792 { 0x06, 0x0002, 0x0001 },
4793 { 0x06, 0x0000, 0x0030 },
4794 { 0x07, 0x0000, 0x2000 },
4795 { 0x00, 0x0000, 0x0020 },
4796 { 0x03, 0x5800, 0x2000 },
4797 { 0x03, 0x0000, 0x0001 },
4798 { 0x01, 0x0800, 0x1000 },
4799 { 0x07, 0x0000, 0x4000 },
4800 { 0x1e, 0x0000, 0x2000 },
4801 { 0x19, 0xffff, 0xfe6c },
4802 { 0x0a, 0x0000, 0x0040 }
4803 };
4804
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004805 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004806
Francois Romieufdf6fc02012-07-06 22:40:38 +02004807 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004808
4809 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4810
4811 RTL_W8(MaxTxPacketSize, TxPacketMax);
4812
4813 rtl_disable_clock_request(pdev);
4814
4815 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004816 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4817 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004818
Francois Romieucecb5fd2011-04-01 10:21:07 +02004819 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004820}
4821
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004822static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08004823{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004824 void __iomem *ioaddr = tp->mmio_addr;
4825 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08004826 static const struct ephy_info e_info_8168e_2[] = {
4827 { 0x09, 0x0000, 0x0080 },
4828 { 0x19, 0x0000, 0x0224 }
4829 };
4830
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004831 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08004832
Francois Romieufdf6fc02012-07-06 22:40:38 +02004833 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08004834
4835 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4836
Francois Romieufdf6fc02012-07-06 22:40:38 +02004837 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4838 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4839 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4840 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4841 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4842 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4843 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4844 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08004845
Hayes Wang3090bd92011-09-06 16:55:15 +08004846 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004847
4848 rtl_disable_clock_request(pdev);
4849
4850 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4851 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4852
4853 /* Adjust EEE LED frequency */
4854 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4855
4856 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4857 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4858 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4859}
4860
Hayes Wang5f886e02012-03-30 14:33:03 +08004861static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08004862{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004863 void __iomem *ioaddr = tp->mmio_addr;
4864 struct pci_dev *pdev = tp->pci_dev;
Hayes Wangc2218922011-09-06 16:55:18 +08004865
Hayes Wang5f886e02012-03-30 14:33:03 +08004866 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08004867
4868 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4869
Francois Romieufdf6fc02012-07-06 22:40:38 +02004870 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4871 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4872 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4873 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4874 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4875 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4876 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4877 rtl_w1w0_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4878 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4879 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08004880
4881 RTL_W8(MaxTxPacketSize, EarlySize);
4882
4883 rtl_disable_clock_request(pdev);
4884
4885 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4886 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
Hayes Wangc2218922011-09-06 16:55:18 +08004887 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4888 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4889 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4890}
4891
Hayes Wang5f886e02012-03-30 14:33:03 +08004892static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
4893{
4894 void __iomem *ioaddr = tp->mmio_addr;
4895 static const struct ephy_info e_info_8168f_1[] = {
4896 { 0x06, 0x00c0, 0x0020 },
4897 { 0x08, 0x0001, 0x0002 },
4898 { 0x09, 0x0000, 0x0080 },
4899 { 0x19, 0x0000, 0x0224 }
4900 };
4901
4902 rtl_hw_start_8168f(tp);
4903
Francois Romieufdf6fc02012-07-06 22:40:38 +02004904 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08004905
Francois Romieufdf6fc02012-07-06 22:40:38 +02004906 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08004907
4908 /* Adjust EEE LED frequency */
4909 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4910}
4911
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004912static void rtl_hw_start_8411(struct rtl8169_private *tp)
4913{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004914 static const struct ephy_info e_info_8168f_1[] = {
4915 { 0x06, 0x00c0, 0x0020 },
4916 { 0x0f, 0xffff, 0x5200 },
4917 { 0x1e, 0x0000, 0x4000 },
4918 { 0x19, 0x0000, 0x0224 }
4919 };
4920
4921 rtl_hw_start_8168f(tp);
4922
Francois Romieufdf6fc02012-07-06 22:40:38 +02004923 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004924
Francois Romieufdf6fc02012-07-06 22:40:38 +02004925 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004926}
4927
Francois Romieu07ce4062007-02-23 23:36:39 +01004928static void rtl_hw_start_8168(struct net_device *dev)
4929{
Francois Romieu2dd99532007-06-11 23:22:52 +02004930 struct rtl8169_private *tp = netdev_priv(dev);
4931 void __iomem *ioaddr = tp->mmio_addr;
4932
4933 RTL_W8(Cfg9346, Cfg9346_Unlock);
4934
françois romieuf0298f82011-01-03 15:07:42 +00004935 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004936
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004937 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004938
Francois Romieu0e485152007-02-20 00:00:26 +01004939 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004940
4941 RTL_W16(CPlusCmd, tp->cp_cmd);
4942
Francois Romieu0e485152007-02-20 00:00:26 +01004943 RTL_W16(IntrMitigate, 0x5151);
4944
4945 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00004946 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01004947 tp->event_slow |= RxFIFOOver | PCSTimeout;
4948 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01004949 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004950
4951 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4952
Francois Romieub8363902008-06-01 12:31:57 +02004953 rtl_set_rx_mode(dev);
4954
4955 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4956 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004957
4958 RTL_R8(IntrMask);
4959
Francois Romieu219a1e92008-06-28 11:58:39 +02004960 switch (tp->mac_version) {
4961 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004962 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004963 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004964
4965 case RTL_GIGA_MAC_VER_12:
4966 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004967 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004968 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004969
4970 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004971 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004972 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004973
4974 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004975 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004976 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004977
4978 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004979 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004980 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004981
Francois Romieu197ff762008-06-28 13:16:02 +02004982 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004983 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004984 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004985
Francois Romieu6fb07052008-06-29 11:54:28 +02004986 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004987 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004988 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004989
Francois Romieuef3386f2008-06-29 12:24:30 +02004990 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004991 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004992 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004993
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004994 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004995 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004996 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004997
Francois Romieu5b538df2008-07-20 16:22:45 +02004998 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004999 case RTL_GIGA_MAC_VER_26:
5000 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005001 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005002 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02005003
françois romieue6de30d2011-01-03 15:08:37 +00005004 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005005 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005006 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02005007
hayeswang4804b3b2011-03-21 01:50:29 +00005008 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005009 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005010 break;
5011
hayeswang01dc7fe2011-03-21 01:50:28 +00005012 case RTL_GIGA_MAC_VER_32:
5013 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005014 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005015 break;
5016 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005017 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005018 break;
françois romieue6de30d2011-01-03 15:08:37 +00005019
Hayes Wangc2218922011-09-06 16:55:18 +08005020 case RTL_GIGA_MAC_VER_35:
5021 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005022 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005023 break;
5024
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005025 case RTL_GIGA_MAC_VER_38:
5026 rtl_hw_start_8411(tp);
5027 break;
5028
Francois Romieu219a1e92008-06-28 11:58:39 +02005029 default:
5030 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
5031 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00005032 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005033 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005034
Francois Romieu0e485152007-02-20 00:00:26 +01005035 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5036
Francois Romieub8363902008-06-01 12:31:57 +02005037 RTL_W8(Cfg9346, Cfg9346_Lock);
5038
Francois Romieu2dd99532007-06-11 23:22:52 +02005039 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005040}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041
Francois Romieu2857ffb2008-08-02 21:08:49 +02005042#define R810X_CPCMD_QUIRK_MASK (\
5043 EnableBist | \
5044 Mac_dbgo_oe | \
5045 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00005046 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02005047 Force_txflow_en | \
5048 Cxpl_dbg_sel | \
5049 ASF | \
5050 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005051 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005052
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005053static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005054{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005055 void __iomem *ioaddr = tp->mmio_addr;
5056 struct pci_dev *pdev = tp->pci_dev;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005057 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02005058 { 0x01, 0, 0x6e65 },
5059 { 0x02, 0, 0x091f },
5060 { 0x03, 0, 0xc2f9 },
5061 { 0x06, 0, 0xafb5 },
5062 { 0x07, 0, 0x0e00 },
5063 { 0x19, 0, 0xec80 },
5064 { 0x01, 0, 0x2e65 },
5065 { 0x01, 0, 0x6e65 }
5066 };
5067 u8 cfg1;
5068
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005069 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005070
5071 RTL_W8(DBG_REG, FIX_NAK_1);
5072
5073 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5074
5075 RTL_W8(Config1,
5076 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5077 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5078
5079 cfg1 = RTL_R8(Config1);
5080 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5081 RTL_W8(Config1, cfg1 & ~LEDS0);
5082
Francois Romieufdf6fc02012-07-06 22:40:38 +02005083 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02005084}
5085
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005086static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005087{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005088 void __iomem *ioaddr = tp->mmio_addr;
5089 struct pci_dev *pdev = tp->pci_dev;
5090
5091 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005092
5093 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5094
5095 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5096 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005097}
5098
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005099static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005100{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005101 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005102
Francois Romieufdf6fc02012-07-06 22:40:38 +02005103 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005104}
5105
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005106static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005107{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005108 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005109 static const struct ephy_info e_info_8105e_1[] = {
5110 { 0x07, 0, 0x4000 },
5111 { 0x19, 0, 0x0200 },
5112 { 0x19, 0, 0x0020 },
5113 { 0x1e, 0, 0x2000 },
5114 { 0x03, 0, 0x0001 },
5115 { 0x19, 0, 0x0100 },
5116 { 0x19, 0, 0x0004 },
5117 { 0x0a, 0, 0x0020 }
5118 };
5119
Francois Romieucecb5fd2011-04-01 10:21:07 +02005120 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005121 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5122
Francois Romieucecb5fd2011-04-01 10:21:07 +02005123 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005124 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5125
5126 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08005127 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005128
Francois Romieufdf6fc02012-07-06 22:40:38 +02005129 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
Hayes Wang5a5e4442011-02-22 17:26:21 +08005130}
5131
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005132static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005133{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005134 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005135 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005136}
5137
Hayes Wang7e18dca2012-03-30 14:33:02 +08005138static void rtl_hw_start_8402(struct rtl8169_private *tp)
5139{
5140 void __iomem *ioaddr = tp->mmio_addr;
5141 static const struct ephy_info e_info_8402[] = {
5142 { 0x19, 0xffff, 0xff64 },
5143 { 0x1e, 0, 0x4000 }
5144 };
5145
5146 rtl_csi_access_enable_2(tp);
5147
5148 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5149 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5150
5151 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5152 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5153
Francois Romieufdf6fc02012-07-06 22:40:38 +02005154 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08005155
5156 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5157
Francois Romieufdf6fc02012-07-06 22:40:38 +02005158 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5159 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
5160 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5161 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5162 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5163 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5164 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005165}
5166
Hayes Wang5598bfe2012-07-02 17:23:21 +08005167static void rtl_hw_start_8106(struct rtl8169_private *tp)
5168{
5169 void __iomem *ioaddr = tp->mmio_addr;
5170
5171 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5172 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5173
5174 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5175 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
5176 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
5177}
5178
Francois Romieu07ce4062007-02-23 23:36:39 +01005179static void rtl_hw_start_8101(struct net_device *dev)
5180{
Francois Romieucdf1a602007-06-11 23:29:50 +02005181 struct rtl8169_private *tp = netdev_priv(dev);
5182 void __iomem *ioaddr = tp->mmio_addr;
5183 struct pci_dev *pdev = tp->pci_dev;
5184
Francois Romieuda78dbf2012-01-26 14:18:23 +01005185 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5186 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00005187
Francois Romieucecb5fd2011-04-01 10:21:07 +02005188 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5189 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00005190 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02005191
5192 if (cap) {
5193 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
5194 PCI_EXP_DEVCTL_NOSNOOP_EN);
5195 }
Francois Romieucdf1a602007-06-11 23:29:50 +02005196 }
5197
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005198 RTL_W8(Cfg9346, Cfg9346_Unlock);
5199
Francois Romieu2857ffb2008-08-02 21:08:49 +02005200 switch (tp->mac_version) {
5201 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005202 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005203 break;
5204
5205 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005206 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005207 break;
5208
5209 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005210 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005211 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005212
5213 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005214 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005215 break;
5216 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005217 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005218 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005219
5220 case RTL_GIGA_MAC_VER_37:
5221 rtl_hw_start_8402(tp);
5222 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08005223
5224 case RTL_GIGA_MAC_VER_39:
5225 rtl_hw_start_8106(tp);
5226 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02005227 }
5228
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005229 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02005230
françois romieuf0298f82011-01-03 15:07:42 +00005231 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02005232
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005233 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02005234
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005235 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02005236 RTL_W16(CPlusCmd, tp->cp_cmd);
5237
5238 RTL_W16(IntrMitigate, 0x0000);
5239
5240 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5241
5242 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5243 rtl_set_rx_tx_config_registers(tp);
5244
Francois Romieucdf1a602007-06-11 23:29:50 +02005245 RTL_R8(IntrMask);
5246
Francois Romieucdf1a602007-06-11 23:29:50 +02005247 rtl_set_rx_mode(dev);
5248
5249 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250}
5251
5252static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5253{
Francois Romieud58d46b2011-05-03 16:38:29 +02005254 struct rtl8169_private *tp = netdev_priv(dev);
5255
5256 if (new_mtu < ETH_ZLEN ||
5257 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 return -EINVAL;
5259
Francois Romieud58d46b2011-05-03 16:38:29 +02005260 if (new_mtu > ETH_DATA_LEN)
5261 rtl_hw_jumbo_enable(tp);
5262 else
5263 rtl_hw_jumbo_disable(tp);
5264
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005266 netdev_update_features(dev);
5267
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005268 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269}
5270
5271static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5272{
Al Viro95e09182007-12-22 18:55:39 +00005273 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5275}
5276
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005277static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5278 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005280 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005281 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005282
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005283 kfree(*data_buff);
5284 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 rtl8169_make_unusable_by_asic(desc);
5286}
5287
5288static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5289{
5290 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5291
5292 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5293}
5294
5295static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5296 u32 rx_buf_sz)
5297{
5298 desc->addr = cpu_to_le64(mapping);
5299 wmb();
5300 rtl8169_mark_to_asic(desc, rx_buf_sz);
5301}
5302
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005303static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005305 return (void *)ALIGN((long)data, 16);
5306}
5307
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005308static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5309 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005310{
5311 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005313 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005314 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005315 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005317 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5318 if (!data)
5319 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005320
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005321 if (rtl8169_align(data) != data) {
5322 kfree(data);
5323 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5324 if (!data)
5325 return NULL;
5326 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005327
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005328 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005329 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005330 if (unlikely(dma_mapping_error(d, mapping))) {
5331 if (net_ratelimit())
5332 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005333 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335
5336 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005337 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005338
5339err_out:
5340 kfree(data);
5341 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342}
5343
5344static void rtl8169_rx_clear(struct rtl8169_private *tp)
5345{
Francois Romieu07d3f512007-02-21 22:40:46 +01005346 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347
5348 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005349 if (tp->Rx_databuff[i]) {
5350 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 tp->RxDescArray + i);
5352 }
5353 }
5354}
5355
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005356static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005358 desc->opts1 |= cpu_to_le32(RingEnd);
5359}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005360
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005361static int rtl8169_rx_fill(struct rtl8169_private *tp)
5362{
5363 unsigned int i;
5364
5365 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005366 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005367
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005368 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005370
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005371 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005372 if (!data) {
5373 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005374 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005375 }
5376 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005379 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5380 return 0;
5381
5382err_out:
5383 rtl8169_rx_clear(tp);
5384 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385}
5386
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387static int rtl8169_init_ring(struct net_device *dev)
5388{
5389 struct rtl8169_private *tp = netdev_priv(dev);
5390
5391 rtl8169_init_ring_indexes(tp);
5392
5393 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005394 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005396 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397}
5398
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005399static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 struct TxDesc *desc)
5401{
5402 unsigned int len = tx_skb->len;
5403
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005404 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5405
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 desc->opts1 = 0x00;
5407 desc->opts2 = 0x00;
5408 desc->addr = 0x00;
5409 tx_skb->len = 0;
5410}
5411
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005412static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5413 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414{
5415 unsigned int i;
5416
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005417 for (i = 0; i < n; i++) {
5418 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 struct ring_info *tx_skb = tp->tx_skb + entry;
5420 unsigned int len = tx_skb->len;
5421
5422 if (len) {
5423 struct sk_buff *skb = tx_skb->skb;
5424
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005425 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 tp->TxDescArray + entry);
5427 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005428 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 dev_kfree_skb(skb);
5430 tx_skb->skb = NULL;
5431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 }
5433 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005434}
5435
5436static void rtl8169_tx_clear(struct rtl8169_private *tp)
5437{
5438 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 tp->cur_tx = tp->dirty_tx = 0;
Igor Maravic036dafa2012-03-05 00:01:25 +01005440 netdev_reset_queue(tp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441}
5442
Francois Romieu4422bcd2012-01-26 11:23:32 +01005443static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444{
David Howellsc4028952006-11-22 14:57:56 +00005445 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005446 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
Francois Romieuda78dbf2012-01-26 14:18:23 +01005448 napi_disable(&tp->napi);
5449 netif_stop_queue(dev);
5450 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451
françois romieuc7c2c392011-12-04 20:30:52 +00005452 rtl8169_hw_reset(tp);
5453
Francois Romieu56de4142011-03-15 17:29:31 +01005454 for (i = 0; i < NUM_RX_DESC; i++)
5455 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5456
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005458 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459
Francois Romieuda78dbf2012-01-26 14:18:23 +01005460 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01005461 rtl_hw_start(dev);
5462 netif_wake_queue(dev);
5463 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464}
5465
5466static void rtl8169_tx_timeout(struct net_device *dev)
5467{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005468 struct rtl8169_private *tp = netdev_priv(dev);
5469
5470 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471}
5472
5473static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005474 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475{
5476 struct skb_shared_info *info = skb_shinfo(skb);
5477 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005478 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005479 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480
5481 entry = tp->cur_tx;
5482 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005483 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 dma_addr_t mapping;
5485 u32 status, len;
5486 void *addr;
5487
5488 entry = (entry + 1) % NUM_TX_DESC;
5489
5490 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005491 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005492 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005493 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005494 if (unlikely(dma_mapping_error(d, mapping))) {
5495 if (net_ratelimit())
5496 netif_err(tp, drv, tp->dev,
5497 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005498 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500
Francois Romieucecb5fd2011-04-01 10:21:07 +02005501 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005502 status = opts[0] | len |
5503 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504
5505 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005506 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 txd->addr = cpu_to_le64(mapping);
5508
5509 tp->tx_skb[entry].len = len;
5510 }
5511
5512 if (cur_frag) {
5513 tp->tx_skb[entry].skb = skb;
5514 txd->opts1 |= cpu_to_le32(LastFrag);
5515 }
5516
5517 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005518
5519err_out:
5520 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5521 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522}
5523
Francois Romieu2b7b4312011-04-18 22:53:24 -07005524static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5525 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005527 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005528 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005529 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530
Francois Romieu2b7b4312011-04-18 22:53:24 -07005531 if (mss) {
5532 opts[0] |= TD_LSO;
5533 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5534 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005535 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536
5537 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005538 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005540 opts[offset] |= info->checksum.udp;
5541 else
5542 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544}
5545
Stephen Hemminger613573252009-08-31 19:50:58 +00005546static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5547 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548{
5549 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005550 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 struct TxDesc *txd = tp->TxDescArray + entry;
5552 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005553 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 dma_addr_t mapping;
5555 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005556 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005557 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005558
Julien Ducourthial477206a2012-05-09 00:00:06 +02005559 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005560 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005561 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 }
5563
5564 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005565 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005567 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005568 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005569 if (unlikely(dma_mapping_error(d, mapping))) {
5570 if (net_ratelimit())
5571 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005572 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574
5575 tp->tx_skb[entry].len = len;
5576 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577
Francois Romieu2b7b4312011-04-18 22:53:24 -07005578 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5579 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005580
Francois Romieu2b7b4312011-04-18 22:53:24 -07005581 rtl8169_tso_csum(tp, skb, opts);
5582
5583 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005584 if (frags < 0)
5585 goto err_dma_1;
5586 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005587 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005588 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005589 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005590 tp->tx_skb[entry].skb = skb;
5591 }
5592
Francois Romieu2b7b4312011-04-18 22:53:24 -07005593 txd->opts2 = cpu_to_le32(opts[1]);
5594
Igor Maravic036dafa2012-03-05 00:01:25 +01005595 netdev_sent_queue(dev, skb->len);
5596
Richard Cochran5047fb52012-03-10 07:29:42 +00005597 skb_tx_timestamp(skb);
5598
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 wmb();
5600
Francois Romieucecb5fd2011-04-01 10:21:07 +02005601 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005602 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 txd->opts1 = cpu_to_le32(status);
5604
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 tp->cur_tx += frags + 1;
5606
David Dillow4c020a92010-03-03 16:33:10 +00005607 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608
Francois Romieucecb5fd2011-04-01 10:21:07 +02005609 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
Francois Romieuda78dbf2012-01-26 14:18:23 +01005611 mmiowb();
5612
Julien Ducourthial477206a2012-05-09 00:00:06 +02005613 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01005614 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5615 * not miss a ring update when it notices a stopped queue.
5616 */
5617 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01005619 /* Sync with rtl_tx:
5620 * - publish queue status and cur_tx ring index (write barrier)
5621 * - refresh dirty_tx ring index (read barrier).
5622 * May the current thread have a pessimistic view of the ring
5623 * status and forget to wake up queue, a racing rtl_tx thread
5624 * can't.
5625 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005626 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02005627 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 netif_wake_queue(dev);
5629 }
5630
Stephen Hemminger613573252009-08-31 19:50:58 +00005631 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005633err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005634 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005635err_dma_0:
5636 dev_kfree_skb(skb);
5637 dev->stats.tx_dropped++;
5638 return NETDEV_TX_OK;
5639
5640err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005642 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005643 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644}
5645
5646static void rtl8169_pcierr_interrupt(struct net_device *dev)
5647{
5648 struct rtl8169_private *tp = netdev_priv(dev);
5649 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 u16 pci_status, pci_cmd;
5651
5652 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5653 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5654
Joe Perchesbf82c182010-02-09 11:49:50 +00005655 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5656 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657
5658 /*
5659 * The recovery sequence below admits a very elaborated explanation:
5660 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005661 * - I did not see what else could be done;
5662 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 *
5664 * Feel free to adjust to your needs.
5665 */
Francois Romieua27993f2006-12-18 00:04:19 +01005666 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005667 pci_cmd &= ~PCI_COMMAND_PARITY;
5668 else
5669 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5670
5671 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672
5673 pci_write_config_word(pdev, PCI_STATUS,
5674 pci_status & (PCI_STATUS_DETECTED_PARITY |
5675 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5676 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5677
5678 /* The infamous DAC f*ckup only happens at boot time */
5679 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005680 void __iomem *ioaddr = tp->mmio_addr;
5681
Joe Perchesbf82c182010-02-09 11:49:50 +00005682 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 tp->cp_cmd &= ~PCIDAC;
5684 RTL_W16(CPlusCmd, tp->cp_cmd);
5685 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 }
5687
françois romieue6de30d2011-01-03 15:08:37 +00005688 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005689
Francois Romieu98ddf982012-01-31 10:47:34 +01005690 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691}
5692
Igor Maravic036dafa2012-03-05 00:01:25 +01005693struct rtl_txc {
5694 int packets;
5695 int bytes;
5696};
5697
Francois Romieuda78dbf2012-01-26 14:18:23 +01005698static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699{
Igor Maravic036dafa2012-03-05 00:01:25 +01005700 struct rtl8169_stats *tx_stats = &tp->tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 unsigned int dirty_tx, tx_left;
Igor Maravic036dafa2012-03-05 00:01:25 +01005702 struct rtl_txc txc = { 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 dirty_tx = tp->dirty_tx;
5705 smp_rmb();
5706 tx_left = tp->cur_tx - dirty_tx;
5707
5708 while (tx_left > 0) {
5709 unsigned int entry = dirty_tx % NUM_TX_DESC;
5710 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 u32 status;
5712
5713 rmb();
5714 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5715 if (status & DescOwn)
5716 break;
5717
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005718 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5719 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 if (status & LastFrag) {
Igor Maravic036dafa2012-03-05 00:01:25 +01005721 struct sk_buff *skb = tx_skb->skb;
5722
5723 txc.packets++;
5724 txc.bytes += skb->len;
5725 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 tx_skb->skb = NULL;
5727 }
5728 dirty_tx++;
5729 tx_left--;
5730 }
5731
Igor Maravic036dafa2012-03-05 00:01:25 +01005732 u64_stats_update_begin(&tx_stats->syncp);
5733 tx_stats->packets += txc.packets;
5734 tx_stats->bytes += txc.bytes;
5735 u64_stats_update_end(&tx_stats->syncp);
5736
5737 netdev_completed_queue(dev, txc.packets, txc.bytes);
5738
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 if (tp->dirty_tx != dirty_tx) {
5740 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005741 /* Sync with rtl8169_start_xmit:
5742 * - publish dirty_tx ring index (write barrier)
5743 * - refresh cur_tx ring index and queue status (read barrier)
5744 * May the current thread miss the stopped queue condition,
5745 * a racing xmit thread can only have a right view of the
5746 * ring status.
5747 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005748 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02005750 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 netif_wake_queue(dev);
5752 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005753 /*
5754 * 8168 hack: TxPoll requests are lost when the Tx packets are
5755 * too close. Let's kick an extra TxPoll request when a burst
5756 * of start_xmit activity is detected (if it is not detected,
5757 * it is slow enough). -- FR
5758 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005759 if (tp->cur_tx != dirty_tx) {
5760 void __iomem *ioaddr = tp->mmio_addr;
5761
Francois Romieud78ae2d2007-08-26 20:08:19 +02005762 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 }
5765}
5766
Francois Romieu126fa4b2005-05-12 20:09:17 -04005767static inline int rtl8169_fragmented_frame(u32 status)
5768{
5769 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5770}
5771
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005772static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 u32 status = opts1 & RxProtoMask;
5775
5776 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005777 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 skb->ip_summed = CHECKSUM_UNNECESSARY;
5779 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005780 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781}
5782
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005783static struct sk_buff *rtl8169_try_rx_copy(void *data,
5784 struct rtl8169_private *tp,
5785 int pkt_size,
5786 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005788 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005789 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005791 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005792 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005793 prefetch(data);
5794 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5795 if (skb)
5796 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005797 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5798
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005799 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800}
5801
Francois Romieuda78dbf2012-01-26 14:18:23 +01005802static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803{
5804 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005805 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 cur_rx = tp->cur_rx;
5808 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005809 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005811 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005813 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 u32 status;
5815
5816 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005817 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818
5819 if (status & DescOwn)
5820 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005821 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005822 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5823 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005824 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005826 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005828 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005829 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005830 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005831 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005832 }
Ben Greear6bbe0212012-02-10 15:04:33 +00005833 if ((status & (RxRUNT | RxCRC)) &&
5834 !(status & (RxRWT | RxFOVF)) &&
5835 (dev->features & NETIF_F_RXALL))
5836 goto process_pkt;
5837
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005838 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005840 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00005841 dma_addr_t addr;
5842 int pkt_size;
5843
5844process_pkt:
5845 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00005846 if (likely(!(dev->features & NETIF_F_RXFCS)))
5847 pkt_size = (status & 0x00003fff) - 4;
5848 else
5849 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850
Francois Romieu126fa4b2005-05-12 20:09:17 -04005851 /*
5852 * The driver does not support incoming fragmented
5853 * frames. They are seen as a symptom of over-mtu
5854 * sized frames.
5855 */
5856 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005857 dev->stats.rx_dropped++;
5858 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005859 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005860 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005861 }
5862
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005863 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5864 tp, pkt_size, addr);
5865 rtl8169_mark_to_asic(desc, rx_buf_sz);
5866 if (!skb) {
5867 dev->stats.rx_dropped++;
5868 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 }
5870
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005871 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 skb_put(skb, pkt_size);
5873 skb->protocol = eth_type_trans(skb, dev);
5874
Francois Romieu7a8fc772011-03-01 17:18:33 +01005875 rtl8169_rx_vlan_tag(desc, skb);
5876
Francois Romieu56de4142011-03-15 17:29:31 +01005877 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878
Junchang Wang8027aa22012-03-04 23:30:32 +01005879 u64_stats_update_begin(&tp->rx_stats.syncp);
5880 tp->rx_stats.packets++;
5881 tp->rx_stats.bytes += pkt_size;
5882 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005884
5885 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005886 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005887 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5888 desc->opts2 = 0;
5889 cur_rx++;
5890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 }
5892
5893 count = cur_rx - tp->cur_rx;
5894 tp->cur_rx = cur_rx;
5895
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005896 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897
5898 return count;
5899}
5900
Francois Romieu07d3f512007-02-21 22:40:46 +01005901static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902{
Francois Romieu07d3f512007-02-21 22:40:46 +01005903 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005906 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005908 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005909 if (status && status != 0xffff) {
5910 status &= RTL_EVENT_NAPI | tp->event_slow;
5911 if (status) {
5912 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00005913
Francois Romieuda78dbf2012-01-26 14:18:23 +01005914 rtl_irq_disable(tp);
5915 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 return IRQ_RETVAL(handled);
5919}
5920
Francois Romieuda78dbf2012-01-26 14:18:23 +01005921/*
5922 * Workqueue context.
5923 */
5924static void rtl_slow_event_work(struct rtl8169_private *tp)
5925{
5926 struct net_device *dev = tp->dev;
5927 u16 status;
5928
5929 status = rtl_get_events(tp) & tp->event_slow;
5930 rtl_ack_events(tp, status);
5931
5932 if (unlikely(status & RxFIFOOver)) {
5933 switch (tp->mac_version) {
5934 /* Work around for rx fifo overflow */
5935 case RTL_GIGA_MAC_VER_11:
5936 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01005937 /* XXX - Hack alert. See rtl_task(). */
5938 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005939 default:
5940 break;
5941 }
5942 }
5943
5944 if (unlikely(status & SYSErr))
5945 rtl8169_pcierr_interrupt(dev);
5946
5947 if (status & LinkChg)
5948 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
5949
françois romieu7dbb4912012-06-09 10:53:16 +00005950 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005951}
5952
Francois Romieu4422bcd2012-01-26 11:23:32 +01005953static void rtl_task(struct work_struct *work)
5954{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005955 static const struct {
5956 int bitnr;
5957 void (*action)(struct rtl8169_private *);
5958 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01005959 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005960 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
5961 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
5962 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
5963 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01005964 struct rtl8169_private *tp =
5965 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005966 struct net_device *dev = tp->dev;
5967 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01005968
Francois Romieuda78dbf2012-01-26 14:18:23 +01005969 rtl_lock_work(tp);
5970
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005971 if (!netif_running(dev) ||
5972 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01005973 goto out_unlock;
5974
5975 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
5976 bool pending;
5977
Francois Romieuda78dbf2012-01-26 14:18:23 +01005978 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005979 if (pending)
5980 rtl_work[i].action(tp);
5981 }
5982
5983out_unlock:
5984 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01005985}
5986
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005987static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005989 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5990 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005991 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
5992 int work_done= 0;
5993 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994
Francois Romieuda78dbf2012-01-26 14:18:23 +01005995 status = rtl_get_events(tp);
5996 rtl_ack_events(tp, status & ~tp->event_slow);
5997
5998 if (status & RTL_EVENT_NAPI_RX)
5999 work_done = rtl_rx(dev, tp, (u32) budget);
6000
6001 if (status & RTL_EVENT_NAPI_TX)
6002 rtl_tx(dev, tp);
6003
6004 if (status & tp->event_slow) {
6005 enable_mask &= ~tp->event_slow;
6006
6007 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
6008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006010 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006011 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00006012
Francois Romieuda78dbf2012-01-26 14:18:23 +01006013 rtl_irq_enable(tp, enable_mask);
6014 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 }
6016
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006017 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019
Francois Romieu523a6092008-09-10 22:28:56 +02006020static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
6021{
6022 struct rtl8169_private *tp = netdev_priv(dev);
6023
6024 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6025 return;
6026
6027 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
6028 RTL_W32(RxMissed, 0);
6029}
6030
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031static void rtl8169_down(struct net_device *dev)
6032{
6033 struct rtl8169_private *tp = netdev_priv(dev);
6034 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035
Francois Romieu4876cc12011-03-11 21:07:11 +01006036 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01006038 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006039 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040
Hayes Wang92fc43b2011-07-06 15:58:03 +08006041 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006042 /*
6043 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01006044 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6045 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006046 */
Francois Romieu523a6092008-09-10 22:28:56 +02006047 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006050 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 rtl8169_tx_clear(tp);
6053
6054 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00006055
6056 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057}
6058
6059static int rtl8169_close(struct net_device *dev)
6060{
6061 struct rtl8169_private *tp = netdev_priv(dev);
6062 struct pci_dev *pdev = tp->pci_dev;
6063
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006064 pm_runtime_get_sync(&pdev->dev);
6065
Francois Romieucecb5fd2011-04-01 10:21:07 +02006066 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08006067 rtl8169_update_counters(dev);
6068
Francois Romieuda78dbf2012-01-26 14:18:23 +01006069 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006070 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006071
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006073 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006075 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00006077 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6078 tp->RxPhyAddr);
6079 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6080 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 tp->TxDescArray = NULL;
6082 tp->RxDescArray = NULL;
6083
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006084 pm_runtime_put_sync(&pdev->dev);
6085
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 return 0;
6087}
6088
Francois Romieudc1c00c2012-03-08 10:06:18 +01006089#ifdef CONFIG_NET_POLL_CONTROLLER
6090static void rtl8169_netpoll(struct net_device *dev)
6091{
6092 struct rtl8169_private *tp = netdev_priv(dev);
6093
6094 rtl8169_interrupt(tp->pci_dev->irq, dev);
6095}
6096#endif
6097
Francois Romieudf43ac72012-03-08 09:48:40 +01006098static int rtl_open(struct net_device *dev)
6099{
6100 struct rtl8169_private *tp = netdev_priv(dev);
6101 void __iomem *ioaddr = tp->mmio_addr;
6102 struct pci_dev *pdev = tp->pci_dev;
6103 int retval = -ENOMEM;
6104
6105 pm_runtime_get_sync(&pdev->dev);
6106
6107 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02006108 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01006109 * dma_alloc_coherent provides more.
6110 */
6111 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6112 &tp->TxPhyAddr, GFP_KERNEL);
6113 if (!tp->TxDescArray)
6114 goto err_pm_runtime_put;
6115
6116 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6117 &tp->RxPhyAddr, GFP_KERNEL);
6118 if (!tp->RxDescArray)
6119 goto err_free_tx_0;
6120
6121 retval = rtl8169_init_ring(dev);
6122 if (retval < 0)
6123 goto err_free_rx_1;
6124
6125 INIT_WORK(&tp->wk.work, rtl_task);
6126
6127 smp_mb();
6128
6129 rtl_request_firmware(tp);
6130
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006131 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01006132 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
6133 dev->name, dev);
6134 if (retval < 0)
6135 goto err_release_fw_2;
6136
6137 rtl_lock_work(tp);
6138
6139 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6140
6141 napi_enable(&tp->napi);
6142
6143 rtl8169_init_phy(dev, tp);
6144
6145 __rtl8169_set_features(dev, dev->features);
6146
6147 rtl_pll_power_up(tp);
6148
6149 rtl_hw_start(dev);
6150
6151 netif_start_queue(dev);
6152
6153 rtl_unlock_work(tp);
6154
6155 tp->saved_wolopts = 0;
6156 pm_runtime_put_noidle(&pdev->dev);
6157
6158 rtl8169_check_link_status(dev, tp, ioaddr);
6159out:
6160 return retval;
6161
6162err_release_fw_2:
6163 rtl_release_firmware(tp);
6164 rtl8169_rx_clear(tp);
6165err_free_rx_1:
6166 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6167 tp->RxPhyAddr);
6168 tp->RxDescArray = NULL;
6169err_free_tx_0:
6170 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6171 tp->TxPhyAddr);
6172 tp->TxDescArray = NULL;
6173err_pm_runtime_put:
6174 pm_runtime_put_noidle(&pdev->dev);
6175 goto out;
6176}
6177
Junchang Wang8027aa22012-03-04 23:30:32 +01006178static struct rtnl_link_stats64 *
6179rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180{
6181 struct rtl8169_private *tp = netdev_priv(dev);
6182 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01006183 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184
Francois Romieuda78dbf2012-01-26 14:18:23 +01006185 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02006186 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02006187
Junchang Wang8027aa22012-03-04 23:30:32 +01006188 do {
6189 start = u64_stats_fetch_begin_bh(&tp->rx_stats.syncp);
6190 stats->rx_packets = tp->rx_stats.packets;
6191 stats->rx_bytes = tp->rx_stats.bytes;
6192 } while (u64_stats_fetch_retry_bh(&tp->rx_stats.syncp, start));
6193
6194
6195 do {
6196 start = u64_stats_fetch_begin_bh(&tp->tx_stats.syncp);
6197 stats->tx_packets = tp->tx_stats.packets;
6198 stats->tx_bytes = tp->tx_stats.bytes;
6199 } while (u64_stats_fetch_retry_bh(&tp->tx_stats.syncp, start));
6200
6201 stats->rx_dropped = dev->stats.rx_dropped;
6202 stats->tx_dropped = dev->stats.tx_dropped;
6203 stats->rx_length_errors = dev->stats.rx_length_errors;
6204 stats->rx_errors = dev->stats.rx_errors;
6205 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6206 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6207 stats->rx_missed_errors = dev->stats.rx_missed_errors;
6208
6209 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210}
6211
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006212static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006213{
françois romieu065c27c2011-01-03 15:08:12 +00006214 struct rtl8169_private *tp = netdev_priv(dev);
6215
Francois Romieu5d06a992006-02-23 00:47:58 +01006216 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006217 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006218
6219 netif_device_detach(dev);
6220 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006221
6222 rtl_lock_work(tp);
6223 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006224 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006225 rtl_unlock_work(tp);
6226
6227 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006228}
Francois Romieu5d06a992006-02-23 00:47:58 +01006229
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006230#ifdef CONFIG_PM
6231
6232static int rtl8169_suspend(struct device *device)
6233{
6234 struct pci_dev *pdev = to_pci_dev(device);
6235 struct net_device *dev = pci_get_drvdata(pdev);
6236
6237 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02006238
Francois Romieu5d06a992006-02-23 00:47:58 +01006239 return 0;
6240}
6241
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006242static void __rtl8169_resume(struct net_device *dev)
6243{
françois romieu065c27c2011-01-03 15:08:12 +00006244 struct rtl8169_private *tp = netdev_priv(dev);
6245
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006246 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006247
6248 rtl_pll_power_up(tp);
6249
Artem Savkovcff4c162012-04-03 10:29:11 +00006250 rtl_lock_work(tp);
6251 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006252 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00006253 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006254
Francois Romieu98ddf982012-01-31 10:47:34 +01006255 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006256}
6257
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006258static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006259{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006260 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01006261 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006262 struct rtl8169_private *tp = netdev_priv(dev);
6263
6264 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01006265
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006266 if (netif_running(dev))
6267 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006268
Francois Romieu5d06a992006-02-23 00:47:58 +01006269 return 0;
6270}
6271
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006272static int rtl8169_runtime_suspend(struct device *device)
6273{
6274 struct pci_dev *pdev = to_pci_dev(device);
6275 struct net_device *dev = pci_get_drvdata(pdev);
6276 struct rtl8169_private *tp = netdev_priv(dev);
6277
6278 if (!tp->TxDescArray)
6279 return 0;
6280
Francois Romieuda78dbf2012-01-26 14:18:23 +01006281 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006282 tp->saved_wolopts = __rtl8169_get_wol(tp);
6283 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006284 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006285
6286 rtl8169_net_suspend(dev);
6287
6288 return 0;
6289}
6290
6291static int rtl8169_runtime_resume(struct device *device)
6292{
6293 struct pci_dev *pdev = to_pci_dev(device);
6294 struct net_device *dev = pci_get_drvdata(pdev);
6295 struct rtl8169_private *tp = netdev_priv(dev);
6296
6297 if (!tp->TxDescArray)
6298 return 0;
6299
Francois Romieuda78dbf2012-01-26 14:18:23 +01006300 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006301 __rtl8169_set_wol(tp, tp->saved_wolopts);
6302 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006303 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006304
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006305 rtl8169_init_phy(dev, tp);
6306
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006307 __rtl8169_resume(dev);
6308
6309 return 0;
6310}
6311
6312static int rtl8169_runtime_idle(struct device *device)
6313{
6314 struct pci_dev *pdev = to_pci_dev(device);
6315 struct net_device *dev = pci_get_drvdata(pdev);
6316 struct rtl8169_private *tp = netdev_priv(dev);
6317
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00006318 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006319}
6320
Alexey Dobriyan47145212009-12-14 18:00:08 -08006321static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006322 .suspend = rtl8169_suspend,
6323 .resume = rtl8169_resume,
6324 .freeze = rtl8169_suspend,
6325 .thaw = rtl8169_resume,
6326 .poweroff = rtl8169_suspend,
6327 .restore = rtl8169_resume,
6328 .runtime_suspend = rtl8169_runtime_suspend,
6329 .runtime_resume = rtl8169_runtime_resume,
6330 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006331};
6332
6333#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6334
6335#else /* !CONFIG_PM */
6336
6337#define RTL8169_PM_OPS NULL
6338
6339#endif /* !CONFIG_PM */
6340
David S. Miller1805b2f2011-10-24 18:18:09 -04006341static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6342{
6343 void __iomem *ioaddr = tp->mmio_addr;
6344
6345 /* WoL fails with 8168b when the receiver is disabled. */
6346 switch (tp->mac_version) {
6347 case RTL_GIGA_MAC_VER_11:
6348 case RTL_GIGA_MAC_VER_12:
6349 case RTL_GIGA_MAC_VER_17:
6350 pci_clear_master(tp->pci_dev);
6351
6352 RTL_W8(ChipCmd, CmdRxEnb);
6353 /* PCI commit */
6354 RTL_R8(ChipCmd);
6355 break;
6356 default:
6357 break;
6358 }
6359}
6360
Francois Romieu1765f952008-09-13 17:21:40 +02006361static void rtl_shutdown(struct pci_dev *pdev)
6362{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006363 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006364 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00006365 struct device *d = &pdev->dev;
6366
6367 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02006368
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006369 rtl8169_net_suspend(dev);
6370
Francois Romieucecb5fd2011-04-01 10:21:07 +02006371 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006372 rtl_rar_set(tp, dev->perm_addr);
6373
Hayes Wang92fc43b2011-07-06 15:58:03 +08006374 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006375
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006376 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006377 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6378 rtl_wol_suspend_quirk(tp);
6379 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006380 }
6381
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006382 pci_wake_from_d3(pdev, true);
6383 pci_set_power_state(pdev, PCI_D3hot);
6384 }
françois romieu2a15cd22012-03-06 01:14:12 +00006385
6386 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006387}
Francois Romieu5d06a992006-02-23 00:47:58 +01006388
Francois Romieue27566e2012-03-08 09:54:01 +01006389static void __devexit rtl_remove_one(struct pci_dev *pdev)
6390{
6391 struct net_device *dev = pci_get_drvdata(pdev);
6392 struct rtl8169_private *tp = netdev_priv(dev);
6393
6394 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6395 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6396 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6397 rtl8168_driver_stop(tp);
6398 }
6399
6400 cancel_work_sync(&tp->wk.work);
6401
Devendra Nagaad1be8d2012-05-31 01:51:20 +00006402 netif_napi_del(&tp->napi);
6403
Francois Romieue27566e2012-03-08 09:54:01 +01006404 unregister_netdev(dev);
6405
6406 rtl_release_firmware(tp);
6407
6408 if (pci_dev_run_wake(pdev))
6409 pm_runtime_get_noresume(&pdev->dev);
6410
6411 /* restore original MAC address */
6412 rtl_rar_set(tp, dev->perm_addr);
6413
6414 rtl_disable_msi(pdev, tp);
6415 rtl8169_release_board(pdev, dev, tp->mmio_addr);
6416 pci_set_drvdata(pdev, NULL);
6417}
6418
Francois Romieufa9c3852012-03-08 10:01:50 +01006419static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01006420 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01006421 .ndo_stop = rtl8169_close,
6422 .ndo_get_stats64 = rtl8169_get_stats64,
6423 .ndo_start_xmit = rtl8169_start_xmit,
6424 .ndo_tx_timeout = rtl8169_tx_timeout,
6425 .ndo_validate_addr = eth_validate_addr,
6426 .ndo_change_mtu = rtl8169_change_mtu,
6427 .ndo_fix_features = rtl8169_fix_features,
6428 .ndo_set_features = rtl8169_set_features,
6429 .ndo_set_mac_address = rtl_set_mac_address,
6430 .ndo_do_ioctl = rtl8169_ioctl,
6431 .ndo_set_rx_mode = rtl_set_rx_mode,
6432#ifdef CONFIG_NET_POLL_CONTROLLER
6433 .ndo_poll_controller = rtl8169_netpoll,
6434#endif
6435
6436};
6437
Francois Romieu31fa8b12012-03-08 10:09:40 +01006438static const struct rtl_cfg_info {
6439 void (*hw_start)(struct net_device *);
6440 unsigned int region;
6441 unsigned int align;
6442 u16 event_slow;
6443 unsigned features;
6444 u8 default_ver;
6445} rtl_cfg_infos [] = {
6446 [RTL_CFG_0] = {
6447 .hw_start = rtl_hw_start_8169,
6448 .region = 1,
6449 .align = 0,
6450 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
6451 .features = RTL_FEATURE_GMII,
6452 .default_ver = RTL_GIGA_MAC_VER_01,
6453 },
6454 [RTL_CFG_1] = {
6455 .hw_start = rtl_hw_start_8168,
6456 .region = 2,
6457 .align = 8,
6458 .event_slow = SYSErr | LinkChg | RxOverflow,
6459 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
6460 .default_ver = RTL_GIGA_MAC_VER_11,
6461 },
6462 [RTL_CFG_2] = {
6463 .hw_start = rtl_hw_start_8101,
6464 .region = 2,
6465 .align = 8,
6466 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
6467 PCSTimeout,
6468 .features = RTL_FEATURE_MSI,
6469 .default_ver = RTL_GIGA_MAC_VER_13,
6470 }
6471};
6472
6473/* Cfg9346_Unlock assumed. */
6474static unsigned rtl_try_msi(struct rtl8169_private *tp,
6475 const struct rtl_cfg_info *cfg)
6476{
6477 void __iomem *ioaddr = tp->mmio_addr;
6478 unsigned msi = 0;
6479 u8 cfg2;
6480
6481 cfg2 = RTL_R8(Config2) & ~MSIEnable;
6482 if (cfg->features & RTL_FEATURE_MSI) {
6483 if (pci_enable_msi(tp->pci_dev)) {
6484 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
6485 } else {
6486 cfg2 |= MSIEnable;
6487 msi = RTL_FEATURE_MSI;
6488 }
6489 }
6490 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6491 RTL_W8(Config2, cfg2);
6492 return msi;
6493}
6494
Francois Romieu3b6cf252012-03-08 09:59:04 +01006495static int __devinit
6496rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6497{
6498 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
6499 const unsigned int region = cfg->region;
6500 struct rtl8169_private *tp;
6501 struct mii_if_info *mii;
6502 struct net_device *dev;
6503 void __iomem *ioaddr;
6504 int chipset, i;
6505 int rc;
6506
6507 if (netif_msg_drv(&debug)) {
6508 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
6509 MODULENAME, RTL8169_VERSION);
6510 }
6511
6512 dev = alloc_etherdev(sizeof (*tp));
6513 if (!dev) {
6514 rc = -ENOMEM;
6515 goto out;
6516 }
6517
6518 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01006519 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006520 tp = netdev_priv(dev);
6521 tp->dev = dev;
6522 tp->pci_dev = pdev;
6523 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
6524
6525 mii = &tp->mii;
6526 mii->dev = dev;
6527 mii->mdio_read = rtl_mdio_read;
6528 mii->mdio_write = rtl_mdio_write;
6529 mii->phy_id_mask = 0x1f;
6530 mii->reg_num_mask = 0x1f;
6531 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
6532
6533 /* disable ASPM completely as that cause random device stop working
6534 * problems as well as full system hangs for some PCIe devices users */
6535 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6536 PCIE_LINK_STATE_CLKPM);
6537
6538 /* enable device (incl. PCI PM wakeup and hotplug setup) */
6539 rc = pci_enable_device(pdev);
6540 if (rc < 0) {
6541 netif_err(tp, probe, dev, "enable failure\n");
6542 goto err_out_free_dev_1;
6543 }
6544
6545 if (pci_set_mwi(pdev) < 0)
6546 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
6547
6548 /* make sure PCI base addr 1 is MMIO */
6549 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
6550 netif_err(tp, probe, dev,
6551 "region #%d not an MMIO resource, aborting\n",
6552 region);
6553 rc = -ENODEV;
6554 goto err_out_mwi_2;
6555 }
6556
6557 /* check for weird/broken PCI region reporting */
6558 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
6559 netif_err(tp, probe, dev,
6560 "Invalid PCI region size(s), aborting\n");
6561 rc = -ENODEV;
6562 goto err_out_mwi_2;
6563 }
6564
6565 rc = pci_request_regions(pdev, MODULENAME);
6566 if (rc < 0) {
6567 netif_err(tp, probe, dev, "could not request regions\n");
6568 goto err_out_mwi_2;
6569 }
6570
6571 tp->cp_cmd = RxChkSum;
6572
6573 if ((sizeof(dma_addr_t) > 4) &&
6574 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
6575 tp->cp_cmd |= PCIDAC;
6576 dev->features |= NETIF_F_HIGHDMA;
6577 } else {
6578 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6579 if (rc < 0) {
6580 netif_err(tp, probe, dev, "DMA configuration failed\n");
6581 goto err_out_free_res_3;
6582 }
6583 }
6584
6585 /* ioremap MMIO region */
6586 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
6587 if (!ioaddr) {
6588 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
6589 rc = -EIO;
6590 goto err_out_free_res_3;
6591 }
6592 tp->mmio_addr = ioaddr;
6593
6594 if (!pci_is_pcie(pdev))
6595 netif_info(tp, probe, dev, "not PCI Express\n");
6596
6597 /* Identify chip attached to board */
6598 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
6599
6600 rtl_init_rxcfg(tp);
6601
6602 rtl_irq_disable(tp);
6603
6604 rtl_hw_reset(tp);
6605
6606 rtl_ack_events(tp, 0xffff);
6607
6608 pci_set_master(pdev);
6609
6610 /*
6611 * Pretend we are using VLANs; This bypasses a nasty bug where
6612 * Interrupts stop flowing on high load on 8110SCd controllers.
6613 */
6614 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6615 tp->cp_cmd |= RxVlan;
6616
6617 rtl_init_mdio_ops(tp);
6618 rtl_init_pll_power_ops(tp);
6619 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006620 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006621
6622 rtl8169_print_mac_version(tp);
6623
6624 chipset = tp->mac_version;
6625 tp->txd_version = rtl_chip_infos[chipset].txd_version;
6626
6627 RTL_W8(Cfg9346, Cfg9346_Unlock);
6628 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
6629 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
6630 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
6631 tp->features |= RTL_FEATURE_WOL;
6632 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
6633 tp->features |= RTL_FEATURE_WOL;
6634 tp->features |= rtl_try_msi(tp, cfg);
6635 RTL_W8(Cfg9346, Cfg9346_Lock);
6636
6637 if (rtl_tbi_enabled(tp)) {
6638 tp->set_speed = rtl8169_set_speed_tbi;
6639 tp->get_settings = rtl8169_gset_tbi;
6640 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
6641 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
6642 tp->link_ok = rtl8169_tbi_link_ok;
6643 tp->do_ioctl = rtl_tbi_ioctl;
6644 } else {
6645 tp->set_speed = rtl8169_set_speed_xmii;
6646 tp->get_settings = rtl8169_gset_xmii;
6647 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
6648 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
6649 tp->link_ok = rtl8169_xmii_link_ok;
6650 tp->do_ioctl = rtl_xmii_ioctl;
6651 }
6652
6653 mutex_init(&tp->wk.mutex);
6654
6655 /* Get MAC address */
6656 for (i = 0; i < ETH_ALEN; i++)
6657 dev->dev_addr[i] = RTL_R8(MAC0 + i);
6658 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
6659
6660 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
6661 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006662
6663 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
6664
6665 /* don't enable SG, IP_CSUM and TSO by default - it might not work
6666 * properly for all devices */
6667 dev->features |= NETIF_F_RXCSUM |
6668 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6669
6670 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6671 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6672 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6673 NETIF_F_HIGHDMA;
6674
6675 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6676 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
6677 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
6678
6679 dev->hw_features |= NETIF_F_RXALL;
6680 dev->hw_features |= NETIF_F_RXFCS;
6681
6682 tp->hw_start = cfg->hw_start;
6683 tp->event_slow = cfg->event_slow;
6684
6685 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
6686 ~(RxBOVF | RxFOVF) : ~0;
6687
6688 init_timer(&tp->timer);
6689 tp->timer.data = (unsigned long) dev;
6690 tp->timer.function = rtl8169_phy_timer;
6691
6692 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
6693
6694 rc = register_netdev(dev);
6695 if (rc < 0)
6696 goto err_out_msi_4;
6697
6698 pci_set_drvdata(pdev, dev);
6699
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006700 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
6701 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
6702 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006703 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
6704 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
6705 "tx checksumming: %s]\n",
6706 rtl_chip_infos[chipset].jumbo_max,
6707 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
6708 }
6709
6710 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6711 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6712 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6713 rtl8168_driver_start(tp);
6714 }
6715
6716 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
6717
6718 if (pci_dev_run_wake(pdev))
6719 pm_runtime_put_noidle(&pdev->dev);
6720
6721 netif_carrier_off(dev);
6722
6723out:
6724 return rc;
6725
6726err_out_msi_4:
Devendra Nagaad1be8d2012-05-31 01:51:20 +00006727 netif_napi_del(&tp->napi);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006728 rtl_disable_msi(pdev, tp);
6729 iounmap(ioaddr);
6730err_out_free_res_3:
6731 pci_release_regions(pdev);
6732err_out_mwi_2:
6733 pci_clear_mwi(pdev);
6734 pci_disable_device(pdev);
6735err_out_free_dev_1:
6736 free_netdev(dev);
6737 goto out;
6738}
6739
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740static struct pci_driver rtl8169_pci_driver = {
6741 .name = MODULENAME,
6742 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01006743 .probe = rtl_init_one,
Francois Romieue27566e2012-03-08 09:54:01 +01006744 .remove = __devexit_p(rtl_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02006745 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006746 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747};
6748
Francois Romieu07d3f512007-02-21 22:40:46 +01006749static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750{
Jeff Garzik29917622006-08-19 17:48:59 -04006751 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752}
6753
Francois Romieu07d3f512007-02-21 22:40:46 +01006754static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755{
6756 pci_unregister_driver(&rtl8169_pci_driver);
6757}
6758
6759module_init(rtl8169_init_module);
6760module_exit(rtl8169_cleanup_module);