blob: 7c6c4b253ea1805755519965fc38644fada75b80 [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
françois romieub646d902011-01-03 15:08:21 +0000829static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
830{
831 void __iomem *ioaddr = tp->mmio_addr;
832 int i;
833
834 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
835 for (i = 0; i < 20; i++) {
836 udelay(100);
837 if (RTL_R32(OCPAR) & OCPAR_FLAG)
838 break;
839 }
840 return RTL_R32(OCPDR);
841}
842
843static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
844{
845 void __iomem *ioaddr = tp->mmio_addr;
846 int i;
847
848 RTL_W32(OCPDR, data);
849 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
850 for (i = 0; i < 20; i++) {
851 udelay(100);
852 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
853 break;
854 }
855}
856
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800857static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000858{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800859 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000860 int i;
861
862 RTL_W8(ERIDR, cmd);
863 RTL_W32(ERIAR, 0x800010e8);
864 msleep(2);
865 for (i = 0; i < 5; i++) {
866 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200867 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000868 break;
869 }
870
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800871 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000872}
873
874#define OOB_CMD_RESET 0x00
875#define OOB_CMD_DRIVER_START 0x05
876#define OOB_CMD_DRIVER_STOP 0x06
877
Francois Romieucecb5fd2011-04-01 10:21:07 +0200878static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
879{
880 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
881}
882
françois romieub646d902011-01-03 15:08:21 +0000883static void rtl8168_driver_start(struct rtl8169_private *tp)
884{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200885 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000886 int i;
887
888 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
889
Francois Romieucecb5fd2011-04-01 10:21:07 +0200890 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000891
françois romieub646d902011-01-03 15:08:21 +0000892 for (i = 0; i < 10; i++) {
893 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000894 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000895 break;
896 }
897}
898
899static void rtl8168_driver_stop(struct rtl8169_private *tp)
900{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200901 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000902 int i;
903
904 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
905
Francois Romieucecb5fd2011-04-01 10:21:07 +0200906 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000907
françois romieub646d902011-01-03 15:08:21 +0000908 for (i = 0; i < 10; i++) {
909 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000910 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000911 break;
912 }
913}
914
hayeswang4804b3b2011-03-21 01:50:29 +0000915static int r8168dp_check_dash(struct rtl8169_private *tp)
916{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200917 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000918
Francois Romieucecb5fd2011-04-01 10:21:07 +0200919 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000920}
françois romieub646d902011-01-03 15:08:21 +0000921
Francois Romieu24192212012-07-06 20:19:42 +0200922static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Francois Romieu24192212012-07-06 20:19:42 +0200924 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 int i;
926
Francois Romieu24192212012-07-06 20:19:42 +0200927 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Francois Romieu23714082006-01-29 00:49:09 +0100929 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100930 /*
931 * Check if the RTL8169 has completed writing to the specified
932 * MII register.
933 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200934 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 break;
Francois Romieu23714082006-01-29 00:49:09 +0100936 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700938 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700939 * According to hardware specs a 20us delay is required after write
940 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700941 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700942 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
Francois Romieu24192212012-07-06 20:19:42 +0200945static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Francois Romieu24192212012-07-06 20:19:42 +0200947 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 int i, value = -1;
949
Francois Romieu24192212012-07-06 20:19:42 +0200950 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Francois Romieu23714082006-01-29 00:49:09 +0100952 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100953 /*
954 * Check if the RTL8169 has completed retrieving data from
955 * the specified MII register.
956 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100958 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 break;
960 }
Francois Romieu23714082006-01-29 00:49:09 +0100961 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700963 /*
964 * According to hardware specs a 20us delay is required after read
965 * complete indication, but before sending next command.
966 */
967 udelay(20);
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return value;
970}
971
Francois Romieu24192212012-07-06 20:19:42 +0200972static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +0000973{
Francois Romieu24192212012-07-06 20:19:42 +0200974 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +0000975 int i;
976
Francois Romieu24192212012-07-06 20:19:42 +0200977 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
françois romieuc0e45c12011-01-03 15:08:04 +0000978 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
979 RTL_W32(EPHY_RXER_NUM, 0);
980
981 for (i = 0; i < 100; i++) {
982 mdelay(1);
983 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
984 break;
985 }
986}
987
Francois Romieu24192212012-07-06 20:19:42 +0200988static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +0000989{
Francois Romieu24192212012-07-06 20:19:42 +0200990 r8168dp_1_mdio_access(tp, reg,
991 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +0000992}
993
Francois Romieu24192212012-07-06 20:19:42 +0200994static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +0000995{
Francois Romieu24192212012-07-06 20:19:42 +0200996 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +0000997 int i;
998
Francois Romieu24192212012-07-06 20:19:42 +0200999 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001000
1001 mdelay(1);
1002 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1003 RTL_W32(EPHY_RXER_NUM, 0);
1004
1005 for (i = 0; i < 100; i++) {
1006 mdelay(1);
1007 if (RTL_R32(OCPAR) & OCPAR_FLAG)
1008 break;
1009 }
1010
1011 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
1012}
1013
françois romieue6de30d2011-01-03 15:08:37 +00001014#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1015
1016static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1017{
1018 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1019}
1020
1021static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1022{
1023 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1024}
1025
Francois Romieu24192212012-07-06 20:19:42 +02001026static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001027{
Francois Romieu24192212012-07-06 20:19:42 +02001028 void __iomem *ioaddr = tp->mmio_addr;
1029
françois romieue6de30d2011-01-03 15:08:37 +00001030 r8168dp_2_mdio_start(ioaddr);
1031
Francois Romieu24192212012-07-06 20:19:42 +02001032 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001033
1034 r8168dp_2_mdio_stop(ioaddr);
1035}
1036
Francois Romieu24192212012-07-06 20:19:42 +02001037static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001038{
Francois Romieu24192212012-07-06 20:19:42 +02001039 void __iomem *ioaddr = tp->mmio_addr;
françois romieue6de30d2011-01-03 15:08:37 +00001040 int value;
1041
1042 r8168dp_2_mdio_start(ioaddr);
1043
Francois Romieu24192212012-07-06 20:19:42 +02001044 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001045
1046 r8168dp_2_mdio_stop(ioaddr);
1047
1048 return value;
1049}
1050
françois romieu4da19632011-01-03 15:07:55 +00001051static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001052{
Francois Romieu24192212012-07-06 20:19:42 +02001053 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001054}
1055
françois romieu4da19632011-01-03 15:07:55 +00001056static int rtl_readphy(struct rtl8169_private *tp, int location)
1057{
Francois Romieu24192212012-07-06 20:19:42 +02001058 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001059}
1060
1061static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1062{
1063 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1064}
1065
1066static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001067{
1068 int val;
1069
françois romieu4da19632011-01-03 15:07:55 +00001070 val = rtl_readphy(tp, reg_addr);
1071 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001072}
1073
Francois Romieuccdffb92008-07-26 14:26:06 +02001074static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1075 int val)
1076{
1077 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001078
françois romieu4da19632011-01-03 15:07:55 +00001079 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001080}
1081
1082static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1083{
1084 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001085
françois romieu4da19632011-01-03 15:07:55 +00001086 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001087}
1088
Francois Romieufdf6fc02012-07-06 22:40:38 +02001089static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001090{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001091 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001092 unsigned int i;
1093
1094 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1095 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1096
1097 for (i = 0; i < 100; i++) {
1098 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1099 break;
1100 udelay(10);
1101 }
1102}
1103
Francois Romieufdf6fc02012-07-06 22:40:38 +02001104static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001105{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001106 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001107 u16 value = 0xffff;
1108 unsigned int i;
1109
1110 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1111
1112 for (i = 0; i < 100; i++) {
1113 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1114 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1115 break;
1116 }
1117 udelay(10);
1118 }
1119
1120 return value;
1121}
1122
Francois Romieufdf6fc02012-07-06 22:40:38 +02001123static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1124 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001125{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001126 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001127 unsigned int i;
1128
1129 BUG_ON((addr & 3) || (mask == 0));
1130 RTL_W32(ERIDR, val);
1131 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1132
1133 for (i = 0; i < 100; i++) {
1134 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1135 break;
1136 udelay(100);
1137 }
1138}
1139
Francois Romieufdf6fc02012-07-06 22:40:38 +02001140static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001141{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001142 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001143 u32 value = ~0x00;
1144 unsigned int i;
1145
1146 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1147
1148 for (i = 0; i < 100; i++) {
1149 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1150 value = RTL_R32(ERIDR);
1151 break;
1152 }
1153 udelay(100);
1154 }
1155
1156 return value;
1157}
1158
Francois Romieufdf6fc02012-07-06 22:40:38 +02001159static void rtl_w1w0_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
1160 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001161{
1162 u32 val;
1163
Francois Romieufdf6fc02012-07-06 22:40:38 +02001164 val = rtl_eri_read(tp, addr, type);
1165 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001166}
1167
françois romieuc28aa382011-08-02 03:53:43 +00001168struct exgmac_reg {
1169 u16 addr;
1170 u16 mask;
1171 u32 val;
1172};
1173
Francois Romieufdf6fc02012-07-06 22:40:38 +02001174static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001175 const struct exgmac_reg *r, int len)
1176{
1177 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001178 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001179 r++;
1180 }
1181}
1182
Francois Romieufdf6fc02012-07-06 22:40:38 +02001183static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001184{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001185 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00001186 u8 value = 0xff;
1187 unsigned int i;
1188
1189 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1190
1191 for (i = 0; i < 300; i++) {
1192 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1193 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1194 break;
1195 }
1196 udelay(100);
1197 }
1198
1199 return value;
1200}
1201
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001202static u16 rtl_get_events(struct rtl8169_private *tp)
1203{
1204 void __iomem *ioaddr = tp->mmio_addr;
1205
1206 return RTL_R16(IntrStatus);
1207}
1208
1209static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1210{
1211 void __iomem *ioaddr = tp->mmio_addr;
1212
1213 RTL_W16(IntrStatus, bits);
1214 mmiowb();
1215}
1216
1217static void rtl_irq_disable(struct rtl8169_private *tp)
1218{
1219 void __iomem *ioaddr = tp->mmio_addr;
1220
1221 RTL_W16(IntrMask, 0);
1222 mmiowb();
1223}
1224
Francois Romieu3e990ff2012-01-26 12:50:01 +01001225static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1226{
1227 void __iomem *ioaddr = tp->mmio_addr;
1228
1229 RTL_W16(IntrMask, bits);
1230}
1231
Francois Romieuda78dbf2012-01-26 14:18:23 +01001232#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1233#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1234#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1235
1236static void rtl_irq_enable_all(struct rtl8169_private *tp)
1237{
1238 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1239}
1240
françois romieu811fd302011-12-04 20:30:45 +00001241static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
françois romieu811fd302011-12-04 20:30:45 +00001243 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001245 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001246 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001247 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
françois romieu4da19632011-01-03 15:07:55 +00001250static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
françois romieu4da19632011-01-03 15:07:55 +00001252 void __iomem *ioaddr = tp->mmio_addr;
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return RTL_R32(TBICSR) & TBIReset;
1255}
1256
françois romieu4da19632011-01-03 15:07:55 +00001257static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
françois romieu4da19632011-01-03 15:07:55 +00001259 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
1261
1262static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1263{
1264 return RTL_R32(TBICSR) & TBILinkOk;
1265}
1266
1267static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1268{
1269 return RTL_R8(PHYstatus) & LinkStatus;
1270}
1271
françois romieu4da19632011-01-03 15:07:55 +00001272static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
françois romieu4da19632011-01-03 15:07:55 +00001274 void __iomem *ioaddr = tp->mmio_addr;
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1277}
1278
françois romieu4da19632011-01-03 15:07:55 +00001279static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 unsigned int val;
1282
françois romieu4da19632011-01-03 15:07:55 +00001283 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1284 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
Hayes Wang70090422011-07-06 15:58:06 +08001287static void rtl_link_chg_patch(struct rtl8169_private *tp)
1288{
1289 void __iomem *ioaddr = tp->mmio_addr;
1290 struct net_device *dev = tp->dev;
1291
1292 if (!netif_running(dev))
1293 return;
1294
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001295 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1296 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Hayes Wang70090422011-07-06 15:58:06 +08001297 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001298 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1299 ERIAR_EXGMAC);
1300 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1301 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001302 } else if (RTL_R8(PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001303 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1304 ERIAR_EXGMAC);
1305 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1306 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001307 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001308 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1309 ERIAR_EXGMAC);
1310 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1311 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001312 }
1313 /* Reset packet filter */
Francois Romieufdf6fc02012-07-06 22:40:38 +02001314 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001315 ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02001316 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001317 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001318 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1319 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1320 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001321 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1322 ERIAR_EXGMAC);
1323 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1324 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001325 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001326 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1327 ERIAR_EXGMAC);
1328 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1329 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001330 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001331 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1332 if (RTL_R8(PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001333 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1334 ERIAR_EXGMAC);
1335 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1336 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001337 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001338 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1339 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001340 }
Hayes Wang70090422011-07-06 15:58:06 +08001341 }
1342}
1343
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001344static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001345 struct rtl8169_private *tp,
1346 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001349 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001350 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001351 if (pm)
1352 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001354 if (net_ratelimit())
1355 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001356 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001358 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001359 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001360 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}
1363
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001364static void rtl8169_check_link_status(struct net_device *dev,
1365 struct rtl8169_private *tp,
1366 void __iomem *ioaddr)
1367{
1368 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1369}
1370
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001371#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1372
1373static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1374{
1375 void __iomem *ioaddr = tp->mmio_addr;
1376 u8 options;
1377 u32 wolopts = 0;
1378
1379 options = RTL_R8(Config1);
1380 if (!(options & PMEnable))
1381 return 0;
1382
1383 options = RTL_R8(Config3);
1384 if (options & LinkUp)
1385 wolopts |= WAKE_PHY;
1386 if (options & MagicPacket)
1387 wolopts |= WAKE_MAGIC;
1388
1389 options = RTL_R8(Config5);
1390 if (options & UWF)
1391 wolopts |= WAKE_UCAST;
1392 if (options & BWF)
1393 wolopts |= WAKE_BCAST;
1394 if (options & MWF)
1395 wolopts |= WAKE_MCAST;
1396
1397 return wolopts;
1398}
1399
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001400static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1401{
1402 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001403
Francois Romieuda78dbf2012-01-26 14:18:23 +01001404 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001405
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001406 wol->supported = WAKE_ANY;
1407 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001408
Francois Romieuda78dbf2012-01-26 14:18:23 +01001409 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001410}
1411
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001412static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001413{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001414 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001415 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001416 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001417 u32 opt;
1418 u16 reg;
1419 u8 mask;
1420 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001421 { WAKE_PHY, Config3, LinkUp },
1422 { WAKE_MAGIC, Config3, MagicPacket },
1423 { WAKE_UCAST, Config5, UWF },
1424 { WAKE_BCAST, Config5, BWF },
1425 { WAKE_MCAST, Config5, MWF },
1426 { WAKE_ANY, Config5, LanWake }
1427 };
Francois Romieu851e6022012-04-17 11:10:11 +02001428 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001429
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001430 RTL_W8(Cfg9346, Cfg9346_Unlock);
1431
1432 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
Francois Romieu851e6022012-04-17 11:10:11 +02001433 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001434 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001435 options |= cfg[i].mask;
1436 RTL_W8(cfg[i].reg, options);
1437 }
1438
Francois Romieu851e6022012-04-17 11:10:11 +02001439 switch (tp->mac_version) {
1440 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1441 options = RTL_R8(Config1) & ~PMEnable;
1442 if (wolopts)
1443 options |= PMEnable;
1444 RTL_W8(Config1, options);
1445 break;
1446 default:
Francois Romieud387b422012-04-17 11:12:01 +02001447 options = RTL_R8(Config2) & ~PME_SIGNAL;
1448 if (wolopts)
1449 options |= PME_SIGNAL;
1450 RTL_W8(Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001451 break;
1452 }
1453
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001454 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001455}
1456
1457static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1458{
1459 struct rtl8169_private *tp = netdev_priv(dev);
1460
Francois Romieuda78dbf2012-01-26 14:18:23 +01001461 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001462
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001463 if (wol->wolopts)
1464 tp->features |= RTL_FEATURE_WOL;
1465 else
1466 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001467 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001468
1469 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001470
françois romieuea809072010-11-08 13:23:58 +00001471 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1472
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001473 return 0;
1474}
1475
Francois Romieu31bd2042011-04-26 18:58:59 +02001476static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1477{
Francois Romieu85bffe62011-04-27 08:22:39 +02001478 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001479}
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481static void rtl8169_get_drvinfo(struct net_device *dev,
1482 struct ethtool_drvinfo *info)
1483{
1484 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001485 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Rick Jones68aad782011-11-07 13:29:27 +00001487 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1488 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1489 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001490 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001491 if (!IS_ERR_OR_NULL(rtl_fw))
1492 strlcpy(info->fw_version, rtl_fw->version,
1493 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
1496static int rtl8169_get_regs_len(struct net_device *dev)
1497{
1498 return R8169_REGS_SIZE;
1499}
1500
1501static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001502 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504 struct rtl8169_private *tp = netdev_priv(dev);
1505 void __iomem *ioaddr = tp->mmio_addr;
1506 int ret = 0;
1507 u32 reg;
1508
1509 reg = RTL_R32(TBICSR);
1510 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1511 (duplex == DUPLEX_FULL)) {
1512 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1513 } else if (autoneg == AUTONEG_ENABLE)
1514 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1515 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001516 netif_warn(tp, link, dev,
1517 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 ret = -EOPNOTSUPP;
1519 }
1520
1521 return ret;
1522}
1523
1524static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001525 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
1527 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001528 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001529 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Hayes Wang716b50a2011-02-22 17:26:18 +08001531 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001534 int auto_nego;
1535
françois romieu4da19632011-01-03 15:07:55 +00001536 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001537 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1538 ADVERTISE_100HALF | ADVERTISE_100FULL);
1539
1540 if (adv & ADVERTISED_10baseT_Half)
1541 auto_nego |= ADVERTISE_10HALF;
1542 if (adv & ADVERTISED_10baseT_Full)
1543 auto_nego |= ADVERTISE_10FULL;
1544 if (adv & ADVERTISED_100baseT_Half)
1545 auto_nego |= ADVERTISE_100HALF;
1546 if (adv & ADVERTISED_100baseT_Full)
1547 auto_nego |= ADVERTISE_100FULL;
1548
françois romieu3577aa12009-05-19 10:46:48 +00001549 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1550
françois romieu4da19632011-01-03 15:07:55 +00001551 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001552 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1553
1554 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001555 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001556 if (adv & ADVERTISED_1000baseT_Half)
1557 giga_ctrl |= ADVERTISE_1000HALF;
1558 if (adv & ADVERTISED_1000baseT_Full)
1559 giga_ctrl |= ADVERTISE_1000FULL;
1560 } else if (adv & (ADVERTISED_1000baseT_Half |
1561 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001562 netif_info(tp, link, dev,
1563 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001564 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
françois romieu3577aa12009-05-19 10:46:48 +00001567 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001568
françois romieu4da19632011-01-03 15:07:55 +00001569 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1570 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001571 } else {
1572 giga_ctrl = 0;
1573
1574 if (speed == SPEED_10)
1575 bmcr = 0;
1576 else if (speed == SPEED_100)
1577 bmcr = BMCR_SPEED100;
1578 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001579 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001580
1581 if (duplex == DUPLEX_FULL)
1582 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001583 }
1584
françois romieu4da19632011-01-03 15:07:55 +00001585 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001586
Francois Romieucecb5fd2011-04-01 10:21:07 +02001587 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1588 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001589 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001590 rtl_writephy(tp, 0x17, 0x2138);
1591 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001592 } else {
françois romieu4da19632011-01-03 15:07:55 +00001593 rtl_writephy(tp, 0x17, 0x2108);
1594 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001595 }
1596 }
1597
Oliver Neukum54405cd2011-01-06 21:55:13 +01001598 rc = 0;
1599out:
1600 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601}
1602
1603static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001604 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
1606 struct rtl8169_private *tp = netdev_priv(dev);
1607 int ret;
1608
Oliver Neukum54405cd2011-01-06 21:55:13 +01001609 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001610 if (ret < 0)
1611 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Francois Romieu4876cc12011-03-11 21:07:11 +01001613 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1614 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001616 }
1617out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 return ret;
1619}
1620
1621static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1622{
1623 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 int ret;
1625
Francois Romieu4876cc12011-03-11 21:07:11 +01001626 del_timer_sync(&tp->timer);
1627
Francois Romieuda78dbf2012-01-26 14:18:23 +01001628 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001629 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001630 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001631 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 return ret;
1634}
1635
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001636static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1637 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Francois Romieud58d46b2011-05-03 16:38:29 +02001639 struct rtl8169_private *tp = netdev_priv(dev);
1640
Francois Romieu2b7b4312011-04-18 22:53:24 -07001641 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001642 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Francois Romieud58d46b2011-05-03 16:38:29 +02001644 if (dev->mtu > JUMBO_1K &&
1645 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1646 features &= ~NETIF_F_IP_CSUM;
1647
Michał Mirosław350fb322011-04-08 06:35:56 +00001648 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
1650
Francois Romieuda78dbf2012-01-26 14:18:23 +01001651static void __rtl8169_set_features(struct net_device *dev,
1652 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
1654 struct rtl8169_private *tp = netdev_priv(dev);
Ben Greear6bbe0212012-02-10 15:04:33 +00001655 netdev_features_t changed = features ^ dev->features;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001656 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Ben Greear6bbe0212012-02-10 15:04:33 +00001658 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)))
1659 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Ben Greear6bbe0212012-02-10 15:04:33 +00001661 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)) {
1662 if (features & NETIF_F_RXCSUM)
1663 tp->cp_cmd |= RxChkSum;
1664 else
1665 tp->cp_cmd &= ~RxChkSum;
Michał Mirosław350fb322011-04-08 06:35:56 +00001666
Ben Greear6bbe0212012-02-10 15:04:33 +00001667 if (dev->features & NETIF_F_HW_VLAN_RX)
1668 tp->cp_cmd |= RxVlan;
1669 else
1670 tp->cp_cmd &= ~RxVlan;
1671
1672 RTL_W16(CPlusCmd, tp->cp_cmd);
1673 RTL_R16(CPlusCmd);
1674 }
1675 if (changed & NETIF_F_RXALL) {
1676 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1677 if (features & NETIF_F_RXALL)
1678 tmp |= (AcceptErr | AcceptRunt);
1679 RTL_W32(RxConfig, tmp);
1680 }
Francois Romieuda78dbf2012-01-26 14:18:23 +01001681}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Francois Romieuda78dbf2012-01-26 14:18:23 +01001683static int rtl8169_set_features(struct net_device *dev,
1684 netdev_features_t features)
1685{
1686 struct rtl8169_private *tp = netdev_priv(dev);
1687
1688 rtl_lock_work(tp);
1689 __rtl8169_set_features(dev, features);
1690 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 return 0;
1693}
1694
Francois Romieuda78dbf2012-01-26 14:18:23 +01001695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1697 struct sk_buff *skb)
1698{
Jesse Grosseab6d182010-10-20 13:56:03 +00001699 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1701}
1702
Francois Romieu7a8fc772011-03-01 17:18:33 +01001703static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
1705 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Francois Romieu7a8fc772011-03-01 17:18:33 +01001707 if (opts2 & RxVlanTag)
1708 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711}
1712
Francois Romieuccdffb92008-07-26 14:26:06 +02001713static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
1715 struct rtl8169_private *tp = netdev_priv(dev);
1716 void __iomem *ioaddr = tp->mmio_addr;
1717 u32 status;
1718
1719 cmd->supported =
1720 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1721 cmd->port = PORT_FIBRE;
1722 cmd->transceiver = XCVR_INTERNAL;
1723
1724 status = RTL_R32(TBICSR);
1725 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1726 cmd->autoneg = !!(status & TBINwEnable);
1727
David Decotigny70739492011-04-27 18:32:40 +00001728 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001730
1731 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
1733
Francois Romieuccdffb92008-07-26 14:26:06 +02001734static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
1736 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Francois Romieuccdffb92008-07-26 14:26:06 +02001738 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739}
1740
1741static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1742{
1743 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001744 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Francois Romieuda78dbf2012-01-26 14:18:23 +01001746 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02001747 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001748 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Francois Romieuccdffb92008-07-26 14:26:06 +02001750 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751}
1752
1753static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1754 void *p)
1755{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001756 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Francois Romieu5b0384f2006-08-16 16:00:01 +02001758 if (regs->len > R8169_REGS_SIZE)
1759 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Francois Romieuda78dbf2012-01-26 14:18:23 +01001761 rtl_lock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001762 memcpy_fromio(p, tp->mmio_addr, regs->len);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001763 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
1765
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001766static u32 rtl8169_get_msglevel(struct net_device *dev)
1767{
1768 struct rtl8169_private *tp = netdev_priv(dev);
1769
1770 return tp->msg_enable;
1771}
1772
1773static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1774{
1775 struct rtl8169_private *tp = netdev_priv(dev);
1776
1777 tp->msg_enable = value;
1778}
1779
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001780static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1781 "tx_packets",
1782 "rx_packets",
1783 "tx_errors",
1784 "rx_errors",
1785 "rx_missed",
1786 "align_errors",
1787 "tx_single_collisions",
1788 "tx_multi_collisions",
1789 "unicast",
1790 "broadcast",
1791 "multicast",
1792 "tx_aborted",
1793 "tx_underrun",
1794};
1795
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001796static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001797{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001798 switch (sset) {
1799 case ETH_SS_STATS:
1800 return ARRAY_SIZE(rtl8169_gstrings);
1801 default:
1802 return -EOPNOTSUPP;
1803 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001804}
1805
Ivan Vecera355423d2009-02-06 21:49:57 -08001806static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001807{
1808 struct rtl8169_private *tp = netdev_priv(dev);
1809 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001810 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001811 struct rtl8169_counters *counters;
1812 dma_addr_t paddr;
1813 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001814 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001815
Ivan Vecera355423d2009-02-06 21:49:57 -08001816 /*
1817 * Some chips are unable to dump tally counters when the receiver
1818 * is disabled.
1819 */
1820 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1821 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001822
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001823 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001824 if (!counters)
1825 return;
1826
1827 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001828 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001829 RTL_W32(CounterAddrLow, cmd);
1830 RTL_W32(CounterAddrLow, cmd | CounterDump);
1831
Ivan Vecera355423d2009-02-06 21:49:57 -08001832 while (wait--) {
1833 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001834 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001835 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001836 }
1837 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001838 }
1839
1840 RTL_W32(CounterAddrLow, 0);
1841 RTL_W32(CounterAddrHigh, 0);
1842
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001843 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001844}
1845
Ivan Vecera355423d2009-02-06 21:49:57 -08001846static void rtl8169_get_ethtool_stats(struct net_device *dev,
1847 struct ethtool_stats *stats, u64 *data)
1848{
1849 struct rtl8169_private *tp = netdev_priv(dev);
1850
1851 ASSERT_RTNL();
1852
1853 rtl8169_update_counters(dev);
1854
1855 data[0] = le64_to_cpu(tp->counters.tx_packets);
1856 data[1] = le64_to_cpu(tp->counters.rx_packets);
1857 data[2] = le64_to_cpu(tp->counters.tx_errors);
1858 data[3] = le32_to_cpu(tp->counters.rx_errors);
1859 data[4] = le16_to_cpu(tp->counters.rx_missed);
1860 data[5] = le16_to_cpu(tp->counters.align_errors);
1861 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1862 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1863 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1864 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1865 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1866 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1867 data[12] = le16_to_cpu(tp->counters.tx_underun);
1868}
1869
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001870static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1871{
1872 switch(stringset) {
1873 case ETH_SS_STATS:
1874 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1875 break;
1876 }
1877}
1878
Jeff Garzik7282d492006-09-13 14:30:00 -04001879static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 .get_drvinfo = rtl8169_get_drvinfo,
1881 .get_regs_len = rtl8169_get_regs_len,
1882 .get_link = ethtool_op_get_link,
1883 .get_settings = rtl8169_get_settings,
1884 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001885 .get_msglevel = rtl8169_get_msglevel,
1886 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001888 .get_wol = rtl8169_get_wol,
1889 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001890 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001891 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001892 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00001893 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894};
1895
Francois Romieu07d3f512007-02-21 22:40:46 +01001896static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001897 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
Francois Romieu5d320a22011-05-08 17:47:36 +02001899 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001900 /*
1901 * The driver currently handles the 8168Bf and the 8168Be identically
1902 * but they can be identified more specifically through the test below
1903 * if needed:
1904 *
1905 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001906 *
1907 * Same thing for the 8101Eb and the 8101Ec:
1908 *
1909 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001910 */
Francois Romieu37441002011-06-17 22:58:54 +02001911 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001913 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 int mac_version;
1915 } mac_info[] = {
Hayes Wangc2218922011-09-06 16:55:18 +08001916 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001917 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08001918 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1919 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1920
hayeswang01dc7fe2011-03-21 01:50:28 +00001921 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001922 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001923 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1924 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1925 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1926
Francois Romieu5b538df2008-07-20 16:22:45 +02001927 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001928 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1929 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001930 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001931
françois romieue6de30d2011-01-03 15:08:37 +00001932 /* 8168DP family. */
1933 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1934 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001935 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001936
Francois Romieuef808d52008-06-29 13:10:54 +02001937 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001938 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001939 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001940 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001941 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001942 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1943 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001944 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001945 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001946 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001947
1948 /* 8168B family. */
1949 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1950 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1951 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1952 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1953
1954 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08001955 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
1956 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08001957 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00001958 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001959 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1960 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1961 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001962 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1963 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1964 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1965 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1966 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1967 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001968 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001969 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001970 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001971 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1972 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001973 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1974 /* FIXME: where did these entries come from ? -- FR */
1975 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1976 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1977
1978 /* 8110 family. */
1979 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1980 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1981 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1982 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1983 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1984 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1985
Jean Delvaref21b75e2009-05-26 20:54:48 -07001986 /* Catch-all */
1987 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001988 };
1989 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 u32 reg;
1991
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001992 reg = RTL_R32(TxConfig);
1993 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 p++;
1995 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001996
1997 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1998 netif_notice(tp, probe, dev,
1999 "unknown MAC, using family default\n");
2000 tp->mac_version = default_version;
2001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002}
2003
2004static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2005{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002006 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
Francois Romieu867763c2007-08-17 18:21:58 +02002009struct phy_reg {
2010 u16 reg;
2011 u16 val;
2012};
2013
françois romieu4da19632011-01-03 15:07:55 +00002014static void rtl_writephy_batch(struct rtl8169_private *tp,
2015 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002016{
2017 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002018 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002019 regs++;
2020 }
2021}
2022
françois romieubca03d52011-01-03 15:07:31 +00002023#define PHY_READ 0x00000000
2024#define PHY_DATA_OR 0x10000000
2025#define PHY_DATA_AND 0x20000000
2026#define PHY_BJMPN 0x30000000
2027#define PHY_READ_EFUSE 0x40000000
2028#define PHY_READ_MAC_BYTE 0x50000000
2029#define PHY_WRITE_MAC_BYTE 0x60000000
2030#define PHY_CLEAR_READCOUNT 0x70000000
2031#define PHY_WRITE 0x80000000
2032#define PHY_READCOUNT_EQ_SKIP 0x90000000
2033#define PHY_COMP_EQ_SKIPN 0xa0000000
2034#define PHY_COMP_NEQ_SKIPN 0xb0000000
2035#define PHY_WRITE_PREVIOUS 0xc0000000
2036#define PHY_SKIPN 0xd0000000
2037#define PHY_DELAY_MS 0xe0000000
2038#define PHY_WRITE_ERI_WORD 0xf0000000
2039
Hayes Wang960aee62011-06-18 11:37:48 +02002040struct fw_info {
2041 u32 magic;
2042 char version[RTL_VER_SIZE];
2043 __le32 fw_start;
2044 __le32 fw_len;
2045 u8 chksum;
2046} __packed;
2047
Francois Romieu1c361ef2011-06-17 17:16:24 +02002048#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2049
2050static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002051{
Francois Romieub6ffd972011-06-17 17:00:05 +02002052 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002053 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002054 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2055 char *version = rtl_fw->version;
2056 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002057
Francois Romieu1c361ef2011-06-17 17:16:24 +02002058 if (fw->size < FW_OPCODE_SIZE)
2059 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002060
2061 if (!fw_info->magic) {
2062 size_t i, size, start;
2063 u8 checksum = 0;
2064
2065 if (fw->size < sizeof(*fw_info))
2066 goto out;
2067
2068 for (i = 0; i < fw->size; i++)
2069 checksum += fw->data[i];
2070 if (checksum != 0)
2071 goto out;
2072
2073 start = le32_to_cpu(fw_info->fw_start);
2074 if (start > fw->size)
2075 goto out;
2076
2077 size = le32_to_cpu(fw_info->fw_len);
2078 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2079 goto out;
2080
2081 memcpy(version, fw_info->version, RTL_VER_SIZE);
2082
2083 pa->code = (__le32 *)(fw->data + start);
2084 pa->size = size;
2085 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002086 if (fw->size % FW_OPCODE_SIZE)
2087 goto out;
2088
2089 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2090
2091 pa->code = (__le32 *)fw->data;
2092 pa->size = fw->size / FW_OPCODE_SIZE;
2093 }
2094 version[RTL_VER_SIZE - 1] = 0;
2095
2096 rc = true;
2097out:
2098 return rc;
2099}
2100
Francois Romieufd112f22011-06-18 00:10:29 +02002101static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2102 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002103{
Francois Romieufd112f22011-06-18 00:10:29 +02002104 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002105 size_t index;
2106
Francois Romieu1c361ef2011-06-17 17:16:24 +02002107 for (index = 0; index < pa->size; index++) {
2108 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002109 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002110
hayeswang42b82dc2011-01-10 02:07:25 +00002111 switch(action & 0xf0000000) {
2112 case PHY_READ:
2113 case PHY_DATA_OR:
2114 case PHY_DATA_AND:
2115 case PHY_READ_EFUSE:
2116 case PHY_CLEAR_READCOUNT:
2117 case PHY_WRITE:
2118 case PHY_WRITE_PREVIOUS:
2119 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002120 break;
2121
hayeswang42b82dc2011-01-10 02:07:25 +00002122 case PHY_BJMPN:
2123 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002124 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002125 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002126 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002127 }
2128 break;
2129 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002130 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002131 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002132 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002133 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002134 }
2135 break;
2136 case PHY_COMP_EQ_SKIPN:
2137 case PHY_COMP_NEQ_SKIPN:
2138 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002139 if (index + 1 + regno >= pa->size) {
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
2146 case PHY_READ_MAC_BYTE:
2147 case PHY_WRITE_MAC_BYTE:
2148 case PHY_WRITE_ERI_WORD:
2149 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002150 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002151 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002152 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002153 }
2154 }
Francois Romieufd112f22011-06-18 00:10:29 +02002155 rc = true;
2156out:
2157 return rc;
2158}
françois romieubca03d52011-01-03 15:07:31 +00002159
Francois Romieufd112f22011-06-18 00:10:29 +02002160static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2161{
2162 struct net_device *dev = tp->dev;
2163 int rc = -EINVAL;
2164
2165 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2166 netif_err(tp, ifup, dev, "invalid firwmare\n");
2167 goto out;
2168 }
2169
2170 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2171 rc = 0;
2172out:
2173 return rc;
2174}
2175
2176static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2177{
2178 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2179 u32 predata, count;
2180 size_t index;
2181
2182 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002183
Francois Romieu1c361ef2011-06-17 17:16:24 +02002184 for (index = 0; index < pa->size; ) {
2185 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002186 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002187 u32 regno = (action & 0x0fff0000) >> 16;
2188
2189 if (!action)
2190 break;
françois romieubca03d52011-01-03 15:07:31 +00002191
2192 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002193 case PHY_READ:
2194 predata = rtl_readphy(tp, regno);
2195 count++;
2196 index++;
françois romieubca03d52011-01-03 15:07:31 +00002197 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002198 case PHY_DATA_OR:
2199 predata |= data;
2200 index++;
2201 break;
2202 case PHY_DATA_AND:
2203 predata &= data;
2204 index++;
2205 break;
2206 case PHY_BJMPN:
2207 index -= regno;
2208 break;
2209 case PHY_READ_EFUSE:
Francois Romieufdf6fc02012-07-06 22:40:38 +02002210 predata = rtl8168d_efuse_read(tp, regno);
hayeswang42b82dc2011-01-10 02:07:25 +00002211 index++;
2212 break;
2213 case PHY_CLEAR_READCOUNT:
2214 count = 0;
2215 index++;
2216 break;
2217 case PHY_WRITE:
2218 rtl_writephy(tp, regno, data);
2219 index++;
2220 break;
2221 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002222 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002223 break;
2224 case PHY_COMP_EQ_SKIPN:
2225 if (predata == data)
2226 index += regno;
2227 index++;
2228 break;
2229 case PHY_COMP_NEQ_SKIPN:
2230 if (predata != data)
2231 index += regno;
2232 index++;
2233 break;
2234 case PHY_WRITE_PREVIOUS:
2235 rtl_writephy(tp, regno, predata);
2236 index++;
2237 break;
2238 case PHY_SKIPN:
2239 index += regno + 1;
2240 break;
2241 case PHY_DELAY_MS:
2242 mdelay(data);
2243 index++;
2244 break;
2245
2246 case PHY_READ_MAC_BYTE:
2247 case PHY_WRITE_MAC_BYTE:
2248 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002249 default:
2250 BUG();
2251 }
2252 }
2253}
2254
françois romieuf1e02ed2011-01-13 13:07:53 +00002255static void rtl_release_firmware(struct rtl8169_private *tp)
2256{
Francois Romieub6ffd972011-06-17 17:00:05 +02002257 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2258 release_firmware(tp->rtl_fw->fw);
2259 kfree(tp->rtl_fw);
2260 }
2261 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002262}
2263
François Romieu953a12c2011-04-24 17:38:48 +02002264static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002265{
Francois Romieub6ffd972011-06-17 17:00:05 +02002266 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002267
2268 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002269 if (!IS_ERR_OR_NULL(rtl_fw))
2270 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002271}
2272
2273static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2274{
2275 if (rtl_readphy(tp, reg) != val)
2276 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2277 else
2278 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002279}
2280
françois romieu4da19632011-01-03 15:07:55 +00002281static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002283 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002284 { 0x1f, 0x0001 },
2285 { 0x06, 0x006e },
2286 { 0x08, 0x0708 },
2287 { 0x15, 0x4000 },
2288 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
françois romieu0b9b5712009-08-10 19:44:56 +00002290 { 0x1f, 0x0001 },
2291 { 0x03, 0x00a1 },
2292 { 0x02, 0x0008 },
2293 { 0x01, 0x0120 },
2294 { 0x00, 0x1000 },
2295 { 0x04, 0x0800 },
2296 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
françois romieu0b9b5712009-08-10 19:44:56 +00002298 { 0x03, 0xff41 },
2299 { 0x02, 0xdf60 },
2300 { 0x01, 0x0140 },
2301 { 0x00, 0x0077 },
2302 { 0x04, 0x7800 },
2303 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
françois romieu0b9b5712009-08-10 19:44:56 +00002305 { 0x03, 0x802f },
2306 { 0x02, 0x4f02 },
2307 { 0x01, 0x0409 },
2308 { 0x00, 0xf0f9 },
2309 { 0x04, 0x9800 },
2310 { 0x04, 0x9000 },
2311
2312 { 0x03, 0xdf01 },
2313 { 0x02, 0xdf20 },
2314 { 0x01, 0xff95 },
2315 { 0x00, 0xba00 },
2316 { 0x04, 0xa800 },
2317 { 0x04, 0xa000 },
2318
2319 { 0x03, 0xff41 },
2320 { 0x02, 0xdf20 },
2321 { 0x01, 0x0140 },
2322 { 0x00, 0x00bb },
2323 { 0x04, 0xb800 },
2324 { 0x04, 0xb000 },
2325
2326 { 0x03, 0xdf41 },
2327 { 0x02, 0xdc60 },
2328 { 0x01, 0x6340 },
2329 { 0x00, 0x007d },
2330 { 0x04, 0xd800 },
2331 { 0x04, 0xd000 },
2332
2333 { 0x03, 0xdf01 },
2334 { 0x02, 0xdf20 },
2335 { 0x01, 0x100a },
2336 { 0x00, 0xa0ff },
2337 { 0x04, 0xf800 },
2338 { 0x04, 0xf000 },
2339
2340 { 0x1f, 0x0000 },
2341 { 0x0b, 0x0000 },
2342 { 0x00, 0x9200 }
2343 };
2344
françois romieu4da19632011-01-03 15:07:55 +00002345 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
2347
françois romieu4da19632011-01-03 15:07:55 +00002348static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002349{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002350 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002351 { 0x1f, 0x0002 },
2352 { 0x01, 0x90d0 },
2353 { 0x1f, 0x0000 }
2354 };
2355
françois romieu4da19632011-01-03 15:07:55 +00002356 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002357}
2358
françois romieu4da19632011-01-03 15:07:55 +00002359static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002360{
2361 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002362
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002363 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2364 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002365 return;
2366
françois romieu4da19632011-01-03 15:07:55 +00002367 rtl_writephy(tp, 0x1f, 0x0001);
2368 rtl_writephy(tp, 0x10, 0xf01b);
2369 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002370}
2371
françois romieu4da19632011-01-03 15:07:55 +00002372static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002373{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002374 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002375 { 0x1f, 0x0001 },
2376 { 0x04, 0x0000 },
2377 { 0x03, 0x00a1 },
2378 { 0x02, 0x0008 },
2379 { 0x01, 0x0120 },
2380 { 0x00, 0x1000 },
2381 { 0x04, 0x0800 },
2382 { 0x04, 0x9000 },
2383 { 0x03, 0x802f },
2384 { 0x02, 0x4f02 },
2385 { 0x01, 0x0409 },
2386 { 0x00, 0xf099 },
2387 { 0x04, 0x9800 },
2388 { 0x04, 0xa000 },
2389 { 0x03, 0xdf01 },
2390 { 0x02, 0xdf20 },
2391 { 0x01, 0xff95 },
2392 { 0x00, 0xba00 },
2393 { 0x04, 0xa800 },
2394 { 0x04, 0xf000 },
2395 { 0x03, 0xdf01 },
2396 { 0x02, 0xdf20 },
2397 { 0x01, 0x101a },
2398 { 0x00, 0xa0ff },
2399 { 0x04, 0xf800 },
2400 { 0x04, 0x0000 },
2401 { 0x1f, 0x0000 },
2402
2403 { 0x1f, 0x0001 },
2404 { 0x10, 0xf41b },
2405 { 0x14, 0xfb54 },
2406 { 0x18, 0xf5c7 },
2407 { 0x1f, 0x0000 },
2408
2409 { 0x1f, 0x0001 },
2410 { 0x17, 0x0cc0 },
2411 { 0x1f, 0x0000 }
2412 };
2413
françois romieu4da19632011-01-03 15:07:55 +00002414 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002415
françois romieu4da19632011-01-03 15:07:55 +00002416 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002417}
2418
françois romieu4da19632011-01-03 15:07:55 +00002419static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002420{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002421 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002422 { 0x1f, 0x0001 },
2423 { 0x04, 0x0000 },
2424 { 0x03, 0x00a1 },
2425 { 0x02, 0x0008 },
2426 { 0x01, 0x0120 },
2427 { 0x00, 0x1000 },
2428 { 0x04, 0x0800 },
2429 { 0x04, 0x9000 },
2430 { 0x03, 0x802f },
2431 { 0x02, 0x4f02 },
2432 { 0x01, 0x0409 },
2433 { 0x00, 0xf099 },
2434 { 0x04, 0x9800 },
2435 { 0x04, 0xa000 },
2436 { 0x03, 0xdf01 },
2437 { 0x02, 0xdf20 },
2438 { 0x01, 0xff95 },
2439 { 0x00, 0xba00 },
2440 { 0x04, 0xa800 },
2441 { 0x04, 0xf000 },
2442 { 0x03, 0xdf01 },
2443 { 0x02, 0xdf20 },
2444 { 0x01, 0x101a },
2445 { 0x00, 0xa0ff },
2446 { 0x04, 0xf800 },
2447 { 0x04, 0x0000 },
2448 { 0x1f, 0x0000 },
2449
2450 { 0x1f, 0x0001 },
2451 { 0x0b, 0x8480 },
2452 { 0x1f, 0x0000 },
2453
2454 { 0x1f, 0x0001 },
2455 { 0x18, 0x67c7 },
2456 { 0x04, 0x2000 },
2457 { 0x03, 0x002f },
2458 { 0x02, 0x4360 },
2459 { 0x01, 0x0109 },
2460 { 0x00, 0x3022 },
2461 { 0x04, 0x2800 },
2462 { 0x1f, 0x0000 },
2463
2464 { 0x1f, 0x0001 },
2465 { 0x17, 0x0cc0 },
2466 { 0x1f, 0x0000 }
2467 };
2468
françois romieu4da19632011-01-03 15:07:55 +00002469 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002470}
2471
françois romieu4da19632011-01-03 15:07:55 +00002472static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002473{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002474 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002475 { 0x10, 0xf41b },
2476 { 0x1f, 0x0000 }
2477 };
2478
françois romieu4da19632011-01-03 15:07:55 +00002479 rtl_writephy(tp, 0x1f, 0x0001);
2480 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002481
françois romieu4da19632011-01-03 15:07:55 +00002482 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002483}
2484
françois romieu4da19632011-01-03 15:07:55 +00002485static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002486{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002487 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002488 { 0x1f, 0x0001 },
2489 { 0x10, 0xf41b },
2490 { 0x1f, 0x0000 }
2491 };
2492
françois romieu4da19632011-01-03 15:07:55 +00002493 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002494}
2495
françois romieu4da19632011-01-03 15:07:55 +00002496static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002497{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002498 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002499 { 0x1f, 0x0000 },
2500 { 0x1d, 0x0f00 },
2501 { 0x1f, 0x0002 },
2502 { 0x0c, 0x1ec8 },
2503 { 0x1f, 0x0000 }
2504 };
2505
françois romieu4da19632011-01-03 15:07:55 +00002506 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002507}
2508
françois romieu4da19632011-01-03 15:07:55 +00002509static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002510{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002511 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002512 { 0x1f, 0x0001 },
2513 { 0x1d, 0x3d98 },
2514 { 0x1f, 0x0000 }
2515 };
2516
françois romieu4da19632011-01-03 15:07:55 +00002517 rtl_writephy(tp, 0x1f, 0x0000);
2518 rtl_patchphy(tp, 0x14, 1 << 5);
2519 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002520
françois romieu4da19632011-01-03 15:07:55 +00002521 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002522}
2523
françois romieu4da19632011-01-03 15:07:55 +00002524static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002525{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002526 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002527 { 0x1f, 0x0001 },
2528 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002529 { 0x1f, 0x0002 },
2530 { 0x00, 0x88d4 },
2531 { 0x01, 0x82b1 },
2532 { 0x03, 0x7002 },
2533 { 0x08, 0x9e30 },
2534 { 0x09, 0x01f0 },
2535 { 0x0a, 0x5500 },
2536 { 0x0c, 0x00c8 },
2537 { 0x1f, 0x0003 },
2538 { 0x12, 0xc096 },
2539 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002540 { 0x1f, 0x0000 },
2541 { 0x1f, 0x0000 },
2542 { 0x09, 0x2000 },
2543 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002544 };
2545
françois romieu4da19632011-01-03 15:07:55 +00002546 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002547
françois romieu4da19632011-01-03 15:07:55 +00002548 rtl_patchphy(tp, 0x14, 1 << 5);
2549 rtl_patchphy(tp, 0x0d, 1 << 5);
2550 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002551}
2552
françois romieu4da19632011-01-03 15:07:55 +00002553static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002554{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002555 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002556 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002557 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002558 { 0x03, 0x802f },
2559 { 0x02, 0x4f02 },
2560 { 0x01, 0x0409 },
2561 { 0x00, 0xf099 },
2562 { 0x04, 0x9800 },
2563 { 0x04, 0x9000 },
2564 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002565 { 0x1f, 0x0002 },
2566 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002567 { 0x06, 0x0761 },
2568 { 0x1f, 0x0003 },
2569 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002570 { 0x1f, 0x0000 }
2571 };
2572
françois romieu4da19632011-01-03 15:07:55 +00002573 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002574
françois romieu4da19632011-01-03 15:07:55 +00002575 rtl_patchphy(tp, 0x16, 1 << 0);
2576 rtl_patchphy(tp, 0x14, 1 << 5);
2577 rtl_patchphy(tp, 0x0d, 1 << 5);
2578 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002579}
2580
françois romieu4da19632011-01-03 15:07:55 +00002581static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002582{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002583 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002584 { 0x1f, 0x0001 },
2585 { 0x12, 0x2300 },
2586 { 0x1d, 0x3d98 },
2587 { 0x1f, 0x0002 },
2588 { 0x0c, 0x7eb8 },
2589 { 0x06, 0x5461 },
2590 { 0x1f, 0x0003 },
2591 { 0x16, 0x0f0a },
2592 { 0x1f, 0x0000 }
2593 };
2594
françois romieu4da19632011-01-03 15:07:55 +00002595 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002596
françois romieu4da19632011-01-03 15:07:55 +00002597 rtl_patchphy(tp, 0x16, 1 << 0);
2598 rtl_patchphy(tp, 0x14, 1 << 5);
2599 rtl_patchphy(tp, 0x0d, 1 << 5);
2600 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002601}
2602
françois romieu4da19632011-01-03 15:07:55 +00002603static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002604{
françois romieu4da19632011-01-03 15:07:55 +00002605 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002606}
2607
françois romieubca03d52011-01-03 15:07:31 +00002608static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002609{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002610 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002611 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002612 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002613 { 0x06, 0x4064 },
2614 { 0x07, 0x2863 },
2615 { 0x08, 0x059c },
2616 { 0x09, 0x26b4 },
2617 { 0x0a, 0x6a19 },
2618 { 0x0b, 0xdcc8 },
2619 { 0x10, 0xf06d },
2620 { 0x14, 0x7f68 },
2621 { 0x18, 0x7fd9 },
2622 { 0x1c, 0xf0ff },
2623 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002624 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002625 { 0x12, 0xf49f },
2626 { 0x13, 0x070b },
2627 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002628 { 0x14, 0x94c0 },
2629
2630 /*
2631 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002632 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002633 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002634 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002635 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002636 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002637 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002638 { 0x06, 0x5561 },
2639
2640 /*
2641 * Can not link to 1Gbps with bad cable
2642 * Decrease SNR threshold form 21.07dB to 19.04dB
2643 */
2644 { 0x1f, 0x0001 },
2645 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002646
2647 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002648 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002649 };
2650
françois romieu4da19632011-01-03 15:07:55 +00002651 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002652
françois romieubca03d52011-01-03 15:07:31 +00002653 /*
2654 * Rx Error Issue
2655 * Fine Tune Switching regulator parameter
2656 */
françois romieu4da19632011-01-03 15:07:55 +00002657 rtl_writephy(tp, 0x1f, 0x0002);
2658 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2659 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002660
Francois Romieufdf6fc02012-07-06 22:40:38 +02002661 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002662 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002663 { 0x1f, 0x0002 },
2664 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002665 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002666 { 0x05, 0x8330 },
2667 { 0x06, 0x669a },
2668 { 0x1f, 0x0002 }
2669 };
2670 int val;
2671
françois romieu4da19632011-01-03 15:07:55 +00002672 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002673
françois romieu4da19632011-01-03 15:07:55 +00002674 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002675
2676 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002677 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002678 0x0065, 0x0066, 0x0067, 0x0068,
2679 0x0069, 0x006a, 0x006b, 0x006c
2680 };
2681 int i;
2682
françois romieu4da19632011-01-03 15:07:55 +00002683 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002684
2685 val &= 0xff00;
2686 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002687 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002688 }
2689 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002690 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002691 { 0x1f, 0x0002 },
2692 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002693 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002694 { 0x05, 0x8330 },
2695 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002696 };
2697
françois romieu4da19632011-01-03 15:07:55 +00002698 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002699 }
2700
françois romieubca03d52011-01-03 15:07:31 +00002701 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002702 rtl_writephy(tp, 0x1f, 0x0002);
2703 rtl_patchphy(tp, 0x0d, 0x0300);
2704 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002705
françois romieubca03d52011-01-03 15:07:31 +00002706 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002707 rtl_writephy(tp, 0x1f, 0x0002);
2708 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2709 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002710
françois romieu4da19632011-01-03 15:07:55 +00002711 rtl_writephy(tp, 0x1f, 0x0005);
2712 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002713
2714 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002715
françois romieu4da19632011-01-03 15:07:55 +00002716 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002717}
2718
françois romieubca03d52011-01-03 15:07:31 +00002719static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002720{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002721 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002722 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002723 { 0x1f, 0x0001 },
2724 { 0x06, 0x4064 },
2725 { 0x07, 0x2863 },
2726 { 0x08, 0x059c },
2727 { 0x09, 0x26b4 },
2728 { 0x0a, 0x6a19 },
2729 { 0x0b, 0xdcc8 },
2730 { 0x10, 0xf06d },
2731 { 0x14, 0x7f68 },
2732 { 0x18, 0x7fd9 },
2733 { 0x1c, 0xf0ff },
2734 { 0x1d, 0x3d9c },
2735 { 0x1f, 0x0003 },
2736 { 0x12, 0xf49f },
2737 { 0x13, 0x070b },
2738 { 0x1a, 0x05ad },
2739 { 0x14, 0x94c0 },
2740
françois romieubca03d52011-01-03 15:07:31 +00002741 /*
2742 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002743 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002744 */
françois romieudaf9df62009-10-07 12:44:20 +00002745 { 0x1f, 0x0002 },
2746 { 0x06, 0x5561 },
2747 { 0x1f, 0x0005 },
2748 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002749 { 0x06, 0x5561 },
2750
2751 /*
2752 * Can not link to 1Gbps with bad cable
2753 * Decrease SNR threshold form 21.07dB to 19.04dB
2754 */
2755 { 0x1f, 0x0001 },
2756 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002757
2758 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002759 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002760 };
2761
françois romieu4da19632011-01-03 15:07:55 +00002762 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002763
Francois Romieufdf6fc02012-07-06 22:40:38 +02002764 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002765 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002766 { 0x1f, 0x0002 },
2767 { 0x05, 0x669a },
2768 { 0x1f, 0x0005 },
2769 { 0x05, 0x8330 },
2770 { 0x06, 0x669a },
2771
2772 { 0x1f, 0x0002 }
2773 };
2774 int val;
2775
françois romieu4da19632011-01-03 15:07:55 +00002776 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002777
françois romieu4da19632011-01-03 15:07:55 +00002778 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002779 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002780 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002781 0x0065, 0x0066, 0x0067, 0x0068,
2782 0x0069, 0x006a, 0x006b, 0x006c
2783 };
2784 int i;
2785
françois romieu4da19632011-01-03 15:07:55 +00002786 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002787
2788 val &= 0xff00;
2789 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002790 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002791 }
2792 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002793 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002794 { 0x1f, 0x0002 },
2795 { 0x05, 0x2642 },
2796 { 0x1f, 0x0005 },
2797 { 0x05, 0x8330 },
2798 { 0x06, 0x2642 }
2799 };
2800
françois romieu4da19632011-01-03 15:07:55 +00002801 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002802 }
2803
françois romieubca03d52011-01-03 15:07:31 +00002804 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002805 rtl_writephy(tp, 0x1f, 0x0002);
2806 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2807 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002808
françois romieubca03d52011-01-03 15:07:31 +00002809 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002810 rtl_writephy(tp, 0x1f, 0x0002);
2811 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002812
françois romieu4da19632011-01-03 15:07:55 +00002813 rtl_writephy(tp, 0x1f, 0x0005);
2814 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002815
2816 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002817
françois romieu4da19632011-01-03 15:07:55 +00002818 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002819}
2820
françois romieu4da19632011-01-03 15:07:55 +00002821static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002822{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002823 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002824 { 0x1f, 0x0002 },
2825 { 0x10, 0x0008 },
2826 { 0x0d, 0x006c },
2827
2828 { 0x1f, 0x0000 },
2829 { 0x0d, 0xf880 },
2830
2831 { 0x1f, 0x0001 },
2832 { 0x17, 0x0cc0 },
2833
2834 { 0x1f, 0x0001 },
2835 { 0x0b, 0xa4d8 },
2836 { 0x09, 0x281c },
2837 { 0x07, 0x2883 },
2838 { 0x0a, 0x6b35 },
2839 { 0x1d, 0x3da4 },
2840 { 0x1c, 0xeffd },
2841 { 0x14, 0x7f52 },
2842 { 0x18, 0x7fc6 },
2843 { 0x08, 0x0601 },
2844 { 0x06, 0x4063 },
2845 { 0x10, 0xf074 },
2846 { 0x1f, 0x0003 },
2847 { 0x13, 0x0789 },
2848 { 0x12, 0xf4bd },
2849 { 0x1a, 0x04fd },
2850 { 0x14, 0x84b0 },
2851 { 0x1f, 0x0000 },
2852 { 0x00, 0x9200 },
2853
2854 { 0x1f, 0x0005 },
2855 { 0x01, 0x0340 },
2856 { 0x1f, 0x0001 },
2857 { 0x04, 0x4000 },
2858 { 0x03, 0x1d21 },
2859 { 0x02, 0x0c32 },
2860 { 0x01, 0x0200 },
2861 { 0x00, 0x5554 },
2862 { 0x04, 0x4800 },
2863 { 0x04, 0x4000 },
2864 { 0x04, 0xf000 },
2865 { 0x03, 0xdf01 },
2866 { 0x02, 0xdf20 },
2867 { 0x01, 0x101a },
2868 { 0x00, 0xa0ff },
2869 { 0x04, 0xf800 },
2870 { 0x04, 0xf000 },
2871 { 0x1f, 0x0000 },
2872
2873 { 0x1f, 0x0007 },
2874 { 0x1e, 0x0023 },
2875 { 0x16, 0x0000 },
2876 { 0x1f, 0x0000 }
2877 };
2878
françois romieu4da19632011-01-03 15:07:55 +00002879 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002880}
2881
françois romieue6de30d2011-01-03 15:08:37 +00002882static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2883{
2884 static const struct phy_reg phy_reg_init[] = {
2885 { 0x1f, 0x0001 },
2886 { 0x17, 0x0cc0 },
2887
2888 { 0x1f, 0x0007 },
2889 { 0x1e, 0x002d },
2890 { 0x18, 0x0040 },
2891 { 0x1f, 0x0000 }
2892 };
2893
2894 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2895 rtl_patchphy(tp, 0x0d, 1 << 5);
2896}
2897
Hayes Wang70090422011-07-06 15:58:06 +08002898static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002899{
2900 static const struct phy_reg phy_reg_init[] = {
2901 /* Enable Delay cap */
2902 { 0x1f, 0x0005 },
2903 { 0x05, 0x8b80 },
2904 { 0x06, 0xc896 },
2905 { 0x1f, 0x0000 },
2906
2907 /* Channel estimation fine tune */
2908 { 0x1f, 0x0001 },
2909 { 0x0b, 0x6c20 },
2910 { 0x07, 0x2872 },
2911 { 0x1c, 0xefff },
2912 { 0x1f, 0x0003 },
2913 { 0x14, 0x6420 },
2914 { 0x1f, 0x0000 },
2915
2916 /* Update PFM & 10M TX idle timer */
2917 { 0x1f, 0x0007 },
2918 { 0x1e, 0x002f },
2919 { 0x15, 0x1919 },
2920 { 0x1f, 0x0000 },
2921
2922 { 0x1f, 0x0007 },
2923 { 0x1e, 0x00ac },
2924 { 0x18, 0x0006 },
2925 { 0x1f, 0x0000 }
2926 };
2927
Francois Romieu15ecd032011-04-27 13:52:22 -07002928 rtl_apply_firmware(tp);
2929
hayeswang01dc7fe2011-03-21 01:50:28 +00002930 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2931
2932 /* DCO enable for 10M IDLE Power */
2933 rtl_writephy(tp, 0x1f, 0x0007);
2934 rtl_writephy(tp, 0x1e, 0x0023);
2935 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2936 rtl_writephy(tp, 0x1f, 0x0000);
2937
2938 /* For impedance matching */
2939 rtl_writephy(tp, 0x1f, 0x0002);
2940 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002941 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002942
2943 /* PHY auto speed down */
2944 rtl_writephy(tp, 0x1f, 0x0007);
2945 rtl_writephy(tp, 0x1e, 0x002d);
2946 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2947 rtl_writephy(tp, 0x1f, 0x0000);
2948 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2949
2950 rtl_writephy(tp, 0x1f, 0x0005);
2951 rtl_writephy(tp, 0x05, 0x8b86);
2952 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2953 rtl_writephy(tp, 0x1f, 0x0000);
2954
2955 rtl_writephy(tp, 0x1f, 0x0005);
2956 rtl_writephy(tp, 0x05, 0x8b85);
2957 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2958 rtl_writephy(tp, 0x1f, 0x0007);
2959 rtl_writephy(tp, 0x1e, 0x0020);
2960 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2961 rtl_writephy(tp, 0x1f, 0x0006);
2962 rtl_writephy(tp, 0x00, 0x5a00);
2963 rtl_writephy(tp, 0x1f, 0x0000);
2964 rtl_writephy(tp, 0x0d, 0x0007);
2965 rtl_writephy(tp, 0x0e, 0x003c);
2966 rtl_writephy(tp, 0x0d, 0x4007);
2967 rtl_writephy(tp, 0x0e, 0x0000);
2968 rtl_writephy(tp, 0x0d, 0x0000);
2969}
2970
Hayes Wang70090422011-07-06 15:58:06 +08002971static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2972{
2973 static const struct phy_reg phy_reg_init[] = {
2974 /* Enable Delay cap */
2975 { 0x1f, 0x0004 },
2976 { 0x1f, 0x0007 },
2977 { 0x1e, 0x00ac },
2978 { 0x18, 0x0006 },
2979 { 0x1f, 0x0002 },
2980 { 0x1f, 0x0000 },
2981 { 0x1f, 0x0000 },
2982
2983 /* Channel estimation fine tune */
2984 { 0x1f, 0x0003 },
2985 { 0x09, 0xa20f },
2986 { 0x1f, 0x0000 },
2987 { 0x1f, 0x0000 },
2988
2989 /* Green Setting */
2990 { 0x1f, 0x0005 },
2991 { 0x05, 0x8b5b },
2992 { 0x06, 0x9222 },
2993 { 0x05, 0x8b6d },
2994 { 0x06, 0x8000 },
2995 { 0x05, 0x8b76 },
2996 { 0x06, 0x8000 },
2997 { 0x1f, 0x0000 }
2998 };
2999
3000 rtl_apply_firmware(tp);
3001
3002 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3003
3004 /* For 4-corner performance improve */
3005 rtl_writephy(tp, 0x1f, 0x0005);
3006 rtl_writephy(tp, 0x05, 0x8b80);
3007 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
3008 rtl_writephy(tp, 0x1f, 0x0000);
3009
3010 /* PHY auto speed down */
3011 rtl_writephy(tp, 0x1f, 0x0004);
3012 rtl_writephy(tp, 0x1f, 0x0007);
3013 rtl_writephy(tp, 0x1e, 0x002d);
3014 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3015 rtl_writephy(tp, 0x1f, 0x0002);
3016 rtl_writephy(tp, 0x1f, 0x0000);
3017 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3018
3019 /* improve 10M EEE waveform */
3020 rtl_writephy(tp, 0x1f, 0x0005);
3021 rtl_writephy(tp, 0x05, 0x8b86);
3022 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3023 rtl_writephy(tp, 0x1f, 0x0000);
3024
3025 /* Improve 2-pair detection performance */
3026 rtl_writephy(tp, 0x1f, 0x0005);
3027 rtl_writephy(tp, 0x05, 0x8b85);
3028 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3029 rtl_writephy(tp, 0x1f, 0x0000);
3030
3031 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003032 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003033 rtl_writephy(tp, 0x1f, 0x0005);
3034 rtl_writephy(tp, 0x05, 0x8b85);
3035 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3036 rtl_writephy(tp, 0x1f, 0x0004);
3037 rtl_writephy(tp, 0x1f, 0x0007);
3038 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04003039 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003040 rtl_writephy(tp, 0x1f, 0x0002);
3041 rtl_writephy(tp, 0x1f, 0x0000);
3042 rtl_writephy(tp, 0x0d, 0x0007);
3043 rtl_writephy(tp, 0x0e, 0x003c);
3044 rtl_writephy(tp, 0x0d, 0x4007);
3045 rtl_writephy(tp, 0x0e, 0x0000);
3046 rtl_writephy(tp, 0x0d, 0x0000);
3047
3048 /* Green feature */
3049 rtl_writephy(tp, 0x1f, 0x0003);
3050 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3051 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3052 rtl_writephy(tp, 0x1f, 0x0000);
3053}
3054
Hayes Wang5f886e02012-03-30 14:33:03 +08003055static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3056{
3057 /* For 4-corner performance improve */
3058 rtl_writephy(tp, 0x1f, 0x0005);
3059 rtl_writephy(tp, 0x05, 0x8b80);
3060 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3061 rtl_writephy(tp, 0x1f, 0x0000);
3062
3063 /* PHY auto speed down */
3064 rtl_writephy(tp, 0x1f, 0x0007);
3065 rtl_writephy(tp, 0x1e, 0x002d);
3066 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3067 rtl_writephy(tp, 0x1f, 0x0000);
3068 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3069
3070 /* Improve 10M EEE waveform */
3071 rtl_writephy(tp, 0x1f, 0x0005);
3072 rtl_writephy(tp, 0x05, 0x8b86);
3073 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3074 rtl_writephy(tp, 0x1f, 0x0000);
3075}
3076
Hayes Wangc2218922011-09-06 16:55:18 +08003077static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3078{
3079 static const struct phy_reg phy_reg_init[] = {
3080 /* Channel estimation fine tune */
3081 { 0x1f, 0x0003 },
3082 { 0x09, 0xa20f },
3083 { 0x1f, 0x0000 },
3084
3085 /* Modify green table for giga & fnet */
3086 { 0x1f, 0x0005 },
3087 { 0x05, 0x8b55 },
3088 { 0x06, 0x0000 },
3089 { 0x05, 0x8b5e },
3090 { 0x06, 0x0000 },
3091 { 0x05, 0x8b67 },
3092 { 0x06, 0x0000 },
3093 { 0x05, 0x8b70 },
3094 { 0x06, 0x0000 },
3095 { 0x1f, 0x0000 },
3096 { 0x1f, 0x0007 },
3097 { 0x1e, 0x0078 },
3098 { 0x17, 0x0000 },
3099 { 0x19, 0x00fb },
3100 { 0x1f, 0x0000 },
3101
3102 /* Modify green table for 10M */
3103 { 0x1f, 0x0005 },
3104 { 0x05, 0x8b79 },
3105 { 0x06, 0xaa00 },
3106 { 0x1f, 0x0000 },
3107
3108 /* Disable hiimpedance detection (RTCT) */
3109 { 0x1f, 0x0003 },
3110 { 0x01, 0x328a },
3111 { 0x1f, 0x0000 }
3112 };
3113
3114 rtl_apply_firmware(tp);
3115
3116 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3117
Hayes Wang5f886e02012-03-30 14:33:03 +08003118 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003119
3120 /* Improve 2-pair detection performance */
3121 rtl_writephy(tp, 0x1f, 0x0005);
3122 rtl_writephy(tp, 0x05, 0x8b85);
3123 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3124 rtl_writephy(tp, 0x1f, 0x0000);
3125}
3126
3127static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3128{
3129 rtl_apply_firmware(tp);
3130
Hayes Wang5f886e02012-03-30 14:33:03 +08003131 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003132}
3133
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003134static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3135{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003136 static const struct phy_reg phy_reg_init[] = {
3137 /* Channel estimation fine tune */
3138 { 0x1f, 0x0003 },
3139 { 0x09, 0xa20f },
3140 { 0x1f, 0x0000 },
3141
3142 /* Modify green table for giga & fnet */
3143 { 0x1f, 0x0005 },
3144 { 0x05, 0x8b55 },
3145 { 0x06, 0x0000 },
3146 { 0x05, 0x8b5e },
3147 { 0x06, 0x0000 },
3148 { 0x05, 0x8b67 },
3149 { 0x06, 0x0000 },
3150 { 0x05, 0x8b70 },
3151 { 0x06, 0x0000 },
3152 { 0x1f, 0x0000 },
3153 { 0x1f, 0x0007 },
3154 { 0x1e, 0x0078 },
3155 { 0x17, 0x0000 },
3156 { 0x19, 0x00aa },
3157 { 0x1f, 0x0000 },
3158
3159 /* Modify green table for 10M */
3160 { 0x1f, 0x0005 },
3161 { 0x05, 0x8b79 },
3162 { 0x06, 0xaa00 },
3163 { 0x1f, 0x0000 },
3164
3165 /* Disable hiimpedance detection (RTCT) */
3166 { 0x1f, 0x0003 },
3167 { 0x01, 0x328a },
3168 { 0x1f, 0x0000 }
3169 };
3170
3171
3172 rtl_apply_firmware(tp);
3173
3174 rtl8168f_hw_phy_config(tp);
3175
3176 /* Improve 2-pair detection performance */
3177 rtl_writephy(tp, 0x1f, 0x0005);
3178 rtl_writephy(tp, 0x05, 0x8b85);
3179 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3180 rtl_writephy(tp, 0x1f, 0x0000);
3181
3182 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3183
3184 /* Modify green table for giga */
3185 rtl_writephy(tp, 0x1f, 0x0005);
3186 rtl_writephy(tp, 0x05, 0x8b54);
3187 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3188 rtl_writephy(tp, 0x05, 0x8b5d);
3189 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3190 rtl_writephy(tp, 0x05, 0x8a7c);
3191 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3192 rtl_writephy(tp, 0x05, 0x8a7f);
3193 rtl_w1w0_phy(tp, 0x06, 0x0100, 0x0000);
3194 rtl_writephy(tp, 0x05, 0x8a82);
3195 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3196 rtl_writephy(tp, 0x05, 0x8a85);
3197 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3198 rtl_writephy(tp, 0x05, 0x8a88);
3199 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3200 rtl_writephy(tp, 0x1f, 0x0000);
3201
3202 /* uc same-seed solution */
3203 rtl_writephy(tp, 0x1f, 0x0005);
3204 rtl_writephy(tp, 0x05, 0x8b85);
3205 rtl_w1w0_phy(tp, 0x06, 0x8000, 0x0000);
3206 rtl_writephy(tp, 0x1f, 0x0000);
3207
3208 /* eee setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003209 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003210 rtl_writephy(tp, 0x1f, 0x0005);
3211 rtl_writephy(tp, 0x05, 0x8b85);
3212 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3213 rtl_writephy(tp, 0x1f, 0x0004);
3214 rtl_writephy(tp, 0x1f, 0x0007);
3215 rtl_writephy(tp, 0x1e, 0x0020);
3216 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
3217 rtl_writephy(tp, 0x1f, 0x0000);
3218 rtl_writephy(tp, 0x0d, 0x0007);
3219 rtl_writephy(tp, 0x0e, 0x003c);
3220 rtl_writephy(tp, 0x0d, 0x4007);
3221 rtl_writephy(tp, 0x0e, 0x0000);
3222 rtl_writephy(tp, 0x0d, 0x0000);
3223
3224 /* Green feature */
3225 rtl_writephy(tp, 0x1f, 0x0003);
3226 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3227 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3228 rtl_writephy(tp, 0x1f, 0x0000);
3229}
3230
françois romieu4da19632011-01-03 15:07:55 +00003231static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003232{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003233 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003234 { 0x1f, 0x0003 },
3235 { 0x08, 0x441d },
3236 { 0x01, 0x9100 },
3237 { 0x1f, 0x0000 }
3238 };
3239
françois romieu4da19632011-01-03 15:07:55 +00003240 rtl_writephy(tp, 0x1f, 0x0000);
3241 rtl_patchphy(tp, 0x11, 1 << 12);
3242 rtl_patchphy(tp, 0x19, 1 << 13);
3243 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003244
françois romieu4da19632011-01-03 15:07:55 +00003245 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003246}
3247
Hayes Wang5a5e4442011-02-22 17:26:21 +08003248static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3249{
3250 static const struct phy_reg phy_reg_init[] = {
3251 { 0x1f, 0x0005 },
3252 { 0x1a, 0x0000 },
3253 { 0x1f, 0x0000 },
3254
3255 { 0x1f, 0x0004 },
3256 { 0x1c, 0x0000 },
3257 { 0x1f, 0x0000 },
3258
3259 { 0x1f, 0x0001 },
3260 { 0x15, 0x7701 },
3261 { 0x1f, 0x0000 }
3262 };
3263
3264 /* Disable ALDPS before ram code */
3265 rtl_writephy(tp, 0x1f, 0x0000);
3266 rtl_writephy(tp, 0x18, 0x0310);
3267 msleep(100);
3268
François Romieu953a12c2011-04-24 17:38:48 +02003269 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003270
3271 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3272}
3273
Hayes Wang7e18dca2012-03-30 14:33:02 +08003274static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3275{
Hayes Wang7e18dca2012-03-30 14:33:02 +08003276 /* Disable ALDPS before setting firmware */
3277 rtl_writephy(tp, 0x1f, 0x0000);
3278 rtl_writephy(tp, 0x18, 0x0310);
3279 msleep(20);
3280
3281 rtl_apply_firmware(tp);
3282
3283 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003284 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003285 rtl_writephy(tp, 0x1f, 0x0004);
3286 rtl_writephy(tp, 0x10, 0x401f);
3287 rtl_writephy(tp, 0x19, 0x7030);
3288 rtl_writephy(tp, 0x1f, 0x0000);
3289}
3290
Hayes Wang5598bfe2012-07-02 17:23:21 +08003291static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3292{
Hayes Wang5598bfe2012-07-02 17:23:21 +08003293 static const struct phy_reg phy_reg_init[] = {
3294 { 0x1f, 0x0004 },
3295 { 0x10, 0xc07f },
3296 { 0x19, 0x7030 },
3297 { 0x1f, 0x0000 }
3298 };
3299
3300 /* Disable ALDPS before ram code */
3301 rtl_writephy(tp, 0x1f, 0x0000);
3302 rtl_writephy(tp, 0x18, 0x0310);
3303 msleep(100);
3304
3305 rtl_apply_firmware(tp);
3306
Francois Romieufdf6fc02012-07-06 22:40:38 +02003307 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003308 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3309
Francois Romieufdf6fc02012-07-06 22:40:38 +02003310 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003311}
3312
Francois Romieu5615d9f2007-08-17 17:50:46 +02003313static void rtl_hw_phy_config(struct net_device *dev)
3314{
3315 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003316
3317 rtl8169_print_mac_version(tp);
3318
3319 switch (tp->mac_version) {
3320 case RTL_GIGA_MAC_VER_01:
3321 break;
3322 case RTL_GIGA_MAC_VER_02:
3323 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003324 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003325 break;
3326 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003327 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003328 break;
françois romieu2e9558562009-08-10 19:44:19 +00003329 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003330 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003331 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003332 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003333 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003334 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003335 case RTL_GIGA_MAC_VER_07:
3336 case RTL_GIGA_MAC_VER_08:
3337 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003338 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003339 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003340 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003341 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003342 break;
3343 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003344 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003345 break;
3346 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003347 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003348 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003349 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003350 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003351 break;
3352 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003353 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003354 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003355 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003356 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003357 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003358 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003359 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003360 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003361 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003362 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003363 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003364 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003365 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003366 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003367 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003368 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003369 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003370 break;
3371 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003372 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003373 break;
3374 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003375 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003376 break;
françois romieue6de30d2011-01-03 15:08:37 +00003377 case RTL_GIGA_MAC_VER_28:
3378 rtl8168d_4_hw_phy_config(tp);
3379 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003380 case RTL_GIGA_MAC_VER_29:
3381 case RTL_GIGA_MAC_VER_30:
3382 rtl8105e_hw_phy_config(tp);
3383 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003384 case RTL_GIGA_MAC_VER_31:
3385 /* None. */
3386 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003387 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003388 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003389 rtl8168e_1_hw_phy_config(tp);
3390 break;
3391 case RTL_GIGA_MAC_VER_34:
3392 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003393 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003394 case RTL_GIGA_MAC_VER_35:
3395 rtl8168f_1_hw_phy_config(tp);
3396 break;
3397 case RTL_GIGA_MAC_VER_36:
3398 rtl8168f_2_hw_phy_config(tp);
3399 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003400
Hayes Wang7e18dca2012-03-30 14:33:02 +08003401 case RTL_GIGA_MAC_VER_37:
3402 rtl8402_hw_phy_config(tp);
3403 break;
3404
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003405 case RTL_GIGA_MAC_VER_38:
3406 rtl8411_hw_phy_config(tp);
3407 break;
3408
Hayes Wang5598bfe2012-07-02 17:23:21 +08003409 case RTL_GIGA_MAC_VER_39:
3410 rtl8106e_hw_phy_config(tp);
3411 break;
3412
Francois Romieu5615d9f2007-08-17 17:50:46 +02003413 default:
3414 break;
3415 }
3416}
3417
Francois Romieuda78dbf2012-01-26 14:18:23 +01003418static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 struct timer_list *timer = &tp->timer;
3421 void __iomem *ioaddr = tp->mmio_addr;
3422 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3423
Francois Romieubcf0bf92006-07-26 23:14:13 +02003424 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
françois romieu4da19632011-01-03 15:07:55 +00003426 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003427 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 * A busy loop could burn quite a few cycles on nowadays CPU.
3429 * Let's delay the execution of the timer for a few ticks.
3430 */
3431 timeout = HZ/10;
3432 goto out_mod_timer;
3433 }
3434
3435 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01003436 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437
Francois Romieuda78dbf2012-01-26 14:18:23 +01003438 netif_warn(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
françois romieu4da19632011-01-03 15:07:55 +00003440 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
3442out_mod_timer:
3443 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003444}
3445
3446static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3447{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003448 if (!test_and_set_bit(flag, tp->wk.flags))
3449 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003450}
3451
3452static void rtl8169_phy_timer(unsigned long __opaque)
3453{
3454 struct net_device *dev = (struct net_device *)__opaque;
3455 struct rtl8169_private *tp = netdev_priv(dev);
3456
Francois Romieu98ddf982012-01-31 10:47:34 +01003457 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458}
3459
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3461 void __iomem *ioaddr)
3462{
3463 iounmap(ioaddr);
3464 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003465 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 pci_disable_device(pdev);
3467 free_netdev(dev);
3468}
3469
Francois Romieubf793292006-11-01 00:53:05 +01003470static void rtl8169_phy_reset(struct net_device *dev,
3471 struct rtl8169_private *tp)
3472{
Francois Romieu07d3f512007-02-21 22:40:46 +01003473 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003474
françois romieu4da19632011-01-03 15:07:55 +00003475 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003476 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003477 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003478 return;
3479 msleep(1);
3480 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003481 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003482}
3483
David S. Miller8decf862011-09-22 03:23:13 -04003484static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3485{
3486 void __iomem *ioaddr = tp->mmio_addr;
3487
3488 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3489 (RTL_R8(PHYstatus) & TBI_Enable);
3490}
3491
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003492static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003494 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003495
Francois Romieu5615d9f2007-08-17 17:50:46 +02003496 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003497
Marcus Sundberg773328942008-07-10 21:28:08 +02003498 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3499 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3500 RTL_W8(0x82, 0x01);
3501 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003502
Francois Romieu6dccd162007-02-13 23:38:05 +01003503 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3504
3505 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3506 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003507
Francois Romieubcf0bf92006-07-26 23:14:13 +02003508 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003509 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3510 RTL_W8(0x82, 0x01);
3511 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003512 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003513 }
3514
Francois Romieubf793292006-11-01 00:53:05 +01003515 rtl8169_phy_reset(dev, tp);
3516
Oliver Neukum54405cd2011-01-06 21:55:13 +01003517 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003518 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3519 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3520 (tp->mii.supports_gmii ?
3521 ADVERTISED_1000baseT_Half |
3522 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003523
David S. Miller8decf862011-09-22 03:23:13 -04003524 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003525 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003526}
3527
Francois Romieu773d2022007-01-31 23:47:43 +01003528static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3529{
3530 void __iomem *ioaddr = tp->mmio_addr;
3531 u32 high;
3532 u32 low;
3533
3534 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3535 high = addr[4] | (addr[5] << 8);
3536
Francois Romieuda78dbf2012-01-26 14:18:23 +01003537 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003538
3539 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2bf2010-04-26 11:42:58 +00003540
Francois Romieu773d2022007-01-31 23:47:43 +01003541 RTL_W32(MAC4, high);
françois romieu908ba2bf2010-04-26 11:42:58 +00003542 RTL_R32(MAC4);
3543
Francois Romieu78f1cd02010-03-27 19:35:46 -07003544 RTL_W32(MAC0, low);
françois romieu908ba2bf2010-04-26 11:42:58 +00003545 RTL_R32(MAC0);
3546
françois romieuc28aa382011-08-02 03:53:43 +00003547 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3548 const struct exgmac_reg e[] = {
3549 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3550 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3551 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3552 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3553 low >> 16 },
3554 };
3555
Francois Romieufdf6fc02012-07-06 22:40:38 +02003556 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
françois romieuc28aa382011-08-02 03:53:43 +00003557 }
3558
Francois Romieu773d2022007-01-31 23:47:43 +01003559 RTL_W8(Cfg9346, Cfg9346_Lock);
3560
Francois Romieuda78dbf2012-01-26 14:18:23 +01003561 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003562}
3563
3564static int rtl_set_mac_address(struct net_device *dev, void *p)
3565{
3566 struct rtl8169_private *tp = netdev_priv(dev);
3567 struct sockaddr *addr = p;
3568
3569 if (!is_valid_ether_addr(addr->sa_data))
3570 return -EADDRNOTAVAIL;
3571
3572 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3573
3574 rtl_rar_set(tp, dev->dev_addr);
3575
3576 return 0;
3577}
3578
Francois Romieu5f787a12006-08-17 13:02:36 +02003579static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3580{
3581 struct rtl8169_private *tp = netdev_priv(dev);
3582 struct mii_ioctl_data *data = if_mii(ifr);
3583
Francois Romieu8b4ab282008-11-19 22:05:25 -08003584 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3585}
Francois Romieu5f787a12006-08-17 13:02:36 +02003586
Francois Romieucecb5fd2011-04-01 10:21:07 +02003587static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3588 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003589{
Francois Romieu5f787a12006-08-17 13:02:36 +02003590 switch (cmd) {
3591 case SIOCGMIIPHY:
3592 data->phy_id = 32; /* Internal PHY */
3593 return 0;
3594
3595 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003596 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003597 return 0;
3598
3599 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003600 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003601 return 0;
3602 }
3603 return -EOPNOTSUPP;
3604}
3605
Francois Romieu8b4ab282008-11-19 22:05:25 -08003606static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3607{
3608 return -EOPNOTSUPP;
3609}
3610
Francois Romieufbac58f2007-10-04 22:51:38 +02003611static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3612{
3613 if (tp->features & RTL_FEATURE_MSI) {
3614 pci_disable_msi(pdev);
3615 tp->features &= ~RTL_FEATURE_MSI;
3616 }
3617}
3618
françois romieuc0e45c12011-01-03 15:08:04 +00003619static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3620{
3621 struct mdio_ops *ops = &tp->mdio_ops;
3622
3623 switch (tp->mac_version) {
3624 case RTL_GIGA_MAC_VER_27:
3625 ops->write = r8168dp_1_mdio_write;
3626 ops->read = r8168dp_1_mdio_read;
3627 break;
françois romieue6de30d2011-01-03 15:08:37 +00003628 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003629 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003630 ops->write = r8168dp_2_mdio_write;
3631 ops->read = r8168dp_2_mdio_read;
3632 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003633 default:
3634 ops->write = r8169_mdio_write;
3635 ops->read = r8169_mdio_read;
3636 break;
3637 }
3638}
3639
David S. Miller1805b2f2011-10-24 18:18:09 -04003640static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3641{
3642 void __iomem *ioaddr = tp->mmio_addr;
3643
3644 switch (tp->mac_version) {
3645 case RTL_GIGA_MAC_VER_29:
3646 case RTL_GIGA_MAC_VER_30:
3647 case RTL_GIGA_MAC_VER_32:
3648 case RTL_GIGA_MAC_VER_33:
3649 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08003650 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003651 case RTL_GIGA_MAC_VER_38:
Hayes Wang5598bfe2012-07-02 17:23:21 +08003652 case RTL_GIGA_MAC_VER_39:
David S. Miller1805b2f2011-10-24 18:18:09 -04003653 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3654 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3655 break;
3656 default:
3657 break;
3658 }
3659}
3660
3661static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3662{
3663 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3664 return false;
3665
3666 rtl_writephy(tp, 0x1f, 0x0000);
3667 rtl_writephy(tp, MII_BMCR, 0x0000);
3668
3669 rtl_wol_suspend_quirk(tp);
3670
3671 return true;
3672}
3673
françois romieu065c27c2011-01-03 15:08:12 +00003674static void r810x_phy_power_down(struct rtl8169_private *tp)
3675{
3676 rtl_writephy(tp, 0x1f, 0x0000);
3677 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3678}
3679
3680static void r810x_phy_power_up(struct rtl8169_private *tp)
3681{
3682 rtl_writephy(tp, 0x1f, 0x0000);
3683 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3684}
3685
3686static void r810x_pll_power_down(struct rtl8169_private *tp)
3687{
Hayes Wang00042992012-03-30 14:33:00 +08003688 void __iomem *ioaddr = tp->mmio_addr;
3689
David S. Miller1805b2f2011-10-24 18:18:09 -04003690 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003691 return;
françois romieu065c27c2011-01-03 15:08:12 +00003692
3693 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003694
3695 switch (tp->mac_version) {
3696 case RTL_GIGA_MAC_VER_07:
3697 case RTL_GIGA_MAC_VER_08:
3698 case RTL_GIGA_MAC_VER_09:
3699 case RTL_GIGA_MAC_VER_10:
3700 case RTL_GIGA_MAC_VER_13:
3701 case RTL_GIGA_MAC_VER_16:
3702 break;
3703 default:
3704 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3705 break;
3706 }
françois romieu065c27c2011-01-03 15:08:12 +00003707}
3708
3709static void r810x_pll_power_up(struct rtl8169_private *tp)
3710{
Hayes Wang00042992012-03-30 14:33:00 +08003711 void __iomem *ioaddr = tp->mmio_addr;
3712
françois romieu065c27c2011-01-03 15:08:12 +00003713 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003714
3715 switch (tp->mac_version) {
3716 case RTL_GIGA_MAC_VER_07:
3717 case RTL_GIGA_MAC_VER_08:
3718 case RTL_GIGA_MAC_VER_09:
3719 case RTL_GIGA_MAC_VER_10:
3720 case RTL_GIGA_MAC_VER_13:
3721 case RTL_GIGA_MAC_VER_16:
3722 break;
3723 default:
3724 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3725 break;
3726 }
françois romieu065c27c2011-01-03 15:08:12 +00003727}
3728
3729static void r8168_phy_power_up(struct rtl8169_private *tp)
3730{
3731 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003732 switch (tp->mac_version) {
3733 case RTL_GIGA_MAC_VER_11:
3734 case RTL_GIGA_MAC_VER_12:
3735 case RTL_GIGA_MAC_VER_17:
3736 case RTL_GIGA_MAC_VER_18:
3737 case RTL_GIGA_MAC_VER_19:
3738 case RTL_GIGA_MAC_VER_20:
3739 case RTL_GIGA_MAC_VER_21:
3740 case RTL_GIGA_MAC_VER_22:
3741 case RTL_GIGA_MAC_VER_23:
3742 case RTL_GIGA_MAC_VER_24:
3743 case RTL_GIGA_MAC_VER_25:
3744 case RTL_GIGA_MAC_VER_26:
3745 case RTL_GIGA_MAC_VER_27:
3746 case RTL_GIGA_MAC_VER_28:
3747 case RTL_GIGA_MAC_VER_31:
3748 rtl_writephy(tp, 0x0e, 0x0000);
3749 break;
3750 default:
3751 break;
3752 }
françois romieu065c27c2011-01-03 15:08:12 +00003753 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3754}
3755
3756static void r8168_phy_power_down(struct rtl8169_private *tp)
3757{
3758 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003759 switch (tp->mac_version) {
3760 case RTL_GIGA_MAC_VER_32:
3761 case RTL_GIGA_MAC_VER_33:
3762 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3763 break;
3764
3765 case RTL_GIGA_MAC_VER_11:
3766 case RTL_GIGA_MAC_VER_12:
3767 case RTL_GIGA_MAC_VER_17:
3768 case RTL_GIGA_MAC_VER_18:
3769 case RTL_GIGA_MAC_VER_19:
3770 case RTL_GIGA_MAC_VER_20:
3771 case RTL_GIGA_MAC_VER_21:
3772 case RTL_GIGA_MAC_VER_22:
3773 case RTL_GIGA_MAC_VER_23:
3774 case RTL_GIGA_MAC_VER_24:
3775 case RTL_GIGA_MAC_VER_25:
3776 case RTL_GIGA_MAC_VER_26:
3777 case RTL_GIGA_MAC_VER_27:
3778 case RTL_GIGA_MAC_VER_28:
3779 case RTL_GIGA_MAC_VER_31:
3780 rtl_writephy(tp, 0x0e, 0x0200);
3781 default:
3782 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3783 break;
3784 }
françois romieu065c27c2011-01-03 15:08:12 +00003785}
3786
3787static void r8168_pll_power_down(struct rtl8169_private *tp)
3788{
3789 void __iomem *ioaddr = tp->mmio_addr;
3790
Francois Romieucecb5fd2011-04-01 10:21:07 +02003791 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3792 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3793 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003794 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003795 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003796 }
françois romieu065c27c2011-01-03 15:08:12 +00003797
Francois Romieucecb5fd2011-04-01 10:21:07 +02003798 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3799 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003800 (RTL_R16(CPlusCmd) & ASF)) {
3801 return;
3802 }
3803
hayeswang01dc7fe2011-03-21 01:50:28 +00003804 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3805 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02003806 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00003807
David S. Miller1805b2f2011-10-24 18:18:09 -04003808 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003809 return;
françois romieu065c27c2011-01-03 15:08:12 +00003810
3811 r8168_phy_power_down(tp);
3812
3813 switch (tp->mac_version) {
3814 case RTL_GIGA_MAC_VER_25:
3815 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003816 case RTL_GIGA_MAC_VER_27:
3817 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003818 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003819 case RTL_GIGA_MAC_VER_32:
3820 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003821 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3822 break;
3823 }
3824}
3825
3826static void r8168_pll_power_up(struct rtl8169_private *tp)
3827{
3828 void __iomem *ioaddr = tp->mmio_addr;
3829
françois romieu065c27c2011-01-03 15:08:12 +00003830 switch (tp->mac_version) {
3831 case RTL_GIGA_MAC_VER_25:
3832 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003833 case RTL_GIGA_MAC_VER_27:
3834 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003835 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003836 case RTL_GIGA_MAC_VER_32:
3837 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003838 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3839 break;
3840 }
3841
3842 r8168_phy_power_up(tp);
3843}
3844
Francois Romieud58d46b2011-05-03 16:38:29 +02003845static void rtl_generic_op(struct rtl8169_private *tp,
3846 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00003847{
3848 if (op)
3849 op(tp);
3850}
3851
3852static void rtl_pll_power_down(struct rtl8169_private *tp)
3853{
Francois Romieud58d46b2011-05-03 16:38:29 +02003854 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00003855}
3856
3857static void rtl_pll_power_up(struct rtl8169_private *tp)
3858{
Francois Romieud58d46b2011-05-03 16:38:29 +02003859 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00003860}
3861
3862static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3863{
3864 struct pll_power_ops *ops = &tp->pll_power_ops;
3865
3866 switch (tp->mac_version) {
3867 case RTL_GIGA_MAC_VER_07:
3868 case RTL_GIGA_MAC_VER_08:
3869 case RTL_GIGA_MAC_VER_09:
3870 case RTL_GIGA_MAC_VER_10:
3871 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003872 case RTL_GIGA_MAC_VER_29:
3873 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08003874 case RTL_GIGA_MAC_VER_37:
Hayes Wang5598bfe2012-07-02 17:23:21 +08003875 case RTL_GIGA_MAC_VER_39:
françois romieu065c27c2011-01-03 15:08:12 +00003876 ops->down = r810x_pll_power_down;
3877 ops->up = r810x_pll_power_up;
3878 break;
3879
3880 case RTL_GIGA_MAC_VER_11:
3881 case RTL_GIGA_MAC_VER_12:
3882 case RTL_GIGA_MAC_VER_17:
3883 case RTL_GIGA_MAC_VER_18:
3884 case RTL_GIGA_MAC_VER_19:
3885 case RTL_GIGA_MAC_VER_20:
3886 case RTL_GIGA_MAC_VER_21:
3887 case RTL_GIGA_MAC_VER_22:
3888 case RTL_GIGA_MAC_VER_23:
3889 case RTL_GIGA_MAC_VER_24:
3890 case RTL_GIGA_MAC_VER_25:
3891 case RTL_GIGA_MAC_VER_26:
3892 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003893 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003894 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003895 case RTL_GIGA_MAC_VER_32:
3896 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003897 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08003898 case RTL_GIGA_MAC_VER_35:
3899 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003900 case RTL_GIGA_MAC_VER_38:
françois romieu065c27c2011-01-03 15:08:12 +00003901 ops->down = r8168_pll_power_down;
3902 ops->up = r8168_pll_power_up;
3903 break;
3904
3905 default:
3906 ops->down = NULL;
3907 ops->up = NULL;
3908 break;
3909 }
3910}
3911
Hayes Wange542a222011-07-06 15:58:04 +08003912static void rtl_init_rxcfg(struct rtl8169_private *tp)
3913{
3914 void __iomem *ioaddr = tp->mmio_addr;
3915
3916 switch (tp->mac_version) {
3917 case RTL_GIGA_MAC_VER_01:
3918 case RTL_GIGA_MAC_VER_02:
3919 case RTL_GIGA_MAC_VER_03:
3920 case RTL_GIGA_MAC_VER_04:
3921 case RTL_GIGA_MAC_VER_05:
3922 case RTL_GIGA_MAC_VER_06:
3923 case RTL_GIGA_MAC_VER_10:
3924 case RTL_GIGA_MAC_VER_11:
3925 case RTL_GIGA_MAC_VER_12:
3926 case RTL_GIGA_MAC_VER_13:
3927 case RTL_GIGA_MAC_VER_14:
3928 case RTL_GIGA_MAC_VER_15:
3929 case RTL_GIGA_MAC_VER_16:
3930 case RTL_GIGA_MAC_VER_17:
3931 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3932 break;
3933 case RTL_GIGA_MAC_VER_18:
3934 case RTL_GIGA_MAC_VER_19:
3935 case RTL_GIGA_MAC_VER_20:
3936 case RTL_GIGA_MAC_VER_21:
3937 case RTL_GIGA_MAC_VER_22:
3938 case RTL_GIGA_MAC_VER_23:
3939 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00003940 case RTL_GIGA_MAC_VER_34:
Hayes Wange542a222011-07-06 15:58:04 +08003941 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3942 break;
3943 default:
3944 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3945 break;
3946 }
3947}
3948
Hayes Wang92fc43b2011-07-06 15:58:03 +08003949static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3950{
3951 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3952}
3953
Francois Romieud58d46b2011-05-03 16:38:29 +02003954static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3955{
françois romieu9c5028e2012-03-02 04:43:14 +00003956 void __iomem *ioaddr = tp->mmio_addr;
3957
3958 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003959 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00003960 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003961}
3962
3963static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3964{
françois romieu9c5028e2012-03-02 04:43:14 +00003965 void __iomem *ioaddr = tp->mmio_addr;
3966
3967 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003968 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00003969 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003970}
3971
3972static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3973{
3974 void __iomem *ioaddr = tp->mmio_addr;
3975
3976 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3977 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3978 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3979}
3980
3981static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3982{
3983 void __iomem *ioaddr = tp->mmio_addr;
3984
3985 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3986 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3987 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3988}
3989
3990static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3991{
3992 void __iomem *ioaddr = tp->mmio_addr;
3993
3994 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3995}
3996
3997static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3998{
3999 void __iomem *ioaddr = tp->mmio_addr;
4000
4001 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4002}
4003
4004static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4005{
4006 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02004007
4008 RTL_W8(MaxTxPacketSize, 0x3f);
4009 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4010 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01004011 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02004012}
4013
4014static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4015{
4016 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02004017
4018 RTL_W8(MaxTxPacketSize, 0x0c);
4019 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4020 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01004021 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02004022}
4023
4024static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4025{
4026 rtl_tx_performance_tweak(tp->pci_dev,
4027 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4028}
4029
4030static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4031{
4032 rtl_tx_performance_tweak(tp->pci_dev,
4033 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4034}
4035
4036static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4037{
4038 void __iomem *ioaddr = tp->mmio_addr;
4039
4040 r8168b_0_hw_jumbo_enable(tp);
4041
4042 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
4043}
4044
4045static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4046{
4047 void __iomem *ioaddr = tp->mmio_addr;
4048
4049 r8168b_0_hw_jumbo_disable(tp);
4050
4051 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4052}
4053
4054static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
4055{
4056 struct jumbo_ops *ops = &tp->jumbo_ops;
4057
4058 switch (tp->mac_version) {
4059 case RTL_GIGA_MAC_VER_11:
4060 ops->disable = r8168b_0_hw_jumbo_disable;
4061 ops->enable = r8168b_0_hw_jumbo_enable;
4062 break;
4063 case RTL_GIGA_MAC_VER_12:
4064 case RTL_GIGA_MAC_VER_17:
4065 ops->disable = r8168b_1_hw_jumbo_disable;
4066 ops->enable = r8168b_1_hw_jumbo_enable;
4067 break;
4068 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4069 case RTL_GIGA_MAC_VER_19:
4070 case RTL_GIGA_MAC_VER_20:
4071 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4072 case RTL_GIGA_MAC_VER_22:
4073 case RTL_GIGA_MAC_VER_23:
4074 case RTL_GIGA_MAC_VER_24:
4075 case RTL_GIGA_MAC_VER_25:
4076 case RTL_GIGA_MAC_VER_26:
4077 ops->disable = r8168c_hw_jumbo_disable;
4078 ops->enable = r8168c_hw_jumbo_enable;
4079 break;
4080 case RTL_GIGA_MAC_VER_27:
4081 case RTL_GIGA_MAC_VER_28:
4082 ops->disable = r8168dp_hw_jumbo_disable;
4083 ops->enable = r8168dp_hw_jumbo_enable;
4084 break;
4085 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4086 case RTL_GIGA_MAC_VER_32:
4087 case RTL_GIGA_MAC_VER_33:
4088 case RTL_GIGA_MAC_VER_34:
4089 ops->disable = r8168e_hw_jumbo_disable;
4090 ops->enable = r8168e_hw_jumbo_enable;
4091 break;
4092
4093 /*
4094 * No action needed for jumbo frames with 8169.
4095 * No jumbo for 810x at all.
4096 */
4097 default:
4098 ops->disable = NULL;
4099 ops->enable = NULL;
4100 break;
4101 }
4102}
4103
Francois Romieu6f43adc2011-04-29 15:05:51 +02004104static void rtl_hw_reset(struct rtl8169_private *tp)
4105{
4106 void __iomem *ioaddr = tp->mmio_addr;
4107 int i;
4108
4109 /* Soft reset the chip. */
4110 RTL_W8(ChipCmd, CmdReset);
4111
4112 /* Check that the chip has finished the reset. */
4113 for (i = 0; i < 100; i++) {
4114 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
4115 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004116 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004117 }
4118}
4119
Francois Romieub6ffd972011-06-17 17:00:05 +02004120static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4121{
4122 struct rtl_fw *rtl_fw;
4123 const char *name;
4124 int rc = -ENOMEM;
4125
4126 name = rtl_lookup_firmware_name(tp);
4127 if (!name)
4128 goto out_no_firmware;
4129
4130 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4131 if (!rtl_fw)
4132 goto err_warn;
4133
4134 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4135 if (rc < 0)
4136 goto err_free;
4137
Francois Romieufd112f22011-06-18 00:10:29 +02004138 rc = rtl_check_firmware(tp, rtl_fw);
4139 if (rc < 0)
4140 goto err_release_firmware;
4141
Francois Romieub6ffd972011-06-17 17:00:05 +02004142 tp->rtl_fw = rtl_fw;
4143out:
4144 return;
4145
Francois Romieufd112f22011-06-18 00:10:29 +02004146err_release_firmware:
4147 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004148err_free:
4149 kfree(rtl_fw);
4150err_warn:
4151 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4152 name, rc);
4153out_no_firmware:
4154 tp->rtl_fw = NULL;
4155 goto out;
4156}
4157
François Romieu953a12c2011-04-24 17:38:48 +02004158static void rtl_request_firmware(struct rtl8169_private *tp)
4159{
Francois Romieub6ffd972011-06-17 17:00:05 +02004160 if (IS_ERR(tp->rtl_fw))
4161 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004162}
4163
Hayes Wang92fc43b2011-07-06 15:58:03 +08004164static void rtl_rx_close(struct rtl8169_private *tp)
4165{
4166 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004167
Francois Romieu1687b562011-07-19 17:21:29 +02004168 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004169}
4170
françois romieue6de30d2011-01-03 15:08:37 +00004171static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172{
françois romieue6de30d2011-01-03 15:08:37 +00004173 void __iomem *ioaddr = tp->mmio_addr;
4174
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004176 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
Hayes Wang92fc43b2011-07-06 15:58:03 +08004178 rtl_rx_close(tp);
4179
Hayes Wang5d2e1952011-02-22 17:26:22 +08004180 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004181 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4182 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00004183 while (RTL_R8(TxPoll) & NPQ)
4184 udelay(20);
Hayes Wangc2218922011-09-06 16:55:18 +08004185 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4186 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
Hayes Wang7e18dca2012-03-30 14:33:02 +08004187 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004188 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
4189 tp->mac_version == RTL_GIGA_MAC_VER_38) {
David S. Miller8decf862011-09-22 03:23:13 -04004190 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08004191 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4192 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004193 } else {
4194 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4195 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004196 }
4197
Hayes Wang92fc43b2011-07-06 15:58:03 +08004198 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199}
4200
Francois Romieu7f796d832007-06-11 23:04:41 +02004201static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004202{
4203 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004204
4205 /* Set DMA burst size and Interframe Gap Time */
4206 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4207 (InterFrameGap << TxInterFrameGapShift));
4208}
4209
Francois Romieu07ce4062007-02-23 23:36:39 +01004210static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211{
4212 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
Francois Romieu07ce4062007-02-23 23:36:39 +01004214 tp->hw_start(dev);
4215
Francois Romieuda78dbf2012-01-26 14:18:23 +01004216 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004217}
4218
Francois Romieu7f796d832007-06-11 23:04:41 +02004219static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4220 void __iomem *ioaddr)
4221{
4222 /*
4223 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4224 * register to be written before TxDescAddrLow to work.
4225 * Switching from MMIO to I/O access fixes the issue as well.
4226 */
4227 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004228 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004229 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004230 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004231}
4232
4233static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4234{
4235 u16 cmd;
4236
4237 cmd = RTL_R16(CPlusCmd);
4238 RTL_W16(CPlusCmd, cmd);
4239 return cmd;
4240}
4241
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004242static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004243{
4244 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004245 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004246}
4247
Francois Romieu6dccd162007-02-13 23:38:05 +01004248static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4249{
Francois Romieu37441002011-06-17 22:58:54 +02004250 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004251 u32 mac_version;
4252 u32 clk;
4253 u32 val;
4254 } cfg2_info [] = {
4255 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4256 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4257 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4258 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004259 };
4260 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004261 unsigned int i;
4262 u32 clk;
4263
4264 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004265 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004266 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4267 RTL_W32(0x7c, p->val);
4268 break;
4269 }
4270 }
4271}
4272
Francois Romieue6b763e2012-03-08 09:35:39 +01004273static void rtl_set_rx_mode(struct net_device *dev)
4274{
4275 struct rtl8169_private *tp = netdev_priv(dev);
4276 void __iomem *ioaddr = tp->mmio_addr;
4277 u32 mc_filter[2]; /* Multicast hash filter */
4278 int rx_mode;
4279 u32 tmp = 0;
4280
4281 if (dev->flags & IFF_PROMISC) {
4282 /* Unconditionally log net taps. */
4283 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4284 rx_mode =
4285 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4286 AcceptAllPhys;
4287 mc_filter[1] = mc_filter[0] = 0xffffffff;
4288 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4289 (dev->flags & IFF_ALLMULTI)) {
4290 /* Too many to filter perfectly -- accept all multicasts. */
4291 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4292 mc_filter[1] = mc_filter[0] = 0xffffffff;
4293 } else {
4294 struct netdev_hw_addr *ha;
4295
4296 rx_mode = AcceptBroadcast | AcceptMyPhys;
4297 mc_filter[1] = mc_filter[0] = 0;
4298 netdev_for_each_mc_addr(ha, dev) {
4299 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4300 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4301 rx_mode |= AcceptMulticast;
4302 }
4303 }
4304
4305 if (dev->features & NETIF_F_RXALL)
4306 rx_mode |= (AcceptErr | AcceptRunt);
4307
4308 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4309
4310 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4311 u32 data = mc_filter[0];
4312
4313 mc_filter[0] = swab32(mc_filter[1]);
4314 mc_filter[1] = swab32(data);
4315 }
4316
4317 RTL_W32(MAR0 + 4, mc_filter[1]);
4318 RTL_W32(MAR0 + 0, mc_filter[0]);
4319
4320 RTL_W32(RxConfig, tmp);
4321}
4322
Francois Romieu07ce4062007-02-23 23:36:39 +01004323static void rtl_hw_start_8169(struct net_device *dev)
4324{
4325 struct rtl8169_private *tp = netdev_priv(dev);
4326 void __iomem *ioaddr = tp->mmio_addr;
4327 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004328
Francois Romieu9cb427b2006-11-02 00:10:16 +01004329 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4330 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4331 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4332 }
4333
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004335 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4336 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4337 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4338 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004339 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4340
Hayes Wange542a222011-07-06 15:58:04 +08004341 rtl_init_rxcfg(tp);
4342
françois romieuf0298f82011-01-03 15:07:42 +00004343 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004345 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
Francois Romieucecb5fd2011-04-01 10:21:07 +02004347 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4348 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4349 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4350 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004351 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
Francois Romieu7f796d832007-06-11 23:04:41 +02004353 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004354
Francois Romieucecb5fd2011-04-01 10:21:07 +02004355 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4356 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004357 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004359 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 }
4361
Francois Romieubcf0bf92006-07-26 23:14:13 +02004362 RTL_W16(CPlusCmd, tp->cp_cmd);
4363
Francois Romieu6dccd162007-02-13 23:38:05 +01004364 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4365
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 /*
4367 * Undocumented corner. Supposedly:
4368 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4369 */
4370 RTL_W16(IntrMitigate, 0x0000);
4371
Francois Romieu7f796d832007-06-11 23:04:41 +02004372 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004373
Francois Romieucecb5fd2011-04-01 10:21:07 +02004374 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4375 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4376 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4377 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004378 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4379 rtl_set_rx_tx_config_registers(tp);
4380 }
4381
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004383
4384 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4385 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386
4387 RTL_W32(RxMissed, 0);
4388
Francois Romieu07ce4062007-02-23 23:36:39 +01004389 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390
4391 /* no early-rx interrupts */
4392 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004393}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004395static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
4396{
4397 if (tp->csi_ops.write)
Francois Romieu52989f02012-07-06 13:37:00 +02004398 tp->csi_ops.write(tp, addr, value);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004399}
4400
4401static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
4402{
Francois Romieu52989f02012-07-06 13:37:00 +02004403 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004404}
4405
4406static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004407{
4408 u32 csi;
4409
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004410 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4411 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00004412}
4413
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004414static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004415{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004416 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00004417}
4418
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004419static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00004420{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004421 rtl_csi_access_enable(tp, 0x27000000);
4422}
4423
Francois Romieu52989f02012-07-06 13:37:00 +02004424static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004425{
Francois Romieu52989f02012-07-06 13:37:00 +02004426 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004427 unsigned int i;
4428
4429 RTL_W32(CSIDR, value);
4430 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4431 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4432
4433 for (i = 0; i < 100; i++) {
4434 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
4435 break;
4436 udelay(10);
4437 }
4438}
4439
Francois Romieu52989f02012-07-06 13:37:00 +02004440static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004441{
Francois Romieu52989f02012-07-06 13:37:00 +02004442 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004443 u32 value = ~0x00;
4444 unsigned int i;
4445
4446 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
4447 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4448
4449 for (i = 0; i < 100; i++) {
4450 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
4451 value = RTL_R32(CSIDR);
4452 break;
4453 }
4454 udelay(10);
4455 }
4456
4457 return value;
4458}
4459
Francois Romieu52989f02012-07-06 13:37:00 +02004460static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wang7e18dca2012-03-30 14:33:02 +08004461{
Francois Romieu52989f02012-07-06 13:37:00 +02004462 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004463 unsigned int i;
4464
4465 RTL_W32(CSIDR, value);
4466 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4467 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
4468 CSIAR_FUNC_NIC);
4469
4470 for (i = 0; i < 100; i++) {
4471 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
4472 break;
4473 udelay(10);
4474 }
4475}
4476
Francois Romieu52989f02012-07-06 13:37:00 +02004477static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wang7e18dca2012-03-30 14:33:02 +08004478{
Francois Romieu52989f02012-07-06 13:37:00 +02004479 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004480 u32 value = ~0x00;
4481 unsigned int i;
4482
4483 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
4484 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4485
4486 for (i = 0; i < 100; i++) {
4487 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
4488 value = RTL_R32(CSIDR);
4489 break;
4490 }
4491 udelay(10);
4492 }
4493
4494 return value;
4495}
4496
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004497static void __devinit rtl_init_csi_ops(struct rtl8169_private *tp)
4498{
4499 struct csi_ops *ops = &tp->csi_ops;
4500
4501 switch (tp->mac_version) {
4502 case RTL_GIGA_MAC_VER_01:
4503 case RTL_GIGA_MAC_VER_02:
4504 case RTL_GIGA_MAC_VER_03:
4505 case RTL_GIGA_MAC_VER_04:
4506 case RTL_GIGA_MAC_VER_05:
4507 case RTL_GIGA_MAC_VER_06:
4508 case RTL_GIGA_MAC_VER_10:
4509 case RTL_GIGA_MAC_VER_11:
4510 case RTL_GIGA_MAC_VER_12:
4511 case RTL_GIGA_MAC_VER_13:
4512 case RTL_GIGA_MAC_VER_14:
4513 case RTL_GIGA_MAC_VER_15:
4514 case RTL_GIGA_MAC_VER_16:
4515 case RTL_GIGA_MAC_VER_17:
4516 ops->write = NULL;
4517 ops->read = NULL;
4518 break;
4519
Hayes Wang7e18dca2012-03-30 14:33:02 +08004520 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004521 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004522 ops->write = r8402_csi_write;
4523 ops->read = r8402_csi_read;
4524 break;
4525
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004526 default:
4527 ops->write = r8169_csi_write;
4528 ops->read = r8169_csi_read;
4529 break;
4530 }
Francois Romieudacf8152008-08-02 20:44:13 +02004531}
4532
4533struct ephy_info {
4534 unsigned int offset;
4535 u16 mask;
4536 u16 bits;
4537};
4538
Francois Romieufdf6fc02012-07-06 22:40:38 +02004539static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
4540 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004541{
4542 u16 w;
4543
4544 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02004545 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4546 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02004547 e++;
4548 }
4549}
4550
Francois Romieub726e492008-06-28 12:22:59 +02004551static void rtl_disable_clock_request(struct pci_dev *pdev)
4552{
Jon Masone44daad2011-06-27 07:46:31 +00004553 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004554
4555 if (cap) {
4556 u16 ctl;
4557
4558 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4559 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4560 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4561 }
4562}
4563
françois romieue6de30d2011-01-03 15:08:37 +00004564static void rtl_enable_clock_request(struct pci_dev *pdev)
4565{
Jon Masone44daad2011-06-27 07:46:31 +00004566 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004567
4568 if (cap) {
4569 u16 ctl;
4570
4571 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4572 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4573 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4574 }
4575}
4576
Francois Romieub726e492008-06-28 12:22:59 +02004577#define R8168_CPCMD_QUIRK_MASK (\
4578 EnableBist | \
4579 Mac_dbgo_oe | \
4580 Force_half_dup | \
4581 Force_rxflow_en | \
4582 Force_txflow_en | \
4583 Cxpl_dbg_sel | \
4584 ASF | \
4585 PktCntrDisable | \
4586 Mac_dbgo_sel)
4587
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004588static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004589{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004590 void __iomem *ioaddr = tp->mmio_addr;
4591 struct pci_dev *pdev = tp->pci_dev;
4592
Francois Romieub726e492008-06-28 12:22:59 +02004593 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4594
4595 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4596
Francois Romieu2e68ae42008-06-28 12:00:55 +02004597 rtl_tx_performance_tweak(pdev,
4598 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004599}
4600
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004601static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004602{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004603 void __iomem *ioaddr = tp->mmio_addr;
4604
4605 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004606
françois romieuf0298f82011-01-03 15:07:42 +00004607 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004608
4609 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004610}
4611
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004612static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004613{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004614 void __iomem *ioaddr = tp->mmio_addr;
4615 struct pci_dev *pdev = tp->pci_dev;
4616
Francois Romieub726e492008-06-28 12:22:59 +02004617 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4618
4619 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4620
Francois Romieu219a1e92008-06-28 11:58:39 +02004621 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004622
4623 rtl_disable_clock_request(pdev);
4624
4625 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004626}
4627
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004628static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004629{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004630 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004631 { 0x01, 0, 0x0001 },
4632 { 0x02, 0x0800, 0x1000 },
4633 { 0x03, 0, 0x0042 },
4634 { 0x06, 0x0080, 0x0000 },
4635 { 0x07, 0, 0x2000 }
4636 };
4637
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004638 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004639
Francois Romieufdf6fc02012-07-06 22:40:38 +02004640 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02004641
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004642 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004643}
4644
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004645static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02004646{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004647 void __iomem *ioaddr = tp->mmio_addr;
4648 struct pci_dev *pdev = tp->pci_dev;
4649
4650 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004651
4652 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4653
4654 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4655
4656 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4657}
4658
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004659static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004660{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004661 void __iomem *ioaddr = tp->mmio_addr;
4662 struct pci_dev *pdev = tp->pci_dev;
4663
4664 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004665
4666 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4667
4668 /* Magic. */
4669 RTL_W8(DBG_REG, 0x20);
4670
françois romieuf0298f82011-01-03 15:07:42 +00004671 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004672
4673 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4674
4675 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4676}
4677
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004678static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004679{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004680 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004681 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004682 { 0x02, 0x0800, 0x1000 },
4683 { 0x03, 0, 0x0002 },
4684 { 0x06, 0x0080, 0x0000 }
4685 };
4686
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004687 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004688
4689 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4690
Francois Romieufdf6fc02012-07-06 22:40:38 +02004691 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02004692
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004693 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004694}
4695
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004696static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004697{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004698 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004699 { 0x01, 0, 0x0001 },
4700 { 0x03, 0x0400, 0x0220 }
4701 };
4702
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004703 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004704
Francois Romieufdf6fc02012-07-06 22:40:38 +02004705 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02004706
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004707 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004708}
4709
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004710static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02004711{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004712 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004713}
4714
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004715static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02004716{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004717 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004718
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004719 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004720}
4721
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004722static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02004723{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004724 void __iomem *ioaddr = tp->mmio_addr;
4725 struct pci_dev *pdev = tp->pci_dev;
4726
4727 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004728
4729 rtl_disable_clock_request(pdev);
4730
françois romieuf0298f82011-01-03 15:07:42 +00004731 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004732
4733 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4734
4735 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4736}
4737
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004738static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00004739{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004740 void __iomem *ioaddr = tp->mmio_addr;
4741 struct pci_dev *pdev = tp->pci_dev;
4742
4743 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004744
4745 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4746
4747 RTL_W8(MaxTxPacketSize, TxPacketMax);
4748
4749 rtl_disable_clock_request(pdev);
4750}
4751
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004752static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004753{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004754 void __iomem *ioaddr = tp->mmio_addr;
4755 struct pci_dev *pdev = tp->pci_dev;
françois romieue6de30d2011-01-03 15:08:37 +00004756 static const struct ephy_info e_info_8168d_4[] = {
4757 { 0x0b, ~0, 0x48 },
4758 { 0x19, 0x20, 0x50 },
4759 { 0x0c, ~0, 0x20 }
4760 };
4761 int i;
4762
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004763 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004764
4765 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4766
4767 RTL_W8(MaxTxPacketSize, TxPacketMax);
4768
4769 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4770 const struct ephy_info *e = e_info_8168d_4 + i;
4771 u16 w;
4772
Francois Romieufdf6fc02012-07-06 22:40:38 +02004773 w = rtl_ephy_read(tp, e->offset);
4774 rtl_ephy_write(tp, 0x03, (w & e->mask) | e->bits);
françois romieue6de30d2011-01-03 15:08:37 +00004775 }
4776
4777 rtl_enable_clock_request(pdev);
4778}
4779
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004780static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00004781{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004782 void __iomem *ioaddr = tp->mmio_addr;
4783 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08004784 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004785 { 0x00, 0x0200, 0x0100 },
4786 { 0x00, 0x0000, 0x0004 },
4787 { 0x06, 0x0002, 0x0001 },
4788 { 0x06, 0x0000, 0x0030 },
4789 { 0x07, 0x0000, 0x2000 },
4790 { 0x00, 0x0000, 0x0020 },
4791 { 0x03, 0x5800, 0x2000 },
4792 { 0x03, 0x0000, 0x0001 },
4793 { 0x01, 0x0800, 0x1000 },
4794 { 0x07, 0x0000, 0x4000 },
4795 { 0x1e, 0x0000, 0x2000 },
4796 { 0x19, 0xffff, 0xfe6c },
4797 { 0x0a, 0x0000, 0x0040 }
4798 };
4799
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004800 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004801
Francois Romieufdf6fc02012-07-06 22:40:38 +02004802 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004803
4804 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4805
4806 RTL_W8(MaxTxPacketSize, TxPacketMax);
4807
4808 rtl_disable_clock_request(pdev);
4809
4810 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004811 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4812 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004813
Francois Romieucecb5fd2011-04-01 10:21:07 +02004814 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004815}
4816
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004817static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08004818{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004819 void __iomem *ioaddr = tp->mmio_addr;
4820 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08004821 static const struct ephy_info e_info_8168e_2[] = {
4822 { 0x09, 0x0000, 0x0080 },
4823 { 0x19, 0x0000, 0x0224 }
4824 };
4825
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004826 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08004827
Francois Romieufdf6fc02012-07-06 22:40:38 +02004828 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08004829
4830 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4831
Francois Romieufdf6fc02012-07-06 22:40:38 +02004832 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4833 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4834 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4835 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4836 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4837 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4838 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4839 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08004840
Hayes Wang3090bd92011-09-06 16:55:15 +08004841 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004842
4843 rtl_disable_clock_request(pdev);
4844
4845 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4846 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4847
4848 /* Adjust EEE LED frequency */
4849 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4850
4851 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4852 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4853 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4854}
4855
Hayes Wang5f886e02012-03-30 14:33:03 +08004856static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08004857{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004858 void __iomem *ioaddr = tp->mmio_addr;
4859 struct pci_dev *pdev = tp->pci_dev;
Hayes Wangc2218922011-09-06 16:55:18 +08004860
Hayes Wang5f886e02012-03-30 14:33:03 +08004861 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08004862
4863 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4864
Francois Romieufdf6fc02012-07-06 22:40:38 +02004865 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4866 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4867 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4868 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4869 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4870 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4871 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4872 rtl_w1w0_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4873 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4874 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08004875
4876 RTL_W8(MaxTxPacketSize, EarlySize);
4877
4878 rtl_disable_clock_request(pdev);
4879
4880 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4881 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
Hayes Wangc2218922011-09-06 16:55:18 +08004882 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4883 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4884 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4885}
4886
Hayes Wang5f886e02012-03-30 14:33:03 +08004887static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
4888{
4889 void __iomem *ioaddr = tp->mmio_addr;
4890 static const struct ephy_info e_info_8168f_1[] = {
4891 { 0x06, 0x00c0, 0x0020 },
4892 { 0x08, 0x0001, 0x0002 },
4893 { 0x09, 0x0000, 0x0080 },
4894 { 0x19, 0x0000, 0x0224 }
4895 };
4896
4897 rtl_hw_start_8168f(tp);
4898
Francois Romieufdf6fc02012-07-06 22:40:38 +02004899 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08004900
Francois Romieufdf6fc02012-07-06 22:40:38 +02004901 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08004902
4903 /* Adjust EEE LED frequency */
4904 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4905}
4906
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004907static void rtl_hw_start_8411(struct rtl8169_private *tp)
4908{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004909 static const struct ephy_info e_info_8168f_1[] = {
4910 { 0x06, 0x00c0, 0x0020 },
4911 { 0x0f, 0xffff, 0x5200 },
4912 { 0x1e, 0x0000, 0x4000 },
4913 { 0x19, 0x0000, 0x0224 }
4914 };
4915
4916 rtl_hw_start_8168f(tp);
4917
Francois Romieufdf6fc02012-07-06 22:40:38 +02004918 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004919
Francois Romieufdf6fc02012-07-06 22:40:38 +02004920 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004921}
4922
Francois Romieu07ce4062007-02-23 23:36:39 +01004923static void rtl_hw_start_8168(struct net_device *dev)
4924{
Francois Romieu2dd99532007-06-11 23:22:52 +02004925 struct rtl8169_private *tp = netdev_priv(dev);
4926 void __iomem *ioaddr = tp->mmio_addr;
4927
4928 RTL_W8(Cfg9346, Cfg9346_Unlock);
4929
françois romieuf0298f82011-01-03 15:07:42 +00004930 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004931
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004932 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004933
Francois Romieu0e485152007-02-20 00:00:26 +01004934 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004935
4936 RTL_W16(CPlusCmd, tp->cp_cmd);
4937
Francois Romieu0e485152007-02-20 00:00:26 +01004938 RTL_W16(IntrMitigate, 0x5151);
4939
4940 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00004941 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01004942 tp->event_slow |= RxFIFOOver | PCSTimeout;
4943 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01004944 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004945
4946 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4947
Francois Romieub8363902008-06-01 12:31:57 +02004948 rtl_set_rx_mode(dev);
4949
4950 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4951 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004952
4953 RTL_R8(IntrMask);
4954
Francois Romieu219a1e92008-06-28 11:58:39 +02004955 switch (tp->mac_version) {
4956 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004957 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004958 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004959
4960 case RTL_GIGA_MAC_VER_12:
4961 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004962 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004963 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004964
4965 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004966 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004967 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004968
4969 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004970 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004971 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004972
4973 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004974 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004975 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004976
Francois Romieu197ff762008-06-28 13:16:02 +02004977 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004978 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004979 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004980
Francois Romieu6fb07052008-06-29 11:54:28 +02004981 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004982 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004983 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004984
Francois Romieuef3386f2008-06-29 12:24:30 +02004985 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004986 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004987 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004988
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004989 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004990 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004991 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004992
Francois Romieu5b538df2008-07-20 16:22:45 +02004993 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004994 case RTL_GIGA_MAC_VER_26:
4995 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004996 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004997 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004998
françois romieue6de30d2011-01-03 15:08:37 +00004999 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005000 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005001 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02005002
hayeswang4804b3b2011-03-21 01:50:29 +00005003 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005004 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005005 break;
5006
hayeswang01dc7fe2011-03-21 01:50:28 +00005007 case RTL_GIGA_MAC_VER_32:
5008 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005009 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005010 break;
5011 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005012 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005013 break;
françois romieue6de30d2011-01-03 15:08:37 +00005014
Hayes Wangc2218922011-09-06 16:55:18 +08005015 case RTL_GIGA_MAC_VER_35:
5016 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005017 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005018 break;
5019
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005020 case RTL_GIGA_MAC_VER_38:
5021 rtl_hw_start_8411(tp);
5022 break;
5023
Francois Romieu219a1e92008-06-28 11:58:39 +02005024 default:
5025 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
5026 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00005027 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005028 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005029
Francois Romieu0e485152007-02-20 00:00:26 +01005030 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5031
Francois Romieub8363902008-06-01 12:31:57 +02005032 RTL_W8(Cfg9346, Cfg9346_Lock);
5033
Francois Romieu2dd99532007-06-11 23:22:52 +02005034 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005035}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036
Francois Romieu2857ffb2008-08-02 21:08:49 +02005037#define R810X_CPCMD_QUIRK_MASK (\
5038 EnableBist | \
5039 Mac_dbgo_oe | \
5040 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00005041 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02005042 Force_txflow_en | \
5043 Cxpl_dbg_sel | \
5044 ASF | \
5045 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005046 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005047
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005048static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005049{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005050 void __iomem *ioaddr = tp->mmio_addr;
5051 struct pci_dev *pdev = tp->pci_dev;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005052 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02005053 { 0x01, 0, 0x6e65 },
5054 { 0x02, 0, 0x091f },
5055 { 0x03, 0, 0xc2f9 },
5056 { 0x06, 0, 0xafb5 },
5057 { 0x07, 0, 0x0e00 },
5058 { 0x19, 0, 0xec80 },
5059 { 0x01, 0, 0x2e65 },
5060 { 0x01, 0, 0x6e65 }
5061 };
5062 u8 cfg1;
5063
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005064 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005065
5066 RTL_W8(DBG_REG, FIX_NAK_1);
5067
5068 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5069
5070 RTL_W8(Config1,
5071 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5072 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5073
5074 cfg1 = RTL_R8(Config1);
5075 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5076 RTL_W8(Config1, cfg1 & ~LEDS0);
5077
Francois Romieufdf6fc02012-07-06 22:40:38 +02005078 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02005079}
5080
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005081static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005082{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005083 void __iomem *ioaddr = tp->mmio_addr;
5084 struct pci_dev *pdev = tp->pci_dev;
5085
5086 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005087
5088 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5089
5090 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5091 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005092}
5093
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005094static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005095{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005096 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005097
Francois Romieufdf6fc02012-07-06 22:40:38 +02005098 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005099}
5100
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005101static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005102{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005103 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005104 static const struct ephy_info e_info_8105e_1[] = {
5105 { 0x07, 0, 0x4000 },
5106 { 0x19, 0, 0x0200 },
5107 { 0x19, 0, 0x0020 },
5108 { 0x1e, 0, 0x2000 },
5109 { 0x03, 0, 0x0001 },
5110 { 0x19, 0, 0x0100 },
5111 { 0x19, 0, 0x0004 },
5112 { 0x0a, 0, 0x0020 }
5113 };
5114
Francois Romieucecb5fd2011-04-01 10:21:07 +02005115 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005116 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5117
Francois Romieucecb5fd2011-04-01 10:21:07 +02005118 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005119 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5120
5121 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08005122 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005123
Francois Romieufdf6fc02012-07-06 22:40:38 +02005124 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
Hayes Wang5a5e4442011-02-22 17:26:21 +08005125}
5126
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005127static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005128{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005129 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005130 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005131}
5132
Hayes Wang7e18dca2012-03-30 14:33:02 +08005133static void rtl_hw_start_8402(struct rtl8169_private *tp)
5134{
5135 void __iomem *ioaddr = tp->mmio_addr;
5136 static const struct ephy_info e_info_8402[] = {
5137 { 0x19, 0xffff, 0xff64 },
5138 { 0x1e, 0, 0x4000 }
5139 };
5140
5141 rtl_csi_access_enable_2(tp);
5142
5143 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5144 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5145
5146 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5147 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5148
Francois Romieufdf6fc02012-07-06 22:40:38 +02005149 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08005150
5151 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5152
Francois Romieufdf6fc02012-07-06 22:40:38 +02005153 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5154 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
5155 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5156 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5157 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5158 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5159 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005160}
5161
Hayes Wang5598bfe2012-07-02 17:23:21 +08005162static void rtl_hw_start_8106(struct rtl8169_private *tp)
5163{
5164 void __iomem *ioaddr = tp->mmio_addr;
5165
5166 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5167 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5168
5169 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5170 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
5171 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
5172}
5173
Francois Romieu07ce4062007-02-23 23:36:39 +01005174static void rtl_hw_start_8101(struct net_device *dev)
5175{
Francois Romieucdf1a602007-06-11 23:29:50 +02005176 struct rtl8169_private *tp = netdev_priv(dev);
5177 void __iomem *ioaddr = tp->mmio_addr;
5178 struct pci_dev *pdev = tp->pci_dev;
5179
Francois Romieuda78dbf2012-01-26 14:18:23 +01005180 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5181 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00005182
Francois Romieucecb5fd2011-04-01 10:21:07 +02005183 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5184 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00005185 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02005186
5187 if (cap) {
5188 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
5189 PCI_EXP_DEVCTL_NOSNOOP_EN);
5190 }
Francois Romieucdf1a602007-06-11 23:29:50 +02005191 }
5192
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005193 RTL_W8(Cfg9346, Cfg9346_Unlock);
5194
Francois Romieu2857ffb2008-08-02 21:08:49 +02005195 switch (tp->mac_version) {
5196 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005197 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005198 break;
5199
5200 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005201 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005202 break;
5203
5204 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005205 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005206 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005207
5208 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005209 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005210 break;
5211 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005212 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005213 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005214
5215 case RTL_GIGA_MAC_VER_37:
5216 rtl_hw_start_8402(tp);
5217 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08005218
5219 case RTL_GIGA_MAC_VER_39:
5220 rtl_hw_start_8106(tp);
5221 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02005222 }
5223
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005224 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02005225
françois romieuf0298f82011-01-03 15:07:42 +00005226 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02005227
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005228 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02005229
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005230 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02005231 RTL_W16(CPlusCmd, tp->cp_cmd);
5232
5233 RTL_W16(IntrMitigate, 0x0000);
5234
5235 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5236
5237 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5238 rtl_set_rx_tx_config_registers(tp);
5239
Francois Romieucdf1a602007-06-11 23:29:50 +02005240 RTL_R8(IntrMask);
5241
Francois Romieucdf1a602007-06-11 23:29:50 +02005242 rtl_set_rx_mode(dev);
5243
5244 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245}
5246
5247static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5248{
Francois Romieud58d46b2011-05-03 16:38:29 +02005249 struct rtl8169_private *tp = netdev_priv(dev);
5250
5251 if (new_mtu < ETH_ZLEN ||
5252 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 return -EINVAL;
5254
Francois Romieud58d46b2011-05-03 16:38:29 +02005255 if (new_mtu > ETH_DATA_LEN)
5256 rtl_hw_jumbo_enable(tp);
5257 else
5258 rtl_hw_jumbo_disable(tp);
5259
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005261 netdev_update_features(dev);
5262
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005263 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264}
5265
5266static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5267{
Al Viro95e09182007-12-22 18:55:39 +00005268 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5270}
5271
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005272static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5273 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005275 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005276 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005277
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005278 kfree(*data_buff);
5279 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 rtl8169_make_unusable_by_asic(desc);
5281}
5282
5283static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5284{
5285 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5286
5287 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5288}
5289
5290static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5291 u32 rx_buf_sz)
5292{
5293 desc->addr = cpu_to_le64(mapping);
5294 wmb();
5295 rtl8169_mark_to_asic(desc, rx_buf_sz);
5296}
5297
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005298static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005300 return (void *)ALIGN((long)data, 16);
5301}
5302
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005303static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5304 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005305{
5306 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005308 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005309 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005310 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005312 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5313 if (!data)
5314 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005315
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005316 if (rtl8169_align(data) != data) {
5317 kfree(data);
5318 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5319 if (!data)
5320 return NULL;
5321 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005322
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005323 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005324 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005325 if (unlikely(dma_mapping_error(d, mapping))) {
5326 if (net_ratelimit())
5327 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005328 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330
5331 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005332 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005333
5334err_out:
5335 kfree(data);
5336 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337}
5338
5339static void rtl8169_rx_clear(struct rtl8169_private *tp)
5340{
Francois Romieu07d3f512007-02-21 22:40:46 +01005341 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342
5343 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005344 if (tp->Rx_databuff[i]) {
5345 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 tp->RxDescArray + i);
5347 }
5348 }
5349}
5350
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005351static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005353 desc->opts1 |= cpu_to_le32(RingEnd);
5354}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005355
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005356static int rtl8169_rx_fill(struct rtl8169_private *tp)
5357{
5358 unsigned int i;
5359
5360 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005361 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005362
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005363 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005365
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005366 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005367 if (!data) {
5368 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005369 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005370 }
5371 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005374 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5375 return 0;
5376
5377err_out:
5378 rtl8169_rx_clear(tp);
5379 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380}
5381
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382static int rtl8169_init_ring(struct net_device *dev)
5383{
5384 struct rtl8169_private *tp = netdev_priv(dev);
5385
5386 rtl8169_init_ring_indexes(tp);
5387
5388 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005389 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005391 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392}
5393
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005394static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 struct TxDesc *desc)
5396{
5397 unsigned int len = tx_skb->len;
5398
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005399 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5400
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 desc->opts1 = 0x00;
5402 desc->opts2 = 0x00;
5403 desc->addr = 0x00;
5404 tx_skb->len = 0;
5405}
5406
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005407static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5408 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409{
5410 unsigned int i;
5411
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005412 for (i = 0; i < n; i++) {
5413 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 struct ring_info *tx_skb = tp->tx_skb + entry;
5415 unsigned int len = tx_skb->len;
5416
5417 if (len) {
5418 struct sk_buff *skb = tx_skb->skb;
5419
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005420 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 tp->TxDescArray + entry);
5422 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005423 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 dev_kfree_skb(skb);
5425 tx_skb->skb = NULL;
5426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 }
5428 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005429}
5430
5431static void rtl8169_tx_clear(struct rtl8169_private *tp)
5432{
5433 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 tp->cur_tx = tp->dirty_tx = 0;
Igor Maravic036dafa2012-03-05 00:01:25 +01005435 netdev_reset_queue(tp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436}
5437
Francois Romieu4422bcd2012-01-26 11:23:32 +01005438static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439{
David Howellsc4028952006-11-22 14:57:56 +00005440 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005441 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442
Francois Romieuda78dbf2012-01-26 14:18:23 +01005443 napi_disable(&tp->napi);
5444 netif_stop_queue(dev);
5445 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
françois romieuc7c2c392011-12-04 20:30:52 +00005447 rtl8169_hw_reset(tp);
5448
Francois Romieu56de4142011-03-15 17:29:31 +01005449 for (i = 0; i < NUM_RX_DESC; i++)
5450 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5451
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005453 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454
Francois Romieuda78dbf2012-01-26 14:18:23 +01005455 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01005456 rtl_hw_start(dev);
5457 netif_wake_queue(dev);
5458 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459}
5460
5461static void rtl8169_tx_timeout(struct net_device *dev)
5462{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005463 struct rtl8169_private *tp = netdev_priv(dev);
5464
5465 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466}
5467
5468static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005469 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470{
5471 struct skb_shared_info *info = skb_shinfo(skb);
5472 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005473 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005474 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475
5476 entry = tp->cur_tx;
5477 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005478 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 dma_addr_t mapping;
5480 u32 status, len;
5481 void *addr;
5482
5483 entry = (entry + 1) % NUM_TX_DESC;
5484
5485 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005486 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005487 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005488 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005489 if (unlikely(dma_mapping_error(d, mapping))) {
5490 if (net_ratelimit())
5491 netif_err(tp, drv, tp->dev,
5492 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005493 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495
Francois Romieucecb5fd2011-04-01 10:21:07 +02005496 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005497 status = opts[0] | len |
5498 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499
5500 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005501 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 txd->addr = cpu_to_le64(mapping);
5503
5504 tp->tx_skb[entry].len = len;
5505 }
5506
5507 if (cur_frag) {
5508 tp->tx_skb[entry].skb = skb;
5509 txd->opts1 |= cpu_to_le32(LastFrag);
5510 }
5511
5512 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005513
5514err_out:
5515 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5516 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517}
5518
Francois Romieu2b7b4312011-04-18 22:53:24 -07005519static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5520 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005522 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005523 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005524 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525
Francois Romieu2b7b4312011-04-18 22:53:24 -07005526 if (mss) {
5527 opts[0] |= TD_LSO;
5528 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5529 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005530 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531
5532 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005533 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005535 opts[offset] |= info->checksum.udp;
5536 else
5537 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539}
5540
Stephen Hemminger613573252009-08-31 19:50:58 +00005541static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5542 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543{
5544 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005545 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 struct TxDesc *txd = tp->TxDescArray + entry;
5547 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005548 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 dma_addr_t mapping;
5550 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005551 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005552 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005553
Julien Ducourthial477206a2012-05-09 00:00:06 +02005554 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005555 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005556 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 }
5558
5559 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005560 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005562 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005563 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005564 if (unlikely(dma_mapping_error(d, mapping))) {
5565 if (net_ratelimit())
5566 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005567 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569
5570 tp->tx_skb[entry].len = len;
5571 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572
Francois Romieu2b7b4312011-04-18 22:53:24 -07005573 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5574 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005575
Francois Romieu2b7b4312011-04-18 22:53:24 -07005576 rtl8169_tso_csum(tp, skb, opts);
5577
5578 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005579 if (frags < 0)
5580 goto err_dma_1;
5581 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005582 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005583 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005584 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005585 tp->tx_skb[entry].skb = skb;
5586 }
5587
Francois Romieu2b7b4312011-04-18 22:53:24 -07005588 txd->opts2 = cpu_to_le32(opts[1]);
5589
Igor Maravic036dafa2012-03-05 00:01:25 +01005590 netdev_sent_queue(dev, skb->len);
5591
Richard Cochran5047fb52012-03-10 07:29:42 +00005592 skb_tx_timestamp(skb);
5593
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 wmb();
5595
Francois Romieucecb5fd2011-04-01 10:21:07 +02005596 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005597 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 txd->opts1 = cpu_to_le32(status);
5599
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 tp->cur_tx += frags + 1;
5601
David Dillow4c020a92010-03-03 16:33:10 +00005602 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603
Francois Romieucecb5fd2011-04-01 10:21:07 +02005604 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605
Francois Romieuda78dbf2012-01-26 14:18:23 +01005606 mmiowb();
5607
Julien Ducourthial477206a2012-05-09 00:00:06 +02005608 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01005609 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5610 * not miss a ring update when it notices a stopped queue.
5611 */
5612 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01005614 /* Sync with rtl_tx:
5615 * - publish queue status and cur_tx ring index (write barrier)
5616 * - refresh dirty_tx ring index (read barrier).
5617 * May the current thread have a pessimistic view of the ring
5618 * status and forget to wake up queue, a racing rtl_tx thread
5619 * can't.
5620 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005621 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02005622 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 netif_wake_queue(dev);
5624 }
5625
Stephen Hemminger613573252009-08-31 19:50:58 +00005626 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005628err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005629 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005630err_dma_0:
5631 dev_kfree_skb(skb);
5632 dev->stats.tx_dropped++;
5633 return NETDEV_TX_OK;
5634
5635err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005637 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005638 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639}
5640
5641static void rtl8169_pcierr_interrupt(struct net_device *dev)
5642{
5643 struct rtl8169_private *tp = netdev_priv(dev);
5644 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 u16 pci_status, pci_cmd;
5646
5647 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5648 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5649
Joe Perchesbf82c182010-02-09 11:49:50 +00005650 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5651 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652
5653 /*
5654 * The recovery sequence below admits a very elaborated explanation:
5655 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005656 * - I did not see what else could be done;
5657 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 *
5659 * Feel free to adjust to your needs.
5660 */
Francois Romieua27993f2006-12-18 00:04:19 +01005661 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005662 pci_cmd &= ~PCI_COMMAND_PARITY;
5663 else
5664 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5665
5666 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667
5668 pci_write_config_word(pdev, PCI_STATUS,
5669 pci_status & (PCI_STATUS_DETECTED_PARITY |
5670 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5671 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5672
5673 /* The infamous DAC f*ckup only happens at boot time */
5674 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005675 void __iomem *ioaddr = tp->mmio_addr;
5676
Joe Perchesbf82c182010-02-09 11:49:50 +00005677 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 tp->cp_cmd &= ~PCIDAC;
5679 RTL_W16(CPlusCmd, tp->cp_cmd);
5680 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 }
5682
françois romieue6de30d2011-01-03 15:08:37 +00005683 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005684
Francois Romieu98ddf982012-01-31 10:47:34 +01005685 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686}
5687
Igor Maravic036dafa2012-03-05 00:01:25 +01005688struct rtl_txc {
5689 int packets;
5690 int bytes;
5691};
5692
Francois Romieuda78dbf2012-01-26 14:18:23 +01005693static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694{
Igor Maravic036dafa2012-03-05 00:01:25 +01005695 struct rtl8169_stats *tx_stats = &tp->tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 unsigned int dirty_tx, tx_left;
Igor Maravic036dafa2012-03-05 00:01:25 +01005697 struct rtl_txc txc = { 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 dirty_tx = tp->dirty_tx;
5700 smp_rmb();
5701 tx_left = tp->cur_tx - dirty_tx;
5702
5703 while (tx_left > 0) {
5704 unsigned int entry = dirty_tx % NUM_TX_DESC;
5705 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 u32 status;
5707
5708 rmb();
5709 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5710 if (status & DescOwn)
5711 break;
5712
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005713 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5714 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 if (status & LastFrag) {
Igor Maravic036dafa2012-03-05 00:01:25 +01005716 struct sk_buff *skb = tx_skb->skb;
5717
5718 txc.packets++;
5719 txc.bytes += skb->len;
5720 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 tx_skb->skb = NULL;
5722 }
5723 dirty_tx++;
5724 tx_left--;
5725 }
5726
Igor Maravic036dafa2012-03-05 00:01:25 +01005727 u64_stats_update_begin(&tx_stats->syncp);
5728 tx_stats->packets += txc.packets;
5729 tx_stats->bytes += txc.bytes;
5730 u64_stats_update_end(&tx_stats->syncp);
5731
5732 netdev_completed_queue(dev, txc.packets, txc.bytes);
5733
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 if (tp->dirty_tx != dirty_tx) {
5735 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005736 /* Sync with rtl8169_start_xmit:
5737 * - publish dirty_tx ring index (write barrier)
5738 * - refresh cur_tx ring index and queue status (read barrier)
5739 * May the current thread miss the stopped queue condition,
5740 * a racing xmit thread can only have a right view of the
5741 * ring status.
5742 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005743 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02005745 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 netif_wake_queue(dev);
5747 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005748 /*
5749 * 8168 hack: TxPoll requests are lost when the Tx packets are
5750 * too close. Let's kick an extra TxPoll request when a burst
5751 * of start_xmit activity is detected (if it is not detected,
5752 * it is slow enough). -- FR
5753 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005754 if (tp->cur_tx != dirty_tx) {
5755 void __iomem *ioaddr = tp->mmio_addr;
5756
Francois Romieud78ae2d2007-08-26 20:08:19 +02005757 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 }
5760}
5761
Francois Romieu126fa4b2005-05-12 20:09:17 -04005762static inline int rtl8169_fragmented_frame(u32 status)
5763{
5764 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5765}
5766
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005767static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 u32 status = opts1 & RxProtoMask;
5770
5771 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005772 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 skb->ip_summed = CHECKSUM_UNNECESSARY;
5774 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005775 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776}
5777
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005778static struct sk_buff *rtl8169_try_rx_copy(void *data,
5779 struct rtl8169_private *tp,
5780 int pkt_size,
5781 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005783 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005784 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005786 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005787 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005788 prefetch(data);
5789 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5790 if (skb)
5791 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005792 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5793
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005794 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795}
5796
Francois Romieuda78dbf2012-01-26 14:18:23 +01005797static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798{
5799 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005800 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 cur_rx = tp->cur_rx;
5803 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005804 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005806 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005808 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 u32 status;
5810
5811 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005812 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813
5814 if (status & DescOwn)
5815 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005816 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005817 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5818 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005819 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005821 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005823 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005824 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005825 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005826 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005827 }
Ben Greear6bbe0212012-02-10 15:04:33 +00005828 if ((status & (RxRUNT | RxCRC)) &&
5829 !(status & (RxRWT | RxFOVF)) &&
5830 (dev->features & NETIF_F_RXALL))
5831 goto process_pkt;
5832
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005833 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005835 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00005836 dma_addr_t addr;
5837 int pkt_size;
5838
5839process_pkt:
5840 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00005841 if (likely(!(dev->features & NETIF_F_RXFCS)))
5842 pkt_size = (status & 0x00003fff) - 4;
5843 else
5844 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845
Francois Romieu126fa4b2005-05-12 20:09:17 -04005846 /*
5847 * The driver does not support incoming fragmented
5848 * frames. They are seen as a symptom of over-mtu
5849 * sized frames.
5850 */
5851 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005852 dev->stats.rx_dropped++;
5853 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005854 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005855 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005856 }
5857
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005858 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5859 tp, pkt_size, addr);
5860 rtl8169_mark_to_asic(desc, rx_buf_sz);
5861 if (!skb) {
5862 dev->stats.rx_dropped++;
5863 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 }
5865
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005866 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 skb_put(skb, pkt_size);
5868 skb->protocol = eth_type_trans(skb, dev);
5869
Francois Romieu7a8fc772011-03-01 17:18:33 +01005870 rtl8169_rx_vlan_tag(desc, skb);
5871
Francois Romieu56de4142011-03-15 17:29:31 +01005872 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873
Junchang Wang8027aa22012-03-04 23:30:32 +01005874 u64_stats_update_begin(&tp->rx_stats.syncp);
5875 tp->rx_stats.packets++;
5876 tp->rx_stats.bytes += pkt_size;
5877 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005879
5880 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005881 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005882 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5883 desc->opts2 = 0;
5884 cur_rx++;
5885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 }
5887
5888 count = cur_rx - tp->cur_rx;
5889 tp->cur_rx = cur_rx;
5890
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005891 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892
5893 return count;
5894}
5895
Francois Romieu07d3f512007-02-21 22:40:46 +01005896static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897{
Francois Romieu07d3f512007-02-21 22:40:46 +01005898 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005901 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005903 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005904 if (status && status != 0xffff) {
5905 status &= RTL_EVENT_NAPI | tp->event_slow;
5906 if (status) {
5907 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00005908
Francois Romieuda78dbf2012-01-26 14:18:23 +01005909 rtl_irq_disable(tp);
5910 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 return IRQ_RETVAL(handled);
5914}
5915
Francois Romieuda78dbf2012-01-26 14:18:23 +01005916/*
5917 * Workqueue context.
5918 */
5919static void rtl_slow_event_work(struct rtl8169_private *tp)
5920{
5921 struct net_device *dev = tp->dev;
5922 u16 status;
5923
5924 status = rtl_get_events(tp) & tp->event_slow;
5925 rtl_ack_events(tp, status);
5926
5927 if (unlikely(status & RxFIFOOver)) {
5928 switch (tp->mac_version) {
5929 /* Work around for rx fifo overflow */
5930 case RTL_GIGA_MAC_VER_11:
5931 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01005932 /* XXX - Hack alert. See rtl_task(). */
5933 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005934 default:
5935 break;
5936 }
5937 }
5938
5939 if (unlikely(status & SYSErr))
5940 rtl8169_pcierr_interrupt(dev);
5941
5942 if (status & LinkChg)
5943 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
5944
françois romieu7dbb4912012-06-09 10:53:16 +00005945 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005946}
5947
Francois Romieu4422bcd2012-01-26 11:23:32 +01005948static void rtl_task(struct work_struct *work)
5949{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005950 static const struct {
5951 int bitnr;
5952 void (*action)(struct rtl8169_private *);
5953 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01005954 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005955 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
5956 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
5957 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
5958 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01005959 struct rtl8169_private *tp =
5960 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005961 struct net_device *dev = tp->dev;
5962 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01005963
Francois Romieuda78dbf2012-01-26 14:18:23 +01005964 rtl_lock_work(tp);
5965
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005966 if (!netif_running(dev) ||
5967 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01005968 goto out_unlock;
5969
5970 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
5971 bool pending;
5972
Francois Romieuda78dbf2012-01-26 14:18:23 +01005973 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005974 if (pending)
5975 rtl_work[i].action(tp);
5976 }
5977
5978out_unlock:
5979 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01005980}
5981
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005982static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005984 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5985 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005986 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
5987 int work_done= 0;
5988 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989
Francois Romieuda78dbf2012-01-26 14:18:23 +01005990 status = rtl_get_events(tp);
5991 rtl_ack_events(tp, status & ~tp->event_slow);
5992
5993 if (status & RTL_EVENT_NAPI_RX)
5994 work_done = rtl_rx(dev, tp, (u32) budget);
5995
5996 if (status & RTL_EVENT_NAPI_TX)
5997 rtl_tx(dev, tp);
5998
5999 if (status & tp->event_slow) {
6000 enable_mask &= ~tp->event_slow;
6001
6002 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
6003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006005 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006006 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00006007
Francois Romieuda78dbf2012-01-26 14:18:23 +01006008 rtl_irq_enable(tp, enable_mask);
6009 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 }
6011
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006012 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014
Francois Romieu523a6092008-09-10 22:28:56 +02006015static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
6016{
6017 struct rtl8169_private *tp = netdev_priv(dev);
6018
6019 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6020 return;
6021
6022 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
6023 RTL_W32(RxMissed, 0);
6024}
6025
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026static void rtl8169_down(struct net_device *dev)
6027{
6028 struct rtl8169_private *tp = netdev_priv(dev);
6029 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030
Francois Romieu4876cc12011-03-11 21:07:11 +01006031 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01006033 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006034 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035
Hayes Wang92fc43b2011-07-06 15:58:03 +08006036 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006037 /*
6038 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01006039 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6040 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006041 */
Francois Romieu523a6092008-09-10 22:28:56 +02006042 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006045 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 rtl8169_tx_clear(tp);
6048
6049 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00006050
6051 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052}
6053
6054static int rtl8169_close(struct net_device *dev)
6055{
6056 struct rtl8169_private *tp = netdev_priv(dev);
6057 struct pci_dev *pdev = tp->pci_dev;
6058
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006059 pm_runtime_get_sync(&pdev->dev);
6060
Francois Romieucecb5fd2011-04-01 10:21:07 +02006061 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08006062 rtl8169_update_counters(dev);
6063
Francois Romieuda78dbf2012-01-26 14:18:23 +01006064 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006065 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006066
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006068 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006070 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00006072 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6073 tp->RxPhyAddr);
6074 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6075 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 tp->TxDescArray = NULL;
6077 tp->RxDescArray = NULL;
6078
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006079 pm_runtime_put_sync(&pdev->dev);
6080
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 return 0;
6082}
6083
Francois Romieudc1c00c2012-03-08 10:06:18 +01006084#ifdef CONFIG_NET_POLL_CONTROLLER
6085static void rtl8169_netpoll(struct net_device *dev)
6086{
6087 struct rtl8169_private *tp = netdev_priv(dev);
6088
6089 rtl8169_interrupt(tp->pci_dev->irq, dev);
6090}
6091#endif
6092
Francois Romieudf43ac72012-03-08 09:48:40 +01006093static int rtl_open(struct net_device *dev)
6094{
6095 struct rtl8169_private *tp = netdev_priv(dev);
6096 void __iomem *ioaddr = tp->mmio_addr;
6097 struct pci_dev *pdev = tp->pci_dev;
6098 int retval = -ENOMEM;
6099
6100 pm_runtime_get_sync(&pdev->dev);
6101
6102 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02006103 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01006104 * dma_alloc_coherent provides more.
6105 */
6106 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6107 &tp->TxPhyAddr, GFP_KERNEL);
6108 if (!tp->TxDescArray)
6109 goto err_pm_runtime_put;
6110
6111 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6112 &tp->RxPhyAddr, GFP_KERNEL);
6113 if (!tp->RxDescArray)
6114 goto err_free_tx_0;
6115
6116 retval = rtl8169_init_ring(dev);
6117 if (retval < 0)
6118 goto err_free_rx_1;
6119
6120 INIT_WORK(&tp->wk.work, rtl_task);
6121
6122 smp_mb();
6123
6124 rtl_request_firmware(tp);
6125
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006126 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01006127 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
6128 dev->name, dev);
6129 if (retval < 0)
6130 goto err_release_fw_2;
6131
6132 rtl_lock_work(tp);
6133
6134 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6135
6136 napi_enable(&tp->napi);
6137
6138 rtl8169_init_phy(dev, tp);
6139
6140 __rtl8169_set_features(dev, dev->features);
6141
6142 rtl_pll_power_up(tp);
6143
6144 rtl_hw_start(dev);
6145
6146 netif_start_queue(dev);
6147
6148 rtl_unlock_work(tp);
6149
6150 tp->saved_wolopts = 0;
6151 pm_runtime_put_noidle(&pdev->dev);
6152
6153 rtl8169_check_link_status(dev, tp, ioaddr);
6154out:
6155 return retval;
6156
6157err_release_fw_2:
6158 rtl_release_firmware(tp);
6159 rtl8169_rx_clear(tp);
6160err_free_rx_1:
6161 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6162 tp->RxPhyAddr);
6163 tp->RxDescArray = NULL;
6164err_free_tx_0:
6165 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6166 tp->TxPhyAddr);
6167 tp->TxDescArray = NULL;
6168err_pm_runtime_put:
6169 pm_runtime_put_noidle(&pdev->dev);
6170 goto out;
6171}
6172
Junchang Wang8027aa22012-03-04 23:30:32 +01006173static struct rtnl_link_stats64 *
6174rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175{
6176 struct rtl8169_private *tp = netdev_priv(dev);
6177 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01006178 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179
Francois Romieuda78dbf2012-01-26 14:18:23 +01006180 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02006181 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02006182
Junchang Wang8027aa22012-03-04 23:30:32 +01006183 do {
6184 start = u64_stats_fetch_begin_bh(&tp->rx_stats.syncp);
6185 stats->rx_packets = tp->rx_stats.packets;
6186 stats->rx_bytes = tp->rx_stats.bytes;
6187 } while (u64_stats_fetch_retry_bh(&tp->rx_stats.syncp, start));
6188
6189
6190 do {
6191 start = u64_stats_fetch_begin_bh(&tp->tx_stats.syncp);
6192 stats->tx_packets = tp->tx_stats.packets;
6193 stats->tx_bytes = tp->tx_stats.bytes;
6194 } while (u64_stats_fetch_retry_bh(&tp->tx_stats.syncp, start));
6195
6196 stats->rx_dropped = dev->stats.rx_dropped;
6197 stats->tx_dropped = dev->stats.tx_dropped;
6198 stats->rx_length_errors = dev->stats.rx_length_errors;
6199 stats->rx_errors = dev->stats.rx_errors;
6200 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6201 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6202 stats->rx_missed_errors = dev->stats.rx_missed_errors;
6203
6204 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205}
6206
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006207static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006208{
françois romieu065c27c2011-01-03 15:08:12 +00006209 struct rtl8169_private *tp = netdev_priv(dev);
6210
Francois Romieu5d06a992006-02-23 00:47:58 +01006211 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006212 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006213
6214 netif_device_detach(dev);
6215 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006216
6217 rtl_lock_work(tp);
6218 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006219 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006220 rtl_unlock_work(tp);
6221
6222 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006223}
Francois Romieu5d06a992006-02-23 00:47:58 +01006224
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006225#ifdef CONFIG_PM
6226
6227static int rtl8169_suspend(struct device *device)
6228{
6229 struct pci_dev *pdev = to_pci_dev(device);
6230 struct net_device *dev = pci_get_drvdata(pdev);
6231
6232 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02006233
Francois Romieu5d06a992006-02-23 00:47:58 +01006234 return 0;
6235}
6236
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006237static void __rtl8169_resume(struct net_device *dev)
6238{
françois romieu065c27c2011-01-03 15:08:12 +00006239 struct rtl8169_private *tp = netdev_priv(dev);
6240
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006241 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006242
6243 rtl_pll_power_up(tp);
6244
Artem Savkovcff4c162012-04-03 10:29:11 +00006245 rtl_lock_work(tp);
6246 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006247 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00006248 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006249
Francois Romieu98ddf982012-01-31 10:47:34 +01006250 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006251}
6252
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006253static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006254{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006255 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01006256 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006257 struct rtl8169_private *tp = netdev_priv(dev);
6258
6259 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01006260
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006261 if (netif_running(dev))
6262 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006263
Francois Romieu5d06a992006-02-23 00:47:58 +01006264 return 0;
6265}
6266
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006267static int rtl8169_runtime_suspend(struct device *device)
6268{
6269 struct pci_dev *pdev = to_pci_dev(device);
6270 struct net_device *dev = pci_get_drvdata(pdev);
6271 struct rtl8169_private *tp = netdev_priv(dev);
6272
6273 if (!tp->TxDescArray)
6274 return 0;
6275
Francois Romieuda78dbf2012-01-26 14:18:23 +01006276 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006277 tp->saved_wolopts = __rtl8169_get_wol(tp);
6278 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006279 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006280
6281 rtl8169_net_suspend(dev);
6282
6283 return 0;
6284}
6285
6286static int rtl8169_runtime_resume(struct device *device)
6287{
6288 struct pci_dev *pdev = to_pci_dev(device);
6289 struct net_device *dev = pci_get_drvdata(pdev);
6290 struct rtl8169_private *tp = netdev_priv(dev);
6291
6292 if (!tp->TxDescArray)
6293 return 0;
6294
Francois Romieuda78dbf2012-01-26 14:18:23 +01006295 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006296 __rtl8169_set_wol(tp, tp->saved_wolopts);
6297 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006298 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006299
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006300 rtl8169_init_phy(dev, tp);
6301
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006302 __rtl8169_resume(dev);
6303
6304 return 0;
6305}
6306
6307static int rtl8169_runtime_idle(struct device *device)
6308{
6309 struct pci_dev *pdev = to_pci_dev(device);
6310 struct net_device *dev = pci_get_drvdata(pdev);
6311 struct rtl8169_private *tp = netdev_priv(dev);
6312
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00006313 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006314}
6315
Alexey Dobriyan47145212009-12-14 18:00:08 -08006316static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006317 .suspend = rtl8169_suspend,
6318 .resume = rtl8169_resume,
6319 .freeze = rtl8169_suspend,
6320 .thaw = rtl8169_resume,
6321 .poweroff = rtl8169_suspend,
6322 .restore = rtl8169_resume,
6323 .runtime_suspend = rtl8169_runtime_suspend,
6324 .runtime_resume = rtl8169_runtime_resume,
6325 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006326};
6327
6328#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6329
6330#else /* !CONFIG_PM */
6331
6332#define RTL8169_PM_OPS NULL
6333
6334#endif /* !CONFIG_PM */
6335
David S. Miller1805b2f2011-10-24 18:18:09 -04006336static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6337{
6338 void __iomem *ioaddr = tp->mmio_addr;
6339
6340 /* WoL fails with 8168b when the receiver is disabled. */
6341 switch (tp->mac_version) {
6342 case RTL_GIGA_MAC_VER_11:
6343 case RTL_GIGA_MAC_VER_12:
6344 case RTL_GIGA_MAC_VER_17:
6345 pci_clear_master(tp->pci_dev);
6346
6347 RTL_W8(ChipCmd, CmdRxEnb);
6348 /* PCI commit */
6349 RTL_R8(ChipCmd);
6350 break;
6351 default:
6352 break;
6353 }
6354}
6355
Francois Romieu1765f952008-09-13 17:21:40 +02006356static void rtl_shutdown(struct pci_dev *pdev)
6357{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006358 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006359 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00006360 struct device *d = &pdev->dev;
6361
6362 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02006363
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006364 rtl8169_net_suspend(dev);
6365
Francois Romieucecb5fd2011-04-01 10:21:07 +02006366 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006367 rtl_rar_set(tp, dev->perm_addr);
6368
Hayes Wang92fc43b2011-07-06 15:58:03 +08006369 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006370
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006371 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006372 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6373 rtl_wol_suspend_quirk(tp);
6374 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006375 }
6376
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006377 pci_wake_from_d3(pdev, true);
6378 pci_set_power_state(pdev, PCI_D3hot);
6379 }
françois romieu2a15cd22012-03-06 01:14:12 +00006380
6381 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006382}
Francois Romieu5d06a992006-02-23 00:47:58 +01006383
Francois Romieue27566e2012-03-08 09:54:01 +01006384static void __devexit rtl_remove_one(struct pci_dev *pdev)
6385{
6386 struct net_device *dev = pci_get_drvdata(pdev);
6387 struct rtl8169_private *tp = netdev_priv(dev);
6388
6389 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6390 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6391 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6392 rtl8168_driver_stop(tp);
6393 }
6394
6395 cancel_work_sync(&tp->wk.work);
6396
Devendra Nagaad1be8d2012-05-31 01:51:20 +00006397 netif_napi_del(&tp->napi);
6398
Francois Romieue27566e2012-03-08 09:54:01 +01006399 unregister_netdev(dev);
6400
6401 rtl_release_firmware(tp);
6402
6403 if (pci_dev_run_wake(pdev))
6404 pm_runtime_get_noresume(&pdev->dev);
6405
6406 /* restore original MAC address */
6407 rtl_rar_set(tp, dev->perm_addr);
6408
6409 rtl_disable_msi(pdev, tp);
6410 rtl8169_release_board(pdev, dev, tp->mmio_addr);
6411 pci_set_drvdata(pdev, NULL);
6412}
6413
Francois Romieufa9c3852012-03-08 10:01:50 +01006414static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01006415 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01006416 .ndo_stop = rtl8169_close,
6417 .ndo_get_stats64 = rtl8169_get_stats64,
6418 .ndo_start_xmit = rtl8169_start_xmit,
6419 .ndo_tx_timeout = rtl8169_tx_timeout,
6420 .ndo_validate_addr = eth_validate_addr,
6421 .ndo_change_mtu = rtl8169_change_mtu,
6422 .ndo_fix_features = rtl8169_fix_features,
6423 .ndo_set_features = rtl8169_set_features,
6424 .ndo_set_mac_address = rtl_set_mac_address,
6425 .ndo_do_ioctl = rtl8169_ioctl,
6426 .ndo_set_rx_mode = rtl_set_rx_mode,
6427#ifdef CONFIG_NET_POLL_CONTROLLER
6428 .ndo_poll_controller = rtl8169_netpoll,
6429#endif
6430
6431};
6432
Francois Romieu31fa8b12012-03-08 10:09:40 +01006433static const struct rtl_cfg_info {
6434 void (*hw_start)(struct net_device *);
6435 unsigned int region;
6436 unsigned int align;
6437 u16 event_slow;
6438 unsigned features;
6439 u8 default_ver;
6440} rtl_cfg_infos [] = {
6441 [RTL_CFG_0] = {
6442 .hw_start = rtl_hw_start_8169,
6443 .region = 1,
6444 .align = 0,
6445 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
6446 .features = RTL_FEATURE_GMII,
6447 .default_ver = RTL_GIGA_MAC_VER_01,
6448 },
6449 [RTL_CFG_1] = {
6450 .hw_start = rtl_hw_start_8168,
6451 .region = 2,
6452 .align = 8,
6453 .event_slow = SYSErr | LinkChg | RxOverflow,
6454 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
6455 .default_ver = RTL_GIGA_MAC_VER_11,
6456 },
6457 [RTL_CFG_2] = {
6458 .hw_start = rtl_hw_start_8101,
6459 .region = 2,
6460 .align = 8,
6461 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
6462 PCSTimeout,
6463 .features = RTL_FEATURE_MSI,
6464 .default_ver = RTL_GIGA_MAC_VER_13,
6465 }
6466};
6467
6468/* Cfg9346_Unlock assumed. */
6469static unsigned rtl_try_msi(struct rtl8169_private *tp,
6470 const struct rtl_cfg_info *cfg)
6471{
6472 void __iomem *ioaddr = tp->mmio_addr;
6473 unsigned msi = 0;
6474 u8 cfg2;
6475
6476 cfg2 = RTL_R8(Config2) & ~MSIEnable;
6477 if (cfg->features & RTL_FEATURE_MSI) {
6478 if (pci_enable_msi(tp->pci_dev)) {
6479 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
6480 } else {
6481 cfg2 |= MSIEnable;
6482 msi = RTL_FEATURE_MSI;
6483 }
6484 }
6485 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6486 RTL_W8(Config2, cfg2);
6487 return msi;
6488}
6489
Francois Romieu3b6cf252012-03-08 09:59:04 +01006490static int __devinit
6491rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6492{
6493 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
6494 const unsigned int region = cfg->region;
6495 struct rtl8169_private *tp;
6496 struct mii_if_info *mii;
6497 struct net_device *dev;
6498 void __iomem *ioaddr;
6499 int chipset, i;
6500 int rc;
6501
6502 if (netif_msg_drv(&debug)) {
6503 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
6504 MODULENAME, RTL8169_VERSION);
6505 }
6506
6507 dev = alloc_etherdev(sizeof (*tp));
6508 if (!dev) {
6509 rc = -ENOMEM;
6510 goto out;
6511 }
6512
6513 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01006514 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006515 tp = netdev_priv(dev);
6516 tp->dev = dev;
6517 tp->pci_dev = pdev;
6518 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
6519
6520 mii = &tp->mii;
6521 mii->dev = dev;
6522 mii->mdio_read = rtl_mdio_read;
6523 mii->mdio_write = rtl_mdio_write;
6524 mii->phy_id_mask = 0x1f;
6525 mii->reg_num_mask = 0x1f;
6526 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
6527
6528 /* disable ASPM completely as that cause random device stop working
6529 * problems as well as full system hangs for some PCIe devices users */
6530 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6531 PCIE_LINK_STATE_CLKPM);
6532
6533 /* enable device (incl. PCI PM wakeup and hotplug setup) */
6534 rc = pci_enable_device(pdev);
6535 if (rc < 0) {
6536 netif_err(tp, probe, dev, "enable failure\n");
6537 goto err_out_free_dev_1;
6538 }
6539
6540 if (pci_set_mwi(pdev) < 0)
6541 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
6542
6543 /* make sure PCI base addr 1 is MMIO */
6544 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
6545 netif_err(tp, probe, dev,
6546 "region #%d not an MMIO resource, aborting\n",
6547 region);
6548 rc = -ENODEV;
6549 goto err_out_mwi_2;
6550 }
6551
6552 /* check for weird/broken PCI region reporting */
6553 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
6554 netif_err(tp, probe, dev,
6555 "Invalid PCI region size(s), aborting\n");
6556 rc = -ENODEV;
6557 goto err_out_mwi_2;
6558 }
6559
6560 rc = pci_request_regions(pdev, MODULENAME);
6561 if (rc < 0) {
6562 netif_err(tp, probe, dev, "could not request regions\n");
6563 goto err_out_mwi_2;
6564 }
6565
6566 tp->cp_cmd = RxChkSum;
6567
6568 if ((sizeof(dma_addr_t) > 4) &&
6569 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
6570 tp->cp_cmd |= PCIDAC;
6571 dev->features |= NETIF_F_HIGHDMA;
6572 } else {
6573 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6574 if (rc < 0) {
6575 netif_err(tp, probe, dev, "DMA configuration failed\n");
6576 goto err_out_free_res_3;
6577 }
6578 }
6579
6580 /* ioremap MMIO region */
6581 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
6582 if (!ioaddr) {
6583 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
6584 rc = -EIO;
6585 goto err_out_free_res_3;
6586 }
6587 tp->mmio_addr = ioaddr;
6588
6589 if (!pci_is_pcie(pdev))
6590 netif_info(tp, probe, dev, "not PCI Express\n");
6591
6592 /* Identify chip attached to board */
6593 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
6594
6595 rtl_init_rxcfg(tp);
6596
6597 rtl_irq_disable(tp);
6598
6599 rtl_hw_reset(tp);
6600
6601 rtl_ack_events(tp, 0xffff);
6602
6603 pci_set_master(pdev);
6604
6605 /*
6606 * Pretend we are using VLANs; This bypasses a nasty bug where
6607 * Interrupts stop flowing on high load on 8110SCd controllers.
6608 */
6609 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6610 tp->cp_cmd |= RxVlan;
6611
6612 rtl_init_mdio_ops(tp);
6613 rtl_init_pll_power_ops(tp);
6614 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006615 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006616
6617 rtl8169_print_mac_version(tp);
6618
6619 chipset = tp->mac_version;
6620 tp->txd_version = rtl_chip_infos[chipset].txd_version;
6621
6622 RTL_W8(Cfg9346, Cfg9346_Unlock);
6623 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
6624 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
6625 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
6626 tp->features |= RTL_FEATURE_WOL;
6627 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
6628 tp->features |= RTL_FEATURE_WOL;
6629 tp->features |= rtl_try_msi(tp, cfg);
6630 RTL_W8(Cfg9346, Cfg9346_Lock);
6631
6632 if (rtl_tbi_enabled(tp)) {
6633 tp->set_speed = rtl8169_set_speed_tbi;
6634 tp->get_settings = rtl8169_gset_tbi;
6635 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
6636 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
6637 tp->link_ok = rtl8169_tbi_link_ok;
6638 tp->do_ioctl = rtl_tbi_ioctl;
6639 } else {
6640 tp->set_speed = rtl8169_set_speed_xmii;
6641 tp->get_settings = rtl8169_gset_xmii;
6642 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
6643 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
6644 tp->link_ok = rtl8169_xmii_link_ok;
6645 tp->do_ioctl = rtl_xmii_ioctl;
6646 }
6647
6648 mutex_init(&tp->wk.mutex);
6649
6650 /* Get MAC address */
6651 for (i = 0; i < ETH_ALEN; i++)
6652 dev->dev_addr[i] = RTL_R8(MAC0 + i);
6653 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
6654
6655 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
6656 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006657
6658 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
6659
6660 /* don't enable SG, IP_CSUM and TSO by default - it might not work
6661 * properly for all devices */
6662 dev->features |= NETIF_F_RXCSUM |
6663 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6664
6665 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6666 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6667 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6668 NETIF_F_HIGHDMA;
6669
6670 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6671 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
6672 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
6673
6674 dev->hw_features |= NETIF_F_RXALL;
6675 dev->hw_features |= NETIF_F_RXFCS;
6676
6677 tp->hw_start = cfg->hw_start;
6678 tp->event_slow = cfg->event_slow;
6679
6680 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
6681 ~(RxBOVF | RxFOVF) : ~0;
6682
6683 init_timer(&tp->timer);
6684 tp->timer.data = (unsigned long) dev;
6685 tp->timer.function = rtl8169_phy_timer;
6686
6687 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
6688
6689 rc = register_netdev(dev);
6690 if (rc < 0)
6691 goto err_out_msi_4;
6692
6693 pci_set_drvdata(pdev, dev);
6694
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006695 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
6696 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
6697 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006698 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
6699 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
6700 "tx checksumming: %s]\n",
6701 rtl_chip_infos[chipset].jumbo_max,
6702 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
6703 }
6704
6705 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6706 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6707 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6708 rtl8168_driver_start(tp);
6709 }
6710
6711 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
6712
6713 if (pci_dev_run_wake(pdev))
6714 pm_runtime_put_noidle(&pdev->dev);
6715
6716 netif_carrier_off(dev);
6717
6718out:
6719 return rc;
6720
6721err_out_msi_4:
Devendra Nagaad1be8d2012-05-31 01:51:20 +00006722 netif_napi_del(&tp->napi);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006723 rtl_disable_msi(pdev, tp);
6724 iounmap(ioaddr);
6725err_out_free_res_3:
6726 pci_release_regions(pdev);
6727err_out_mwi_2:
6728 pci_clear_mwi(pdev);
6729 pci_disable_device(pdev);
6730err_out_free_dev_1:
6731 free_netdev(dev);
6732 goto out;
6733}
6734
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735static struct pci_driver rtl8169_pci_driver = {
6736 .name = MODULENAME,
6737 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01006738 .probe = rtl_init_one,
Francois Romieue27566e2012-03-08 09:54:01 +01006739 .remove = __devexit_p(rtl_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02006740 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006741 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742};
6743
Francois Romieu07d3f512007-02-21 22:40:46 +01006744static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745{
Jeff Garzik29917622006-08-19 17:48:59 -04006746 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747}
6748
Francois Romieu07d3f512007-02-21 22:40:46 +01006749static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750{
6751 pci_unregister_driver(&rtl8169_pci_driver);
6752}
6753
6754module_init(rtl8169_init_module);
6755module_exit(rtl8169_cleanup_module);