blob: 7afc59336fd76a71ed35705c0332ca84ad39ecf8 [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 Romieuf0e837d2005-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 {
731 void (*write)(void __iomem *, int, int);
732 int (*read)(void __iomem *, int);
733 } 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 {
746 void (*write)(void __iomem *, int, int);
747 u32 (*read)(void __iomem *, int);
748 } 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
françois romieu4da19632011-01-03 15:07:55 +0000922static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
924 int i;
925
Francois Romieua6baf3a2007-11-08 23:23:21 +0100926 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Francois Romieu23714082006-01-29 00:49:09 +0100928 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100929 /*
930 * Check if the RTL8169 has completed writing to the specified
931 * MII register.
932 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200933 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 break;
Francois Romieu23714082006-01-29 00:49:09 +0100935 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700937 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700938 * According to hardware specs a 20us delay is required after write
939 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700940 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700941 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
françois romieu4da19632011-01-03 15:07:55 +0000944static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
946 int i, value = -1;
947
Francois Romieua6baf3a2007-11-08 23:23:21 +0100948 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Francois Romieu23714082006-01-29 00:49:09 +0100950 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100951 /*
952 * Check if the RTL8169 has completed retrieving data from
953 * the specified MII register.
954 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100956 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 break;
958 }
Francois Romieu23714082006-01-29 00:49:09 +0100959 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700961 /*
962 * According to hardware specs a 20us delay is required after read
963 * complete indication, but before sending next command.
964 */
965 udelay(20);
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return value;
968}
969
françois romieuc0e45c12011-01-03 15:08:04 +0000970static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
971{
972 int i;
973
974 RTL_W32(OCPDR, data |
975 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
976 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
977 RTL_W32(EPHY_RXER_NUM, 0);
978
979 for (i = 0; i < 100; i++) {
980 mdelay(1);
981 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
982 break;
983 }
984}
985
986static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
987{
988 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
989 (value & OCPDR_DATA_MASK));
990}
991
992static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
993{
994 int i;
995
996 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
997
998 mdelay(1);
999 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1000 RTL_W32(EPHY_RXER_NUM, 0);
1001
1002 for (i = 0; i < 100; i++) {
1003 mdelay(1);
1004 if (RTL_R32(OCPAR) & OCPAR_FLAG)
1005 break;
1006 }
1007
1008 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
1009}
1010
françois romieue6de30d2011-01-03 15:08:37 +00001011#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1012
1013static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1014{
1015 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1016}
1017
1018static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1019{
1020 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1021}
1022
1023static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
1024{
1025 r8168dp_2_mdio_start(ioaddr);
1026
1027 r8169_mdio_write(ioaddr, reg_addr, value);
1028
1029 r8168dp_2_mdio_stop(ioaddr);
1030}
1031
1032static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
1033{
1034 int value;
1035
1036 r8168dp_2_mdio_start(ioaddr);
1037
1038 value = r8169_mdio_read(ioaddr, reg_addr);
1039
1040 r8168dp_2_mdio_stop(ioaddr);
1041
1042 return value;
1043}
1044
françois romieu4da19632011-01-03 15:07:55 +00001045static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001046{
françois romieuc0e45c12011-01-03 15:08:04 +00001047 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001048}
1049
françois romieu4da19632011-01-03 15:07:55 +00001050static int rtl_readphy(struct rtl8169_private *tp, int location)
1051{
françois romieuc0e45c12011-01-03 15:08:04 +00001052 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +00001053}
1054
1055static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1056{
1057 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1058}
1059
1060static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001061{
1062 int val;
1063
françois romieu4da19632011-01-03 15:07:55 +00001064 val = rtl_readphy(tp, reg_addr);
1065 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001066}
1067
Francois Romieuccdffb92008-07-26 14:26:06 +02001068static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1069 int val)
1070{
1071 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001072
françois romieu4da19632011-01-03 15:07:55 +00001073 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001074}
1075
1076static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1077{
1078 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001079
françois romieu4da19632011-01-03 15:07:55 +00001080 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001081}
1082
Francois Romieudacf8152008-08-02 20:44:13 +02001083static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
1084{
1085 unsigned int i;
1086
1087 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1088 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1089
1090 for (i = 0; i < 100; i++) {
1091 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1092 break;
1093 udelay(10);
1094 }
1095}
1096
1097static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1098{
1099 u16 value = 0xffff;
1100 unsigned int i;
1101
1102 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1103
1104 for (i = 0; i < 100; i++) {
1105 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1106 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1107 break;
1108 }
1109 udelay(10);
1110 }
1111
1112 return value;
1113}
1114
Hayes Wang133ac402011-07-06 15:58:05 +08001115static
1116void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1117{
1118 unsigned int i;
1119
1120 BUG_ON((addr & 3) || (mask == 0));
1121 RTL_W32(ERIDR, val);
1122 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1123
1124 for (i = 0; i < 100; i++) {
1125 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1126 break;
1127 udelay(100);
1128 }
1129}
1130
1131static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1132{
1133 u32 value = ~0x00;
1134 unsigned int i;
1135
1136 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1137
1138 for (i = 0; i < 100; i++) {
1139 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1140 value = RTL_R32(ERIDR);
1141 break;
1142 }
1143 udelay(100);
1144 }
1145
1146 return value;
1147}
1148
1149static void
1150rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1151{
1152 u32 val;
1153
1154 val = rtl_eri_read(ioaddr, addr, type);
1155 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1156}
1157
françois romieuc28aa382011-08-02 03:53:43 +00001158struct exgmac_reg {
1159 u16 addr;
1160 u16 mask;
1161 u32 val;
1162};
1163
1164static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1165 const struct exgmac_reg *r, int len)
1166{
1167 while (len-- > 0) {
1168 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1169 r++;
1170 }
1171}
1172
françois romieudaf9df62009-10-07 12:44:20 +00001173static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1174{
1175 u8 value = 0xff;
1176 unsigned int i;
1177
1178 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1179
1180 for (i = 0; i < 300; i++) {
1181 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1182 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1183 break;
1184 }
1185 udelay(100);
1186 }
1187
1188 return value;
1189}
1190
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001191static u16 rtl_get_events(struct rtl8169_private *tp)
1192{
1193 void __iomem *ioaddr = tp->mmio_addr;
1194
1195 return RTL_R16(IntrStatus);
1196}
1197
1198static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1199{
1200 void __iomem *ioaddr = tp->mmio_addr;
1201
1202 RTL_W16(IntrStatus, bits);
1203 mmiowb();
1204}
1205
1206static void rtl_irq_disable(struct rtl8169_private *tp)
1207{
1208 void __iomem *ioaddr = tp->mmio_addr;
1209
1210 RTL_W16(IntrMask, 0);
1211 mmiowb();
1212}
1213
Francois Romieu3e990ff2012-01-26 12:50:01 +01001214static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1215{
1216 void __iomem *ioaddr = tp->mmio_addr;
1217
1218 RTL_W16(IntrMask, bits);
1219}
1220
Francois Romieuda78dbf2012-01-26 14:18:23 +01001221#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1222#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1223#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1224
1225static void rtl_irq_enable_all(struct rtl8169_private *tp)
1226{
1227 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1228}
1229
françois romieu811fd302011-12-04 20:30:45 +00001230static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
françois romieu811fd302011-12-04 20:30:45 +00001232 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001234 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001235 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001236 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
françois romieu4da19632011-01-03 15:07:55 +00001239static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
françois romieu4da19632011-01-03 15:07:55 +00001241 void __iomem *ioaddr = tp->mmio_addr;
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return RTL_R32(TBICSR) & TBIReset;
1244}
1245
françois romieu4da19632011-01-03 15:07:55 +00001246static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
françois romieu4da19632011-01-03 15:07:55 +00001248 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
1251static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1252{
1253 return RTL_R32(TBICSR) & TBILinkOk;
1254}
1255
1256static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1257{
1258 return RTL_R8(PHYstatus) & LinkStatus;
1259}
1260
françois romieu4da19632011-01-03 15:07:55 +00001261static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
françois romieu4da19632011-01-03 15:07:55 +00001263 void __iomem *ioaddr = tp->mmio_addr;
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1266}
1267
françois romieu4da19632011-01-03 15:07:55 +00001268static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 unsigned int val;
1271
françois romieu4da19632011-01-03 15:07:55 +00001272 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1273 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
Hayes Wang70090422011-07-06 15:58:06 +08001276static void rtl_link_chg_patch(struct rtl8169_private *tp)
1277{
1278 void __iomem *ioaddr = tp->mmio_addr;
1279 struct net_device *dev = tp->dev;
1280
1281 if (!netif_running(dev))
1282 return;
1283
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001284 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1285 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Hayes Wang70090422011-07-06 15:58:06 +08001286 if (RTL_R8(PHYstatus) & _1000bpsF) {
1287 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1288 0x00000011, ERIAR_EXGMAC);
1289 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1290 0x00000005, ERIAR_EXGMAC);
1291 } else if (RTL_R8(PHYstatus) & _100bps) {
1292 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1293 0x0000001f, ERIAR_EXGMAC);
1294 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1295 0x00000005, ERIAR_EXGMAC);
1296 } else {
1297 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1298 0x0000001f, ERIAR_EXGMAC);
1299 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1300 0x0000003f, ERIAR_EXGMAC);
1301 }
1302 /* Reset packet filter */
1303 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1304 ERIAR_EXGMAC);
1305 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1306 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001307 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1308 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1309 if (RTL_R8(PHYstatus) & _1000bpsF) {
1310 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1311 0x00000011, ERIAR_EXGMAC);
1312 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1313 0x00000005, ERIAR_EXGMAC);
1314 } else {
1315 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1316 0x0000001f, ERIAR_EXGMAC);
1317 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1318 0x0000003f, ERIAR_EXGMAC);
1319 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001320 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1321 if (RTL_R8(PHYstatus) & _10bps) {
1322 rtl_eri_write(ioaddr, 0x1d0, ERIAR_MASK_0011,
1323 0x4d02, ERIAR_EXGMAC);
1324 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_0011,
1325 0x0060, ERIAR_EXGMAC);
1326 } else {
1327 rtl_eri_write(ioaddr, 0x1d0, ERIAR_MASK_0011,
1328 0x0000, ERIAR_EXGMAC);
1329 }
Hayes Wang70090422011-07-06 15:58:06 +08001330 }
1331}
1332
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001333static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001334 struct rtl8169_private *tp,
1335 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001338 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001339 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001340 if (pm)
1341 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001343 if (net_ratelimit())
1344 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001345 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001347 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001348 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001349 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
1352
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001353static void rtl8169_check_link_status(struct net_device *dev,
1354 struct rtl8169_private *tp,
1355 void __iomem *ioaddr)
1356{
1357 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1358}
1359
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001360#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1361
1362static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1363{
1364 void __iomem *ioaddr = tp->mmio_addr;
1365 u8 options;
1366 u32 wolopts = 0;
1367
1368 options = RTL_R8(Config1);
1369 if (!(options & PMEnable))
1370 return 0;
1371
1372 options = RTL_R8(Config3);
1373 if (options & LinkUp)
1374 wolopts |= WAKE_PHY;
1375 if (options & MagicPacket)
1376 wolopts |= WAKE_MAGIC;
1377
1378 options = RTL_R8(Config5);
1379 if (options & UWF)
1380 wolopts |= WAKE_UCAST;
1381 if (options & BWF)
1382 wolopts |= WAKE_BCAST;
1383 if (options & MWF)
1384 wolopts |= WAKE_MCAST;
1385
1386 return wolopts;
1387}
1388
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001389static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1390{
1391 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001392
Francois Romieuda78dbf2012-01-26 14:18:23 +01001393 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001394
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001395 wol->supported = WAKE_ANY;
1396 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001397
Francois Romieuda78dbf2012-01-26 14:18:23 +01001398 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001399}
1400
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001401static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001402{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001403 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001404 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001405 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001406 u32 opt;
1407 u16 reg;
1408 u8 mask;
1409 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001410 { WAKE_PHY, Config3, LinkUp },
1411 { WAKE_MAGIC, Config3, MagicPacket },
1412 { WAKE_UCAST, Config5, UWF },
1413 { WAKE_BCAST, Config5, BWF },
1414 { WAKE_MCAST, Config5, MWF },
1415 { WAKE_ANY, Config5, LanWake }
1416 };
Francois Romieu851e6022012-04-17 11:10:11 +02001417 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001418
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001419 RTL_W8(Cfg9346, Cfg9346_Unlock);
1420
1421 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
Francois Romieu851e6022012-04-17 11:10:11 +02001422 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001423 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001424 options |= cfg[i].mask;
1425 RTL_W8(cfg[i].reg, options);
1426 }
1427
Francois Romieu851e6022012-04-17 11:10:11 +02001428 switch (tp->mac_version) {
1429 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1430 options = RTL_R8(Config1) & ~PMEnable;
1431 if (wolopts)
1432 options |= PMEnable;
1433 RTL_W8(Config1, options);
1434 break;
1435 default:
Francois Romieud387b422012-04-17 11:12:01 +02001436 options = RTL_R8(Config2) & ~PME_SIGNAL;
1437 if (wolopts)
1438 options |= PME_SIGNAL;
1439 RTL_W8(Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001440 break;
1441 }
1442
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001443 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001444}
1445
1446static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1447{
1448 struct rtl8169_private *tp = netdev_priv(dev);
1449
Francois Romieuda78dbf2012-01-26 14:18:23 +01001450 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001451
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001452 if (wol->wolopts)
1453 tp->features |= RTL_FEATURE_WOL;
1454 else
1455 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001456 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001457
1458 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001459
françois romieuea809072010-11-08 13:23:58 +00001460 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1461
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001462 return 0;
1463}
1464
Francois Romieu31bd2042011-04-26 18:58:59 +02001465static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1466{
Francois Romieu85bffe62011-04-27 08:22:39 +02001467 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001468}
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470static void rtl8169_get_drvinfo(struct net_device *dev,
1471 struct ethtool_drvinfo *info)
1472{
1473 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001474 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Rick Jones68aad782011-11-07 13:29:27 +00001476 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1477 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1478 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001479 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001480 if (!IS_ERR_OR_NULL(rtl_fw))
1481 strlcpy(info->fw_version, rtl_fw->version,
1482 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484
1485static int rtl8169_get_regs_len(struct net_device *dev)
1486{
1487 return R8169_REGS_SIZE;
1488}
1489
1490static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001491 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 struct rtl8169_private *tp = netdev_priv(dev);
1494 void __iomem *ioaddr = tp->mmio_addr;
1495 int ret = 0;
1496 u32 reg;
1497
1498 reg = RTL_R32(TBICSR);
1499 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1500 (duplex == DUPLEX_FULL)) {
1501 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1502 } else if (autoneg == AUTONEG_ENABLE)
1503 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1504 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001505 netif_warn(tp, link, dev,
1506 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 ret = -EOPNOTSUPP;
1508 }
1509
1510 return ret;
1511}
1512
1513static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001514 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001517 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001518 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Hayes Wang716b50a2011-02-22 17:26:18 +08001520 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001523 int auto_nego;
1524
françois romieu4da19632011-01-03 15:07:55 +00001525 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001526 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1527 ADVERTISE_100HALF | ADVERTISE_100FULL);
1528
1529 if (adv & ADVERTISED_10baseT_Half)
1530 auto_nego |= ADVERTISE_10HALF;
1531 if (adv & ADVERTISED_10baseT_Full)
1532 auto_nego |= ADVERTISE_10FULL;
1533 if (adv & ADVERTISED_100baseT_Half)
1534 auto_nego |= ADVERTISE_100HALF;
1535 if (adv & ADVERTISED_100baseT_Full)
1536 auto_nego |= ADVERTISE_100FULL;
1537
françois romieu3577aa12009-05-19 10:46:48 +00001538 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1539
françois romieu4da19632011-01-03 15:07:55 +00001540 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001541 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1542
1543 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001544 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001545 if (adv & ADVERTISED_1000baseT_Half)
1546 giga_ctrl |= ADVERTISE_1000HALF;
1547 if (adv & ADVERTISED_1000baseT_Full)
1548 giga_ctrl |= ADVERTISE_1000FULL;
1549 } else if (adv & (ADVERTISED_1000baseT_Half |
1550 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001551 netif_info(tp, link, dev,
1552 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001553 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
françois romieu3577aa12009-05-19 10:46:48 +00001556 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001557
françois romieu4da19632011-01-03 15:07:55 +00001558 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1559 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001560 } else {
1561 giga_ctrl = 0;
1562
1563 if (speed == SPEED_10)
1564 bmcr = 0;
1565 else if (speed == SPEED_100)
1566 bmcr = BMCR_SPEED100;
1567 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001568 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001569
1570 if (duplex == DUPLEX_FULL)
1571 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001572 }
1573
françois romieu4da19632011-01-03 15:07:55 +00001574 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001575
Francois Romieucecb5fd2011-04-01 10:21:07 +02001576 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1577 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001578 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001579 rtl_writephy(tp, 0x17, 0x2138);
1580 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001581 } else {
françois romieu4da19632011-01-03 15:07:55 +00001582 rtl_writephy(tp, 0x17, 0x2108);
1583 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001584 }
1585 }
1586
Oliver Neukum54405cd2011-01-06 21:55:13 +01001587 rc = 0;
1588out:
1589 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591
1592static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001593 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
1595 struct rtl8169_private *tp = netdev_priv(dev);
1596 int ret;
1597
Oliver Neukum54405cd2011-01-06 21:55:13 +01001598 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001599 if (ret < 0)
1600 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Francois Romieu4876cc12011-03-11 21:07:11 +01001602 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1603 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001605 }
1606out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 return ret;
1608}
1609
1610static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1611{
1612 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 int ret;
1614
Francois Romieu4876cc12011-03-11 21:07:11 +01001615 del_timer_sync(&tp->timer);
1616
Francois Romieuda78dbf2012-01-26 14:18:23 +01001617 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001618 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001619 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001620 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 return ret;
1623}
1624
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001625static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1626 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
Francois Romieud58d46b2011-05-03 16:38:29 +02001628 struct rtl8169_private *tp = netdev_priv(dev);
1629
Francois Romieu2b7b4312011-04-18 22:53:24 -07001630 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001631 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Francois Romieud58d46b2011-05-03 16:38:29 +02001633 if (dev->mtu > JUMBO_1K &&
1634 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1635 features &= ~NETIF_F_IP_CSUM;
1636
Michał Mirosław350fb322011-04-08 06:35:56 +00001637 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
Francois Romieuda78dbf2012-01-26 14:18:23 +01001640static void __rtl8169_set_features(struct net_device *dev,
1641 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
1643 struct rtl8169_private *tp = netdev_priv(dev);
Ben Greear6bbe0212012-02-10 15:04:33 +00001644 netdev_features_t changed = features ^ dev->features;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001645 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Ben Greear6bbe0212012-02-10 15:04:33 +00001647 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)))
1648 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Ben Greear6bbe0212012-02-10 15:04:33 +00001650 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)) {
1651 if (features & NETIF_F_RXCSUM)
1652 tp->cp_cmd |= RxChkSum;
1653 else
1654 tp->cp_cmd &= ~RxChkSum;
Michał Mirosław350fb322011-04-08 06:35:56 +00001655
Ben Greear6bbe0212012-02-10 15:04:33 +00001656 if (dev->features & NETIF_F_HW_VLAN_RX)
1657 tp->cp_cmd |= RxVlan;
1658 else
1659 tp->cp_cmd &= ~RxVlan;
1660
1661 RTL_W16(CPlusCmd, tp->cp_cmd);
1662 RTL_R16(CPlusCmd);
1663 }
1664 if (changed & NETIF_F_RXALL) {
1665 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1666 if (features & NETIF_F_RXALL)
1667 tmp |= (AcceptErr | AcceptRunt);
1668 RTL_W32(RxConfig, tmp);
1669 }
Francois Romieuda78dbf2012-01-26 14:18:23 +01001670}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Francois Romieuda78dbf2012-01-26 14:18:23 +01001672static int rtl8169_set_features(struct net_device *dev,
1673 netdev_features_t features)
1674{
1675 struct rtl8169_private *tp = netdev_priv(dev);
1676
1677 rtl_lock_work(tp);
1678 __rtl8169_set_features(dev, features);
1679 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 return 0;
1682}
1683
Francois Romieuda78dbf2012-01-26 14:18:23 +01001684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1686 struct sk_buff *skb)
1687{
Jesse Grosseab6d182010-10-20 13:56:03 +00001688 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1690}
1691
Francois Romieu7a8fc772011-03-01 17:18:33 +01001692static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
1694 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Francois Romieu7a8fc772011-03-01 17:18:33 +01001696 if (opts2 & RxVlanTag)
1697 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
Francois Romieuccdffb92008-07-26 14:26:06 +02001702static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
1704 struct rtl8169_private *tp = netdev_priv(dev);
1705 void __iomem *ioaddr = tp->mmio_addr;
1706 u32 status;
1707
1708 cmd->supported =
1709 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1710 cmd->port = PORT_FIBRE;
1711 cmd->transceiver = XCVR_INTERNAL;
1712
1713 status = RTL_R32(TBICSR);
1714 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1715 cmd->autoneg = !!(status & TBINwEnable);
1716
David Decotigny70739492011-04-27 18:32:40 +00001717 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001719
1720 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721}
1722
Francois Romieuccdffb92008-07-26 14:26:06 +02001723static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
1725 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Francois Romieuccdffb92008-07-26 14:26:06 +02001727 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
1730static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1731{
1732 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001733 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Francois Romieuda78dbf2012-01-26 14:18:23 +01001735 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02001736 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001737 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Francois Romieuccdffb92008-07-26 14:26:06 +02001739 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740}
1741
1742static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1743 void *p)
1744{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001745 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Francois Romieu5b0384f2006-08-16 16:00:01 +02001747 if (regs->len > R8169_REGS_SIZE)
1748 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Francois Romieuda78dbf2012-01-26 14:18:23 +01001750 rtl_lock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001751 memcpy_fromio(p, tp->mmio_addr, regs->len);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001752 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
1754
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001755static u32 rtl8169_get_msglevel(struct net_device *dev)
1756{
1757 struct rtl8169_private *tp = netdev_priv(dev);
1758
1759 return tp->msg_enable;
1760}
1761
1762static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1763{
1764 struct rtl8169_private *tp = netdev_priv(dev);
1765
1766 tp->msg_enable = value;
1767}
1768
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001769static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1770 "tx_packets",
1771 "rx_packets",
1772 "tx_errors",
1773 "rx_errors",
1774 "rx_missed",
1775 "align_errors",
1776 "tx_single_collisions",
1777 "tx_multi_collisions",
1778 "unicast",
1779 "broadcast",
1780 "multicast",
1781 "tx_aborted",
1782 "tx_underrun",
1783};
1784
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001785static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001786{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001787 switch (sset) {
1788 case ETH_SS_STATS:
1789 return ARRAY_SIZE(rtl8169_gstrings);
1790 default:
1791 return -EOPNOTSUPP;
1792 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001793}
1794
Ivan Vecera355423d2009-02-06 21:49:57 -08001795static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001796{
1797 struct rtl8169_private *tp = netdev_priv(dev);
1798 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001799 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001800 struct rtl8169_counters *counters;
1801 dma_addr_t paddr;
1802 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001803 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001804
Ivan Vecera355423d2009-02-06 21:49:57 -08001805 /*
1806 * Some chips are unable to dump tally counters when the receiver
1807 * is disabled.
1808 */
1809 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1810 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001811
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001812 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001813 if (!counters)
1814 return;
1815
1816 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001817 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001818 RTL_W32(CounterAddrLow, cmd);
1819 RTL_W32(CounterAddrLow, cmd | CounterDump);
1820
Ivan Vecera355423d2009-02-06 21:49:57 -08001821 while (wait--) {
1822 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001823 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001824 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001825 }
1826 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001827 }
1828
1829 RTL_W32(CounterAddrLow, 0);
1830 RTL_W32(CounterAddrHigh, 0);
1831
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001832 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001833}
1834
Ivan Vecera355423d2009-02-06 21:49:57 -08001835static void rtl8169_get_ethtool_stats(struct net_device *dev,
1836 struct ethtool_stats *stats, u64 *data)
1837{
1838 struct rtl8169_private *tp = netdev_priv(dev);
1839
1840 ASSERT_RTNL();
1841
1842 rtl8169_update_counters(dev);
1843
1844 data[0] = le64_to_cpu(tp->counters.tx_packets);
1845 data[1] = le64_to_cpu(tp->counters.rx_packets);
1846 data[2] = le64_to_cpu(tp->counters.tx_errors);
1847 data[3] = le32_to_cpu(tp->counters.rx_errors);
1848 data[4] = le16_to_cpu(tp->counters.rx_missed);
1849 data[5] = le16_to_cpu(tp->counters.align_errors);
1850 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1851 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1852 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1853 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1854 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1855 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1856 data[12] = le16_to_cpu(tp->counters.tx_underun);
1857}
1858
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001859static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1860{
1861 switch(stringset) {
1862 case ETH_SS_STATS:
1863 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1864 break;
1865 }
1866}
1867
Jeff Garzik7282d492006-09-13 14:30:00 -04001868static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 .get_drvinfo = rtl8169_get_drvinfo,
1870 .get_regs_len = rtl8169_get_regs_len,
1871 .get_link = ethtool_op_get_link,
1872 .get_settings = rtl8169_get_settings,
1873 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001874 .get_msglevel = rtl8169_get_msglevel,
1875 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001877 .get_wol = rtl8169_get_wol,
1878 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001879 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001880 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001881 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00001882 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883};
1884
Francois Romieu07d3f512007-02-21 22:40:46 +01001885static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001886 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
Francois Romieu5d320a22011-05-08 17:47:36 +02001888 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001889 /*
1890 * The driver currently handles the 8168Bf and the 8168Be identically
1891 * but they can be identified more specifically through the test below
1892 * if needed:
1893 *
1894 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001895 *
1896 * Same thing for the 8101Eb and the 8101Ec:
1897 *
1898 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001899 */
Francois Romieu37441002011-06-17 22:58:54 +02001900 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001902 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 int mac_version;
1904 } mac_info[] = {
Hayes Wangc2218922011-09-06 16:55:18 +08001905 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001906 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08001907 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1908 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1909
hayeswang01dc7fe2011-03-21 01:50:28 +00001910 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001911 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001912 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1913 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1914 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1915
Francois Romieu5b538df2008-07-20 16:22:45 +02001916 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001917 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1918 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001919 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001920
françois romieue6de30d2011-01-03 15:08:37 +00001921 /* 8168DP family. */
1922 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1923 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001924 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001925
Francois Romieuef808d52008-06-29 13:10:54 +02001926 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001927 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001928 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001929 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001930 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001931 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1932 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001933 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001934 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001935 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001936
1937 /* 8168B family. */
1938 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1939 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1940 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1941 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1942
1943 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08001944 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
1945 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08001946 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00001947 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001948 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1949 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1950 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001951 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1952 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1953 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1954 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1955 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1956 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001957 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001958 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001959 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001960 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1961 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001962 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1963 /* FIXME: where did these entries come from ? -- FR */
1964 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1965 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1966
1967 /* 8110 family. */
1968 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1969 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1970 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1971 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1972 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1973 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1974
Jean Delvaref21b75e2009-05-26 20:54:48 -07001975 /* Catch-all */
1976 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001977 };
1978 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 u32 reg;
1980
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001981 reg = RTL_R32(TxConfig);
1982 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 p++;
1984 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001985
1986 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1987 netif_notice(tp, probe, dev,
1988 "unknown MAC, using family default\n");
1989 tp->mac_version = default_version;
1990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991}
1992
1993static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1994{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001995 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
1997
Francois Romieu867763c2007-08-17 18:21:58 +02001998struct phy_reg {
1999 u16 reg;
2000 u16 val;
2001};
2002
françois romieu4da19632011-01-03 15:07:55 +00002003static void rtl_writephy_batch(struct rtl8169_private *tp,
2004 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002005{
2006 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002007 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002008 regs++;
2009 }
2010}
2011
françois romieubca03d52011-01-03 15:07:31 +00002012#define PHY_READ 0x00000000
2013#define PHY_DATA_OR 0x10000000
2014#define PHY_DATA_AND 0x20000000
2015#define PHY_BJMPN 0x30000000
2016#define PHY_READ_EFUSE 0x40000000
2017#define PHY_READ_MAC_BYTE 0x50000000
2018#define PHY_WRITE_MAC_BYTE 0x60000000
2019#define PHY_CLEAR_READCOUNT 0x70000000
2020#define PHY_WRITE 0x80000000
2021#define PHY_READCOUNT_EQ_SKIP 0x90000000
2022#define PHY_COMP_EQ_SKIPN 0xa0000000
2023#define PHY_COMP_NEQ_SKIPN 0xb0000000
2024#define PHY_WRITE_PREVIOUS 0xc0000000
2025#define PHY_SKIPN 0xd0000000
2026#define PHY_DELAY_MS 0xe0000000
2027#define PHY_WRITE_ERI_WORD 0xf0000000
2028
Hayes Wang960aee62011-06-18 11:37:48 +02002029struct fw_info {
2030 u32 magic;
2031 char version[RTL_VER_SIZE];
2032 __le32 fw_start;
2033 __le32 fw_len;
2034 u8 chksum;
2035} __packed;
2036
Francois Romieu1c361ef2011-06-17 17:16:24 +02002037#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2038
2039static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002040{
Francois Romieub6ffd972011-06-17 17:00:05 +02002041 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002042 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002043 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2044 char *version = rtl_fw->version;
2045 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002046
Francois Romieu1c361ef2011-06-17 17:16:24 +02002047 if (fw->size < FW_OPCODE_SIZE)
2048 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002049
2050 if (!fw_info->magic) {
2051 size_t i, size, start;
2052 u8 checksum = 0;
2053
2054 if (fw->size < sizeof(*fw_info))
2055 goto out;
2056
2057 for (i = 0; i < fw->size; i++)
2058 checksum += fw->data[i];
2059 if (checksum != 0)
2060 goto out;
2061
2062 start = le32_to_cpu(fw_info->fw_start);
2063 if (start > fw->size)
2064 goto out;
2065
2066 size = le32_to_cpu(fw_info->fw_len);
2067 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2068 goto out;
2069
2070 memcpy(version, fw_info->version, RTL_VER_SIZE);
2071
2072 pa->code = (__le32 *)(fw->data + start);
2073 pa->size = size;
2074 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002075 if (fw->size % FW_OPCODE_SIZE)
2076 goto out;
2077
2078 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2079
2080 pa->code = (__le32 *)fw->data;
2081 pa->size = fw->size / FW_OPCODE_SIZE;
2082 }
2083 version[RTL_VER_SIZE - 1] = 0;
2084
2085 rc = true;
2086out:
2087 return rc;
2088}
2089
Francois Romieufd112f22011-06-18 00:10:29 +02002090static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2091 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002092{
Francois Romieufd112f22011-06-18 00:10:29 +02002093 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002094 size_t index;
2095
Francois Romieu1c361ef2011-06-17 17:16:24 +02002096 for (index = 0; index < pa->size; index++) {
2097 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002098 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002099
hayeswang42b82dc2011-01-10 02:07:25 +00002100 switch(action & 0xf0000000) {
2101 case PHY_READ:
2102 case PHY_DATA_OR:
2103 case PHY_DATA_AND:
2104 case PHY_READ_EFUSE:
2105 case PHY_CLEAR_READCOUNT:
2106 case PHY_WRITE:
2107 case PHY_WRITE_PREVIOUS:
2108 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002109 break;
2110
hayeswang42b82dc2011-01-10 02:07:25 +00002111 case PHY_BJMPN:
2112 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002113 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002114 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002115 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002116 }
2117 break;
2118 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002119 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002120 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002121 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002122 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002123 }
2124 break;
2125 case PHY_COMP_EQ_SKIPN:
2126 case PHY_COMP_NEQ_SKIPN:
2127 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002128 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002129 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002130 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002131 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002132 }
2133 break;
2134
2135 case PHY_READ_MAC_BYTE:
2136 case PHY_WRITE_MAC_BYTE:
2137 case PHY_WRITE_ERI_WORD:
2138 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002139 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002140 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002141 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002142 }
2143 }
Francois Romieufd112f22011-06-18 00:10:29 +02002144 rc = true;
2145out:
2146 return rc;
2147}
françois romieubca03d52011-01-03 15:07:31 +00002148
Francois Romieufd112f22011-06-18 00:10:29 +02002149static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2150{
2151 struct net_device *dev = tp->dev;
2152 int rc = -EINVAL;
2153
2154 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2155 netif_err(tp, ifup, dev, "invalid firwmare\n");
2156 goto out;
2157 }
2158
2159 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2160 rc = 0;
2161out:
2162 return rc;
2163}
2164
2165static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2166{
2167 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2168 u32 predata, count;
2169 size_t index;
2170
2171 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002172
Francois Romieu1c361ef2011-06-17 17:16:24 +02002173 for (index = 0; index < pa->size; ) {
2174 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002175 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002176 u32 regno = (action & 0x0fff0000) >> 16;
2177
2178 if (!action)
2179 break;
françois romieubca03d52011-01-03 15:07:31 +00002180
2181 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002182 case PHY_READ:
2183 predata = rtl_readphy(tp, regno);
2184 count++;
2185 index++;
françois romieubca03d52011-01-03 15:07:31 +00002186 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002187 case PHY_DATA_OR:
2188 predata |= data;
2189 index++;
2190 break;
2191 case PHY_DATA_AND:
2192 predata &= data;
2193 index++;
2194 break;
2195 case PHY_BJMPN:
2196 index -= regno;
2197 break;
2198 case PHY_READ_EFUSE:
2199 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2200 index++;
2201 break;
2202 case PHY_CLEAR_READCOUNT:
2203 count = 0;
2204 index++;
2205 break;
2206 case PHY_WRITE:
2207 rtl_writephy(tp, regno, data);
2208 index++;
2209 break;
2210 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002211 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002212 break;
2213 case PHY_COMP_EQ_SKIPN:
2214 if (predata == data)
2215 index += regno;
2216 index++;
2217 break;
2218 case PHY_COMP_NEQ_SKIPN:
2219 if (predata != data)
2220 index += regno;
2221 index++;
2222 break;
2223 case PHY_WRITE_PREVIOUS:
2224 rtl_writephy(tp, regno, predata);
2225 index++;
2226 break;
2227 case PHY_SKIPN:
2228 index += regno + 1;
2229 break;
2230 case PHY_DELAY_MS:
2231 mdelay(data);
2232 index++;
2233 break;
2234
2235 case PHY_READ_MAC_BYTE:
2236 case PHY_WRITE_MAC_BYTE:
2237 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002238 default:
2239 BUG();
2240 }
2241 }
2242}
2243
françois romieuf1e02ed2011-01-13 13:07:53 +00002244static void rtl_release_firmware(struct rtl8169_private *tp)
2245{
Francois Romieub6ffd972011-06-17 17:00:05 +02002246 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2247 release_firmware(tp->rtl_fw->fw);
2248 kfree(tp->rtl_fw);
2249 }
2250 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002251}
2252
François Romieu953a12c2011-04-24 17:38:48 +02002253static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002254{
Francois Romieub6ffd972011-06-17 17:00:05 +02002255 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002256
2257 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002258 if (!IS_ERR_OR_NULL(rtl_fw))
2259 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002260}
2261
2262static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2263{
2264 if (rtl_readphy(tp, reg) != val)
2265 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2266 else
2267 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002268}
2269
françois romieu4da19632011-01-03 15:07:55 +00002270static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002272 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002273 { 0x1f, 0x0001 },
2274 { 0x06, 0x006e },
2275 { 0x08, 0x0708 },
2276 { 0x15, 0x4000 },
2277 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
françois romieu0b9b5712009-08-10 19:44:56 +00002279 { 0x1f, 0x0001 },
2280 { 0x03, 0x00a1 },
2281 { 0x02, 0x0008 },
2282 { 0x01, 0x0120 },
2283 { 0x00, 0x1000 },
2284 { 0x04, 0x0800 },
2285 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
françois romieu0b9b5712009-08-10 19:44:56 +00002287 { 0x03, 0xff41 },
2288 { 0x02, 0xdf60 },
2289 { 0x01, 0x0140 },
2290 { 0x00, 0x0077 },
2291 { 0x04, 0x7800 },
2292 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
françois romieu0b9b5712009-08-10 19:44:56 +00002294 { 0x03, 0x802f },
2295 { 0x02, 0x4f02 },
2296 { 0x01, 0x0409 },
2297 { 0x00, 0xf0f9 },
2298 { 0x04, 0x9800 },
2299 { 0x04, 0x9000 },
2300
2301 { 0x03, 0xdf01 },
2302 { 0x02, 0xdf20 },
2303 { 0x01, 0xff95 },
2304 { 0x00, 0xba00 },
2305 { 0x04, 0xa800 },
2306 { 0x04, 0xa000 },
2307
2308 { 0x03, 0xff41 },
2309 { 0x02, 0xdf20 },
2310 { 0x01, 0x0140 },
2311 { 0x00, 0x00bb },
2312 { 0x04, 0xb800 },
2313 { 0x04, 0xb000 },
2314
2315 { 0x03, 0xdf41 },
2316 { 0x02, 0xdc60 },
2317 { 0x01, 0x6340 },
2318 { 0x00, 0x007d },
2319 { 0x04, 0xd800 },
2320 { 0x04, 0xd000 },
2321
2322 { 0x03, 0xdf01 },
2323 { 0x02, 0xdf20 },
2324 { 0x01, 0x100a },
2325 { 0x00, 0xa0ff },
2326 { 0x04, 0xf800 },
2327 { 0x04, 0xf000 },
2328
2329 { 0x1f, 0x0000 },
2330 { 0x0b, 0x0000 },
2331 { 0x00, 0x9200 }
2332 };
2333
françois romieu4da19632011-01-03 15:07:55 +00002334 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335}
2336
françois romieu4da19632011-01-03 15:07:55 +00002337static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002338{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002339 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002340 { 0x1f, 0x0002 },
2341 { 0x01, 0x90d0 },
2342 { 0x1f, 0x0000 }
2343 };
2344
françois romieu4da19632011-01-03 15:07:55 +00002345 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002346}
2347
françois romieu4da19632011-01-03 15:07:55 +00002348static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002349{
2350 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002351
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002352 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2353 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002354 return;
2355
françois romieu4da19632011-01-03 15:07:55 +00002356 rtl_writephy(tp, 0x1f, 0x0001);
2357 rtl_writephy(tp, 0x10, 0xf01b);
2358 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002359}
2360
françois romieu4da19632011-01-03 15:07:55 +00002361static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002362{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002363 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002364 { 0x1f, 0x0001 },
2365 { 0x04, 0x0000 },
2366 { 0x03, 0x00a1 },
2367 { 0x02, 0x0008 },
2368 { 0x01, 0x0120 },
2369 { 0x00, 0x1000 },
2370 { 0x04, 0x0800 },
2371 { 0x04, 0x9000 },
2372 { 0x03, 0x802f },
2373 { 0x02, 0x4f02 },
2374 { 0x01, 0x0409 },
2375 { 0x00, 0xf099 },
2376 { 0x04, 0x9800 },
2377 { 0x04, 0xa000 },
2378 { 0x03, 0xdf01 },
2379 { 0x02, 0xdf20 },
2380 { 0x01, 0xff95 },
2381 { 0x00, 0xba00 },
2382 { 0x04, 0xa800 },
2383 { 0x04, 0xf000 },
2384 { 0x03, 0xdf01 },
2385 { 0x02, 0xdf20 },
2386 { 0x01, 0x101a },
2387 { 0x00, 0xa0ff },
2388 { 0x04, 0xf800 },
2389 { 0x04, 0x0000 },
2390 { 0x1f, 0x0000 },
2391
2392 { 0x1f, 0x0001 },
2393 { 0x10, 0xf41b },
2394 { 0x14, 0xfb54 },
2395 { 0x18, 0xf5c7 },
2396 { 0x1f, 0x0000 },
2397
2398 { 0x1f, 0x0001 },
2399 { 0x17, 0x0cc0 },
2400 { 0x1f, 0x0000 }
2401 };
2402
françois romieu4da19632011-01-03 15:07:55 +00002403 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002404
françois romieu4da19632011-01-03 15:07:55 +00002405 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002406}
2407
françois romieu4da19632011-01-03 15:07:55 +00002408static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002409{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002410 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002411 { 0x1f, 0x0001 },
2412 { 0x04, 0x0000 },
2413 { 0x03, 0x00a1 },
2414 { 0x02, 0x0008 },
2415 { 0x01, 0x0120 },
2416 { 0x00, 0x1000 },
2417 { 0x04, 0x0800 },
2418 { 0x04, 0x9000 },
2419 { 0x03, 0x802f },
2420 { 0x02, 0x4f02 },
2421 { 0x01, 0x0409 },
2422 { 0x00, 0xf099 },
2423 { 0x04, 0x9800 },
2424 { 0x04, 0xa000 },
2425 { 0x03, 0xdf01 },
2426 { 0x02, 0xdf20 },
2427 { 0x01, 0xff95 },
2428 { 0x00, 0xba00 },
2429 { 0x04, 0xa800 },
2430 { 0x04, 0xf000 },
2431 { 0x03, 0xdf01 },
2432 { 0x02, 0xdf20 },
2433 { 0x01, 0x101a },
2434 { 0x00, 0xa0ff },
2435 { 0x04, 0xf800 },
2436 { 0x04, 0x0000 },
2437 { 0x1f, 0x0000 },
2438
2439 { 0x1f, 0x0001 },
2440 { 0x0b, 0x8480 },
2441 { 0x1f, 0x0000 },
2442
2443 { 0x1f, 0x0001 },
2444 { 0x18, 0x67c7 },
2445 { 0x04, 0x2000 },
2446 { 0x03, 0x002f },
2447 { 0x02, 0x4360 },
2448 { 0x01, 0x0109 },
2449 { 0x00, 0x3022 },
2450 { 0x04, 0x2800 },
2451 { 0x1f, 0x0000 },
2452
2453 { 0x1f, 0x0001 },
2454 { 0x17, 0x0cc0 },
2455 { 0x1f, 0x0000 }
2456 };
2457
françois romieu4da19632011-01-03 15:07:55 +00002458 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002459}
2460
françois romieu4da19632011-01-03 15:07:55 +00002461static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002462{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002463 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002464 { 0x10, 0xf41b },
2465 { 0x1f, 0x0000 }
2466 };
2467
françois romieu4da19632011-01-03 15:07:55 +00002468 rtl_writephy(tp, 0x1f, 0x0001);
2469 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002470
françois romieu4da19632011-01-03 15:07:55 +00002471 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002472}
2473
françois romieu4da19632011-01-03 15:07:55 +00002474static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002475{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002476 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002477 { 0x1f, 0x0001 },
2478 { 0x10, 0xf41b },
2479 { 0x1f, 0x0000 }
2480 };
2481
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 rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002486{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002487 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002488 { 0x1f, 0x0000 },
2489 { 0x1d, 0x0f00 },
2490 { 0x1f, 0x0002 },
2491 { 0x0c, 0x1ec8 },
2492 { 0x1f, 0x0000 }
2493 };
2494
françois romieu4da19632011-01-03 15:07:55 +00002495 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002496}
2497
françois romieu4da19632011-01-03 15:07:55 +00002498static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002499{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002500 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002501 { 0x1f, 0x0001 },
2502 { 0x1d, 0x3d98 },
2503 { 0x1f, 0x0000 }
2504 };
2505
françois romieu4da19632011-01-03 15:07:55 +00002506 rtl_writephy(tp, 0x1f, 0x0000);
2507 rtl_patchphy(tp, 0x14, 1 << 5);
2508 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002509
françois romieu4da19632011-01-03 15:07:55 +00002510 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002511}
2512
françois romieu4da19632011-01-03 15:07:55 +00002513static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002514{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002515 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002516 { 0x1f, 0x0001 },
2517 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002518 { 0x1f, 0x0002 },
2519 { 0x00, 0x88d4 },
2520 { 0x01, 0x82b1 },
2521 { 0x03, 0x7002 },
2522 { 0x08, 0x9e30 },
2523 { 0x09, 0x01f0 },
2524 { 0x0a, 0x5500 },
2525 { 0x0c, 0x00c8 },
2526 { 0x1f, 0x0003 },
2527 { 0x12, 0xc096 },
2528 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002529 { 0x1f, 0x0000 },
2530 { 0x1f, 0x0000 },
2531 { 0x09, 0x2000 },
2532 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002533 };
2534
françois romieu4da19632011-01-03 15:07:55 +00002535 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002536
françois romieu4da19632011-01-03 15:07:55 +00002537 rtl_patchphy(tp, 0x14, 1 << 5);
2538 rtl_patchphy(tp, 0x0d, 1 << 5);
2539 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002540}
2541
françois romieu4da19632011-01-03 15:07:55 +00002542static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002543{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002544 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002545 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002546 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002547 { 0x03, 0x802f },
2548 { 0x02, 0x4f02 },
2549 { 0x01, 0x0409 },
2550 { 0x00, 0xf099 },
2551 { 0x04, 0x9800 },
2552 { 0x04, 0x9000 },
2553 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002554 { 0x1f, 0x0002 },
2555 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002556 { 0x06, 0x0761 },
2557 { 0x1f, 0x0003 },
2558 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002559 { 0x1f, 0x0000 }
2560 };
2561
françois romieu4da19632011-01-03 15:07:55 +00002562 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002563
françois romieu4da19632011-01-03 15:07:55 +00002564 rtl_patchphy(tp, 0x16, 1 << 0);
2565 rtl_patchphy(tp, 0x14, 1 << 5);
2566 rtl_patchphy(tp, 0x0d, 1 << 5);
2567 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002568}
2569
françois romieu4da19632011-01-03 15:07:55 +00002570static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002571{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002572 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002573 { 0x1f, 0x0001 },
2574 { 0x12, 0x2300 },
2575 { 0x1d, 0x3d98 },
2576 { 0x1f, 0x0002 },
2577 { 0x0c, 0x7eb8 },
2578 { 0x06, 0x5461 },
2579 { 0x1f, 0x0003 },
2580 { 0x16, 0x0f0a },
2581 { 0x1f, 0x0000 }
2582 };
2583
françois romieu4da19632011-01-03 15:07:55 +00002584 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002585
françois romieu4da19632011-01-03 15:07:55 +00002586 rtl_patchphy(tp, 0x16, 1 << 0);
2587 rtl_patchphy(tp, 0x14, 1 << 5);
2588 rtl_patchphy(tp, 0x0d, 1 << 5);
2589 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002590}
2591
françois romieu4da19632011-01-03 15:07:55 +00002592static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002593{
françois romieu4da19632011-01-03 15:07:55 +00002594 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002595}
2596
françois romieubca03d52011-01-03 15:07:31 +00002597static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002598{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002599 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002600 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002601 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002602 { 0x06, 0x4064 },
2603 { 0x07, 0x2863 },
2604 { 0x08, 0x059c },
2605 { 0x09, 0x26b4 },
2606 { 0x0a, 0x6a19 },
2607 { 0x0b, 0xdcc8 },
2608 { 0x10, 0xf06d },
2609 { 0x14, 0x7f68 },
2610 { 0x18, 0x7fd9 },
2611 { 0x1c, 0xf0ff },
2612 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002613 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002614 { 0x12, 0xf49f },
2615 { 0x13, 0x070b },
2616 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002617 { 0x14, 0x94c0 },
2618
2619 /*
2620 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002621 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002622 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002623 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002624 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002625 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002626 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002627 { 0x06, 0x5561 },
2628
2629 /*
2630 * Can not link to 1Gbps with bad cable
2631 * Decrease SNR threshold form 21.07dB to 19.04dB
2632 */
2633 { 0x1f, 0x0001 },
2634 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002635
2636 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002637 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002638 };
françois romieubca03d52011-01-03 15:07:31 +00002639 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002640
françois romieu4da19632011-01-03 15:07:55 +00002641 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002642
françois romieubca03d52011-01-03 15:07:31 +00002643 /*
2644 * Rx Error Issue
2645 * Fine Tune Switching regulator parameter
2646 */
françois romieu4da19632011-01-03 15:07:55 +00002647 rtl_writephy(tp, 0x1f, 0x0002);
2648 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2649 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002650
françois romieudaf9df62009-10-07 12:44:20 +00002651 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002652 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002653 { 0x1f, 0x0002 },
2654 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002655 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002656 { 0x05, 0x8330 },
2657 { 0x06, 0x669a },
2658 { 0x1f, 0x0002 }
2659 };
2660 int val;
2661
françois romieu4da19632011-01-03 15:07:55 +00002662 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002663
françois romieu4da19632011-01-03 15:07:55 +00002664 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002665
2666 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002667 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002668 0x0065, 0x0066, 0x0067, 0x0068,
2669 0x0069, 0x006a, 0x006b, 0x006c
2670 };
2671 int i;
2672
françois romieu4da19632011-01-03 15:07:55 +00002673 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002674
2675 val &= 0xff00;
2676 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002677 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002678 }
2679 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002680 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002681 { 0x1f, 0x0002 },
2682 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002683 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002684 { 0x05, 0x8330 },
2685 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002686 };
2687
françois romieu4da19632011-01-03 15:07:55 +00002688 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002689 }
2690
françois romieubca03d52011-01-03 15:07:31 +00002691 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002692 rtl_writephy(tp, 0x1f, 0x0002);
2693 rtl_patchphy(tp, 0x0d, 0x0300);
2694 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002695
françois romieubca03d52011-01-03 15:07:31 +00002696 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002697 rtl_writephy(tp, 0x1f, 0x0002);
2698 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2699 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002700
françois romieu4da19632011-01-03 15:07:55 +00002701 rtl_writephy(tp, 0x1f, 0x0005);
2702 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002703
2704 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002705
françois romieu4da19632011-01-03 15:07:55 +00002706 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002707}
2708
françois romieubca03d52011-01-03 15:07:31 +00002709static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002710{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002711 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002712 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002713 { 0x1f, 0x0001 },
2714 { 0x06, 0x4064 },
2715 { 0x07, 0x2863 },
2716 { 0x08, 0x059c },
2717 { 0x09, 0x26b4 },
2718 { 0x0a, 0x6a19 },
2719 { 0x0b, 0xdcc8 },
2720 { 0x10, 0xf06d },
2721 { 0x14, 0x7f68 },
2722 { 0x18, 0x7fd9 },
2723 { 0x1c, 0xf0ff },
2724 { 0x1d, 0x3d9c },
2725 { 0x1f, 0x0003 },
2726 { 0x12, 0xf49f },
2727 { 0x13, 0x070b },
2728 { 0x1a, 0x05ad },
2729 { 0x14, 0x94c0 },
2730
françois romieubca03d52011-01-03 15:07:31 +00002731 /*
2732 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002733 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002734 */
françois romieudaf9df62009-10-07 12:44:20 +00002735 { 0x1f, 0x0002 },
2736 { 0x06, 0x5561 },
2737 { 0x1f, 0x0005 },
2738 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002739 { 0x06, 0x5561 },
2740
2741 /*
2742 * Can not link to 1Gbps with bad cable
2743 * Decrease SNR threshold form 21.07dB to 19.04dB
2744 */
2745 { 0x1f, 0x0001 },
2746 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002747
2748 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002749 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002750 };
françois romieubca03d52011-01-03 15:07:31 +00002751 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002752
françois romieu4da19632011-01-03 15:07:55 +00002753 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002754
2755 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002756 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002757 { 0x1f, 0x0002 },
2758 { 0x05, 0x669a },
2759 { 0x1f, 0x0005 },
2760 { 0x05, 0x8330 },
2761 { 0x06, 0x669a },
2762
2763 { 0x1f, 0x0002 }
2764 };
2765 int val;
2766
françois romieu4da19632011-01-03 15:07:55 +00002767 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002768
françois romieu4da19632011-01-03 15:07:55 +00002769 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002770 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002771 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002772 0x0065, 0x0066, 0x0067, 0x0068,
2773 0x0069, 0x006a, 0x006b, 0x006c
2774 };
2775 int i;
2776
françois romieu4da19632011-01-03 15:07:55 +00002777 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002778
2779 val &= 0xff00;
2780 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002781 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002782 }
2783 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002784 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002785 { 0x1f, 0x0002 },
2786 { 0x05, 0x2642 },
2787 { 0x1f, 0x0005 },
2788 { 0x05, 0x8330 },
2789 { 0x06, 0x2642 }
2790 };
2791
françois romieu4da19632011-01-03 15:07:55 +00002792 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002793 }
2794
françois romieubca03d52011-01-03 15:07:31 +00002795 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002796 rtl_writephy(tp, 0x1f, 0x0002);
2797 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2798 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002799
françois romieubca03d52011-01-03 15:07:31 +00002800 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002801 rtl_writephy(tp, 0x1f, 0x0002);
2802 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002803
françois romieu4da19632011-01-03 15:07:55 +00002804 rtl_writephy(tp, 0x1f, 0x0005);
2805 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002806
2807 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002808
françois romieu4da19632011-01-03 15:07:55 +00002809 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002810}
2811
françois romieu4da19632011-01-03 15:07:55 +00002812static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002813{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002814 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002815 { 0x1f, 0x0002 },
2816 { 0x10, 0x0008 },
2817 { 0x0d, 0x006c },
2818
2819 { 0x1f, 0x0000 },
2820 { 0x0d, 0xf880 },
2821
2822 { 0x1f, 0x0001 },
2823 { 0x17, 0x0cc0 },
2824
2825 { 0x1f, 0x0001 },
2826 { 0x0b, 0xa4d8 },
2827 { 0x09, 0x281c },
2828 { 0x07, 0x2883 },
2829 { 0x0a, 0x6b35 },
2830 { 0x1d, 0x3da4 },
2831 { 0x1c, 0xeffd },
2832 { 0x14, 0x7f52 },
2833 { 0x18, 0x7fc6 },
2834 { 0x08, 0x0601 },
2835 { 0x06, 0x4063 },
2836 { 0x10, 0xf074 },
2837 { 0x1f, 0x0003 },
2838 { 0x13, 0x0789 },
2839 { 0x12, 0xf4bd },
2840 { 0x1a, 0x04fd },
2841 { 0x14, 0x84b0 },
2842 { 0x1f, 0x0000 },
2843 { 0x00, 0x9200 },
2844
2845 { 0x1f, 0x0005 },
2846 { 0x01, 0x0340 },
2847 { 0x1f, 0x0001 },
2848 { 0x04, 0x4000 },
2849 { 0x03, 0x1d21 },
2850 { 0x02, 0x0c32 },
2851 { 0x01, 0x0200 },
2852 { 0x00, 0x5554 },
2853 { 0x04, 0x4800 },
2854 { 0x04, 0x4000 },
2855 { 0x04, 0xf000 },
2856 { 0x03, 0xdf01 },
2857 { 0x02, 0xdf20 },
2858 { 0x01, 0x101a },
2859 { 0x00, 0xa0ff },
2860 { 0x04, 0xf800 },
2861 { 0x04, 0xf000 },
2862 { 0x1f, 0x0000 },
2863
2864 { 0x1f, 0x0007 },
2865 { 0x1e, 0x0023 },
2866 { 0x16, 0x0000 },
2867 { 0x1f, 0x0000 }
2868 };
2869
françois romieu4da19632011-01-03 15:07:55 +00002870 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002871}
2872
françois romieue6de30d2011-01-03 15:08:37 +00002873static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2874{
2875 static const struct phy_reg phy_reg_init[] = {
2876 { 0x1f, 0x0001 },
2877 { 0x17, 0x0cc0 },
2878
2879 { 0x1f, 0x0007 },
2880 { 0x1e, 0x002d },
2881 { 0x18, 0x0040 },
2882 { 0x1f, 0x0000 }
2883 };
2884
2885 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2886 rtl_patchphy(tp, 0x0d, 1 << 5);
2887}
2888
Hayes Wang70090422011-07-06 15:58:06 +08002889static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002890{
2891 static const struct phy_reg phy_reg_init[] = {
2892 /* Enable Delay cap */
2893 { 0x1f, 0x0005 },
2894 { 0x05, 0x8b80 },
2895 { 0x06, 0xc896 },
2896 { 0x1f, 0x0000 },
2897
2898 /* Channel estimation fine tune */
2899 { 0x1f, 0x0001 },
2900 { 0x0b, 0x6c20 },
2901 { 0x07, 0x2872 },
2902 { 0x1c, 0xefff },
2903 { 0x1f, 0x0003 },
2904 { 0x14, 0x6420 },
2905 { 0x1f, 0x0000 },
2906
2907 /* Update PFM & 10M TX idle timer */
2908 { 0x1f, 0x0007 },
2909 { 0x1e, 0x002f },
2910 { 0x15, 0x1919 },
2911 { 0x1f, 0x0000 },
2912
2913 { 0x1f, 0x0007 },
2914 { 0x1e, 0x00ac },
2915 { 0x18, 0x0006 },
2916 { 0x1f, 0x0000 }
2917 };
2918
Francois Romieu15ecd032011-04-27 13:52:22 -07002919 rtl_apply_firmware(tp);
2920
hayeswang01dc7fe2011-03-21 01:50:28 +00002921 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2922
2923 /* DCO enable for 10M IDLE Power */
2924 rtl_writephy(tp, 0x1f, 0x0007);
2925 rtl_writephy(tp, 0x1e, 0x0023);
2926 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2927 rtl_writephy(tp, 0x1f, 0x0000);
2928
2929 /* For impedance matching */
2930 rtl_writephy(tp, 0x1f, 0x0002);
2931 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002932 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002933
2934 /* PHY auto speed down */
2935 rtl_writephy(tp, 0x1f, 0x0007);
2936 rtl_writephy(tp, 0x1e, 0x002d);
2937 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2938 rtl_writephy(tp, 0x1f, 0x0000);
2939 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2940
2941 rtl_writephy(tp, 0x1f, 0x0005);
2942 rtl_writephy(tp, 0x05, 0x8b86);
2943 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2944 rtl_writephy(tp, 0x1f, 0x0000);
2945
2946 rtl_writephy(tp, 0x1f, 0x0005);
2947 rtl_writephy(tp, 0x05, 0x8b85);
2948 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2949 rtl_writephy(tp, 0x1f, 0x0007);
2950 rtl_writephy(tp, 0x1e, 0x0020);
2951 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2952 rtl_writephy(tp, 0x1f, 0x0006);
2953 rtl_writephy(tp, 0x00, 0x5a00);
2954 rtl_writephy(tp, 0x1f, 0x0000);
2955 rtl_writephy(tp, 0x0d, 0x0007);
2956 rtl_writephy(tp, 0x0e, 0x003c);
2957 rtl_writephy(tp, 0x0d, 0x4007);
2958 rtl_writephy(tp, 0x0e, 0x0000);
2959 rtl_writephy(tp, 0x0d, 0x0000);
2960}
2961
Hayes Wang70090422011-07-06 15:58:06 +08002962static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2963{
2964 static const struct phy_reg phy_reg_init[] = {
2965 /* Enable Delay cap */
2966 { 0x1f, 0x0004 },
2967 { 0x1f, 0x0007 },
2968 { 0x1e, 0x00ac },
2969 { 0x18, 0x0006 },
2970 { 0x1f, 0x0002 },
2971 { 0x1f, 0x0000 },
2972 { 0x1f, 0x0000 },
2973
2974 /* Channel estimation fine tune */
2975 { 0x1f, 0x0003 },
2976 { 0x09, 0xa20f },
2977 { 0x1f, 0x0000 },
2978 { 0x1f, 0x0000 },
2979
2980 /* Green Setting */
2981 { 0x1f, 0x0005 },
2982 { 0x05, 0x8b5b },
2983 { 0x06, 0x9222 },
2984 { 0x05, 0x8b6d },
2985 { 0x06, 0x8000 },
2986 { 0x05, 0x8b76 },
2987 { 0x06, 0x8000 },
2988 { 0x1f, 0x0000 }
2989 };
2990
2991 rtl_apply_firmware(tp);
2992
2993 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2994
2995 /* For 4-corner performance improve */
2996 rtl_writephy(tp, 0x1f, 0x0005);
2997 rtl_writephy(tp, 0x05, 0x8b80);
2998 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2999 rtl_writephy(tp, 0x1f, 0x0000);
3000
3001 /* PHY auto speed down */
3002 rtl_writephy(tp, 0x1f, 0x0004);
3003 rtl_writephy(tp, 0x1f, 0x0007);
3004 rtl_writephy(tp, 0x1e, 0x002d);
3005 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3006 rtl_writephy(tp, 0x1f, 0x0002);
3007 rtl_writephy(tp, 0x1f, 0x0000);
3008 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3009
3010 /* improve 10M EEE waveform */
3011 rtl_writephy(tp, 0x1f, 0x0005);
3012 rtl_writephy(tp, 0x05, 0x8b86);
3013 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3014 rtl_writephy(tp, 0x1f, 0x0000);
3015
3016 /* Improve 2-pair detection performance */
3017 rtl_writephy(tp, 0x1f, 0x0005);
3018 rtl_writephy(tp, 0x05, 0x8b85);
3019 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3020 rtl_writephy(tp, 0x1f, 0x0000);
3021
3022 /* EEE setting */
3023 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
3024 ERIAR_EXGMAC);
3025 rtl_writephy(tp, 0x1f, 0x0005);
3026 rtl_writephy(tp, 0x05, 0x8b85);
3027 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3028 rtl_writephy(tp, 0x1f, 0x0004);
3029 rtl_writephy(tp, 0x1f, 0x0007);
3030 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04003031 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003032 rtl_writephy(tp, 0x1f, 0x0002);
3033 rtl_writephy(tp, 0x1f, 0x0000);
3034 rtl_writephy(tp, 0x0d, 0x0007);
3035 rtl_writephy(tp, 0x0e, 0x003c);
3036 rtl_writephy(tp, 0x0d, 0x4007);
3037 rtl_writephy(tp, 0x0e, 0x0000);
3038 rtl_writephy(tp, 0x0d, 0x0000);
3039
3040 /* Green feature */
3041 rtl_writephy(tp, 0x1f, 0x0003);
3042 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3043 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3044 rtl_writephy(tp, 0x1f, 0x0000);
3045}
3046
Hayes Wang5f886e02012-03-30 14:33:03 +08003047static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3048{
3049 /* For 4-corner performance improve */
3050 rtl_writephy(tp, 0x1f, 0x0005);
3051 rtl_writephy(tp, 0x05, 0x8b80);
3052 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3053 rtl_writephy(tp, 0x1f, 0x0000);
3054
3055 /* PHY auto speed down */
3056 rtl_writephy(tp, 0x1f, 0x0007);
3057 rtl_writephy(tp, 0x1e, 0x002d);
3058 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3059 rtl_writephy(tp, 0x1f, 0x0000);
3060 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3061
3062 /* Improve 10M EEE waveform */
3063 rtl_writephy(tp, 0x1f, 0x0005);
3064 rtl_writephy(tp, 0x05, 0x8b86);
3065 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3066 rtl_writephy(tp, 0x1f, 0x0000);
3067}
3068
Hayes Wangc2218922011-09-06 16:55:18 +08003069static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3070{
3071 static const struct phy_reg phy_reg_init[] = {
3072 /* Channel estimation fine tune */
3073 { 0x1f, 0x0003 },
3074 { 0x09, 0xa20f },
3075 { 0x1f, 0x0000 },
3076
3077 /* Modify green table for giga & fnet */
3078 { 0x1f, 0x0005 },
3079 { 0x05, 0x8b55 },
3080 { 0x06, 0x0000 },
3081 { 0x05, 0x8b5e },
3082 { 0x06, 0x0000 },
3083 { 0x05, 0x8b67 },
3084 { 0x06, 0x0000 },
3085 { 0x05, 0x8b70 },
3086 { 0x06, 0x0000 },
3087 { 0x1f, 0x0000 },
3088 { 0x1f, 0x0007 },
3089 { 0x1e, 0x0078 },
3090 { 0x17, 0x0000 },
3091 { 0x19, 0x00fb },
3092 { 0x1f, 0x0000 },
3093
3094 /* Modify green table for 10M */
3095 { 0x1f, 0x0005 },
3096 { 0x05, 0x8b79 },
3097 { 0x06, 0xaa00 },
3098 { 0x1f, 0x0000 },
3099
3100 /* Disable hiimpedance detection (RTCT) */
3101 { 0x1f, 0x0003 },
3102 { 0x01, 0x328a },
3103 { 0x1f, 0x0000 }
3104 };
3105
3106 rtl_apply_firmware(tp);
3107
3108 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3109
Hayes Wang5f886e02012-03-30 14:33:03 +08003110 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003111
3112 /* Improve 2-pair detection performance */
3113 rtl_writephy(tp, 0x1f, 0x0005);
3114 rtl_writephy(tp, 0x05, 0x8b85);
3115 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3116 rtl_writephy(tp, 0x1f, 0x0000);
3117}
3118
3119static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3120{
3121 rtl_apply_firmware(tp);
3122
Hayes Wang5f886e02012-03-30 14:33:03 +08003123 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003124}
3125
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003126static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3127{
3128 void __iomem *ioaddr = tp->mmio_addr;
3129 static const struct phy_reg phy_reg_init[] = {
3130 /* Channel estimation fine tune */
3131 { 0x1f, 0x0003 },
3132 { 0x09, 0xa20f },
3133 { 0x1f, 0x0000 },
3134
3135 /* Modify green table for giga & fnet */
3136 { 0x1f, 0x0005 },
3137 { 0x05, 0x8b55 },
3138 { 0x06, 0x0000 },
3139 { 0x05, 0x8b5e },
3140 { 0x06, 0x0000 },
3141 { 0x05, 0x8b67 },
3142 { 0x06, 0x0000 },
3143 { 0x05, 0x8b70 },
3144 { 0x06, 0x0000 },
3145 { 0x1f, 0x0000 },
3146 { 0x1f, 0x0007 },
3147 { 0x1e, 0x0078 },
3148 { 0x17, 0x0000 },
3149 { 0x19, 0x00aa },
3150 { 0x1f, 0x0000 },
3151
3152 /* Modify green table for 10M */
3153 { 0x1f, 0x0005 },
3154 { 0x05, 0x8b79 },
3155 { 0x06, 0xaa00 },
3156 { 0x1f, 0x0000 },
3157
3158 /* Disable hiimpedance detection (RTCT) */
3159 { 0x1f, 0x0003 },
3160 { 0x01, 0x328a },
3161 { 0x1f, 0x0000 }
3162 };
3163
3164
3165 rtl_apply_firmware(tp);
3166
3167 rtl8168f_hw_phy_config(tp);
3168
3169 /* Improve 2-pair detection performance */
3170 rtl_writephy(tp, 0x1f, 0x0005);
3171 rtl_writephy(tp, 0x05, 0x8b85);
3172 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3173 rtl_writephy(tp, 0x1f, 0x0000);
3174
3175 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3176
3177 /* Modify green table for giga */
3178 rtl_writephy(tp, 0x1f, 0x0005);
3179 rtl_writephy(tp, 0x05, 0x8b54);
3180 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3181 rtl_writephy(tp, 0x05, 0x8b5d);
3182 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3183 rtl_writephy(tp, 0x05, 0x8a7c);
3184 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3185 rtl_writephy(tp, 0x05, 0x8a7f);
3186 rtl_w1w0_phy(tp, 0x06, 0x0100, 0x0000);
3187 rtl_writephy(tp, 0x05, 0x8a82);
3188 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3189 rtl_writephy(tp, 0x05, 0x8a85);
3190 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3191 rtl_writephy(tp, 0x05, 0x8a88);
3192 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3193 rtl_writephy(tp, 0x1f, 0x0000);
3194
3195 /* uc same-seed solution */
3196 rtl_writephy(tp, 0x1f, 0x0005);
3197 rtl_writephy(tp, 0x05, 0x8b85);
3198 rtl_w1w0_phy(tp, 0x06, 0x8000, 0x0000);
3199 rtl_writephy(tp, 0x1f, 0x0000);
3200
3201 /* eee setting */
3202 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
3203 rtl_writephy(tp, 0x1f, 0x0005);
3204 rtl_writephy(tp, 0x05, 0x8b85);
3205 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3206 rtl_writephy(tp, 0x1f, 0x0004);
3207 rtl_writephy(tp, 0x1f, 0x0007);
3208 rtl_writephy(tp, 0x1e, 0x0020);
3209 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
3210 rtl_writephy(tp, 0x1f, 0x0000);
3211 rtl_writephy(tp, 0x0d, 0x0007);
3212 rtl_writephy(tp, 0x0e, 0x003c);
3213 rtl_writephy(tp, 0x0d, 0x4007);
3214 rtl_writephy(tp, 0x0e, 0x0000);
3215 rtl_writephy(tp, 0x0d, 0x0000);
3216
3217 /* Green feature */
3218 rtl_writephy(tp, 0x1f, 0x0003);
3219 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3220 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3221 rtl_writephy(tp, 0x1f, 0x0000);
3222}
3223
françois romieu4da19632011-01-03 15:07:55 +00003224static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003225{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003226 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003227 { 0x1f, 0x0003 },
3228 { 0x08, 0x441d },
3229 { 0x01, 0x9100 },
3230 { 0x1f, 0x0000 }
3231 };
3232
françois romieu4da19632011-01-03 15:07:55 +00003233 rtl_writephy(tp, 0x1f, 0x0000);
3234 rtl_patchphy(tp, 0x11, 1 << 12);
3235 rtl_patchphy(tp, 0x19, 1 << 13);
3236 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003237
françois romieu4da19632011-01-03 15:07:55 +00003238 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003239}
3240
Hayes Wang5a5e4442011-02-22 17:26:21 +08003241static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3242{
3243 static const struct phy_reg phy_reg_init[] = {
3244 { 0x1f, 0x0005 },
3245 { 0x1a, 0x0000 },
3246 { 0x1f, 0x0000 },
3247
3248 { 0x1f, 0x0004 },
3249 { 0x1c, 0x0000 },
3250 { 0x1f, 0x0000 },
3251
3252 { 0x1f, 0x0001 },
3253 { 0x15, 0x7701 },
3254 { 0x1f, 0x0000 }
3255 };
3256
3257 /* Disable ALDPS before ram code */
3258 rtl_writephy(tp, 0x1f, 0x0000);
3259 rtl_writephy(tp, 0x18, 0x0310);
3260 msleep(100);
3261
François Romieu953a12c2011-04-24 17:38:48 +02003262 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003263
3264 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3265}
3266
Hayes Wang7e18dca2012-03-30 14:33:02 +08003267static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3268{
3269 void __iomem *ioaddr = tp->mmio_addr;
3270
3271 /* Disable ALDPS before setting firmware */
3272 rtl_writephy(tp, 0x1f, 0x0000);
3273 rtl_writephy(tp, 0x18, 0x0310);
3274 msleep(20);
3275
3276 rtl_apply_firmware(tp);
3277
3278 /* EEE setting */
3279 rtl_eri_write(ioaddr, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3280 rtl_writephy(tp, 0x1f, 0x0004);
3281 rtl_writephy(tp, 0x10, 0x401f);
3282 rtl_writephy(tp, 0x19, 0x7030);
3283 rtl_writephy(tp, 0x1f, 0x0000);
3284}
3285
Hayes Wang5598bfe2012-07-02 17:23:21 +08003286static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3287{
3288 void __iomem *ioaddr = tp->mmio_addr;
3289
3290 static const struct phy_reg phy_reg_init[] = {
3291 { 0x1f, 0x0004 },
3292 { 0x10, 0xc07f },
3293 { 0x19, 0x7030 },
3294 { 0x1f, 0x0000 }
3295 };
3296
3297 /* Disable ALDPS before ram code */
3298 rtl_writephy(tp, 0x1f, 0x0000);
3299 rtl_writephy(tp, 0x18, 0x0310);
3300 msleep(100);
3301
3302 rtl_apply_firmware(tp);
3303
3304 rtl_eri_write(ioaddr, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3305 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3306
3307 rtl_eri_write(ioaddr, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3308}
3309
Francois Romieu5615d9f2007-08-17 17:50:46 +02003310static void rtl_hw_phy_config(struct net_device *dev)
3311{
3312 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003313
3314 rtl8169_print_mac_version(tp);
3315
3316 switch (tp->mac_version) {
3317 case RTL_GIGA_MAC_VER_01:
3318 break;
3319 case RTL_GIGA_MAC_VER_02:
3320 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003321 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003322 break;
3323 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003324 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003325 break;
françois romieu2e9558562009-08-10 19:44:19 +00003326 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003327 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003328 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003329 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003330 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003331 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003332 case RTL_GIGA_MAC_VER_07:
3333 case RTL_GIGA_MAC_VER_08:
3334 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003335 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003336 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003337 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003338 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003339 break;
3340 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003341 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003342 break;
3343 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003344 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003345 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003346 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003347 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003348 break;
3349 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003350 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003351 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003352 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003353 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003354 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003355 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003356 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003357 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003358 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003359 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003360 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003361 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003362 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003363 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003364 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003365 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003366 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003367 break;
3368 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003369 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003370 break;
3371 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003372 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003373 break;
françois romieue6de30d2011-01-03 15:08:37 +00003374 case RTL_GIGA_MAC_VER_28:
3375 rtl8168d_4_hw_phy_config(tp);
3376 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003377 case RTL_GIGA_MAC_VER_29:
3378 case RTL_GIGA_MAC_VER_30:
3379 rtl8105e_hw_phy_config(tp);
3380 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003381 case RTL_GIGA_MAC_VER_31:
3382 /* None. */
3383 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003384 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003385 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003386 rtl8168e_1_hw_phy_config(tp);
3387 break;
3388 case RTL_GIGA_MAC_VER_34:
3389 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003390 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003391 case RTL_GIGA_MAC_VER_35:
3392 rtl8168f_1_hw_phy_config(tp);
3393 break;
3394 case RTL_GIGA_MAC_VER_36:
3395 rtl8168f_2_hw_phy_config(tp);
3396 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003397
Hayes Wang7e18dca2012-03-30 14:33:02 +08003398 case RTL_GIGA_MAC_VER_37:
3399 rtl8402_hw_phy_config(tp);
3400 break;
3401
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003402 case RTL_GIGA_MAC_VER_38:
3403 rtl8411_hw_phy_config(tp);
3404 break;
3405
Hayes Wang5598bfe2012-07-02 17:23:21 +08003406 case RTL_GIGA_MAC_VER_39:
3407 rtl8106e_hw_phy_config(tp);
3408 break;
3409
Francois Romieu5615d9f2007-08-17 17:50:46 +02003410 default:
3411 break;
3412 }
3413}
3414
Francois Romieuda78dbf2012-01-26 14:18:23 +01003415static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 struct timer_list *timer = &tp->timer;
3418 void __iomem *ioaddr = tp->mmio_addr;
3419 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3420
Francois Romieubcf0bf92006-07-26 23:14:13 +02003421 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422
françois romieu4da19632011-01-03 15:07:55 +00003423 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003424 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 * A busy loop could burn quite a few cycles on nowadays CPU.
3426 * Let's delay the execution of the timer for a few ticks.
3427 */
3428 timeout = HZ/10;
3429 goto out_mod_timer;
3430 }
3431
3432 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01003433 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
Francois Romieuda78dbf2012-01-26 14:18:23 +01003435 netif_warn(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
françois romieu4da19632011-01-03 15:07:55 +00003437 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
3439out_mod_timer:
3440 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003441}
3442
3443static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3444{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003445 if (!test_and_set_bit(flag, tp->wk.flags))
3446 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003447}
3448
3449static void rtl8169_phy_timer(unsigned long __opaque)
3450{
3451 struct net_device *dev = (struct net_device *)__opaque;
3452 struct rtl8169_private *tp = netdev_priv(dev);
3453
Francois Romieu98ddf982012-01-31 10:47:34 +01003454 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455}
3456
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3458 void __iomem *ioaddr)
3459{
3460 iounmap(ioaddr);
3461 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003462 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 pci_disable_device(pdev);
3464 free_netdev(dev);
3465}
3466
Francois Romieubf793292006-11-01 00:53:05 +01003467static void rtl8169_phy_reset(struct net_device *dev,
3468 struct rtl8169_private *tp)
3469{
Francois Romieu07d3f512007-02-21 22:40:46 +01003470 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003471
françois romieu4da19632011-01-03 15:07:55 +00003472 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003473 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003474 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003475 return;
3476 msleep(1);
3477 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003478 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003479}
3480
David S. Miller8decf862011-09-22 03:23:13 -04003481static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3482{
3483 void __iomem *ioaddr = tp->mmio_addr;
3484
3485 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3486 (RTL_R8(PHYstatus) & TBI_Enable);
3487}
3488
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003489static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003491 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003492
Francois Romieu5615d9f2007-08-17 17:50:46 +02003493 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003494
Marcus Sundberg773328942008-07-10 21:28:08 +02003495 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3496 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3497 RTL_W8(0x82, 0x01);
3498 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003499
Francois Romieu6dccd162007-02-13 23:38:05 +01003500 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3501
3502 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3503 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003504
Francois Romieubcf0bf92006-07-26 23:14:13 +02003505 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003506 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3507 RTL_W8(0x82, 0x01);
3508 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003509 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003510 }
3511
Francois Romieubf793292006-11-01 00:53:05 +01003512 rtl8169_phy_reset(dev, tp);
3513
Oliver Neukum54405cd2011-01-06 21:55:13 +01003514 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003515 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3516 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3517 (tp->mii.supports_gmii ?
3518 ADVERTISED_1000baseT_Half |
3519 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003520
David S. Miller8decf862011-09-22 03:23:13 -04003521 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003522 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003523}
3524
Francois Romieu773d2022007-01-31 23:47:43 +01003525static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3526{
3527 void __iomem *ioaddr = tp->mmio_addr;
3528 u32 high;
3529 u32 low;
3530
3531 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3532 high = addr[4] | (addr[5] << 8);
3533
Francois Romieuda78dbf2012-01-26 14:18:23 +01003534 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003535
3536 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003537
Francois Romieu773d2022007-01-31 23:47:43 +01003538 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00003539 RTL_R32(MAC4);
3540
Francois Romieu78f1cd02010-03-27 19:35:46 -07003541 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00003542 RTL_R32(MAC0);
3543
françois romieuc28aa382011-08-02 03:53:43 +00003544 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3545 const struct exgmac_reg e[] = {
3546 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3547 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3548 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3549 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3550 low >> 16 },
3551 };
3552
3553 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3554 }
3555
Francois Romieu773d2022007-01-31 23:47:43 +01003556 RTL_W8(Cfg9346, Cfg9346_Lock);
3557
Francois Romieuda78dbf2012-01-26 14:18:23 +01003558 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003559}
3560
3561static int rtl_set_mac_address(struct net_device *dev, void *p)
3562{
3563 struct rtl8169_private *tp = netdev_priv(dev);
3564 struct sockaddr *addr = p;
3565
3566 if (!is_valid_ether_addr(addr->sa_data))
3567 return -EADDRNOTAVAIL;
3568
3569 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3570
3571 rtl_rar_set(tp, dev->dev_addr);
3572
3573 return 0;
3574}
3575
Francois Romieu5f787a12006-08-17 13:02:36 +02003576static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3577{
3578 struct rtl8169_private *tp = netdev_priv(dev);
3579 struct mii_ioctl_data *data = if_mii(ifr);
3580
Francois Romieu8b4ab282008-11-19 22:05:25 -08003581 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3582}
Francois Romieu5f787a12006-08-17 13:02:36 +02003583
Francois Romieucecb5fd2011-04-01 10:21:07 +02003584static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3585 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003586{
Francois Romieu5f787a12006-08-17 13:02:36 +02003587 switch (cmd) {
3588 case SIOCGMIIPHY:
3589 data->phy_id = 32; /* Internal PHY */
3590 return 0;
3591
3592 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003593 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003594 return 0;
3595
3596 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003597 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003598 return 0;
3599 }
3600 return -EOPNOTSUPP;
3601}
3602
Francois Romieu8b4ab282008-11-19 22:05:25 -08003603static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3604{
3605 return -EOPNOTSUPP;
3606}
3607
Francois Romieufbac58f2007-10-04 22:51:38 +02003608static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3609{
3610 if (tp->features & RTL_FEATURE_MSI) {
3611 pci_disable_msi(pdev);
3612 tp->features &= ~RTL_FEATURE_MSI;
3613 }
3614}
3615
françois romieuc0e45c12011-01-03 15:08:04 +00003616static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3617{
3618 struct mdio_ops *ops = &tp->mdio_ops;
3619
3620 switch (tp->mac_version) {
3621 case RTL_GIGA_MAC_VER_27:
3622 ops->write = r8168dp_1_mdio_write;
3623 ops->read = r8168dp_1_mdio_read;
3624 break;
françois romieue6de30d2011-01-03 15:08:37 +00003625 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003626 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003627 ops->write = r8168dp_2_mdio_write;
3628 ops->read = r8168dp_2_mdio_read;
3629 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003630 default:
3631 ops->write = r8169_mdio_write;
3632 ops->read = r8169_mdio_read;
3633 break;
3634 }
3635}
3636
David S. Miller1805b2f2011-10-24 18:18:09 -04003637static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3638{
3639 void __iomem *ioaddr = tp->mmio_addr;
3640
3641 switch (tp->mac_version) {
3642 case RTL_GIGA_MAC_VER_29:
3643 case RTL_GIGA_MAC_VER_30:
3644 case RTL_GIGA_MAC_VER_32:
3645 case RTL_GIGA_MAC_VER_33:
3646 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08003647 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003648 case RTL_GIGA_MAC_VER_38:
Hayes Wang5598bfe2012-07-02 17:23:21 +08003649 case RTL_GIGA_MAC_VER_39:
David S. Miller1805b2f2011-10-24 18:18:09 -04003650 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3651 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3652 break;
3653 default:
3654 break;
3655 }
3656}
3657
3658static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3659{
3660 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3661 return false;
3662
3663 rtl_writephy(tp, 0x1f, 0x0000);
3664 rtl_writephy(tp, MII_BMCR, 0x0000);
3665
3666 rtl_wol_suspend_quirk(tp);
3667
3668 return true;
3669}
3670
françois romieu065c27c2011-01-03 15:08:12 +00003671static void r810x_phy_power_down(struct rtl8169_private *tp)
3672{
3673 rtl_writephy(tp, 0x1f, 0x0000);
3674 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3675}
3676
3677static void r810x_phy_power_up(struct rtl8169_private *tp)
3678{
3679 rtl_writephy(tp, 0x1f, 0x0000);
3680 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3681}
3682
3683static void r810x_pll_power_down(struct rtl8169_private *tp)
3684{
Hayes Wang00042992012-03-30 14:33:00 +08003685 void __iomem *ioaddr = tp->mmio_addr;
3686
David S. Miller1805b2f2011-10-24 18:18:09 -04003687 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003688 return;
françois romieu065c27c2011-01-03 15:08:12 +00003689
3690 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003691
3692 switch (tp->mac_version) {
3693 case RTL_GIGA_MAC_VER_07:
3694 case RTL_GIGA_MAC_VER_08:
3695 case RTL_GIGA_MAC_VER_09:
3696 case RTL_GIGA_MAC_VER_10:
3697 case RTL_GIGA_MAC_VER_13:
3698 case RTL_GIGA_MAC_VER_16:
3699 break;
3700 default:
3701 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3702 break;
3703 }
françois romieu065c27c2011-01-03 15:08:12 +00003704}
3705
3706static void r810x_pll_power_up(struct rtl8169_private *tp)
3707{
Hayes Wang00042992012-03-30 14:33:00 +08003708 void __iomem *ioaddr = tp->mmio_addr;
3709
françois romieu065c27c2011-01-03 15:08:12 +00003710 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003711
3712 switch (tp->mac_version) {
3713 case RTL_GIGA_MAC_VER_07:
3714 case RTL_GIGA_MAC_VER_08:
3715 case RTL_GIGA_MAC_VER_09:
3716 case RTL_GIGA_MAC_VER_10:
3717 case RTL_GIGA_MAC_VER_13:
3718 case RTL_GIGA_MAC_VER_16:
3719 break;
3720 default:
3721 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3722 break;
3723 }
françois romieu065c27c2011-01-03 15:08:12 +00003724}
3725
3726static void r8168_phy_power_up(struct rtl8169_private *tp)
3727{
3728 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003729 switch (tp->mac_version) {
3730 case RTL_GIGA_MAC_VER_11:
3731 case RTL_GIGA_MAC_VER_12:
3732 case RTL_GIGA_MAC_VER_17:
3733 case RTL_GIGA_MAC_VER_18:
3734 case RTL_GIGA_MAC_VER_19:
3735 case RTL_GIGA_MAC_VER_20:
3736 case RTL_GIGA_MAC_VER_21:
3737 case RTL_GIGA_MAC_VER_22:
3738 case RTL_GIGA_MAC_VER_23:
3739 case RTL_GIGA_MAC_VER_24:
3740 case RTL_GIGA_MAC_VER_25:
3741 case RTL_GIGA_MAC_VER_26:
3742 case RTL_GIGA_MAC_VER_27:
3743 case RTL_GIGA_MAC_VER_28:
3744 case RTL_GIGA_MAC_VER_31:
3745 rtl_writephy(tp, 0x0e, 0x0000);
3746 break;
3747 default:
3748 break;
3749 }
françois romieu065c27c2011-01-03 15:08:12 +00003750 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3751}
3752
3753static void r8168_phy_power_down(struct rtl8169_private *tp)
3754{
3755 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003756 switch (tp->mac_version) {
3757 case RTL_GIGA_MAC_VER_32:
3758 case RTL_GIGA_MAC_VER_33:
3759 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3760 break;
3761
3762 case RTL_GIGA_MAC_VER_11:
3763 case RTL_GIGA_MAC_VER_12:
3764 case RTL_GIGA_MAC_VER_17:
3765 case RTL_GIGA_MAC_VER_18:
3766 case RTL_GIGA_MAC_VER_19:
3767 case RTL_GIGA_MAC_VER_20:
3768 case RTL_GIGA_MAC_VER_21:
3769 case RTL_GIGA_MAC_VER_22:
3770 case RTL_GIGA_MAC_VER_23:
3771 case RTL_GIGA_MAC_VER_24:
3772 case RTL_GIGA_MAC_VER_25:
3773 case RTL_GIGA_MAC_VER_26:
3774 case RTL_GIGA_MAC_VER_27:
3775 case RTL_GIGA_MAC_VER_28:
3776 case RTL_GIGA_MAC_VER_31:
3777 rtl_writephy(tp, 0x0e, 0x0200);
3778 default:
3779 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3780 break;
3781 }
françois romieu065c27c2011-01-03 15:08:12 +00003782}
3783
3784static void r8168_pll_power_down(struct rtl8169_private *tp)
3785{
3786 void __iomem *ioaddr = tp->mmio_addr;
3787
Francois Romieucecb5fd2011-04-01 10:21:07 +02003788 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3789 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3790 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003791 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003792 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003793 }
françois romieu065c27c2011-01-03 15:08:12 +00003794
Francois Romieucecb5fd2011-04-01 10:21:07 +02003795 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3796 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003797 (RTL_R16(CPlusCmd) & ASF)) {
3798 return;
3799 }
3800
hayeswang01dc7fe2011-03-21 01:50:28 +00003801 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3802 tp->mac_version == RTL_GIGA_MAC_VER_33)
3803 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3804
David S. Miller1805b2f2011-10-24 18:18:09 -04003805 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003806 return;
françois romieu065c27c2011-01-03 15:08:12 +00003807
3808 r8168_phy_power_down(tp);
3809
3810 switch (tp->mac_version) {
3811 case RTL_GIGA_MAC_VER_25:
3812 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003813 case RTL_GIGA_MAC_VER_27:
3814 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003815 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003816 case RTL_GIGA_MAC_VER_32:
3817 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003818 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3819 break;
3820 }
3821}
3822
3823static void r8168_pll_power_up(struct rtl8169_private *tp)
3824{
3825 void __iomem *ioaddr = tp->mmio_addr;
3826
françois romieu065c27c2011-01-03 15:08:12 +00003827 switch (tp->mac_version) {
3828 case RTL_GIGA_MAC_VER_25:
3829 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003830 case RTL_GIGA_MAC_VER_27:
3831 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003832 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003833 case RTL_GIGA_MAC_VER_32:
3834 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003835 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3836 break;
3837 }
3838
3839 r8168_phy_power_up(tp);
3840}
3841
Francois Romieud58d46b2011-05-03 16:38:29 +02003842static void rtl_generic_op(struct rtl8169_private *tp,
3843 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00003844{
3845 if (op)
3846 op(tp);
3847}
3848
3849static void rtl_pll_power_down(struct rtl8169_private *tp)
3850{
Francois Romieud58d46b2011-05-03 16:38:29 +02003851 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00003852}
3853
3854static void rtl_pll_power_up(struct rtl8169_private *tp)
3855{
Francois Romieud58d46b2011-05-03 16:38:29 +02003856 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00003857}
3858
3859static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3860{
3861 struct pll_power_ops *ops = &tp->pll_power_ops;
3862
3863 switch (tp->mac_version) {
3864 case RTL_GIGA_MAC_VER_07:
3865 case RTL_GIGA_MAC_VER_08:
3866 case RTL_GIGA_MAC_VER_09:
3867 case RTL_GIGA_MAC_VER_10:
3868 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003869 case RTL_GIGA_MAC_VER_29:
3870 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08003871 case RTL_GIGA_MAC_VER_37:
Hayes Wang5598bfe2012-07-02 17:23:21 +08003872 case RTL_GIGA_MAC_VER_39:
françois romieu065c27c2011-01-03 15:08:12 +00003873 ops->down = r810x_pll_power_down;
3874 ops->up = r810x_pll_power_up;
3875 break;
3876
3877 case RTL_GIGA_MAC_VER_11:
3878 case RTL_GIGA_MAC_VER_12:
3879 case RTL_GIGA_MAC_VER_17:
3880 case RTL_GIGA_MAC_VER_18:
3881 case RTL_GIGA_MAC_VER_19:
3882 case RTL_GIGA_MAC_VER_20:
3883 case RTL_GIGA_MAC_VER_21:
3884 case RTL_GIGA_MAC_VER_22:
3885 case RTL_GIGA_MAC_VER_23:
3886 case RTL_GIGA_MAC_VER_24:
3887 case RTL_GIGA_MAC_VER_25:
3888 case RTL_GIGA_MAC_VER_26:
3889 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003890 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003891 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003892 case RTL_GIGA_MAC_VER_32:
3893 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003894 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08003895 case RTL_GIGA_MAC_VER_35:
3896 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003897 case RTL_GIGA_MAC_VER_38:
françois romieu065c27c2011-01-03 15:08:12 +00003898 ops->down = r8168_pll_power_down;
3899 ops->up = r8168_pll_power_up;
3900 break;
3901
3902 default:
3903 ops->down = NULL;
3904 ops->up = NULL;
3905 break;
3906 }
3907}
3908
Hayes Wange542a222011-07-06 15:58:04 +08003909static void rtl_init_rxcfg(struct rtl8169_private *tp)
3910{
3911 void __iomem *ioaddr = tp->mmio_addr;
3912
3913 switch (tp->mac_version) {
3914 case RTL_GIGA_MAC_VER_01:
3915 case RTL_GIGA_MAC_VER_02:
3916 case RTL_GIGA_MAC_VER_03:
3917 case RTL_GIGA_MAC_VER_04:
3918 case RTL_GIGA_MAC_VER_05:
3919 case RTL_GIGA_MAC_VER_06:
3920 case RTL_GIGA_MAC_VER_10:
3921 case RTL_GIGA_MAC_VER_11:
3922 case RTL_GIGA_MAC_VER_12:
3923 case RTL_GIGA_MAC_VER_13:
3924 case RTL_GIGA_MAC_VER_14:
3925 case RTL_GIGA_MAC_VER_15:
3926 case RTL_GIGA_MAC_VER_16:
3927 case RTL_GIGA_MAC_VER_17:
3928 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3929 break;
3930 case RTL_GIGA_MAC_VER_18:
3931 case RTL_GIGA_MAC_VER_19:
3932 case RTL_GIGA_MAC_VER_20:
3933 case RTL_GIGA_MAC_VER_21:
3934 case RTL_GIGA_MAC_VER_22:
3935 case RTL_GIGA_MAC_VER_23:
3936 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00003937 case RTL_GIGA_MAC_VER_34:
Hayes Wange542a222011-07-06 15:58:04 +08003938 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3939 break;
3940 default:
3941 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3942 break;
3943 }
3944}
3945
Hayes Wang92fc43b2011-07-06 15:58:03 +08003946static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3947{
3948 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3949}
3950
Francois Romieud58d46b2011-05-03 16:38:29 +02003951static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3952{
françois romieu9c5028e2012-03-02 04:43:14 +00003953 void __iomem *ioaddr = tp->mmio_addr;
3954
3955 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003956 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00003957 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003958}
3959
3960static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3961{
françois romieu9c5028e2012-03-02 04:43:14 +00003962 void __iomem *ioaddr = tp->mmio_addr;
3963
3964 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003965 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00003966 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003967}
3968
3969static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3970{
3971 void __iomem *ioaddr = tp->mmio_addr;
3972
3973 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3974 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3975 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3976}
3977
3978static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3979{
3980 void __iomem *ioaddr = tp->mmio_addr;
3981
3982 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3983 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3984 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3985}
3986
3987static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3988{
3989 void __iomem *ioaddr = tp->mmio_addr;
3990
3991 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3992}
3993
3994static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3995{
3996 void __iomem *ioaddr = tp->mmio_addr;
3997
3998 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3999}
4000
4001static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4002{
4003 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02004004
4005 RTL_W8(MaxTxPacketSize, 0x3f);
4006 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4007 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01004008 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02004009}
4010
4011static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4012{
4013 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02004014
4015 RTL_W8(MaxTxPacketSize, 0x0c);
4016 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4017 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01004018 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02004019}
4020
4021static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4022{
4023 rtl_tx_performance_tweak(tp->pci_dev,
4024 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4025}
4026
4027static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4028{
4029 rtl_tx_performance_tweak(tp->pci_dev,
4030 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4031}
4032
4033static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4034{
4035 void __iomem *ioaddr = tp->mmio_addr;
4036
4037 r8168b_0_hw_jumbo_enable(tp);
4038
4039 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
4040}
4041
4042static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4043{
4044 void __iomem *ioaddr = tp->mmio_addr;
4045
4046 r8168b_0_hw_jumbo_disable(tp);
4047
4048 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4049}
4050
4051static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
4052{
4053 struct jumbo_ops *ops = &tp->jumbo_ops;
4054
4055 switch (tp->mac_version) {
4056 case RTL_GIGA_MAC_VER_11:
4057 ops->disable = r8168b_0_hw_jumbo_disable;
4058 ops->enable = r8168b_0_hw_jumbo_enable;
4059 break;
4060 case RTL_GIGA_MAC_VER_12:
4061 case RTL_GIGA_MAC_VER_17:
4062 ops->disable = r8168b_1_hw_jumbo_disable;
4063 ops->enable = r8168b_1_hw_jumbo_enable;
4064 break;
4065 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4066 case RTL_GIGA_MAC_VER_19:
4067 case RTL_GIGA_MAC_VER_20:
4068 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4069 case RTL_GIGA_MAC_VER_22:
4070 case RTL_GIGA_MAC_VER_23:
4071 case RTL_GIGA_MAC_VER_24:
4072 case RTL_GIGA_MAC_VER_25:
4073 case RTL_GIGA_MAC_VER_26:
4074 ops->disable = r8168c_hw_jumbo_disable;
4075 ops->enable = r8168c_hw_jumbo_enable;
4076 break;
4077 case RTL_GIGA_MAC_VER_27:
4078 case RTL_GIGA_MAC_VER_28:
4079 ops->disable = r8168dp_hw_jumbo_disable;
4080 ops->enable = r8168dp_hw_jumbo_enable;
4081 break;
4082 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4083 case RTL_GIGA_MAC_VER_32:
4084 case RTL_GIGA_MAC_VER_33:
4085 case RTL_GIGA_MAC_VER_34:
4086 ops->disable = r8168e_hw_jumbo_disable;
4087 ops->enable = r8168e_hw_jumbo_enable;
4088 break;
4089
4090 /*
4091 * No action needed for jumbo frames with 8169.
4092 * No jumbo for 810x at all.
4093 */
4094 default:
4095 ops->disable = NULL;
4096 ops->enable = NULL;
4097 break;
4098 }
4099}
4100
Francois Romieu6f43adc2011-04-29 15:05:51 +02004101static void rtl_hw_reset(struct rtl8169_private *tp)
4102{
4103 void __iomem *ioaddr = tp->mmio_addr;
4104 int i;
4105
4106 /* Soft reset the chip. */
4107 RTL_W8(ChipCmd, CmdReset);
4108
4109 /* Check that the chip has finished the reset. */
4110 for (i = 0; i < 100; i++) {
4111 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
4112 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004113 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004114 }
4115}
4116
Francois Romieub6ffd972011-06-17 17:00:05 +02004117static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4118{
4119 struct rtl_fw *rtl_fw;
4120 const char *name;
4121 int rc = -ENOMEM;
4122
4123 name = rtl_lookup_firmware_name(tp);
4124 if (!name)
4125 goto out_no_firmware;
4126
4127 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4128 if (!rtl_fw)
4129 goto err_warn;
4130
4131 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4132 if (rc < 0)
4133 goto err_free;
4134
Francois Romieufd112f22011-06-18 00:10:29 +02004135 rc = rtl_check_firmware(tp, rtl_fw);
4136 if (rc < 0)
4137 goto err_release_firmware;
4138
Francois Romieub6ffd972011-06-17 17:00:05 +02004139 tp->rtl_fw = rtl_fw;
4140out:
4141 return;
4142
Francois Romieufd112f22011-06-18 00:10:29 +02004143err_release_firmware:
4144 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004145err_free:
4146 kfree(rtl_fw);
4147err_warn:
4148 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4149 name, rc);
4150out_no_firmware:
4151 tp->rtl_fw = NULL;
4152 goto out;
4153}
4154
François Romieu953a12c2011-04-24 17:38:48 +02004155static void rtl_request_firmware(struct rtl8169_private *tp)
4156{
Francois Romieub6ffd972011-06-17 17:00:05 +02004157 if (IS_ERR(tp->rtl_fw))
4158 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004159}
4160
Hayes Wang92fc43b2011-07-06 15:58:03 +08004161static void rtl_rx_close(struct rtl8169_private *tp)
4162{
4163 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004164
Francois Romieu1687b562011-07-19 17:21:29 +02004165 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004166}
4167
françois romieue6de30d2011-01-03 15:08:37 +00004168static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169{
françois romieue6de30d2011-01-03 15:08:37 +00004170 void __iomem *ioaddr = tp->mmio_addr;
4171
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004173 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
Hayes Wang92fc43b2011-07-06 15:58:03 +08004175 rtl_rx_close(tp);
4176
Hayes Wang5d2e1952011-02-22 17:26:22 +08004177 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004178 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4179 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00004180 while (RTL_R8(TxPoll) & NPQ)
4181 udelay(20);
Hayes Wangc2218922011-09-06 16:55:18 +08004182 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4183 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
Hayes Wang7e18dca2012-03-30 14:33:02 +08004184 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004185 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
4186 tp->mac_version == RTL_GIGA_MAC_VER_38) {
David S. Miller8decf862011-09-22 03:23:13 -04004187 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08004188 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4189 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004190 } else {
4191 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4192 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004193 }
4194
Hayes Wang92fc43b2011-07-06 15:58:03 +08004195 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196}
4197
Francois Romieu7f796d832007-06-11 23:04:41 +02004198static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004199{
4200 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004201
4202 /* Set DMA burst size and Interframe Gap Time */
4203 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4204 (InterFrameGap << TxInterFrameGapShift));
4205}
4206
Francois Romieu07ce4062007-02-23 23:36:39 +01004207static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208{
4209 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
Francois Romieu07ce4062007-02-23 23:36:39 +01004211 tp->hw_start(dev);
4212
Francois Romieuda78dbf2012-01-26 14:18:23 +01004213 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004214}
4215
Francois Romieu7f796d832007-06-11 23:04:41 +02004216static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4217 void __iomem *ioaddr)
4218{
4219 /*
4220 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4221 * register to be written before TxDescAddrLow to work.
4222 * Switching from MMIO to I/O access fixes the issue as well.
4223 */
4224 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004225 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004226 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004227 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004228}
4229
4230static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4231{
4232 u16 cmd;
4233
4234 cmd = RTL_R16(CPlusCmd);
4235 RTL_W16(CPlusCmd, cmd);
4236 return cmd;
4237}
4238
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004239static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004240{
4241 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004242 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004243}
4244
Francois Romieu6dccd162007-02-13 23:38:05 +01004245static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4246{
Francois Romieu37441002011-06-17 22:58:54 +02004247 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004248 u32 mac_version;
4249 u32 clk;
4250 u32 val;
4251 } cfg2_info [] = {
4252 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4253 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4254 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4255 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004256 };
4257 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004258 unsigned int i;
4259 u32 clk;
4260
4261 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004262 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004263 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4264 RTL_W32(0x7c, p->val);
4265 break;
4266 }
4267 }
4268}
4269
Francois Romieue6b763e2012-03-08 09:35:39 +01004270static void rtl_set_rx_mode(struct net_device *dev)
4271{
4272 struct rtl8169_private *tp = netdev_priv(dev);
4273 void __iomem *ioaddr = tp->mmio_addr;
4274 u32 mc_filter[2]; /* Multicast hash filter */
4275 int rx_mode;
4276 u32 tmp = 0;
4277
4278 if (dev->flags & IFF_PROMISC) {
4279 /* Unconditionally log net taps. */
4280 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4281 rx_mode =
4282 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4283 AcceptAllPhys;
4284 mc_filter[1] = mc_filter[0] = 0xffffffff;
4285 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4286 (dev->flags & IFF_ALLMULTI)) {
4287 /* Too many to filter perfectly -- accept all multicasts. */
4288 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4289 mc_filter[1] = mc_filter[0] = 0xffffffff;
4290 } else {
4291 struct netdev_hw_addr *ha;
4292
4293 rx_mode = AcceptBroadcast | AcceptMyPhys;
4294 mc_filter[1] = mc_filter[0] = 0;
4295 netdev_for_each_mc_addr(ha, dev) {
4296 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4297 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4298 rx_mode |= AcceptMulticast;
4299 }
4300 }
4301
4302 if (dev->features & NETIF_F_RXALL)
4303 rx_mode |= (AcceptErr | AcceptRunt);
4304
4305 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4306
4307 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4308 u32 data = mc_filter[0];
4309
4310 mc_filter[0] = swab32(mc_filter[1]);
4311 mc_filter[1] = swab32(data);
4312 }
4313
4314 RTL_W32(MAR0 + 4, mc_filter[1]);
4315 RTL_W32(MAR0 + 0, mc_filter[0]);
4316
4317 RTL_W32(RxConfig, tmp);
4318}
4319
Francois Romieu07ce4062007-02-23 23:36:39 +01004320static void rtl_hw_start_8169(struct net_device *dev)
4321{
4322 struct rtl8169_private *tp = netdev_priv(dev);
4323 void __iomem *ioaddr = tp->mmio_addr;
4324 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004325
Francois Romieu9cb427b2006-11-02 00:10:16 +01004326 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4327 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4328 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4329 }
4330
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004332 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4333 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4334 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4335 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004336 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4337
Hayes Wange542a222011-07-06 15:58:04 +08004338 rtl_init_rxcfg(tp);
4339
françois romieuf0298f82011-01-03 15:07:42 +00004340 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004342 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
Francois Romieucecb5fd2011-04-01 10:21:07 +02004344 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4345 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4346 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4347 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004348 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
Francois Romieu7f796d832007-06-11 23:04:41 +02004350 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004351
Francois Romieucecb5fd2011-04-01 10:21:07 +02004352 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4353 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004354 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004356 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 }
4358
Francois Romieubcf0bf92006-07-26 23:14:13 +02004359 RTL_W16(CPlusCmd, tp->cp_cmd);
4360
Francois Romieu6dccd162007-02-13 23:38:05 +01004361 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4362
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 /*
4364 * Undocumented corner. Supposedly:
4365 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4366 */
4367 RTL_W16(IntrMitigate, 0x0000);
4368
Francois Romieu7f796d832007-06-11 23:04:41 +02004369 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004370
Francois Romieucecb5fd2011-04-01 10:21:07 +02004371 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4372 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4373 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4374 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004375 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4376 rtl_set_rx_tx_config_registers(tp);
4377 }
4378
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004380
4381 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4382 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383
4384 RTL_W32(RxMissed, 0);
4385
Francois Romieu07ce4062007-02-23 23:36:39 +01004386 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
4388 /* no early-rx interrupts */
4389 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004390}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004392static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
4393{
4394 if (tp->csi_ops.write)
4395 tp->csi_ops.write(tp->mmio_addr, addr, value);
4396}
4397
4398static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
4399{
4400 if (tp->csi_ops.read)
4401 return tp->csi_ops.read(tp->mmio_addr, addr);
4402 else
4403 return ~0;
4404}
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
4424static void r8169_csi_write(void __iomem *ioaddr, int addr, int value)
4425{
4426 unsigned int i;
4427
4428 RTL_W32(CSIDR, value);
4429 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4430 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4431
4432 for (i = 0; i < 100; i++) {
4433 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
4434 break;
4435 udelay(10);
4436 }
4437}
4438
4439static u32 r8169_csi_read(void __iomem *ioaddr, int addr)
4440{
4441 u32 value = ~0x00;
4442 unsigned int i;
4443
4444 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
4445 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4446
4447 for (i = 0; i < 100; i++) {
4448 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
4449 value = RTL_R32(CSIDR);
4450 break;
4451 }
4452 udelay(10);
4453 }
4454
4455 return value;
4456}
4457
Hayes Wang7e18dca2012-03-30 14:33:02 +08004458static void r8402_csi_write(void __iomem *ioaddr, int addr, int value)
4459{
4460 unsigned int i;
4461
4462 RTL_W32(CSIDR, value);
4463 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4464 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
4465 CSIAR_FUNC_NIC);
4466
4467 for (i = 0; i < 100; i++) {
4468 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
4469 break;
4470 udelay(10);
4471 }
4472}
4473
4474static u32 r8402_csi_read(void __iomem *ioaddr, int addr)
4475{
4476 u32 value = ~0x00;
4477 unsigned int i;
4478
4479 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
4480 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4481
4482 for (i = 0; i < 100; i++) {
4483 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
4484 value = RTL_R32(CSIDR);
4485 break;
4486 }
4487 udelay(10);
4488 }
4489
4490 return value;
4491}
4492
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004493static void __devinit rtl_init_csi_ops(struct rtl8169_private *tp)
4494{
4495 struct csi_ops *ops = &tp->csi_ops;
4496
4497 switch (tp->mac_version) {
4498 case RTL_GIGA_MAC_VER_01:
4499 case RTL_GIGA_MAC_VER_02:
4500 case RTL_GIGA_MAC_VER_03:
4501 case RTL_GIGA_MAC_VER_04:
4502 case RTL_GIGA_MAC_VER_05:
4503 case RTL_GIGA_MAC_VER_06:
4504 case RTL_GIGA_MAC_VER_10:
4505 case RTL_GIGA_MAC_VER_11:
4506 case RTL_GIGA_MAC_VER_12:
4507 case RTL_GIGA_MAC_VER_13:
4508 case RTL_GIGA_MAC_VER_14:
4509 case RTL_GIGA_MAC_VER_15:
4510 case RTL_GIGA_MAC_VER_16:
4511 case RTL_GIGA_MAC_VER_17:
4512 ops->write = NULL;
4513 ops->read = NULL;
4514 break;
4515
Hayes Wang7e18dca2012-03-30 14:33:02 +08004516 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004517 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004518 ops->write = r8402_csi_write;
4519 ops->read = r8402_csi_read;
4520 break;
4521
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004522 default:
4523 ops->write = r8169_csi_write;
4524 ops->read = r8169_csi_read;
4525 break;
4526 }
Francois Romieudacf8152008-08-02 20:44:13 +02004527}
4528
4529struct ephy_info {
4530 unsigned int offset;
4531 u16 mask;
4532 u16 bits;
4533};
4534
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004535static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004536{
4537 u16 w;
4538
4539 while (len-- > 0) {
4540 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4541 rtl_ephy_write(ioaddr, e->offset, w);
4542 e++;
4543 }
4544}
4545
Francois Romieub726e492008-06-28 12:22:59 +02004546static void rtl_disable_clock_request(struct pci_dev *pdev)
4547{
Jon Masone44daad2011-06-27 07:46:31 +00004548 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004549
4550 if (cap) {
4551 u16 ctl;
4552
4553 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4554 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4555 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4556 }
4557}
4558
françois romieue6de30d2011-01-03 15:08:37 +00004559static void rtl_enable_clock_request(struct pci_dev *pdev)
4560{
Jon Masone44daad2011-06-27 07:46:31 +00004561 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004562
4563 if (cap) {
4564 u16 ctl;
4565
4566 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4567 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4568 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4569 }
4570}
4571
Francois Romieub726e492008-06-28 12:22:59 +02004572#define R8168_CPCMD_QUIRK_MASK (\
4573 EnableBist | \
4574 Mac_dbgo_oe | \
4575 Force_half_dup | \
4576 Force_rxflow_en | \
4577 Force_txflow_en | \
4578 Cxpl_dbg_sel | \
4579 ASF | \
4580 PktCntrDisable | \
4581 Mac_dbgo_sel)
4582
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004583static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004584{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004585 void __iomem *ioaddr = tp->mmio_addr;
4586 struct pci_dev *pdev = tp->pci_dev;
4587
Francois Romieub726e492008-06-28 12:22:59 +02004588 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4589
4590 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4591
Francois Romieu2e68ae42008-06-28 12:00:55 +02004592 rtl_tx_performance_tweak(pdev,
4593 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004594}
4595
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004596static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004597{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004598 void __iomem *ioaddr = tp->mmio_addr;
4599
4600 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004601
françois romieuf0298f82011-01-03 15:07:42 +00004602 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004603
4604 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004605}
4606
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004607static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004608{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004609 void __iomem *ioaddr = tp->mmio_addr;
4610 struct pci_dev *pdev = tp->pci_dev;
4611
Francois Romieub726e492008-06-28 12:22:59 +02004612 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4613
4614 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4615
Francois Romieu219a1e92008-06-28 11:58:39 +02004616 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004617
4618 rtl_disable_clock_request(pdev);
4619
4620 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004621}
4622
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004623static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004624{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004625 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004626 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004627 { 0x01, 0, 0x0001 },
4628 { 0x02, 0x0800, 0x1000 },
4629 { 0x03, 0, 0x0042 },
4630 { 0x06, 0x0080, 0x0000 },
4631 { 0x07, 0, 0x2000 }
4632 };
4633
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004634 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004635
4636 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4637
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004638 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004639}
4640
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004641static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02004642{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004643 void __iomem *ioaddr = tp->mmio_addr;
4644 struct pci_dev *pdev = tp->pci_dev;
4645
4646 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004647
4648 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4649
4650 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4651
4652 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4653}
4654
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004655static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004656{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004657 void __iomem *ioaddr = tp->mmio_addr;
4658 struct pci_dev *pdev = tp->pci_dev;
4659
4660 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004661
4662 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4663
4664 /* Magic. */
4665 RTL_W8(DBG_REG, 0x20);
4666
françois romieuf0298f82011-01-03 15:07:42 +00004667 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004668
4669 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4670
4671 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4672}
4673
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004674static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004675{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004676 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004677 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004678 { 0x02, 0x0800, 0x1000 },
4679 { 0x03, 0, 0x0002 },
4680 { 0x06, 0x0080, 0x0000 }
4681 };
4682
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004683 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004684
4685 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4686
4687 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4688
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004689 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004690}
4691
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004692static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004693{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004694 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004695 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004696 { 0x01, 0, 0x0001 },
4697 { 0x03, 0x0400, 0x0220 }
4698 };
4699
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004700 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004701
4702 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4703
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004704 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004705}
4706
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004707static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02004708{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004709 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004710}
4711
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004712static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02004713{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004714 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004715
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004716 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004717}
4718
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004719static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02004720{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004721 void __iomem *ioaddr = tp->mmio_addr;
4722 struct pci_dev *pdev = tp->pci_dev;
4723
4724 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004725
4726 rtl_disable_clock_request(pdev);
4727
françois romieuf0298f82011-01-03 15:07:42 +00004728 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004729
4730 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4731
4732 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4733}
4734
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004735static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00004736{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004737 void __iomem *ioaddr = tp->mmio_addr;
4738 struct pci_dev *pdev = tp->pci_dev;
4739
4740 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004741
4742 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4743
4744 RTL_W8(MaxTxPacketSize, TxPacketMax);
4745
4746 rtl_disable_clock_request(pdev);
4747}
4748
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004749static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004750{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004751 void __iomem *ioaddr = tp->mmio_addr;
4752 struct pci_dev *pdev = tp->pci_dev;
françois romieue6de30d2011-01-03 15:08:37 +00004753 static const struct ephy_info e_info_8168d_4[] = {
4754 { 0x0b, ~0, 0x48 },
4755 { 0x19, 0x20, 0x50 },
4756 { 0x0c, ~0, 0x20 }
4757 };
4758 int i;
4759
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004760 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004761
4762 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4763
4764 RTL_W8(MaxTxPacketSize, TxPacketMax);
4765
4766 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4767 const struct ephy_info *e = e_info_8168d_4 + i;
4768 u16 w;
4769
4770 w = rtl_ephy_read(ioaddr, e->offset);
4771 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4772 }
4773
4774 rtl_enable_clock_request(pdev);
4775}
4776
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004777static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00004778{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004779 void __iomem *ioaddr = tp->mmio_addr;
4780 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08004781 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004782 { 0x00, 0x0200, 0x0100 },
4783 { 0x00, 0x0000, 0x0004 },
4784 { 0x06, 0x0002, 0x0001 },
4785 { 0x06, 0x0000, 0x0030 },
4786 { 0x07, 0x0000, 0x2000 },
4787 { 0x00, 0x0000, 0x0020 },
4788 { 0x03, 0x5800, 0x2000 },
4789 { 0x03, 0x0000, 0x0001 },
4790 { 0x01, 0x0800, 0x1000 },
4791 { 0x07, 0x0000, 0x4000 },
4792 { 0x1e, 0x0000, 0x2000 },
4793 { 0x19, 0xffff, 0xfe6c },
4794 { 0x0a, 0x0000, 0x0040 }
4795 };
4796
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004797 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004798
Hayes Wang70090422011-07-06 15:58:06 +08004799 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004800
4801 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4802
4803 RTL_W8(MaxTxPacketSize, TxPacketMax);
4804
4805 rtl_disable_clock_request(pdev);
4806
4807 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004808 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4809 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004810
Francois Romieucecb5fd2011-04-01 10:21:07 +02004811 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004812}
4813
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004814static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08004815{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004816 void __iomem *ioaddr = tp->mmio_addr;
4817 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08004818 static const struct ephy_info e_info_8168e_2[] = {
4819 { 0x09, 0x0000, 0x0080 },
4820 { 0x19, 0x0000, 0x0224 }
4821 };
4822
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004823 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08004824
4825 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4826
4827 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4828
4829 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4830 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4831 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4832 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4833 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4834 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4835 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4836 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4837 ERIAR_EXGMAC);
4838
Hayes Wang3090bd92011-09-06 16:55:15 +08004839 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004840
4841 rtl_disable_clock_request(pdev);
4842
4843 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4844 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4845
4846 /* Adjust EEE LED frequency */
4847 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4848
4849 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4850 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4851 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4852}
4853
Hayes Wang5f886e02012-03-30 14:33:03 +08004854static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08004855{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004856 void __iomem *ioaddr = tp->mmio_addr;
4857 struct pci_dev *pdev = tp->pci_dev;
Hayes Wangc2218922011-09-06 16:55:18 +08004858
Hayes Wang5f886e02012-03-30 14:33:03 +08004859 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08004860
4861 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4862
4863 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4864 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4865 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4866 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4867 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4868 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4869 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4870 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4871 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4872 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08004873
4874 RTL_W8(MaxTxPacketSize, EarlySize);
4875
4876 rtl_disable_clock_request(pdev);
4877
4878 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4879 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
Hayes Wangc2218922011-09-06 16:55:18 +08004880 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4881 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4882 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4883}
4884
Hayes Wang5f886e02012-03-30 14:33:03 +08004885static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
4886{
4887 void __iomem *ioaddr = tp->mmio_addr;
4888 static const struct ephy_info e_info_8168f_1[] = {
4889 { 0x06, 0x00c0, 0x0020 },
4890 { 0x08, 0x0001, 0x0002 },
4891 { 0x09, 0x0000, 0x0080 },
4892 { 0x19, 0x0000, 0x0224 }
4893 };
4894
4895 rtl_hw_start_8168f(tp);
4896
4897 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4898
4899 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4900 ERIAR_EXGMAC);
4901
4902 /* Adjust EEE LED frequency */
4903 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4904}
4905
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004906static void rtl_hw_start_8411(struct rtl8169_private *tp)
4907{
4908 void __iomem *ioaddr = tp->mmio_addr;
4909 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
4918 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4919
4920 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000,
4921 ERIAR_EXGMAC);
4922}
4923
Francois Romieu07ce4062007-02-23 23:36:39 +01004924static void rtl_hw_start_8168(struct net_device *dev)
4925{
Francois Romieu2dd99532007-06-11 23:22:52 +02004926 struct rtl8169_private *tp = netdev_priv(dev);
4927 void __iomem *ioaddr = tp->mmio_addr;
4928
4929 RTL_W8(Cfg9346, Cfg9346_Unlock);
4930
françois romieuf0298f82011-01-03 15:07:42 +00004931 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004932
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004933 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004934
Francois Romieu0e485152007-02-20 00:00:26 +01004935 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004936
4937 RTL_W16(CPlusCmd, tp->cp_cmd);
4938
Francois Romieu0e485152007-02-20 00:00:26 +01004939 RTL_W16(IntrMitigate, 0x5151);
4940
4941 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00004942 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01004943 tp->event_slow |= RxFIFOOver | PCSTimeout;
4944 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01004945 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004946
4947 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4948
Francois Romieub8363902008-06-01 12:31:57 +02004949 rtl_set_rx_mode(dev);
4950
4951 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4952 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004953
4954 RTL_R8(IntrMask);
4955
Francois Romieu219a1e92008-06-28 11:58:39 +02004956 switch (tp->mac_version) {
4957 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004958 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004959 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004960
4961 case RTL_GIGA_MAC_VER_12:
4962 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004963 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004964 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004965
4966 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004967 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004968 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004969
4970 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004971 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004972 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004973
4974 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004975 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004976 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004977
Francois Romieu197ff762008-06-28 13:16:02 +02004978 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004979 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004980 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004981
Francois Romieu6fb07052008-06-29 11:54:28 +02004982 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004983 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004984 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004985
Francois Romieuef3386f2008-06-29 12:24:30 +02004986 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004987 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004988 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004989
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004990 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004991 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004992 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004993
Francois Romieu5b538df2008-07-20 16:22:45 +02004994 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004995 case RTL_GIGA_MAC_VER_26:
4996 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004997 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004998 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004999
françois romieue6de30d2011-01-03 15:08:37 +00005000 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005001 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005002 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02005003
hayeswang4804b3b2011-03-21 01:50:29 +00005004 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005005 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005006 break;
5007
hayeswang01dc7fe2011-03-21 01:50:28 +00005008 case RTL_GIGA_MAC_VER_32:
5009 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005010 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005011 break;
5012 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005013 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005014 break;
françois romieue6de30d2011-01-03 15:08:37 +00005015
Hayes Wangc2218922011-09-06 16:55:18 +08005016 case RTL_GIGA_MAC_VER_35:
5017 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005018 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005019 break;
5020
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005021 case RTL_GIGA_MAC_VER_38:
5022 rtl_hw_start_8411(tp);
5023 break;
5024
Francois Romieu219a1e92008-06-28 11:58:39 +02005025 default:
5026 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
5027 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00005028 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005029 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005030
Francois Romieu0e485152007-02-20 00:00:26 +01005031 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5032
Francois Romieub8363902008-06-01 12:31:57 +02005033 RTL_W8(Cfg9346, Cfg9346_Lock);
5034
Francois Romieu2dd99532007-06-11 23:22:52 +02005035 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005036}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037
Francois Romieu2857ffb2008-08-02 21:08:49 +02005038#define R810X_CPCMD_QUIRK_MASK (\
5039 EnableBist | \
5040 Mac_dbgo_oe | \
5041 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00005042 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02005043 Force_txflow_en | \
5044 Cxpl_dbg_sel | \
5045 ASF | \
5046 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005047 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005048
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005049static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005050{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005051 void __iomem *ioaddr = tp->mmio_addr;
5052 struct pci_dev *pdev = tp->pci_dev;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005053 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02005054 { 0x01, 0, 0x6e65 },
5055 { 0x02, 0, 0x091f },
5056 { 0x03, 0, 0xc2f9 },
5057 { 0x06, 0, 0xafb5 },
5058 { 0x07, 0, 0x0e00 },
5059 { 0x19, 0, 0xec80 },
5060 { 0x01, 0, 0x2e65 },
5061 { 0x01, 0, 0x6e65 }
5062 };
5063 u8 cfg1;
5064
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005065 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005066
5067 RTL_W8(DBG_REG, FIX_NAK_1);
5068
5069 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5070
5071 RTL_W8(Config1,
5072 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5073 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5074
5075 cfg1 = RTL_R8(Config1);
5076 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5077 RTL_W8(Config1, cfg1 & ~LEDS0);
5078
Francois Romieu2857ffb2008-08-02 21:08:49 +02005079 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
5080}
5081
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005082static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005083{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005084 void __iomem *ioaddr = tp->mmio_addr;
5085 struct pci_dev *pdev = tp->pci_dev;
5086
5087 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005088
5089 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5090
5091 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5092 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005093}
5094
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005095static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005096{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005097 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005098
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005099 rtl_ephy_write(tp->mmio_addr, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005100}
5101
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005102static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005103{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005104 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005105 static const struct ephy_info e_info_8105e_1[] = {
5106 { 0x07, 0, 0x4000 },
5107 { 0x19, 0, 0x0200 },
5108 { 0x19, 0, 0x0020 },
5109 { 0x1e, 0, 0x2000 },
5110 { 0x03, 0, 0x0001 },
5111 { 0x19, 0, 0x0100 },
5112 { 0x19, 0, 0x0004 },
5113 { 0x0a, 0, 0x0020 }
5114 };
5115
Francois Romieucecb5fd2011-04-01 10:21:07 +02005116 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005117 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5118
Francois Romieucecb5fd2011-04-01 10:21:07 +02005119 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005120 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5121
5122 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08005123 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005124
5125 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
5126}
5127
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005128static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005129{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005130 void __iomem *ioaddr = tp->mmio_addr;
5131
5132 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005133 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
5134}
5135
Hayes Wang7e18dca2012-03-30 14:33:02 +08005136static void rtl_hw_start_8402(struct rtl8169_private *tp)
5137{
5138 void __iomem *ioaddr = tp->mmio_addr;
5139 static const struct ephy_info e_info_8402[] = {
5140 { 0x19, 0xffff, 0xff64 },
5141 { 0x1e, 0, 0x4000 }
5142 };
5143
5144 rtl_csi_access_enable_2(tp);
5145
5146 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5147 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5148
5149 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5150 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5151
5152 rtl_ephy_init(ioaddr, e_info_8402, ARRAY_SIZE(e_info_8402));
5153
5154 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5155
5156 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5157 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
5158 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5159 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5160 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5161 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5162 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00,
5163 ERIAR_EXGMAC);
5164}
5165
Hayes Wang5598bfe2012-07-02 17:23:21 +08005166static void rtl_hw_start_8106(struct rtl8169_private *tp)
5167{
5168 void __iomem *ioaddr = tp->mmio_addr;
5169
5170 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5171 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5172
5173 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5174 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
5175 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
5176}
5177
Francois Romieu07ce4062007-02-23 23:36:39 +01005178static void rtl_hw_start_8101(struct net_device *dev)
5179{
Francois Romieucdf1a602007-06-11 23:29:50 +02005180 struct rtl8169_private *tp = netdev_priv(dev);
5181 void __iomem *ioaddr = tp->mmio_addr;
5182 struct pci_dev *pdev = tp->pci_dev;
5183
Francois Romieuda78dbf2012-01-26 14:18:23 +01005184 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5185 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00005186
Francois Romieucecb5fd2011-04-01 10:21:07 +02005187 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5188 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00005189 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02005190
5191 if (cap) {
5192 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
5193 PCI_EXP_DEVCTL_NOSNOOP_EN);
5194 }
Francois Romieucdf1a602007-06-11 23:29:50 +02005195 }
5196
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005197 RTL_W8(Cfg9346, Cfg9346_Unlock);
5198
Francois Romieu2857ffb2008-08-02 21:08:49 +02005199 switch (tp->mac_version) {
5200 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005201 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005202 break;
5203
5204 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005205 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005206 break;
5207
5208 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005209 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005210 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005211
5212 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005213 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005214 break;
5215 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005216 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005217 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005218
5219 case RTL_GIGA_MAC_VER_37:
5220 rtl_hw_start_8402(tp);
5221 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08005222
5223 case RTL_GIGA_MAC_VER_39:
5224 rtl_hw_start_8106(tp);
5225 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02005226 }
5227
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005228 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02005229
françois romieuf0298f82011-01-03 15:07:42 +00005230 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02005231
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005232 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02005233
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005234 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02005235 RTL_W16(CPlusCmd, tp->cp_cmd);
5236
5237 RTL_W16(IntrMitigate, 0x0000);
5238
5239 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5240
5241 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5242 rtl_set_rx_tx_config_registers(tp);
5243
Francois Romieucdf1a602007-06-11 23:29:50 +02005244 RTL_R8(IntrMask);
5245
Francois Romieucdf1a602007-06-11 23:29:50 +02005246 rtl_set_rx_mode(dev);
5247
5248 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249}
5250
5251static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5252{
Francois Romieud58d46b2011-05-03 16:38:29 +02005253 struct rtl8169_private *tp = netdev_priv(dev);
5254
5255 if (new_mtu < ETH_ZLEN ||
5256 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 return -EINVAL;
5258
Francois Romieud58d46b2011-05-03 16:38:29 +02005259 if (new_mtu > ETH_DATA_LEN)
5260 rtl_hw_jumbo_enable(tp);
5261 else
5262 rtl_hw_jumbo_disable(tp);
5263
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005265 netdev_update_features(dev);
5266
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005267 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268}
5269
5270static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5271{
Al Viro95e09182007-12-22 18:55:39 +00005272 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5274}
5275
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005276static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5277 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005279 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005280 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005281
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005282 kfree(*data_buff);
5283 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 rtl8169_make_unusable_by_asic(desc);
5285}
5286
5287static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5288{
5289 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5290
5291 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5292}
5293
5294static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5295 u32 rx_buf_sz)
5296{
5297 desc->addr = cpu_to_le64(mapping);
5298 wmb();
5299 rtl8169_mark_to_asic(desc, rx_buf_sz);
5300}
5301
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005302static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005304 return (void *)ALIGN((long)data, 16);
5305}
5306
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005307static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5308 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005309{
5310 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005312 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005313 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005314 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005316 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5317 if (!data)
5318 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005319
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005320 if (rtl8169_align(data) != data) {
5321 kfree(data);
5322 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5323 if (!data)
5324 return NULL;
5325 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005326
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005327 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005328 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005329 if (unlikely(dma_mapping_error(d, mapping))) {
5330 if (net_ratelimit())
5331 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005332 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334
5335 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005336 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005337
5338err_out:
5339 kfree(data);
5340 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341}
5342
5343static void rtl8169_rx_clear(struct rtl8169_private *tp)
5344{
Francois Romieu07d3f512007-02-21 22:40:46 +01005345 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346
5347 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005348 if (tp->Rx_databuff[i]) {
5349 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 tp->RxDescArray + i);
5351 }
5352 }
5353}
5354
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005355static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005357 desc->opts1 |= cpu_to_le32(RingEnd);
5358}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005359
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005360static int rtl8169_rx_fill(struct rtl8169_private *tp)
5361{
5362 unsigned int i;
5363
5364 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005365 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005366
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005367 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005369
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005370 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005371 if (!data) {
5372 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005373 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005374 }
5375 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005378 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5379 return 0;
5380
5381err_out:
5382 rtl8169_rx_clear(tp);
5383 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384}
5385
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386static int rtl8169_init_ring(struct net_device *dev)
5387{
5388 struct rtl8169_private *tp = netdev_priv(dev);
5389
5390 rtl8169_init_ring_indexes(tp);
5391
5392 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005393 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005395 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396}
5397
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005398static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 struct TxDesc *desc)
5400{
5401 unsigned int len = tx_skb->len;
5402
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005403 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5404
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 desc->opts1 = 0x00;
5406 desc->opts2 = 0x00;
5407 desc->addr = 0x00;
5408 tx_skb->len = 0;
5409}
5410
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005411static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5412 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413{
5414 unsigned int i;
5415
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005416 for (i = 0; i < n; i++) {
5417 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 struct ring_info *tx_skb = tp->tx_skb + entry;
5419 unsigned int len = tx_skb->len;
5420
5421 if (len) {
5422 struct sk_buff *skb = tx_skb->skb;
5423
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005424 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 tp->TxDescArray + entry);
5426 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005427 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 dev_kfree_skb(skb);
5429 tx_skb->skb = NULL;
5430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 }
5432 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005433}
5434
5435static void rtl8169_tx_clear(struct rtl8169_private *tp)
5436{
5437 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 tp->cur_tx = tp->dirty_tx = 0;
Igor Maravic036dafa2012-03-05 00:01:25 +01005439 netdev_reset_queue(tp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440}
5441
Francois Romieu4422bcd2012-01-26 11:23:32 +01005442static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443{
David Howellsc4028952006-11-22 14:57:56 +00005444 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005445 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
Francois Romieuda78dbf2012-01-26 14:18:23 +01005447 napi_disable(&tp->napi);
5448 netif_stop_queue(dev);
5449 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450
françois romieuc7c2c392011-12-04 20:30:52 +00005451 rtl8169_hw_reset(tp);
5452
Francois Romieu56de4142011-03-15 17:29:31 +01005453 for (i = 0; i < NUM_RX_DESC; i++)
5454 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5455
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005457 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458
Francois Romieuda78dbf2012-01-26 14:18:23 +01005459 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01005460 rtl_hw_start(dev);
5461 netif_wake_queue(dev);
5462 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463}
5464
5465static void rtl8169_tx_timeout(struct net_device *dev)
5466{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005467 struct rtl8169_private *tp = netdev_priv(dev);
5468
5469 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470}
5471
5472static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005473 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474{
5475 struct skb_shared_info *info = skb_shinfo(skb);
5476 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005477 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005478 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479
5480 entry = tp->cur_tx;
5481 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005482 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 dma_addr_t mapping;
5484 u32 status, len;
5485 void *addr;
5486
5487 entry = (entry + 1) % NUM_TX_DESC;
5488
5489 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005490 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005491 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005492 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005493 if (unlikely(dma_mapping_error(d, mapping))) {
5494 if (net_ratelimit())
5495 netif_err(tp, drv, tp->dev,
5496 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005497 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499
Francois Romieucecb5fd2011-04-01 10:21:07 +02005500 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005501 status = opts[0] | len |
5502 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503
5504 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005505 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 txd->addr = cpu_to_le64(mapping);
5507
5508 tp->tx_skb[entry].len = len;
5509 }
5510
5511 if (cur_frag) {
5512 tp->tx_skb[entry].skb = skb;
5513 txd->opts1 |= cpu_to_le32(LastFrag);
5514 }
5515
5516 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005517
5518err_out:
5519 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5520 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521}
5522
Francois Romieu2b7b4312011-04-18 22:53:24 -07005523static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5524 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005526 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005527 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005528 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
Francois Romieu2b7b4312011-04-18 22:53:24 -07005530 if (mss) {
5531 opts[0] |= TD_LSO;
5532 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5533 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005534 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535
5536 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005537 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005539 opts[offset] |= info->checksum.udp;
5540 else
5541 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543}
5544
Stephen Hemminger613573252009-08-31 19:50:58 +00005545static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5546 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547{
5548 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005549 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 struct TxDesc *txd = tp->TxDescArray + entry;
5551 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005552 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 dma_addr_t mapping;
5554 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005555 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005556 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005557
Julien Ducourthial477206a2012-05-09 00:00:06 +02005558 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005559 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005560 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 }
5562
5563 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005564 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005566 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005567 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005568 if (unlikely(dma_mapping_error(d, mapping))) {
5569 if (net_ratelimit())
5570 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005571 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573
5574 tp->tx_skb[entry].len = len;
5575 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576
Francois Romieu2b7b4312011-04-18 22:53:24 -07005577 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5578 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005579
Francois Romieu2b7b4312011-04-18 22:53:24 -07005580 rtl8169_tso_csum(tp, skb, opts);
5581
5582 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005583 if (frags < 0)
5584 goto err_dma_1;
5585 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005586 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005587 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005588 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005589 tp->tx_skb[entry].skb = skb;
5590 }
5591
Francois Romieu2b7b4312011-04-18 22:53:24 -07005592 txd->opts2 = cpu_to_le32(opts[1]);
5593
Igor Maravic036dafa2012-03-05 00:01:25 +01005594 netdev_sent_queue(dev, skb->len);
5595
Richard Cochran5047fb52012-03-10 07:29:42 +00005596 skb_tx_timestamp(skb);
5597
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 wmb();
5599
Francois Romieucecb5fd2011-04-01 10:21:07 +02005600 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005601 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 txd->opts1 = cpu_to_le32(status);
5603
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 tp->cur_tx += frags + 1;
5605
David Dillow4c020a92010-03-03 16:33:10 +00005606 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607
Francois Romieucecb5fd2011-04-01 10:21:07 +02005608 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609
Francois Romieuda78dbf2012-01-26 14:18:23 +01005610 mmiowb();
5611
Julien Ducourthial477206a2012-05-09 00:00:06 +02005612 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01005613 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5614 * not miss a ring update when it notices a stopped queue.
5615 */
5616 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01005618 /* Sync with rtl_tx:
5619 * - publish queue status and cur_tx ring index (write barrier)
5620 * - refresh dirty_tx ring index (read barrier).
5621 * May the current thread have a pessimistic view of the ring
5622 * status and forget to wake up queue, a racing rtl_tx thread
5623 * can't.
5624 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005625 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02005626 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 netif_wake_queue(dev);
5628 }
5629
Stephen Hemminger613573252009-08-31 19:50:58 +00005630 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005632err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005633 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005634err_dma_0:
5635 dev_kfree_skb(skb);
5636 dev->stats.tx_dropped++;
5637 return NETDEV_TX_OK;
5638
5639err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005641 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005642 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643}
5644
5645static void rtl8169_pcierr_interrupt(struct net_device *dev)
5646{
5647 struct rtl8169_private *tp = netdev_priv(dev);
5648 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 u16 pci_status, pci_cmd;
5650
5651 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5652 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5653
Joe Perchesbf82c182010-02-09 11:49:50 +00005654 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5655 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656
5657 /*
5658 * The recovery sequence below admits a very elaborated explanation:
5659 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005660 * - I did not see what else could be done;
5661 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 *
5663 * Feel free to adjust to your needs.
5664 */
Francois Romieua27993f2006-12-18 00:04:19 +01005665 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005666 pci_cmd &= ~PCI_COMMAND_PARITY;
5667 else
5668 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5669
5670 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671
5672 pci_write_config_word(pdev, PCI_STATUS,
5673 pci_status & (PCI_STATUS_DETECTED_PARITY |
5674 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5675 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5676
5677 /* The infamous DAC f*ckup only happens at boot time */
5678 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005679 void __iomem *ioaddr = tp->mmio_addr;
5680
Joe Perchesbf82c182010-02-09 11:49:50 +00005681 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 tp->cp_cmd &= ~PCIDAC;
5683 RTL_W16(CPlusCmd, tp->cp_cmd);
5684 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 }
5686
françois romieue6de30d2011-01-03 15:08:37 +00005687 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005688
Francois Romieu98ddf982012-01-31 10:47:34 +01005689 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690}
5691
Igor Maravic036dafa2012-03-05 00:01:25 +01005692struct rtl_txc {
5693 int packets;
5694 int bytes;
5695};
5696
Francois Romieuda78dbf2012-01-26 14:18:23 +01005697static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698{
Igor Maravic036dafa2012-03-05 00:01:25 +01005699 struct rtl8169_stats *tx_stats = &tp->tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 unsigned int dirty_tx, tx_left;
Igor Maravic036dafa2012-03-05 00:01:25 +01005701 struct rtl_txc txc = { 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 dirty_tx = tp->dirty_tx;
5704 smp_rmb();
5705 tx_left = tp->cur_tx - dirty_tx;
5706
5707 while (tx_left > 0) {
5708 unsigned int entry = dirty_tx % NUM_TX_DESC;
5709 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 u32 status;
5711
5712 rmb();
5713 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5714 if (status & DescOwn)
5715 break;
5716
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005717 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5718 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 if (status & LastFrag) {
Igor Maravic036dafa2012-03-05 00:01:25 +01005720 struct sk_buff *skb = tx_skb->skb;
5721
5722 txc.packets++;
5723 txc.bytes += skb->len;
5724 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 tx_skb->skb = NULL;
5726 }
5727 dirty_tx++;
5728 tx_left--;
5729 }
5730
Igor Maravic036dafa2012-03-05 00:01:25 +01005731 u64_stats_update_begin(&tx_stats->syncp);
5732 tx_stats->packets += txc.packets;
5733 tx_stats->bytes += txc.bytes;
5734 u64_stats_update_end(&tx_stats->syncp);
5735
5736 netdev_completed_queue(dev, txc.packets, txc.bytes);
5737
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 if (tp->dirty_tx != dirty_tx) {
5739 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005740 /* Sync with rtl8169_start_xmit:
5741 * - publish dirty_tx ring index (write barrier)
5742 * - refresh cur_tx ring index and queue status (read barrier)
5743 * May the current thread miss the stopped queue condition,
5744 * a racing xmit thread can only have a right view of the
5745 * ring status.
5746 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005747 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02005749 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 netif_wake_queue(dev);
5751 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005752 /*
5753 * 8168 hack: TxPoll requests are lost when the Tx packets are
5754 * too close. Let's kick an extra TxPoll request when a burst
5755 * of start_xmit activity is detected (if it is not detected,
5756 * it is slow enough). -- FR
5757 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005758 if (tp->cur_tx != dirty_tx) {
5759 void __iomem *ioaddr = tp->mmio_addr;
5760
Francois Romieud78ae2d2007-08-26 20:08:19 +02005761 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 }
5764}
5765
Francois Romieu126fa4b2005-05-12 20:09:17 -04005766static inline int rtl8169_fragmented_frame(u32 status)
5767{
5768 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5769}
5770
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005771static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 u32 status = opts1 & RxProtoMask;
5774
5775 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005776 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 skb->ip_summed = CHECKSUM_UNNECESSARY;
5778 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005779 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780}
5781
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005782static struct sk_buff *rtl8169_try_rx_copy(void *data,
5783 struct rtl8169_private *tp,
5784 int pkt_size,
5785 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005787 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005788 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005790 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005791 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005792 prefetch(data);
5793 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5794 if (skb)
5795 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005796 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5797
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005798 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799}
5800
Francois Romieuda78dbf2012-01-26 14:18:23 +01005801static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802{
5803 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005804 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 cur_rx = tp->cur_rx;
5807 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005808 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005810 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005812 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 u32 status;
5814
5815 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005816 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817
5818 if (status & DescOwn)
5819 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005820 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005821 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5822 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005823 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005825 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005827 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005828 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005829 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005830 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005831 }
Ben Greear6bbe0212012-02-10 15:04:33 +00005832 if ((status & (RxRUNT | RxCRC)) &&
5833 !(status & (RxRWT | RxFOVF)) &&
5834 (dev->features & NETIF_F_RXALL))
5835 goto process_pkt;
5836
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005837 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005839 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00005840 dma_addr_t addr;
5841 int pkt_size;
5842
5843process_pkt:
5844 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00005845 if (likely(!(dev->features & NETIF_F_RXFCS)))
5846 pkt_size = (status & 0x00003fff) - 4;
5847 else
5848 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849
Francois Romieu126fa4b2005-05-12 20:09:17 -04005850 /*
5851 * The driver does not support incoming fragmented
5852 * frames. They are seen as a symptom of over-mtu
5853 * sized frames.
5854 */
5855 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005856 dev->stats.rx_dropped++;
5857 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005858 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005859 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005860 }
5861
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005862 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5863 tp, pkt_size, addr);
5864 rtl8169_mark_to_asic(desc, rx_buf_sz);
5865 if (!skb) {
5866 dev->stats.rx_dropped++;
5867 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 }
5869
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005870 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871 skb_put(skb, pkt_size);
5872 skb->protocol = eth_type_trans(skb, dev);
5873
Francois Romieu7a8fc772011-03-01 17:18:33 +01005874 rtl8169_rx_vlan_tag(desc, skb);
5875
Francois Romieu56de4142011-03-15 17:29:31 +01005876 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877
Junchang Wang8027aa22012-03-04 23:30:32 +01005878 u64_stats_update_begin(&tp->rx_stats.syncp);
5879 tp->rx_stats.packets++;
5880 tp->rx_stats.bytes += pkt_size;
5881 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005883
5884 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005885 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005886 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5887 desc->opts2 = 0;
5888 cur_rx++;
5889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 }
5891
5892 count = cur_rx - tp->cur_rx;
5893 tp->cur_rx = cur_rx;
5894
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005895 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896
5897 return count;
5898}
5899
Francois Romieu07d3f512007-02-21 22:40:46 +01005900static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901{
Francois Romieu07d3f512007-02-21 22:40:46 +01005902 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005905 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005907 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005908 if (status && status != 0xffff) {
5909 status &= RTL_EVENT_NAPI | tp->event_slow;
5910 if (status) {
5911 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00005912
Francois Romieuda78dbf2012-01-26 14:18:23 +01005913 rtl_irq_disable(tp);
5914 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 return IRQ_RETVAL(handled);
5918}
5919
Francois Romieuda78dbf2012-01-26 14:18:23 +01005920/*
5921 * Workqueue context.
5922 */
5923static void rtl_slow_event_work(struct rtl8169_private *tp)
5924{
5925 struct net_device *dev = tp->dev;
5926 u16 status;
5927
5928 status = rtl_get_events(tp) & tp->event_slow;
5929 rtl_ack_events(tp, status);
5930
5931 if (unlikely(status & RxFIFOOver)) {
5932 switch (tp->mac_version) {
5933 /* Work around for rx fifo overflow */
5934 case RTL_GIGA_MAC_VER_11:
5935 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01005936 /* XXX - Hack alert. See rtl_task(). */
5937 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005938 default:
5939 break;
5940 }
5941 }
5942
5943 if (unlikely(status & SYSErr))
5944 rtl8169_pcierr_interrupt(dev);
5945
5946 if (status & LinkChg)
5947 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
5948
françois romieu7dbb4912012-06-09 10:53:16 +00005949 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005950}
5951
Francois Romieu4422bcd2012-01-26 11:23:32 +01005952static void rtl_task(struct work_struct *work)
5953{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005954 static const struct {
5955 int bitnr;
5956 void (*action)(struct rtl8169_private *);
5957 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01005958 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005959 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
5960 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
5961 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
5962 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01005963 struct rtl8169_private *tp =
5964 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005965 struct net_device *dev = tp->dev;
5966 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01005967
Francois Romieuda78dbf2012-01-26 14:18:23 +01005968 rtl_lock_work(tp);
5969
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005970 if (!netif_running(dev) ||
5971 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01005972 goto out_unlock;
5973
5974 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
5975 bool pending;
5976
Francois Romieuda78dbf2012-01-26 14:18:23 +01005977 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005978 if (pending)
5979 rtl_work[i].action(tp);
5980 }
5981
5982out_unlock:
5983 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01005984}
5985
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005986static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005988 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5989 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005990 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
5991 int work_done= 0;
5992 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993
Francois Romieuda78dbf2012-01-26 14:18:23 +01005994 status = rtl_get_events(tp);
5995 rtl_ack_events(tp, status & ~tp->event_slow);
5996
5997 if (status & RTL_EVENT_NAPI_RX)
5998 work_done = rtl_rx(dev, tp, (u32) budget);
5999
6000 if (status & RTL_EVENT_NAPI_TX)
6001 rtl_tx(dev, tp);
6002
6003 if (status & tp->event_slow) {
6004 enable_mask &= ~tp->event_slow;
6005
6006 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
6007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006009 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006010 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00006011
Francois Romieuda78dbf2012-01-26 14:18:23 +01006012 rtl_irq_enable(tp, enable_mask);
6013 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 }
6015
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006016 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018
Francois Romieu523a6092008-09-10 22:28:56 +02006019static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
6020{
6021 struct rtl8169_private *tp = netdev_priv(dev);
6022
6023 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6024 return;
6025
6026 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
6027 RTL_W32(RxMissed, 0);
6028}
6029
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030static void rtl8169_down(struct net_device *dev)
6031{
6032 struct rtl8169_private *tp = netdev_priv(dev);
6033 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034
Francois Romieu4876cc12011-03-11 21:07:11 +01006035 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01006037 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006038 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039
Hayes Wang92fc43b2011-07-06 15:58:03 +08006040 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006041 /*
6042 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01006043 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6044 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006045 */
Francois Romieu523a6092008-09-10 22:28:56 +02006046 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006049 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 rtl8169_tx_clear(tp);
6052
6053 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00006054
6055 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056}
6057
6058static int rtl8169_close(struct net_device *dev)
6059{
6060 struct rtl8169_private *tp = netdev_priv(dev);
6061 struct pci_dev *pdev = tp->pci_dev;
6062
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006063 pm_runtime_get_sync(&pdev->dev);
6064
Francois Romieucecb5fd2011-04-01 10:21:07 +02006065 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08006066 rtl8169_update_counters(dev);
6067
Francois Romieuda78dbf2012-01-26 14:18:23 +01006068 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006069 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006070
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006072 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006074 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00006076 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6077 tp->RxPhyAddr);
6078 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6079 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 tp->TxDescArray = NULL;
6081 tp->RxDescArray = NULL;
6082
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006083 pm_runtime_put_sync(&pdev->dev);
6084
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 return 0;
6086}
6087
Francois Romieudc1c00c2012-03-08 10:06:18 +01006088#ifdef CONFIG_NET_POLL_CONTROLLER
6089static void rtl8169_netpoll(struct net_device *dev)
6090{
6091 struct rtl8169_private *tp = netdev_priv(dev);
6092
6093 rtl8169_interrupt(tp->pci_dev->irq, dev);
6094}
6095#endif
6096
Francois Romieudf43ac72012-03-08 09:48:40 +01006097static int rtl_open(struct net_device *dev)
6098{
6099 struct rtl8169_private *tp = netdev_priv(dev);
6100 void __iomem *ioaddr = tp->mmio_addr;
6101 struct pci_dev *pdev = tp->pci_dev;
6102 int retval = -ENOMEM;
6103
6104 pm_runtime_get_sync(&pdev->dev);
6105
6106 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02006107 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01006108 * dma_alloc_coherent provides more.
6109 */
6110 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6111 &tp->TxPhyAddr, GFP_KERNEL);
6112 if (!tp->TxDescArray)
6113 goto err_pm_runtime_put;
6114
6115 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6116 &tp->RxPhyAddr, GFP_KERNEL);
6117 if (!tp->RxDescArray)
6118 goto err_free_tx_0;
6119
6120 retval = rtl8169_init_ring(dev);
6121 if (retval < 0)
6122 goto err_free_rx_1;
6123
6124 INIT_WORK(&tp->wk.work, rtl_task);
6125
6126 smp_mb();
6127
6128 rtl_request_firmware(tp);
6129
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006130 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01006131 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
6132 dev->name, dev);
6133 if (retval < 0)
6134 goto err_release_fw_2;
6135
6136 rtl_lock_work(tp);
6137
6138 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6139
6140 napi_enable(&tp->napi);
6141
6142 rtl8169_init_phy(dev, tp);
6143
6144 __rtl8169_set_features(dev, dev->features);
6145
6146 rtl_pll_power_up(tp);
6147
6148 rtl_hw_start(dev);
6149
6150 netif_start_queue(dev);
6151
6152 rtl_unlock_work(tp);
6153
6154 tp->saved_wolopts = 0;
6155 pm_runtime_put_noidle(&pdev->dev);
6156
6157 rtl8169_check_link_status(dev, tp, ioaddr);
6158out:
6159 return retval;
6160
6161err_release_fw_2:
6162 rtl_release_firmware(tp);
6163 rtl8169_rx_clear(tp);
6164err_free_rx_1:
6165 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6166 tp->RxPhyAddr);
6167 tp->RxDescArray = NULL;
6168err_free_tx_0:
6169 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6170 tp->TxPhyAddr);
6171 tp->TxDescArray = NULL;
6172err_pm_runtime_put:
6173 pm_runtime_put_noidle(&pdev->dev);
6174 goto out;
6175}
6176
Junchang Wang8027aa22012-03-04 23:30:32 +01006177static struct rtnl_link_stats64 *
6178rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179{
6180 struct rtl8169_private *tp = netdev_priv(dev);
6181 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01006182 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183
Francois Romieuda78dbf2012-01-26 14:18:23 +01006184 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02006185 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02006186
Junchang Wang8027aa22012-03-04 23:30:32 +01006187 do {
6188 start = u64_stats_fetch_begin_bh(&tp->rx_stats.syncp);
6189 stats->rx_packets = tp->rx_stats.packets;
6190 stats->rx_bytes = tp->rx_stats.bytes;
6191 } while (u64_stats_fetch_retry_bh(&tp->rx_stats.syncp, start));
6192
6193
6194 do {
6195 start = u64_stats_fetch_begin_bh(&tp->tx_stats.syncp);
6196 stats->tx_packets = tp->tx_stats.packets;
6197 stats->tx_bytes = tp->tx_stats.bytes;
6198 } while (u64_stats_fetch_retry_bh(&tp->tx_stats.syncp, start));
6199
6200 stats->rx_dropped = dev->stats.rx_dropped;
6201 stats->tx_dropped = dev->stats.tx_dropped;
6202 stats->rx_length_errors = dev->stats.rx_length_errors;
6203 stats->rx_errors = dev->stats.rx_errors;
6204 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6205 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6206 stats->rx_missed_errors = dev->stats.rx_missed_errors;
6207
6208 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209}
6210
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006211static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006212{
françois romieu065c27c2011-01-03 15:08:12 +00006213 struct rtl8169_private *tp = netdev_priv(dev);
6214
Francois Romieu5d06a992006-02-23 00:47:58 +01006215 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006216 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006217
6218 netif_device_detach(dev);
6219 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006220
6221 rtl_lock_work(tp);
6222 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006223 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006224 rtl_unlock_work(tp);
6225
6226 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006227}
Francois Romieu5d06a992006-02-23 00:47:58 +01006228
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006229#ifdef CONFIG_PM
6230
6231static int rtl8169_suspend(struct device *device)
6232{
6233 struct pci_dev *pdev = to_pci_dev(device);
6234 struct net_device *dev = pci_get_drvdata(pdev);
6235
6236 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02006237
Francois Romieu5d06a992006-02-23 00:47:58 +01006238 return 0;
6239}
6240
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006241static void __rtl8169_resume(struct net_device *dev)
6242{
françois romieu065c27c2011-01-03 15:08:12 +00006243 struct rtl8169_private *tp = netdev_priv(dev);
6244
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006245 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006246
6247 rtl_pll_power_up(tp);
6248
Artem Savkovcff4c162012-04-03 10:29:11 +00006249 rtl_lock_work(tp);
6250 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006251 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00006252 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006253
Francois Romieu98ddf982012-01-31 10:47:34 +01006254 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006255}
6256
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006257static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006258{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006259 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01006260 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006261 struct rtl8169_private *tp = netdev_priv(dev);
6262
6263 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01006264
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006265 if (netif_running(dev))
6266 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006267
Francois Romieu5d06a992006-02-23 00:47:58 +01006268 return 0;
6269}
6270
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006271static int rtl8169_runtime_suspend(struct device *device)
6272{
6273 struct pci_dev *pdev = to_pci_dev(device);
6274 struct net_device *dev = pci_get_drvdata(pdev);
6275 struct rtl8169_private *tp = netdev_priv(dev);
6276
6277 if (!tp->TxDescArray)
6278 return 0;
6279
Francois Romieuda78dbf2012-01-26 14:18:23 +01006280 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006281 tp->saved_wolopts = __rtl8169_get_wol(tp);
6282 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006283 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006284
6285 rtl8169_net_suspend(dev);
6286
6287 return 0;
6288}
6289
6290static int rtl8169_runtime_resume(struct device *device)
6291{
6292 struct pci_dev *pdev = to_pci_dev(device);
6293 struct net_device *dev = pci_get_drvdata(pdev);
6294 struct rtl8169_private *tp = netdev_priv(dev);
6295
6296 if (!tp->TxDescArray)
6297 return 0;
6298
Francois Romieuda78dbf2012-01-26 14:18:23 +01006299 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006300 __rtl8169_set_wol(tp, tp->saved_wolopts);
6301 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006302 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006303
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006304 rtl8169_init_phy(dev, tp);
6305
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006306 __rtl8169_resume(dev);
6307
6308 return 0;
6309}
6310
6311static int rtl8169_runtime_idle(struct device *device)
6312{
6313 struct pci_dev *pdev = to_pci_dev(device);
6314 struct net_device *dev = pci_get_drvdata(pdev);
6315 struct rtl8169_private *tp = netdev_priv(dev);
6316
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00006317 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006318}
6319
Alexey Dobriyan47145212009-12-14 18:00:08 -08006320static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006321 .suspend = rtl8169_suspend,
6322 .resume = rtl8169_resume,
6323 .freeze = rtl8169_suspend,
6324 .thaw = rtl8169_resume,
6325 .poweroff = rtl8169_suspend,
6326 .restore = rtl8169_resume,
6327 .runtime_suspend = rtl8169_runtime_suspend,
6328 .runtime_resume = rtl8169_runtime_resume,
6329 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006330};
6331
6332#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6333
6334#else /* !CONFIG_PM */
6335
6336#define RTL8169_PM_OPS NULL
6337
6338#endif /* !CONFIG_PM */
6339
David S. Miller1805b2f2011-10-24 18:18:09 -04006340static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6341{
6342 void __iomem *ioaddr = tp->mmio_addr;
6343
6344 /* WoL fails with 8168b when the receiver is disabled. */
6345 switch (tp->mac_version) {
6346 case RTL_GIGA_MAC_VER_11:
6347 case RTL_GIGA_MAC_VER_12:
6348 case RTL_GIGA_MAC_VER_17:
6349 pci_clear_master(tp->pci_dev);
6350
6351 RTL_W8(ChipCmd, CmdRxEnb);
6352 /* PCI commit */
6353 RTL_R8(ChipCmd);
6354 break;
6355 default:
6356 break;
6357 }
6358}
6359
Francois Romieu1765f952008-09-13 17:21:40 +02006360static void rtl_shutdown(struct pci_dev *pdev)
6361{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006362 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006363 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00006364 struct device *d = &pdev->dev;
6365
6366 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02006367
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006368 rtl8169_net_suspend(dev);
6369
Francois Romieucecb5fd2011-04-01 10:21:07 +02006370 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006371 rtl_rar_set(tp, dev->perm_addr);
6372
Hayes Wang92fc43b2011-07-06 15:58:03 +08006373 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006374
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006375 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006376 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6377 rtl_wol_suspend_quirk(tp);
6378 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006379 }
6380
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006381 pci_wake_from_d3(pdev, true);
6382 pci_set_power_state(pdev, PCI_D3hot);
6383 }
françois romieu2a15cd22012-03-06 01:14:12 +00006384
6385 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006386}
Francois Romieu5d06a992006-02-23 00:47:58 +01006387
Francois Romieue27566e2012-03-08 09:54:01 +01006388static void __devexit rtl_remove_one(struct pci_dev *pdev)
6389{
6390 struct net_device *dev = pci_get_drvdata(pdev);
6391 struct rtl8169_private *tp = netdev_priv(dev);
6392
6393 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6394 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6395 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6396 rtl8168_driver_stop(tp);
6397 }
6398
6399 cancel_work_sync(&tp->wk.work);
6400
Devendra Nagaad1be8d2012-05-31 01:51:20 +00006401 netif_napi_del(&tp->napi);
6402
Francois Romieue27566e2012-03-08 09:54:01 +01006403 unregister_netdev(dev);
6404
6405 rtl_release_firmware(tp);
6406
6407 if (pci_dev_run_wake(pdev))
6408 pm_runtime_get_noresume(&pdev->dev);
6409
6410 /* restore original MAC address */
6411 rtl_rar_set(tp, dev->perm_addr);
6412
6413 rtl_disable_msi(pdev, tp);
6414 rtl8169_release_board(pdev, dev, tp->mmio_addr);
6415 pci_set_drvdata(pdev, NULL);
6416}
6417
Francois Romieufa9c3852012-03-08 10:01:50 +01006418static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01006419 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01006420 .ndo_stop = rtl8169_close,
6421 .ndo_get_stats64 = rtl8169_get_stats64,
6422 .ndo_start_xmit = rtl8169_start_xmit,
6423 .ndo_tx_timeout = rtl8169_tx_timeout,
6424 .ndo_validate_addr = eth_validate_addr,
6425 .ndo_change_mtu = rtl8169_change_mtu,
6426 .ndo_fix_features = rtl8169_fix_features,
6427 .ndo_set_features = rtl8169_set_features,
6428 .ndo_set_mac_address = rtl_set_mac_address,
6429 .ndo_do_ioctl = rtl8169_ioctl,
6430 .ndo_set_rx_mode = rtl_set_rx_mode,
6431#ifdef CONFIG_NET_POLL_CONTROLLER
6432 .ndo_poll_controller = rtl8169_netpoll,
6433#endif
6434
6435};
6436
Francois Romieu31fa8b12012-03-08 10:09:40 +01006437static const struct rtl_cfg_info {
6438 void (*hw_start)(struct net_device *);
6439 unsigned int region;
6440 unsigned int align;
6441 u16 event_slow;
6442 unsigned features;
6443 u8 default_ver;
6444} rtl_cfg_infos [] = {
6445 [RTL_CFG_0] = {
6446 .hw_start = rtl_hw_start_8169,
6447 .region = 1,
6448 .align = 0,
6449 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
6450 .features = RTL_FEATURE_GMII,
6451 .default_ver = RTL_GIGA_MAC_VER_01,
6452 },
6453 [RTL_CFG_1] = {
6454 .hw_start = rtl_hw_start_8168,
6455 .region = 2,
6456 .align = 8,
6457 .event_slow = SYSErr | LinkChg | RxOverflow,
6458 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
6459 .default_ver = RTL_GIGA_MAC_VER_11,
6460 },
6461 [RTL_CFG_2] = {
6462 .hw_start = rtl_hw_start_8101,
6463 .region = 2,
6464 .align = 8,
6465 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
6466 PCSTimeout,
6467 .features = RTL_FEATURE_MSI,
6468 .default_ver = RTL_GIGA_MAC_VER_13,
6469 }
6470};
6471
6472/* Cfg9346_Unlock assumed. */
6473static unsigned rtl_try_msi(struct rtl8169_private *tp,
6474 const struct rtl_cfg_info *cfg)
6475{
6476 void __iomem *ioaddr = tp->mmio_addr;
6477 unsigned msi = 0;
6478 u8 cfg2;
6479
6480 cfg2 = RTL_R8(Config2) & ~MSIEnable;
6481 if (cfg->features & RTL_FEATURE_MSI) {
6482 if (pci_enable_msi(tp->pci_dev)) {
6483 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
6484 } else {
6485 cfg2 |= MSIEnable;
6486 msi = RTL_FEATURE_MSI;
6487 }
6488 }
6489 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6490 RTL_W8(Config2, cfg2);
6491 return msi;
6492}
6493
Francois Romieu3b6cf252012-03-08 09:59:04 +01006494static int __devinit
6495rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6496{
6497 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
6498 const unsigned int region = cfg->region;
6499 struct rtl8169_private *tp;
6500 struct mii_if_info *mii;
6501 struct net_device *dev;
6502 void __iomem *ioaddr;
6503 int chipset, i;
6504 int rc;
6505
6506 if (netif_msg_drv(&debug)) {
6507 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
6508 MODULENAME, RTL8169_VERSION);
6509 }
6510
6511 dev = alloc_etherdev(sizeof (*tp));
6512 if (!dev) {
6513 rc = -ENOMEM;
6514 goto out;
6515 }
6516
6517 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01006518 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006519 tp = netdev_priv(dev);
6520 tp->dev = dev;
6521 tp->pci_dev = pdev;
6522 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
6523
6524 mii = &tp->mii;
6525 mii->dev = dev;
6526 mii->mdio_read = rtl_mdio_read;
6527 mii->mdio_write = rtl_mdio_write;
6528 mii->phy_id_mask = 0x1f;
6529 mii->reg_num_mask = 0x1f;
6530 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
6531
6532 /* disable ASPM completely as that cause random device stop working
6533 * problems as well as full system hangs for some PCIe devices users */
6534 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6535 PCIE_LINK_STATE_CLKPM);
6536
6537 /* enable device (incl. PCI PM wakeup and hotplug setup) */
6538 rc = pci_enable_device(pdev);
6539 if (rc < 0) {
6540 netif_err(tp, probe, dev, "enable failure\n");
6541 goto err_out_free_dev_1;
6542 }
6543
6544 if (pci_set_mwi(pdev) < 0)
6545 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
6546
6547 /* make sure PCI base addr 1 is MMIO */
6548 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
6549 netif_err(tp, probe, dev,
6550 "region #%d not an MMIO resource, aborting\n",
6551 region);
6552 rc = -ENODEV;
6553 goto err_out_mwi_2;
6554 }
6555
6556 /* check for weird/broken PCI region reporting */
6557 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
6558 netif_err(tp, probe, dev,
6559 "Invalid PCI region size(s), aborting\n");
6560 rc = -ENODEV;
6561 goto err_out_mwi_2;
6562 }
6563
6564 rc = pci_request_regions(pdev, MODULENAME);
6565 if (rc < 0) {
6566 netif_err(tp, probe, dev, "could not request regions\n");
6567 goto err_out_mwi_2;
6568 }
6569
6570 tp->cp_cmd = RxChkSum;
6571
6572 if ((sizeof(dma_addr_t) > 4) &&
6573 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
6574 tp->cp_cmd |= PCIDAC;
6575 dev->features |= NETIF_F_HIGHDMA;
6576 } else {
6577 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6578 if (rc < 0) {
6579 netif_err(tp, probe, dev, "DMA configuration failed\n");
6580 goto err_out_free_res_3;
6581 }
6582 }
6583
6584 /* ioremap MMIO region */
6585 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
6586 if (!ioaddr) {
6587 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
6588 rc = -EIO;
6589 goto err_out_free_res_3;
6590 }
6591 tp->mmio_addr = ioaddr;
6592
6593 if (!pci_is_pcie(pdev))
6594 netif_info(tp, probe, dev, "not PCI Express\n");
6595
6596 /* Identify chip attached to board */
6597 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
6598
6599 rtl_init_rxcfg(tp);
6600
6601 rtl_irq_disable(tp);
6602
6603 rtl_hw_reset(tp);
6604
6605 rtl_ack_events(tp, 0xffff);
6606
6607 pci_set_master(pdev);
6608
6609 /*
6610 * Pretend we are using VLANs; This bypasses a nasty bug where
6611 * Interrupts stop flowing on high load on 8110SCd controllers.
6612 */
6613 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6614 tp->cp_cmd |= RxVlan;
6615
6616 rtl_init_mdio_ops(tp);
6617 rtl_init_pll_power_ops(tp);
6618 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006619 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006620
6621 rtl8169_print_mac_version(tp);
6622
6623 chipset = tp->mac_version;
6624 tp->txd_version = rtl_chip_infos[chipset].txd_version;
6625
6626 RTL_W8(Cfg9346, Cfg9346_Unlock);
6627 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
6628 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
6629 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
6630 tp->features |= RTL_FEATURE_WOL;
6631 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
6632 tp->features |= RTL_FEATURE_WOL;
6633 tp->features |= rtl_try_msi(tp, cfg);
6634 RTL_W8(Cfg9346, Cfg9346_Lock);
6635
6636 if (rtl_tbi_enabled(tp)) {
6637 tp->set_speed = rtl8169_set_speed_tbi;
6638 tp->get_settings = rtl8169_gset_tbi;
6639 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
6640 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
6641 tp->link_ok = rtl8169_tbi_link_ok;
6642 tp->do_ioctl = rtl_tbi_ioctl;
6643 } else {
6644 tp->set_speed = rtl8169_set_speed_xmii;
6645 tp->get_settings = rtl8169_gset_xmii;
6646 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
6647 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
6648 tp->link_ok = rtl8169_xmii_link_ok;
6649 tp->do_ioctl = rtl_xmii_ioctl;
6650 }
6651
6652 mutex_init(&tp->wk.mutex);
6653
6654 /* Get MAC address */
6655 for (i = 0; i < ETH_ALEN; i++)
6656 dev->dev_addr[i] = RTL_R8(MAC0 + i);
6657 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
6658
6659 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
6660 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006661
6662 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
6663
6664 /* don't enable SG, IP_CSUM and TSO by default - it might not work
6665 * properly for all devices */
6666 dev->features |= NETIF_F_RXCSUM |
6667 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6668
6669 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6670 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6671 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6672 NETIF_F_HIGHDMA;
6673
6674 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6675 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
6676 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
6677
6678 dev->hw_features |= NETIF_F_RXALL;
6679 dev->hw_features |= NETIF_F_RXFCS;
6680
6681 tp->hw_start = cfg->hw_start;
6682 tp->event_slow = cfg->event_slow;
6683
6684 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
6685 ~(RxBOVF | RxFOVF) : ~0;
6686
6687 init_timer(&tp->timer);
6688 tp->timer.data = (unsigned long) dev;
6689 tp->timer.function = rtl8169_phy_timer;
6690
6691 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
6692
6693 rc = register_netdev(dev);
6694 if (rc < 0)
6695 goto err_out_msi_4;
6696
6697 pci_set_drvdata(pdev, dev);
6698
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006699 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
6700 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
6701 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006702 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
6703 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
6704 "tx checksumming: %s]\n",
6705 rtl_chip_infos[chipset].jumbo_max,
6706 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
6707 }
6708
6709 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6710 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6711 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6712 rtl8168_driver_start(tp);
6713 }
6714
6715 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
6716
6717 if (pci_dev_run_wake(pdev))
6718 pm_runtime_put_noidle(&pdev->dev);
6719
6720 netif_carrier_off(dev);
6721
6722out:
6723 return rc;
6724
6725err_out_msi_4:
Devendra Nagaad1be8d2012-05-31 01:51:20 +00006726 netif_napi_del(&tp->napi);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006727 rtl_disable_msi(pdev, tp);
6728 iounmap(ioaddr);
6729err_out_free_res_3:
6730 pci_release_regions(pdev);
6731err_out_mwi_2:
6732 pci_clear_mwi(pdev);
6733 pci_disable_device(pdev);
6734err_out_free_dev_1:
6735 free_netdev(dev);
6736 goto out;
6737}
6738
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739static struct pci_driver rtl8169_pci_driver = {
6740 .name = MODULENAME,
6741 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01006742 .probe = rtl_init_one,
Francois Romieue27566e2012-03-08 09:54:01 +01006743 .remove = __devexit_p(rtl_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02006744 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006745 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746};
6747
Francois Romieu07d3f512007-02-21 22:40:46 +01006748static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749{
Jeff Garzik29917622006-08-19 17:48:59 -04006750 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751}
6752
Francois Romieu07d3f512007-02-21 22:40:46 +01006753static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754{
6755 pci_unregister_driver(&rtl8169_pci_driver);
6756}
6757
6758module_init(rtl8169_init_module);
6759module_exit(rtl8169_cleanup_module);