blob: 5360f6de2b0ae9c6f0808d0baa2898432a884d08 [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 Wang4f6b00e2011-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 Wang4f6b00e2011-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 Wang4f6b00e2011-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 Wang4f6b00e2011-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 Wang4f6b00e2011-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 Wang4f6b00e2011-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 Wang4f6b00e2011-07-06 15:58:02 +0800467 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100468 CmdReset = 0x10,
469 CmdRxEnb = 0x08,
470 CmdTxEnb = 0x04,
471 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Francois Romieu275391a2007-02-23 23:50:28 +0100473 /* TXPoll register p.5 */
474 HPQ = 0x80, /* Poll cmd on the high prio queue */
475 NPQ = 0x40, /* Poll cmd on the low prio queue */
476 FSWInt = 0x01, /* Forced software interrupt */
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100479 Cfg9346_Lock = 0x00,
480 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100483 AcceptErr = 0x20,
484 AcceptRunt = 0x10,
485 AcceptBroadcast = 0x08,
486 AcceptMulticast = 0x04,
487 AcceptMyPhys = 0x02,
488 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200489#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /* TxConfigBits */
492 TxInterFrameGapShift = 24,
493 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
494
Francois Romieu5d06a992006-02-23 00:47:58 +0100495 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200496 LEDS1 = (1 << 7),
497 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200498 Speed_down = (1 << 4),
499 MEMMAP = (1 << 3),
500 IOMAP = (1 << 2),
501 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100502 PMEnable = (1 << 0), /* Power Management Enable */
503
Francois Romieu6dccd162007-02-13 23:38:05 +0100504 /* Config2 register p. 25 */
françois romieu2ca6cf02011-12-15 08:37:43 +0000505 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100506 PCI_Clock_66MHz = 0x01,
507 PCI_Clock_33MHz = 0x00,
508
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100509 /* Config3 register p.25 */
510 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
511 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200512 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200513 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100514
Francois Romieud58d46b2011-05-03 16:38:29 +0200515 /* Config4 register */
516 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
517
Francois Romieu5d06a992006-02-23 00:47:58 +0100518 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100519 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
520 MWF = (1 << 5), /* Accept Multicast wakeup frame */
521 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200522 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100523 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100524 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* TBICSR p.28 */
527 TBIReset = 0x80000000,
528 TBILoopback = 0x40000000,
529 TBINwEnable = 0x20000000,
530 TBINwRestart = 0x10000000,
531 TBILinkOk = 0x02000000,
532 TBINwComplete = 0x01000000,
533
534 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200535 EnableBist = (1 << 15), // 8168 8101
536 Mac_dbgo_oe = (1 << 14), // 8168 8101
537 Normal_mode = (1 << 13), // unused
538 Force_half_dup = (1 << 12), // 8168 8101
539 Force_rxflow_en = (1 << 11), // 8168 8101
540 Force_txflow_en = (1 << 10), // 8168 8101
541 Cxpl_dbg_sel = (1 << 9), // 8168 8101
542 ASF = (1 << 8), // 8168 8101
543 PktCntrDisable = (1 << 7), // 8168 8101
544 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 RxVlan = (1 << 6),
546 RxChkSum = (1 << 5),
547 PCIDAC = (1 << 4),
548 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100549 INTT_0 = 0x0000, // 8168
550 INTT_1 = 0x0001, // 8168
551 INTT_2 = 0x0002, // 8168
552 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100555 TBI_Enable = 0x80,
556 TxFlowCtrl = 0x40,
557 RxFlowCtrl = 0x20,
558 _1000bpsF = 0x10,
559 _100bps = 0x08,
560 _10bps = 0x04,
561 LinkStatus = 0x02,
562 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100565 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200566
567 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100568 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569};
570
Francois Romieu2b7b4312011-04-18 22:53:24 -0700571enum rtl_desc_bit {
572 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
574 RingEnd = (1 << 30), /* End of descriptor ring */
575 FirstFrag = (1 << 29), /* First segment of a packet */
576 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700577};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Francois Romieu2b7b4312011-04-18 22:53:24 -0700579/* Generic case. */
580enum rtl_tx_desc_bit {
581 /* First doubleword. */
582 TD_LSO = (1 << 27), /* Large Send Offload */
583#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Francois Romieu2b7b4312011-04-18 22:53:24 -0700585 /* Second doubleword. */
586 TxVlanTag = (1 << 17), /* Add VLAN tag */
587};
588
589/* 8169, 8168b and 810x except 8102e. */
590enum rtl_tx_desc_bit_0 {
591 /* First doubleword. */
592#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
593 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
594 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
595 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
596};
597
598/* 8102e, 8168c and beyond. */
599enum rtl_tx_desc_bit_1 {
600 /* Second doubleword. */
601#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
602 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
603 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
604 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
605};
606
607static const struct rtl_tx_desc_info {
608 struct {
609 u32 udp;
610 u32 tcp;
611 } checksum;
612 u16 mss_shift;
613 u16 opts_offset;
614} tx_desc_info [] = {
615 [RTL_TD_0] = {
616 .checksum = {
617 .udp = TD0_IP_CS | TD0_UDP_CS,
618 .tcp = TD0_IP_CS | TD0_TCP_CS
619 },
620 .mss_shift = TD0_MSS_SHIFT,
621 .opts_offset = 0
622 },
623 [RTL_TD_1] = {
624 .checksum = {
625 .udp = TD1_IP_CS | TD1_UDP_CS,
626 .tcp = TD1_IP_CS | TD1_TCP_CS
627 },
628 .mss_shift = TD1_MSS_SHIFT,
629 .opts_offset = 1
630 }
631};
632
633enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /* Rx private */
635 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
636 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
637
638#define RxProtoUDP (PID1)
639#define RxProtoTCP (PID0)
640#define RxProtoIP (PID1 | PID0)
641#define RxProtoMask RxProtoIP
642
643 IPFail = (1 << 16), /* IP checksum failed */
644 UDPFail = (1 << 15), /* UDP/IP checksum failed */
645 TCPFail = (1 << 14), /* TCP/IP checksum failed */
646 RxVlanTag = (1 << 16), /* VLAN tag available */
647};
648
649#define RsvdMask 0x3fffc000
650
651struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200652 __le32 opts1;
653 __le32 opts2;
654 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655};
656
657struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200658 __le32 opts1;
659 __le32 opts2;
660 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661};
662
663struct ring_info {
664 struct sk_buff *skb;
665 u32 len;
666 u8 __pad[sizeof(void *) - sizeof(u32)];
667};
668
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200669enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200670 RTL_FEATURE_WOL = (1 << 0),
671 RTL_FEATURE_MSI = (1 << 1),
672 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200673};
674
Ivan Vecera355423d2009-02-06 21:49:57 -0800675struct rtl8169_counters {
676 __le64 tx_packets;
677 __le64 rx_packets;
678 __le64 tx_errors;
679 __le32 rx_errors;
680 __le16 rx_missed;
681 __le16 align_errors;
682 __le32 tx_one_collision;
683 __le32 tx_multi_collision;
684 __le64 rx_unicast;
685 __le64 rx_broadcast;
686 __le32 rx_multicast;
687 __le16 tx_aborted;
688 __le16 tx_underun;
689};
690
Francois Romieuda78dbf2012-01-26 14:18:23 +0100691enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100692 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100693 RTL_FLAG_TASK_SLOW_PENDING,
694 RTL_FLAG_TASK_RESET_PENDING,
695 RTL_FLAG_TASK_PHY_PENDING,
696 RTL_FLAG_MAX
697};
698
Junchang Wang8027aa22012-03-04 23:30:32 +0100699struct rtl8169_stats {
700 u64 packets;
701 u64 bytes;
702 struct u64_stats_sync syncp;
703};
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705struct rtl8169_private {
706 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200707 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000708 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700709 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200710 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700711 u16 txd_version;
712 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
714 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
715 u32 dirty_rx;
716 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100717 struct rtl8169_stats rx_stats;
718 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
720 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
721 dma_addr_t TxPhyAddr;
722 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000723 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 struct timer_list timer;
726 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100727
728 u16 event_slow;
françois romieuc0e45c12011-01-03 15:08:04 +0000729
730 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200731 void (*write)(struct rtl8169_private *, int, int);
732 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000733 } mdio_ops;
734
françois romieu065c27c2011-01-03 15:08:12 +0000735 struct pll_power_ops {
736 void (*down)(struct rtl8169_private *);
737 void (*up)(struct rtl8169_private *);
738 } pll_power_ops;
739
Francois Romieud58d46b2011-05-03 16:38:29 +0200740 struct jumbo_ops {
741 void (*enable)(struct rtl8169_private *);
742 void (*disable)(struct rtl8169_private *);
743 } jumbo_ops;
744
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800745 struct csi_ops {
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
Francois Romieu24192212012-07-06 20:19:42 +0200922static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Francois Romieu24192212012-07-06 20:19:42 +0200924 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 int i;
926
Francois Romieu24192212012-07-06 20:19:42 +0200927 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Francois Romieu23714082006-01-29 00:49:09 +0100929 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100930 /*
931 * Check if the RTL8169 has completed writing to the specified
932 * MII register.
933 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200934 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 break;
Francois Romieu23714082006-01-29 00:49:09 +0100936 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700938 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700939 * According to hardware specs a 20us delay is required after write
940 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700941 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700942 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
Francois Romieu24192212012-07-06 20:19:42 +0200945static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Francois Romieu24192212012-07-06 20:19:42 +0200947 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 int i, value = -1;
949
Francois Romieu24192212012-07-06 20:19:42 +0200950 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Francois Romieu23714082006-01-29 00:49:09 +0100952 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100953 /*
954 * Check if the RTL8169 has completed retrieving data from
955 * the specified MII register.
956 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100958 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 break;
960 }
Francois Romieu23714082006-01-29 00:49:09 +0100961 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700963 /*
964 * According to hardware specs a 20us delay is required after read
965 * complete indication, but before sending next command.
966 */
967 udelay(20);
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return value;
970}
971
Francois Romieu24192212012-07-06 20:19:42 +0200972static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +0000973{
Francois Romieu24192212012-07-06 20:19:42 +0200974 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +0000975 int i;
976
Francois Romieu24192212012-07-06 20:19:42 +0200977 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
françois romieuc0e45c12011-01-03 15:08:04 +0000978 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
979 RTL_W32(EPHY_RXER_NUM, 0);
980
981 for (i = 0; i < 100; i++) {
982 mdelay(1);
983 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
984 break;
985 }
986}
987
Francois Romieu24192212012-07-06 20:19:42 +0200988static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +0000989{
Francois Romieu24192212012-07-06 20:19:42 +0200990 r8168dp_1_mdio_access(tp, reg,
991 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +0000992}
993
Francois Romieu24192212012-07-06 20:19:42 +0200994static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +0000995{
Francois Romieu24192212012-07-06 20:19:42 +0200996 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +0000997 int i;
998
Francois Romieu24192212012-07-06 20:19:42 +0200999 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001000
1001 mdelay(1);
1002 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1003 RTL_W32(EPHY_RXER_NUM, 0);
1004
1005 for (i = 0; i < 100; i++) {
1006 mdelay(1);
1007 if (RTL_R32(OCPAR) & OCPAR_FLAG)
1008 break;
1009 }
1010
1011 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
1012}
1013
françois romieue6de30d2011-01-03 15:08:37 +00001014#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1015
1016static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1017{
1018 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1019}
1020
1021static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1022{
1023 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1024}
1025
Francois Romieu24192212012-07-06 20:19:42 +02001026static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001027{
Francois Romieu24192212012-07-06 20:19:42 +02001028 void __iomem *ioaddr = tp->mmio_addr;
1029
françois romieue6de30d2011-01-03 15:08:37 +00001030 r8168dp_2_mdio_start(ioaddr);
1031
Francois Romieu24192212012-07-06 20:19:42 +02001032 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001033
1034 r8168dp_2_mdio_stop(ioaddr);
1035}
1036
Francois Romieu24192212012-07-06 20:19:42 +02001037static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001038{
Francois Romieu24192212012-07-06 20:19:42 +02001039 void __iomem *ioaddr = tp->mmio_addr;
françois romieue6de30d2011-01-03 15:08:37 +00001040 int value;
1041
1042 r8168dp_2_mdio_start(ioaddr);
1043
Francois Romieu24192212012-07-06 20:19:42 +02001044 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001045
1046 r8168dp_2_mdio_stop(ioaddr);
1047
1048 return value;
1049}
1050
françois romieu4da19632011-01-03 15:07:55 +00001051static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001052{
Francois Romieu24192212012-07-06 20:19:42 +02001053 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001054}
1055
françois romieu4da19632011-01-03 15:07:55 +00001056static int rtl_readphy(struct rtl8169_private *tp, int location)
1057{
Francois Romieu24192212012-07-06 20:19:42 +02001058 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001059}
1060
1061static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1062{
1063 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1064}
1065
1066static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001067{
1068 int val;
1069
françois romieu4da19632011-01-03 15:07:55 +00001070 val = rtl_readphy(tp, reg_addr);
1071 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001072}
1073
Francois Romieuccdffb92008-07-26 14:26:06 +02001074static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1075 int val)
1076{
1077 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001078
françois romieu4da19632011-01-03 15:07:55 +00001079 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001080}
1081
1082static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1083{
1084 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001085
françois romieu4da19632011-01-03 15:07:55 +00001086 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001087}
1088
Francois Romieudacf8152008-08-02 20:44:13 +02001089static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
1090{
1091 unsigned int i;
1092
1093 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1094 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1095
1096 for (i = 0; i < 100; i++) {
1097 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1098 break;
1099 udelay(10);
1100 }
1101}
1102
1103static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1104{
1105 u16 value = 0xffff;
1106 unsigned int i;
1107
1108 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1109
1110 for (i = 0; i < 100; i++) {
1111 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1112 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1113 break;
1114 }
1115 udelay(10);
1116 }
1117
1118 return value;
1119}
1120
Hayes Wang133ac402011-07-06 15:58:05 +08001121static
1122void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1123{
1124 unsigned int i;
1125
1126 BUG_ON((addr & 3) || (mask == 0));
1127 RTL_W32(ERIDR, val);
1128 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1129
1130 for (i = 0; i < 100; i++) {
1131 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1132 break;
1133 udelay(100);
1134 }
1135}
1136
1137static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1138{
1139 u32 value = ~0x00;
1140 unsigned int i;
1141
1142 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1143
1144 for (i = 0; i < 100; i++) {
1145 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1146 value = RTL_R32(ERIDR);
1147 break;
1148 }
1149 udelay(100);
1150 }
1151
1152 return value;
1153}
1154
1155static void
1156rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1157{
1158 u32 val;
1159
1160 val = rtl_eri_read(ioaddr, addr, type);
1161 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1162}
1163
françois romieuc28aa382011-08-02 03:53:43 +00001164struct exgmac_reg {
1165 u16 addr;
1166 u16 mask;
1167 u32 val;
1168};
1169
1170static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1171 const struct exgmac_reg *r, int len)
1172{
1173 while (len-- > 0) {
1174 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1175 r++;
1176 }
1177}
1178
françois romieudaf9df62009-10-07 12:44:20 +00001179static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1180{
1181 u8 value = 0xff;
1182 unsigned int i;
1183
1184 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1185
1186 for (i = 0; i < 300; i++) {
1187 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1188 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1189 break;
1190 }
1191 udelay(100);
1192 }
1193
1194 return value;
1195}
1196
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001197static u16 rtl_get_events(struct rtl8169_private *tp)
1198{
1199 void __iomem *ioaddr = tp->mmio_addr;
1200
1201 return RTL_R16(IntrStatus);
1202}
1203
1204static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1205{
1206 void __iomem *ioaddr = tp->mmio_addr;
1207
1208 RTL_W16(IntrStatus, bits);
1209 mmiowb();
1210}
1211
1212static void rtl_irq_disable(struct rtl8169_private *tp)
1213{
1214 void __iomem *ioaddr = tp->mmio_addr;
1215
1216 RTL_W16(IntrMask, 0);
1217 mmiowb();
1218}
1219
Francois Romieu3e990ff2012-01-26 12:50:01 +01001220static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1221{
1222 void __iomem *ioaddr = tp->mmio_addr;
1223
1224 RTL_W16(IntrMask, bits);
1225}
1226
Francois Romieuda78dbf2012-01-26 14:18:23 +01001227#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1228#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1229#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1230
1231static void rtl_irq_enable_all(struct rtl8169_private *tp)
1232{
1233 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1234}
1235
françois romieu811fd302011-12-04 20:30:45 +00001236static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
françois romieu811fd302011-12-04 20:30:45 +00001238 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001240 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001241 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001242 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
françois romieu4da19632011-01-03 15:07:55 +00001245static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
françois romieu4da19632011-01-03 15:07:55 +00001247 void __iomem *ioaddr = tp->mmio_addr;
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return RTL_R32(TBICSR) & TBIReset;
1250}
1251
françois romieu4da19632011-01-03 15:07:55 +00001252static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
françois romieu4da19632011-01-03 15:07:55 +00001254 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
1257static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1258{
1259 return RTL_R32(TBICSR) & TBILinkOk;
1260}
1261
1262static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1263{
1264 return RTL_R8(PHYstatus) & LinkStatus;
1265}
1266
françois romieu4da19632011-01-03 15:07:55 +00001267static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
françois romieu4da19632011-01-03 15:07:55 +00001269 void __iomem *ioaddr = tp->mmio_addr;
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1272}
1273
françois romieu4da19632011-01-03 15:07:55 +00001274static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
1276 unsigned int val;
1277
françois romieu4da19632011-01-03 15:07:55 +00001278 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1279 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
Hayes Wang70090422011-07-06 15:58:06 +08001282static void rtl_link_chg_patch(struct rtl8169_private *tp)
1283{
1284 void __iomem *ioaddr = tp->mmio_addr;
1285 struct net_device *dev = tp->dev;
1286
1287 if (!netif_running(dev))
1288 return;
1289
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001290 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1291 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Hayes Wang70090422011-07-06 15:58:06 +08001292 if (RTL_R8(PHYstatus) & _1000bpsF) {
1293 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1294 0x00000011, ERIAR_EXGMAC);
1295 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1296 0x00000005, ERIAR_EXGMAC);
1297 } else if (RTL_R8(PHYstatus) & _100bps) {
1298 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1299 0x0000001f, ERIAR_EXGMAC);
1300 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1301 0x00000005, ERIAR_EXGMAC);
1302 } else {
1303 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1304 0x0000001f, ERIAR_EXGMAC);
1305 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1306 0x0000003f, ERIAR_EXGMAC);
1307 }
1308 /* Reset packet filter */
1309 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1310 ERIAR_EXGMAC);
1311 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1312 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001313 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1314 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1315 if (RTL_R8(PHYstatus) & _1000bpsF) {
1316 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1317 0x00000011, ERIAR_EXGMAC);
1318 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1319 0x00000005, ERIAR_EXGMAC);
1320 } else {
1321 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1322 0x0000001f, ERIAR_EXGMAC);
1323 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1324 0x0000003f, ERIAR_EXGMAC);
1325 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001326 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1327 if (RTL_R8(PHYstatus) & _10bps) {
1328 rtl_eri_write(ioaddr, 0x1d0, ERIAR_MASK_0011,
1329 0x4d02, ERIAR_EXGMAC);
1330 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_0011,
1331 0x0060, ERIAR_EXGMAC);
1332 } else {
1333 rtl_eri_write(ioaddr, 0x1d0, ERIAR_MASK_0011,
1334 0x0000, ERIAR_EXGMAC);
1335 }
Hayes Wang70090422011-07-06 15:58:06 +08001336 }
1337}
1338
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001339static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001340 struct rtl8169_private *tp,
1341 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001344 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001345 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001346 if (pm)
1347 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001349 if (net_ratelimit())
1350 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001351 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001353 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001354 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001355 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001359static void rtl8169_check_link_status(struct net_device *dev,
1360 struct rtl8169_private *tp,
1361 void __iomem *ioaddr)
1362{
1363 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1364}
1365
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001366#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1367
1368static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1369{
1370 void __iomem *ioaddr = tp->mmio_addr;
1371 u8 options;
1372 u32 wolopts = 0;
1373
1374 options = RTL_R8(Config1);
1375 if (!(options & PMEnable))
1376 return 0;
1377
1378 options = RTL_R8(Config3);
1379 if (options & LinkUp)
1380 wolopts |= WAKE_PHY;
1381 if (options & MagicPacket)
1382 wolopts |= WAKE_MAGIC;
1383
1384 options = RTL_R8(Config5);
1385 if (options & UWF)
1386 wolopts |= WAKE_UCAST;
1387 if (options & BWF)
1388 wolopts |= WAKE_BCAST;
1389 if (options & MWF)
1390 wolopts |= WAKE_MCAST;
1391
1392 return wolopts;
1393}
1394
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001395static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1396{
1397 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001398
Francois Romieuda78dbf2012-01-26 14:18:23 +01001399 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001400
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001401 wol->supported = WAKE_ANY;
1402 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001403
Francois Romieuda78dbf2012-01-26 14:18:23 +01001404 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001405}
1406
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001407static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001408{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001409 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001410 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001411 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001412 u32 opt;
1413 u16 reg;
1414 u8 mask;
1415 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001416 { WAKE_PHY, Config3, LinkUp },
1417 { WAKE_MAGIC, Config3, MagicPacket },
1418 { WAKE_UCAST, Config5, UWF },
1419 { WAKE_BCAST, Config5, BWF },
1420 { WAKE_MCAST, Config5, MWF },
1421 { WAKE_ANY, Config5, LanWake }
1422 };
Francois Romieu851e6022012-04-17 11:10:11 +02001423 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001424
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001425 RTL_W8(Cfg9346, Cfg9346_Unlock);
1426
1427 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
Francois Romieu851e6022012-04-17 11:10:11 +02001428 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001429 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001430 options |= cfg[i].mask;
1431 RTL_W8(cfg[i].reg, options);
1432 }
1433
Francois Romieu851e6022012-04-17 11:10:11 +02001434 switch (tp->mac_version) {
1435 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1436 options = RTL_R8(Config1) & ~PMEnable;
1437 if (wolopts)
1438 options |= PMEnable;
1439 RTL_W8(Config1, options);
1440 break;
1441 default:
Francois Romieud387b422012-04-17 11:12:01 +02001442 options = RTL_R8(Config2) & ~PME_SIGNAL;
1443 if (wolopts)
1444 options |= PME_SIGNAL;
1445 RTL_W8(Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001446 break;
1447 }
1448
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001449 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001450}
1451
1452static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1453{
1454 struct rtl8169_private *tp = netdev_priv(dev);
1455
Francois Romieuda78dbf2012-01-26 14:18:23 +01001456 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001457
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001458 if (wol->wolopts)
1459 tp->features |= RTL_FEATURE_WOL;
1460 else
1461 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001462 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001463
1464 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001465
françois romieuea809072010-11-08 13:23:58 +00001466 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1467
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001468 return 0;
1469}
1470
Francois Romieu31bd2042011-04-26 18:58:59 +02001471static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1472{
Francois Romieu85bffe62011-04-27 08:22:39 +02001473 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001474}
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476static void rtl8169_get_drvinfo(struct net_device *dev,
1477 struct ethtool_drvinfo *info)
1478{
1479 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001480 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Rick Jones68aad782011-11-07 13:29:27 +00001482 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1483 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1484 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001485 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001486 if (!IS_ERR_OR_NULL(rtl_fw))
1487 strlcpy(info->fw_version, rtl_fw->version,
1488 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
1491static int rtl8169_get_regs_len(struct net_device *dev)
1492{
1493 return R8169_REGS_SIZE;
1494}
1495
1496static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001497 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
1499 struct rtl8169_private *tp = netdev_priv(dev);
1500 void __iomem *ioaddr = tp->mmio_addr;
1501 int ret = 0;
1502 u32 reg;
1503
1504 reg = RTL_R32(TBICSR);
1505 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1506 (duplex == DUPLEX_FULL)) {
1507 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1508 } else if (autoneg == AUTONEG_ENABLE)
1509 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1510 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001511 netif_warn(tp, link, dev,
1512 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 ret = -EOPNOTSUPP;
1514 }
1515
1516 return ret;
1517}
1518
1519static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001520 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001523 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001524 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Hayes Wang716b50a2011-02-22 17:26:18 +08001526 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001529 int auto_nego;
1530
françois romieu4da19632011-01-03 15:07:55 +00001531 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001532 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1533 ADVERTISE_100HALF | ADVERTISE_100FULL);
1534
1535 if (adv & ADVERTISED_10baseT_Half)
1536 auto_nego |= ADVERTISE_10HALF;
1537 if (adv & ADVERTISED_10baseT_Full)
1538 auto_nego |= ADVERTISE_10FULL;
1539 if (adv & ADVERTISED_100baseT_Half)
1540 auto_nego |= ADVERTISE_100HALF;
1541 if (adv & ADVERTISED_100baseT_Full)
1542 auto_nego |= ADVERTISE_100FULL;
1543
françois romieu3577aa12009-05-19 10:46:48 +00001544 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1545
françois romieu4da19632011-01-03 15:07:55 +00001546 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001547 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1548
1549 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001550 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001551 if (adv & ADVERTISED_1000baseT_Half)
1552 giga_ctrl |= ADVERTISE_1000HALF;
1553 if (adv & ADVERTISED_1000baseT_Full)
1554 giga_ctrl |= ADVERTISE_1000FULL;
1555 } else if (adv & (ADVERTISED_1000baseT_Half |
1556 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001557 netif_info(tp, link, dev,
1558 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001559 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
françois romieu3577aa12009-05-19 10:46:48 +00001562 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001563
françois romieu4da19632011-01-03 15:07:55 +00001564 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1565 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001566 } else {
1567 giga_ctrl = 0;
1568
1569 if (speed == SPEED_10)
1570 bmcr = 0;
1571 else if (speed == SPEED_100)
1572 bmcr = BMCR_SPEED100;
1573 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001574 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001575
1576 if (duplex == DUPLEX_FULL)
1577 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001578 }
1579
françois romieu4da19632011-01-03 15:07:55 +00001580 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001581
Francois Romieucecb5fd2011-04-01 10:21:07 +02001582 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1583 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001584 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001585 rtl_writephy(tp, 0x17, 0x2138);
1586 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001587 } else {
françois romieu4da19632011-01-03 15:07:55 +00001588 rtl_writephy(tp, 0x17, 0x2108);
1589 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001590 }
1591 }
1592
Oliver Neukum54405cd2011-01-06 21:55:13 +01001593 rc = 0;
1594out:
1595 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596}
1597
1598static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001599 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
1601 struct rtl8169_private *tp = netdev_priv(dev);
1602 int ret;
1603
Oliver Neukum54405cd2011-01-06 21:55:13 +01001604 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001605 if (ret < 0)
1606 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Francois Romieu4876cc12011-03-11 21:07:11 +01001608 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1609 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001611 }
1612out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 return ret;
1614}
1615
1616static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1617{
1618 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 int ret;
1620
Francois Romieu4876cc12011-03-11 21:07:11 +01001621 del_timer_sync(&tp->timer);
1622
Francois Romieuda78dbf2012-01-26 14:18:23 +01001623 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001624 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001625 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001626 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 return ret;
1629}
1630
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001631static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1632 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
Francois Romieud58d46b2011-05-03 16:38:29 +02001634 struct rtl8169_private *tp = netdev_priv(dev);
1635
Francois Romieu2b7b4312011-04-18 22:53:24 -07001636 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001637 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Francois Romieud58d46b2011-05-03 16:38:29 +02001639 if (dev->mtu > JUMBO_1K &&
1640 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1641 features &= ~NETIF_F_IP_CSUM;
1642
Michał Mirosław350fb322011-04-08 06:35:56 +00001643 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
1645
Francois Romieuda78dbf2012-01-26 14:18:23 +01001646static void __rtl8169_set_features(struct net_device *dev,
1647 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
1649 struct rtl8169_private *tp = netdev_priv(dev);
Ben Greear6bbe0212012-02-10 15:04:33 +00001650 netdev_features_t changed = features ^ dev->features;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001651 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Ben Greear6bbe0212012-02-10 15:04:33 +00001653 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)))
1654 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Ben Greear6bbe0212012-02-10 15:04:33 +00001656 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)) {
1657 if (features & NETIF_F_RXCSUM)
1658 tp->cp_cmd |= RxChkSum;
1659 else
1660 tp->cp_cmd &= ~RxChkSum;
Michał Mirosław350fb322011-04-08 06:35:56 +00001661
Ben Greear6bbe0212012-02-10 15:04:33 +00001662 if (dev->features & NETIF_F_HW_VLAN_RX)
1663 tp->cp_cmd |= RxVlan;
1664 else
1665 tp->cp_cmd &= ~RxVlan;
1666
1667 RTL_W16(CPlusCmd, tp->cp_cmd);
1668 RTL_R16(CPlusCmd);
1669 }
1670 if (changed & NETIF_F_RXALL) {
1671 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1672 if (features & NETIF_F_RXALL)
1673 tmp |= (AcceptErr | AcceptRunt);
1674 RTL_W32(RxConfig, tmp);
1675 }
Francois Romieuda78dbf2012-01-26 14:18:23 +01001676}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Francois Romieuda78dbf2012-01-26 14:18:23 +01001678static int rtl8169_set_features(struct net_device *dev,
1679 netdev_features_t features)
1680{
1681 struct rtl8169_private *tp = netdev_priv(dev);
1682
1683 rtl_lock_work(tp);
1684 __rtl8169_set_features(dev, features);
1685 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 return 0;
1688}
1689
Francois Romieuda78dbf2012-01-26 14:18:23 +01001690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1692 struct sk_buff *skb)
1693{
Jesse Grosseab6d182010-10-20 13:56:03 +00001694 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1696}
1697
Francois Romieu7a8fc772011-03-01 17:18:33 +01001698static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
1700 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Francois Romieu7a8fc772011-03-01 17:18:33 +01001702 if (opts2 & RxVlanTag)
1703 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
Francois Romieuccdffb92008-07-26 14:26:06 +02001708static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
1710 struct rtl8169_private *tp = netdev_priv(dev);
1711 void __iomem *ioaddr = tp->mmio_addr;
1712 u32 status;
1713
1714 cmd->supported =
1715 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1716 cmd->port = PORT_FIBRE;
1717 cmd->transceiver = XCVR_INTERNAL;
1718
1719 status = RTL_R32(TBICSR);
1720 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1721 cmd->autoneg = !!(status & TBINwEnable);
1722
David Decotigny70739492011-04-27 18:32:40 +00001723 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001725
1726 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
Francois Romieuccdffb92008-07-26 14:26:06 +02001729static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
1731 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Francois Romieuccdffb92008-07-26 14:26:06 +02001733 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734}
1735
1736static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1737{
1738 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001739 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Francois Romieuda78dbf2012-01-26 14:18:23 +01001741 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02001742 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001743 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Francois Romieuccdffb92008-07-26 14:26:06 +02001745 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746}
1747
1748static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1749 void *p)
1750{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001751 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Francois Romieu5b0384f2006-08-16 16:00:01 +02001753 if (regs->len > R8169_REGS_SIZE)
1754 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Francois Romieuda78dbf2012-01-26 14:18:23 +01001756 rtl_lock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001757 memcpy_fromio(p, tp->mmio_addr, regs->len);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001758 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001761static u32 rtl8169_get_msglevel(struct net_device *dev)
1762{
1763 struct rtl8169_private *tp = netdev_priv(dev);
1764
1765 return tp->msg_enable;
1766}
1767
1768static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1769{
1770 struct rtl8169_private *tp = netdev_priv(dev);
1771
1772 tp->msg_enable = value;
1773}
1774
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001775static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1776 "tx_packets",
1777 "rx_packets",
1778 "tx_errors",
1779 "rx_errors",
1780 "rx_missed",
1781 "align_errors",
1782 "tx_single_collisions",
1783 "tx_multi_collisions",
1784 "unicast",
1785 "broadcast",
1786 "multicast",
1787 "tx_aborted",
1788 "tx_underrun",
1789};
1790
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001791static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001792{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001793 switch (sset) {
1794 case ETH_SS_STATS:
1795 return ARRAY_SIZE(rtl8169_gstrings);
1796 default:
1797 return -EOPNOTSUPP;
1798 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001799}
1800
Ivan Vecera355423d2009-02-06 21:49:57 -08001801static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001802{
1803 struct rtl8169_private *tp = netdev_priv(dev);
1804 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001805 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001806 struct rtl8169_counters *counters;
1807 dma_addr_t paddr;
1808 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001809 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001810
Ivan Vecera355423d2009-02-06 21:49:57 -08001811 /*
1812 * Some chips are unable to dump tally counters when the receiver
1813 * is disabled.
1814 */
1815 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1816 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001817
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001818 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001819 if (!counters)
1820 return;
1821
1822 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001823 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001824 RTL_W32(CounterAddrLow, cmd);
1825 RTL_W32(CounterAddrLow, cmd | CounterDump);
1826
Ivan Vecera355423d2009-02-06 21:49:57 -08001827 while (wait--) {
1828 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001829 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001830 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001831 }
1832 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001833 }
1834
1835 RTL_W32(CounterAddrLow, 0);
1836 RTL_W32(CounterAddrHigh, 0);
1837
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001838 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001839}
1840
Ivan Vecera355423d2009-02-06 21:49:57 -08001841static void rtl8169_get_ethtool_stats(struct net_device *dev,
1842 struct ethtool_stats *stats, u64 *data)
1843{
1844 struct rtl8169_private *tp = netdev_priv(dev);
1845
1846 ASSERT_RTNL();
1847
1848 rtl8169_update_counters(dev);
1849
1850 data[0] = le64_to_cpu(tp->counters.tx_packets);
1851 data[1] = le64_to_cpu(tp->counters.rx_packets);
1852 data[2] = le64_to_cpu(tp->counters.tx_errors);
1853 data[3] = le32_to_cpu(tp->counters.rx_errors);
1854 data[4] = le16_to_cpu(tp->counters.rx_missed);
1855 data[5] = le16_to_cpu(tp->counters.align_errors);
1856 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1857 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1858 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1859 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1860 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1861 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1862 data[12] = le16_to_cpu(tp->counters.tx_underun);
1863}
1864
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001865static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1866{
1867 switch(stringset) {
1868 case ETH_SS_STATS:
1869 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1870 break;
1871 }
1872}
1873
Jeff Garzik7282d492006-09-13 14:30:00 -04001874static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 .get_drvinfo = rtl8169_get_drvinfo,
1876 .get_regs_len = rtl8169_get_regs_len,
1877 .get_link = ethtool_op_get_link,
1878 .get_settings = rtl8169_get_settings,
1879 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001880 .get_msglevel = rtl8169_get_msglevel,
1881 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001883 .get_wol = rtl8169_get_wol,
1884 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001885 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001886 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001887 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00001888 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889};
1890
Francois Romieu07d3f512007-02-21 22:40:46 +01001891static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001892 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
Francois Romieu5d320a22011-05-08 17:47:36 +02001894 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001895 /*
1896 * The driver currently handles the 8168Bf and the 8168Be identically
1897 * but they can be identified more specifically through the test below
1898 * if needed:
1899 *
1900 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001901 *
1902 * Same thing for the 8101Eb and the 8101Ec:
1903 *
1904 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001905 */
Francois Romieu37441002011-06-17 22:58:54 +02001906 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001908 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 int mac_version;
1910 } mac_info[] = {
Hayes Wangc2218922011-09-06 16:55:18 +08001911 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001912 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08001913 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1914 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1915
hayeswang01dc7fe2011-03-21 01:50:28 +00001916 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001917 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001918 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1919 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1920 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1921
Francois Romieu5b538df2008-07-20 16:22:45 +02001922 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001923 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1924 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001925 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001926
françois romieue6de30d2011-01-03 15:08:37 +00001927 /* 8168DP family. */
1928 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1929 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001930 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001931
Francois Romieuef808d52008-06-29 13:10:54 +02001932 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001933 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001934 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001935 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001936 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001937 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1938 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001939 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001940 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001941 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001942
1943 /* 8168B family. */
1944 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1945 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1946 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1947 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1948
1949 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08001950 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
1951 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08001952 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00001953 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001954 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1955 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1956 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001957 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1958 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1959 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1960 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1961 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1962 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001963 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001964 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001965 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001966 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1967 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001968 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1969 /* FIXME: where did these entries come from ? -- FR */
1970 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1971 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1972
1973 /* 8110 family. */
1974 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1975 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1976 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1977 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1978 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1979 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1980
Jean Delvaref21b75e2009-05-26 20:54:48 -07001981 /* Catch-all */
1982 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001983 };
1984 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 u32 reg;
1986
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001987 reg = RTL_R32(TxConfig);
1988 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 p++;
1990 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001991
1992 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1993 netif_notice(tp, probe, dev,
1994 "unknown MAC, using family default\n");
1995 tp->mac_version = default_version;
1996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
1999static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2000{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002001 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002}
2003
Francois Romieu867763c2007-08-17 18:21:58 +02002004struct phy_reg {
2005 u16 reg;
2006 u16 val;
2007};
2008
françois romieu4da19632011-01-03 15:07:55 +00002009static void rtl_writephy_batch(struct rtl8169_private *tp,
2010 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002011{
2012 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002013 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002014 regs++;
2015 }
2016}
2017
françois romieubca03d52011-01-03 15:07:31 +00002018#define PHY_READ 0x00000000
2019#define PHY_DATA_OR 0x10000000
2020#define PHY_DATA_AND 0x20000000
2021#define PHY_BJMPN 0x30000000
2022#define PHY_READ_EFUSE 0x40000000
2023#define PHY_READ_MAC_BYTE 0x50000000
2024#define PHY_WRITE_MAC_BYTE 0x60000000
2025#define PHY_CLEAR_READCOUNT 0x70000000
2026#define PHY_WRITE 0x80000000
2027#define PHY_READCOUNT_EQ_SKIP 0x90000000
2028#define PHY_COMP_EQ_SKIPN 0xa0000000
2029#define PHY_COMP_NEQ_SKIPN 0xb0000000
2030#define PHY_WRITE_PREVIOUS 0xc0000000
2031#define PHY_SKIPN 0xd0000000
2032#define PHY_DELAY_MS 0xe0000000
2033#define PHY_WRITE_ERI_WORD 0xf0000000
2034
Hayes Wang960aee62011-06-18 11:37:48 +02002035struct fw_info {
2036 u32 magic;
2037 char version[RTL_VER_SIZE];
2038 __le32 fw_start;
2039 __le32 fw_len;
2040 u8 chksum;
2041} __packed;
2042
Francois Romieu1c361ef2011-06-17 17:16:24 +02002043#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2044
2045static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002046{
Francois Romieub6ffd972011-06-17 17:00:05 +02002047 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002048 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002049 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2050 char *version = rtl_fw->version;
2051 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002052
Francois Romieu1c361ef2011-06-17 17:16:24 +02002053 if (fw->size < FW_OPCODE_SIZE)
2054 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002055
2056 if (!fw_info->magic) {
2057 size_t i, size, start;
2058 u8 checksum = 0;
2059
2060 if (fw->size < sizeof(*fw_info))
2061 goto out;
2062
2063 for (i = 0; i < fw->size; i++)
2064 checksum += fw->data[i];
2065 if (checksum != 0)
2066 goto out;
2067
2068 start = le32_to_cpu(fw_info->fw_start);
2069 if (start > fw->size)
2070 goto out;
2071
2072 size = le32_to_cpu(fw_info->fw_len);
2073 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2074 goto out;
2075
2076 memcpy(version, fw_info->version, RTL_VER_SIZE);
2077
2078 pa->code = (__le32 *)(fw->data + start);
2079 pa->size = size;
2080 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002081 if (fw->size % FW_OPCODE_SIZE)
2082 goto out;
2083
2084 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2085
2086 pa->code = (__le32 *)fw->data;
2087 pa->size = fw->size / FW_OPCODE_SIZE;
2088 }
2089 version[RTL_VER_SIZE - 1] = 0;
2090
2091 rc = true;
2092out:
2093 return rc;
2094}
2095
Francois Romieufd112f22011-06-18 00:10:29 +02002096static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2097 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002098{
Francois Romieufd112f22011-06-18 00:10:29 +02002099 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002100 size_t index;
2101
Francois Romieu1c361ef2011-06-17 17:16:24 +02002102 for (index = 0; index < pa->size; index++) {
2103 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002104 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002105
hayeswang42b82dc2011-01-10 02:07:25 +00002106 switch(action & 0xf0000000) {
2107 case PHY_READ:
2108 case PHY_DATA_OR:
2109 case PHY_DATA_AND:
2110 case PHY_READ_EFUSE:
2111 case PHY_CLEAR_READCOUNT:
2112 case PHY_WRITE:
2113 case PHY_WRITE_PREVIOUS:
2114 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002115 break;
2116
hayeswang42b82dc2011-01-10 02:07:25 +00002117 case PHY_BJMPN:
2118 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002119 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002120 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002121 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002122 }
2123 break;
2124 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002125 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002126 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002127 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002128 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002129 }
2130 break;
2131 case PHY_COMP_EQ_SKIPN:
2132 case PHY_COMP_NEQ_SKIPN:
2133 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002134 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002135 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002136 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002137 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002138 }
2139 break;
2140
2141 case PHY_READ_MAC_BYTE:
2142 case PHY_WRITE_MAC_BYTE:
2143 case PHY_WRITE_ERI_WORD:
2144 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002145 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002146 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002147 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002148 }
2149 }
Francois Romieufd112f22011-06-18 00:10:29 +02002150 rc = true;
2151out:
2152 return rc;
2153}
françois romieubca03d52011-01-03 15:07:31 +00002154
Francois Romieufd112f22011-06-18 00:10:29 +02002155static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2156{
2157 struct net_device *dev = tp->dev;
2158 int rc = -EINVAL;
2159
2160 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2161 netif_err(tp, ifup, dev, "invalid firwmare\n");
2162 goto out;
2163 }
2164
2165 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2166 rc = 0;
2167out:
2168 return rc;
2169}
2170
2171static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2172{
2173 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2174 u32 predata, count;
2175 size_t index;
2176
2177 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002178
Francois Romieu1c361ef2011-06-17 17:16:24 +02002179 for (index = 0; index < pa->size; ) {
2180 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002181 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002182 u32 regno = (action & 0x0fff0000) >> 16;
2183
2184 if (!action)
2185 break;
françois romieubca03d52011-01-03 15:07:31 +00002186
2187 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002188 case PHY_READ:
2189 predata = rtl_readphy(tp, regno);
2190 count++;
2191 index++;
françois romieubca03d52011-01-03 15:07:31 +00002192 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002193 case PHY_DATA_OR:
2194 predata |= data;
2195 index++;
2196 break;
2197 case PHY_DATA_AND:
2198 predata &= data;
2199 index++;
2200 break;
2201 case PHY_BJMPN:
2202 index -= regno;
2203 break;
2204 case PHY_READ_EFUSE:
2205 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2206 index++;
2207 break;
2208 case PHY_CLEAR_READCOUNT:
2209 count = 0;
2210 index++;
2211 break;
2212 case PHY_WRITE:
2213 rtl_writephy(tp, regno, data);
2214 index++;
2215 break;
2216 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002217 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002218 break;
2219 case PHY_COMP_EQ_SKIPN:
2220 if (predata == data)
2221 index += regno;
2222 index++;
2223 break;
2224 case PHY_COMP_NEQ_SKIPN:
2225 if (predata != data)
2226 index += regno;
2227 index++;
2228 break;
2229 case PHY_WRITE_PREVIOUS:
2230 rtl_writephy(tp, regno, predata);
2231 index++;
2232 break;
2233 case PHY_SKIPN:
2234 index += regno + 1;
2235 break;
2236 case PHY_DELAY_MS:
2237 mdelay(data);
2238 index++;
2239 break;
2240
2241 case PHY_READ_MAC_BYTE:
2242 case PHY_WRITE_MAC_BYTE:
2243 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002244 default:
2245 BUG();
2246 }
2247 }
2248}
2249
françois romieuf1e02ed2011-01-13 13:07:53 +00002250static void rtl_release_firmware(struct rtl8169_private *tp)
2251{
Francois Romieub6ffd972011-06-17 17:00:05 +02002252 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2253 release_firmware(tp->rtl_fw->fw);
2254 kfree(tp->rtl_fw);
2255 }
2256 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002257}
2258
François Romieu953a12c2011-04-24 17:38:48 +02002259static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002260{
Francois Romieub6ffd972011-06-17 17:00:05 +02002261 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002262
2263 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002264 if (!IS_ERR_OR_NULL(rtl_fw))
2265 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002266}
2267
2268static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2269{
2270 if (rtl_readphy(tp, reg) != val)
2271 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2272 else
2273 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002274}
2275
françois romieu4da19632011-01-03 15:07:55 +00002276static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002278 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002279 { 0x1f, 0x0001 },
2280 { 0x06, 0x006e },
2281 { 0x08, 0x0708 },
2282 { 0x15, 0x4000 },
2283 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
françois romieu0b9b5712009-08-10 19:44:56 +00002285 { 0x1f, 0x0001 },
2286 { 0x03, 0x00a1 },
2287 { 0x02, 0x0008 },
2288 { 0x01, 0x0120 },
2289 { 0x00, 0x1000 },
2290 { 0x04, 0x0800 },
2291 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
françois romieu0b9b5712009-08-10 19:44:56 +00002293 { 0x03, 0xff41 },
2294 { 0x02, 0xdf60 },
2295 { 0x01, 0x0140 },
2296 { 0x00, 0x0077 },
2297 { 0x04, 0x7800 },
2298 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
françois romieu0b9b5712009-08-10 19:44:56 +00002300 { 0x03, 0x802f },
2301 { 0x02, 0x4f02 },
2302 { 0x01, 0x0409 },
2303 { 0x00, 0xf0f9 },
2304 { 0x04, 0x9800 },
2305 { 0x04, 0x9000 },
2306
2307 { 0x03, 0xdf01 },
2308 { 0x02, 0xdf20 },
2309 { 0x01, 0xff95 },
2310 { 0x00, 0xba00 },
2311 { 0x04, 0xa800 },
2312 { 0x04, 0xa000 },
2313
2314 { 0x03, 0xff41 },
2315 { 0x02, 0xdf20 },
2316 { 0x01, 0x0140 },
2317 { 0x00, 0x00bb },
2318 { 0x04, 0xb800 },
2319 { 0x04, 0xb000 },
2320
2321 { 0x03, 0xdf41 },
2322 { 0x02, 0xdc60 },
2323 { 0x01, 0x6340 },
2324 { 0x00, 0x007d },
2325 { 0x04, 0xd800 },
2326 { 0x04, 0xd000 },
2327
2328 { 0x03, 0xdf01 },
2329 { 0x02, 0xdf20 },
2330 { 0x01, 0x100a },
2331 { 0x00, 0xa0ff },
2332 { 0x04, 0xf800 },
2333 { 0x04, 0xf000 },
2334
2335 { 0x1f, 0x0000 },
2336 { 0x0b, 0x0000 },
2337 { 0x00, 0x9200 }
2338 };
2339
françois romieu4da19632011-01-03 15:07:55 +00002340 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341}
2342
françois romieu4da19632011-01-03 15:07:55 +00002343static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002344{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002345 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002346 { 0x1f, 0x0002 },
2347 { 0x01, 0x90d0 },
2348 { 0x1f, 0x0000 }
2349 };
2350
françois romieu4da19632011-01-03 15:07:55 +00002351 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002352}
2353
françois romieu4da19632011-01-03 15:07:55 +00002354static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002355{
2356 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002357
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002358 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2359 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002360 return;
2361
françois romieu4da19632011-01-03 15:07:55 +00002362 rtl_writephy(tp, 0x1f, 0x0001);
2363 rtl_writephy(tp, 0x10, 0xf01b);
2364 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002365}
2366
françois romieu4da19632011-01-03 15:07:55 +00002367static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002368{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002369 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002370 { 0x1f, 0x0001 },
2371 { 0x04, 0x0000 },
2372 { 0x03, 0x00a1 },
2373 { 0x02, 0x0008 },
2374 { 0x01, 0x0120 },
2375 { 0x00, 0x1000 },
2376 { 0x04, 0x0800 },
2377 { 0x04, 0x9000 },
2378 { 0x03, 0x802f },
2379 { 0x02, 0x4f02 },
2380 { 0x01, 0x0409 },
2381 { 0x00, 0xf099 },
2382 { 0x04, 0x9800 },
2383 { 0x04, 0xa000 },
2384 { 0x03, 0xdf01 },
2385 { 0x02, 0xdf20 },
2386 { 0x01, 0xff95 },
2387 { 0x00, 0xba00 },
2388 { 0x04, 0xa800 },
2389 { 0x04, 0xf000 },
2390 { 0x03, 0xdf01 },
2391 { 0x02, 0xdf20 },
2392 { 0x01, 0x101a },
2393 { 0x00, 0xa0ff },
2394 { 0x04, 0xf800 },
2395 { 0x04, 0x0000 },
2396 { 0x1f, 0x0000 },
2397
2398 { 0x1f, 0x0001 },
2399 { 0x10, 0xf41b },
2400 { 0x14, 0xfb54 },
2401 { 0x18, 0xf5c7 },
2402 { 0x1f, 0x0000 },
2403
2404 { 0x1f, 0x0001 },
2405 { 0x17, 0x0cc0 },
2406 { 0x1f, 0x0000 }
2407 };
2408
françois romieu4da19632011-01-03 15:07:55 +00002409 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002410
françois romieu4da19632011-01-03 15:07:55 +00002411 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002412}
2413
françois romieu4da19632011-01-03 15:07:55 +00002414static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002415{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002416 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002417 { 0x1f, 0x0001 },
2418 { 0x04, 0x0000 },
2419 { 0x03, 0x00a1 },
2420 { 0x02, 0x0008 },
2421 { 0x01, 0x0120 },
2422 { 0x00, 0x1000 },
2423 { 0x04, 0x0800 },
2424 { 0x04, 0x9000 },
2425 { 0x03, 0x802f },
2426 { 0x02, 0x4f02 },
2427 { 0x01, 0x0409 },
2428 { 0x00, 0xf099 },
2429 { 0x04, 0x9800 },
2430 { 0x04, 0xa000 },
2431 { 0x03, 0xdf01 },
2432 { 0x02, 0xdf20 },
2433 { 0x01, 0xff95 },
2434 { 0x00, 0xba00 },
2435 { 0x04, 0xa800 },
2436 { 0x04, 0xf000 },
2437 { 0x03, 0xdf01 },
2438 { 0x02, 0xdf20 },
2439 { 0x01, 0x101a },
2440 { 0x00, 0xa0ff },
2441 { 0x04, 0xf800 },
2442 { 0x04, 0x0000 },
2443 { 0x1f, 0x0000 },
2444
2445 { 0x1f, 0x0001 },
2446 { 0x0b, 0x8480 },
2447 { 0x1f, 0x0000 },
2448
2449 { 0x1f, 0x0001 },
2450 { 0x18, 0x67c7 },
2451 { 0x04, 0x2000 },
2452 { 0x03, 0x002f },
2453 { 0x02, 0x4360 },
2454 { 0x01, 0x0109 },
2455 { 0x00, 0x3022 },
2456 { 0x04, 0x2800 },
2457 { 0x1f, 0x0000 },
2458
2459 { 0x1f, 0x0001 },
2460 { 0x17, 0x0cc0 },
2461 { 0x1f, 0x0000 }
2462 };
2463
françois romieu4da19632011-01-03 15:07:55 +00002464 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002465}
2466
françois romieu4da19632011-01-03 15:07:55 +00002467static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002468{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002469 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002470 { 0x10, 0xf41b },
2471 { 0x1f, 0x0000 }
2472 };
2473
françois romieu4da19632011-01-03 15:07:55 +00002474 rtl_writephy(tp, 0x1f, 0x0001);
2475 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002476
françois romieu4da19632011-01-03 15:07:55 +00002477 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002478}
2479
françois romieu4da19632011-01-03 15:07:55 +00002480static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002481{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002482 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002483 { 0x1f, 0x0001 },
2484 { 0x10, 0xf41b },
2485 { 0x1f, 0x0000 }
2486 };
2487
françois romieu4da19632011-01-03 15:07:55 +00002488 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002489}
2490
françois romieu4da19632011-01-03 15:07:55 +00002491static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002492{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002493 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002494 { 0x1f, 0x0000 },
2495 { 0x1d, 0x0f00 },
2496 { 0x1f, 0x0002 },
2497 { 0x0c, 0x1ec8 },
2498 { 0x1f, 0x0000 }
2499 };
2500
françois romieu4da19632011-01-03 15:07:55 +00002501 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002502}
2503
françois romieu4da19632011-01-03 15:07:55 +00002504static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002505{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002506 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002507 { 0x1f, 0x0001 },
2508 { 0x1d, 0x3d98 },
2509 { 0x1f, 0x0000 }
2510 };
2511
françois romieu4da19632011-01-03 15:07:55 +00002512 rtl_writephy(tp, 0x1f, 0x0000);
2513 rtl_patchphy(tp, 0x14, 1 << 5);
2514 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002515
françois romieu4da19632011-01-03 15:07:55 +00002516 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002517}
2518
françois romieu4da19632011-01-03 15:07:55 +00002519static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002520{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002521 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002522 { 0x1f, 0x0001 },
2523 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002524 { 0x1f, 0x0002 },
2525 { 0x00, 0x88d4 },
2526 { 0x01, 0x82b1 },
2527 { 0x03, 0x7002 },
2528 { 0x08, 0x9e30 },
2529 { 0x09, 0x01f0 },
2530 { 0x0a, 0x5500 },
2531 { 0x0c, 0x00c8 },
2532 { 0x1f, 0x0003 },
2533 { 0x12, 0xc096 },
2534 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002535 { 0x1f, 0x0000 },
2536 { 0x1f, 0x0000 },
2537 { 0x09, 0x2000 },
2538 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002539 };
2540
françois romieu4da19632011-01-03 15:07:55 +00002541 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002542
françois romieu4da19632011-01-03 15:07:55 +00002543 rtl_patchphy(tp, 0x14, 1 << 5);
2544 rtl_patchphy(tp, 0x0d, 1 << 5);
2545 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002546}
2547
françois romieu4da19632011-01-03 15:07:55 +00002548static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002549{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002550 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002551 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002552 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002553 { 0x03, 0x802f },
2554 { 0x02, 0x4f02 },
2555 { 0x01, 0x0409 },
2556 { 0x00, 0xf099 },
2557 { 0x04, 0x9800 },
2558 { 0x04, 0x9000 },
2559 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002560 { 0x1f, 0x0002 },
2561 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002562 { 0x06, 0x0761 },
2563 { 0x1f, 0x0003 },
2564 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002565 { 0x1f, 0x0000 }
2566 };
2567
françois romieu4da19632011-01-03 15:07:55 +00002568 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002569
françois romieu4da19632011-01-03 15:07:55 +00002570 rtl_patchphy(tp, 0x16, 1 << 0);
2571 rtl_patchphy(tp, 0x14, 1 << 5);
2572 rtl_patchphy(tp, 0x0d, 1 << 5);
2573 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002574}
2575
françois romieu4da19632011-01-03 15:07:55 +00002576static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002577{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002578 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002579 { 0x1f, 0x0001 },
2580 { 0x12, 0x2300 },
2581 { 0x1d, 0x3d98 },
2582 { 0x1f, 0x0002 },
2583 { 0x0c, 0x7eb8 },
2584 { 0x06, 0x5461 },
2585 { 0x1f, 0x0003 },
2586 { 0x16, 0x0f0a },
2587 { 0x1f, 0x0000 }
2588 };
2589
françois romieu4da19632011-01-03 15:07:55 +00002590 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002591
françois romieu4da19632011-01-03 15:07:55 +00002592 rtl_patchphy(tp, 0x16, 1 << 0);
2593 rtl_patchphy(tp, 0x14, 1 << 5);
2594 rtl_patchphy(tp, 0x0d, 1 << 5);
2595 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002596}
2597
françois romieu4da19632011-01-03 15:07:55 +00002598static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002599{
françois romieu4da19632011-01-03 15:07:55 +00002600 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002601}
2602
françois romieubca03d52011-01-03 15:07:31 +00002603static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002604{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002605 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002606 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002607 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002608 { 0x06, 0x4064 },
2609 { 0x07, 0x2863 },
2610 { 0x08, 0x059c },
2611 { 0x09, 0x26b4 },
2612 { 0x0a, 0x6a19 },
2613 { 0x0b, 0xdcc8 },
2614 { 0x10, 0xf06d },
2615 { 0x14, 0x7f68 },
2616 { 0x18, 0x7fd9 },
2617 { 0x1c, 0xf0ff },
2618 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002619 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002620 { 0x12, 0xf49f },
2621 { 0x13, 0x070b },
2622 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002623 { 0x14, 0x94c0 },
2624
2625 /*
2626 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002627 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002628 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002629 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002630 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002631 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002632 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002633 { 0x06, 0x5561 },
2634
2635 /*
2636 * Can not link to 1Gbps with bad cable
2637 * Decrease SNR threshold form 21.07dB to 19.04dB
2638 */
2639 { 0x1f, 0x0001 },
2640 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002641
2642 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002643 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002644 };
françois romieubca03d52011-01-03 15:07:31 +00002645 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002646
françois romieu4da19632011-01-03 15:07:55 +00002647 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002648
françois romieubca03d52011-01-03 15:07:31 +00002649 /*
2650 * Rx Error Issue
2651 * Fine Tune Switching regulator parameter
2652 */
françois romieu4da19632011-01-03 15:07:55 +00002653 rtl_writephy(tp, 0x1f, 0x0002);
2654 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2655 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002656
françois romieudaf9df62009-10-07 12:44:20 +00002657 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002658 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002659 { 0x1f, 0x0002 },
2660 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002661 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002662 { 0x05, 0x8330 },
2663 { 0x06, 0x669a },
2664 { 0x1f, 0x0002 }
2665 };
2666 int val;
2667
françois romieu4da19632011-01-03 15:07:55 +00002668 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002669
françois romieu4da19632011-01-03 15:07:55 +00002670 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002671
2672 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002673 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002674 0x0065, 0x0066, 0x0067, 0x0068,
2675 0x0069, 0x006a, 0x006b, 0x006c
2676 };
2677 int i;
2678
françois romieu4da19632011-01-03 15:07:55 +00002679 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002680
2681 val &= 0xff00;
2682 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002683 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002684 }
2685 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002686 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002687 { 0x1f, 0x0002 },
2688 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002689 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002690 { 0x05, 0x8330 },
2691 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002692 };
2693
françois romieu4da19632011-01-03 15:07:55 +00002694 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002695 }
2696
françois romieubca03d52011-01-03 15:07:31 +00002697 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002698 rtl_writephy(tp, 0x1f, 0x0002);
2699 rtl_patchphy(tp, 0x0d, 0x0300);
2700 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002701
françois romieubca03d52011-01-03 15:07:31 +00002702 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002703 rtl_writephy(tp, 0x1f, 0x0002);
2704 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2705 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002706
françois romieu4da19632011-01-03 15:07:55 +00002707 rtl_writephy(tp, 0x1f, 0x0005);
2708 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002709
2710 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002711
françois romieu4da19632011-01-03 15:07:55 +00002712 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002713}
2714
françois romieubca03d52011-01-03 15:07:31 +00002715static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002716{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002717 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002718 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002719 { 0x1f, 0x0001 },
2720 { 0x06, 0x4064 },
2721 { 0x07, 0x2863 },
2722 { 0x08, 0x059c },
2723 { 0x09, 0x26b4 },
2724 { 0x0a, 0x6a19 },
2725 { 0x0b, 0xdcc8 },
2726 { 0x10, 0xf06d },
2727 { 0x14, 0x7f68 },
2728 { 0x18, 0x7fd9 },
2729 { 0x1c, 0xf0ff },
2730 { 0x1d, 0x3d9c },
2731 { 0x1f, 0x0003 },
2732 { 0x12, 0xf49f },
2733 { 0x13, 0x070b },
2734 { 0x1a, 0x05ad },
2735 { 0x14, 0x94c0 },
2736
françois romieubca03d52011-01-03 15:07:31 +00002737 /*
2738 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002739 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002740 */
françois romieudaf9df62009-10-07 12:44:20 +00002741 { 0x1f, 0x0002 },
2742 { 0x06, 0x5561 },
2743 { 0x1f, 0x0005 },
2744 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002745 { 0x06, 0x5561 },
2746
2747 /*
2748 * Can not link to 1Gbps with bad cable
2749 * Decrease SNR threshold form 21.07dB to 19.04dB
2750 */
2751 { 0x1f, 0x0001 },
2752 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002753
2754 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002755 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002756 };
françois romieubca03d52011-01-03 15:07:31 +00002757 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002758
françois romieu4da19632011-01-03 15:07:55 +00002759 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002760
2761 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002762 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002763 { 0x1f, 0x0002 },
2764 { 0x05, 0x669a },
2765 { 0x1f, 0x0005 },
2766 { 0x05, 0x8330 },
2767 { 0x06, 0x669a },
2768
2769 { 0x1f, 0x0002 }
2770 };
2771 int val;
2772
françois romieu4da19632011-01-03 15:07:55 +00002773 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002774
françois romieu4da19632011-01-03 15:07:55 +00002775 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002776 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002777 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002778 0x0065, 0x0066, 0x0067, 0x0068,
2779 0x0069, 0x006a, 0x006b, 0x006c
2780 };
2781 int i;
2782
françois romieu4da19632011-01-03 15:07:55 +00002783 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002784
2785 val &= 0xff00;
2786 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002787 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002788 }
2789 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002790 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002791 { 0x1f, 0x0002 },
2792 { 0x05, 0x2642 },
2793 { 0x1f, 0x0005 },
2794 { 0x05, 0x8330 },
2795 { 0x06, 0x2642 }
2796 };
2797
françois romieu4da19632011-01-03 15:07:55 +00002798 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002799 }
2800
françois romieubca03d52011-01-03 15:07:31 +00002801 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002802 rtl_writephy(tp, 0x1f, 0x0002);
2803 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2804 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002805
françois romieubca03d52011-01-03 15:07:31 +00002806 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002807 rtl_writephy(tp, 0x1f, 0x0002);
2808 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002809
françois romieu4da19632011-01-03 15:07:55 +00002810 rtl_writephy(tp, 0x1f, 0x0005);
2811 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002812
2813 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002814
françois romieu4da19632011-01-03 15:07:55 +00002815 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002816}
2817
françois romieu4da19632011-01-03 15:07:55 +00002818static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002819{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002820 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002821 { 0x1f, 0x0002 },
2822 { 0x10, 0x0008 },
2823 { 0x0d, 0x006c },
2824
2825 { 0x1f, 0x0000 },
2826 { 0x0d, 0xf880 },
2827
2828 { 0x1f, 0x0001 },
2829 { 0x17, 0x0cc0 },
2830
2831 { 0x1f, 0x0001 },
2832 { 0x0b, 0xa4d8 },
2833 { 0x09, 0x281c },
2834 { 0x07, 0x2883 },
2835 { 0x0a, 0x6b35 },
2836 { 0x1d, 0x3da4 },
2837 { 0x1c, 0xeffd },
2838 { 0x14, 0x7f52 },
2839 { 0x18, 0x7fc6 },
2840 { 0x08, 0x0601 },
2841 { 0x06, 0x4063 },
2842 { 0x10, 0xf074 },
2843 { 0x1f, 0x0003 },
2844 { 0x13, 0x0789 },
2845 { 0x12, 0xf4bd },
2846 { 0x1a, 0x04fd },
2847 { 0x14, 0x84b0 },
2848 { 0x1f, 0x0000 },
2849 { 0x00, 0x9200 },
2850
2851 { 0x1f, 0x0005 },
2852 { 0x01, 0x0340 },
2853 { 0x1f, 0x0001 },
2854 { 0x04, 0x4000 },
2855 { 0x03, 0x1d21 },
2856 { 0x02, 0x0c32 },
2857 { 0x01, 0x0200 },
2858 { 0x00, 0x5554 },
2859 { 0x04, 0x4800 },
2860 { 0x04, 0x4000 },
2861 { 0x04, 0xf000 },
2862 { 0x03, 0xdf01 },
2863 { 0x02, 0xdf20 },
2864 { 0x01, 0x101a },
2865 { 0x00, 0xa0ff },
2866 { 0x04, 0xf800 },
2867 { 0x04, 0xf000 },
2868 { 0x1f, 0x0000 },
2869
2870 { 0x1f, 0x0007 },
2871 { 0x1e, 0x0023 },
2872 { 0x16, 0x0000 },
2873 { 0x1f, 0x0000 }
2874 };
2875
françois romieu4da19632011-01-03 15:07:55 +00002876 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002877}
2878
françois romieue6de30d2011-01-03 15:08:37 +00002879static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2880{
2881 static const struct phy_reg phy_reg_init[] = {
2882 { 0x1f, 0x0001 },
2883 { 0x17, 0x0cc0 },
2884
2885 { 0x1f, 0x0007 },
2886 { 0x1e, 0x002d },
2887 { 0x18, 0x0040 },
2888 { 0x1f, 0x0000 }
2889 };
2890
2891 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2892 rtl_patchphy(tp, 0x0d, 1 << 5);
2893}
2894
Hayes Wang70090422011-07-06 15:58:06 +08002895static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002896{
2897 static const struct phy_reg phy_reg_init[] = {
2898 /* Enable Delay cap */
2899 { 0x1f, 0x0005 },
2900 { 0x05, 0x8b80 },
2901 { 0x06, 0xc896 },
2902 { 0x1f, 0x0000 },
2903
2904 /* Channel estimation fine tune */
2905 { 0x1f, 0x0001 },
2906 { 0x0b, 0x6c20 },
2907 { 0x07, 0x2872 },
2908 { 0x1c, 0xefff },
2909 { 0x1f, 0x0003 },
2910 { 0x14, 0x6420 },
2911 { 0x1f, 0x0000 },
2912
2913 /* Update PFM & 10M TX idle timer */
2914 { 0x1f, 0x0007 },
2915 { 0x1e, 0x002f },
2916 { 0x15, 0x1919 },
2917 { 0x1f, 0x0000 },
2918
2919 { 0x1f, 0x0007 },
2920 { 0x1e, 0x00ac },
2921 { 0x18, 0x0006 },
2922 { 0x1f, 0x0000 }
2923 };
2924
Francois Romieu15ecd032011-04-27 13:52:22 -07002925 rtl_apply_firmware(tp);
2926
hayeswang01dc7fe2011-03-21 01:50:28 +00002927 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2928
2929 /* DCO enable for 10M IDLE Power */
2930 rtl_writephy(tp, 0x1f, 0x0007);
2931 rtl_writephy(tp, 0x1e, 0x0023);
2932 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2933 rtl_writephy(tp, 0x1f, 0x0000);
2934
2935 /* For impedance matching */
2936 rtl_writephy(tp, 0x1f, 0x0002);
2937 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002938 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002939
2940 /* PHY auto speed down */
2941 rtl_writephy(tp, 0x1f, 0x0007);
2942 rtl_writephy(tp, 0x1e, 0x002d);
2943 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2944 rtl_writephy(tp, 0x1f, 0x0000);
2945 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2946
2947 rtl_writephy(tp, 0x1f, 0x0005);
2948 rtl_writephy(tp, 0x05, 0x8b86);
2949 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2950 rtl_writephy(tp, 0x1f, 0x0000);
2951
2952 rtl_writephy(tp, 0x1f, 0x0005);
2953 rtl_writephy(tp, 0x05, 0x8b85);
2954 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2955 rtl_writephy(tp, 0x1f, 0x0007);
2956 rtl_writephy(tp, 0x1e, 0x0020);
2957 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2958 rtl_writephy(tp, 0x1f, 0x0006);
2959 rtl_writephy(tp, 0x00, 0x5a00);
2960 rtl_writephy(tp, 0x1f, 0x0000);
2961 rtl_writephy(tp, 0x0d, 0x0007);
2962 rtl_writephy(tp, 0x0e, 0x003c);
2963 rtl_writephy(tp, 0x0d, 0x4007);
2964 rtl_writephy(tp, 0x0e, 0x0000);
2965 rtl_writephy(tp, 0x0d, 0x0000);
2966}
2967
Hayes Wang70090422011-07-06 15:58:06 +08002968static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2969{
2970 static const struct phy_reg phy_reg_init[] = {
2971 /* Enable Delay cap */
2972 { 0x1f, 0x0004 },
2973 { 0x1f, 0x0007 },
2974 { 0x1e, 0x00ac },
2975 { 0x18, 0x0006 },
2976 { 0x1f, 0x0002 },
2977 { 0x1f, 0x0000 },
2978 { 0x1f, 0x0000 },
2979
2980 /* Channel estimation fine tune */
2981 { 0x1f, 0x0003 },
2982 { 0x09, 0xa20f },
2983 { 0x1f, 0x0000 },
2984 { 0x1f, 0x0000 },
2985
2986 /* Green Setting */
2987 { 0x1f, 0x0005 },
2988 { 0x05, 0x8b5b },
2989 { 0x06, 0x9222 },
2990 { 0x05, 0x8b6d },
2991 { 0x06, 0x8000 },
2992 { 0x05, 0x8b76 },
2993 { 0x06, 0x8000 },
2994 { 0x1f, 0x0000 }
2995 };
2996
2997 rtl_apply_firmware(tp);
2998
2999 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3000
3001 /* For 4-corner performance improve */
3002 rtl_writephy(tp, 0x1f, 0x0005);
3003 rtl_writephy(tp, 0x05, 0x8b80);
3004 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
3005 rtl_writephy(tp, 0x1f, 0x0000);
3006
3007 /* PHY auto speed down */
3008 rtl_writephy(tp, 0x1f, 0x0004);
3009 rtl_writephy(tp, 0x1f, 0x0007);
3010 rtl_writephy(tp, 0x1e, 0x002d);
3011 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3012 rtl_writephy(tp, 0x1f, 0x0002);
3013 rtl_writephy(tp, 0x1f, 0x0000);
3014 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3015
3016 /* improve 10M EEE waveform */
3017 rtl_writephy(tp, 0x1f, 0x0005);
3018 rtl_writephy(tp, 0x05, 0x8b86);
3019 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3020 rtl_writephy(tp, 0x1f, 0x0000);
3021
3022 /* Improve 2-pair detection performance */
3023 rtl_writephy(tp, 0x1f, 0x0005);
3024 rtl_writephy(tp, 0x05, 0x8b85);
3025 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3026 rtl_writephy(tp, 0x1f, 0x0000);
3027
3028 /* EEE setting */
3029 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
3030 ERIAR_EXGMAC);
3031 rtl_writephy(tp, 0x1f, 0x0005);
3032 rtl_writephy(tp, 0x05, 0x8b85);
3033 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3034 rtl_writephy(tp, 0x1f, 0x0004);
3035 rtl_writephy(tp, 0x1f, 0x0007);
3036 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04003037 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003038 rtl_writephy(tp, 0x1f, 0x0002);
3039 rtl_writephy(tp, 0x1f, 0x0000);
3040 rtl_writephy(tp, 0x0d, 0x0007);
3041 rtl_writephy(tp, 0x0e, 0x003c);
3042 rtl_writephy(tp, 0x0d, 0x4007);
3043 rtl_writephy(tp, 0x0e, 0x0000);
3044 rtl_writephy(tp, 0x0d, 0x0000);
3045
3046 /* Green feature */
3047 rtl_writephy(tp, 0x1f, 0x0003);
3048 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3049 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3050 rtl_writephy(tp, 0x1f, 0x0000);
3051}
3052
Hayes Wang5f886e02012-03-30 14:33:03 +08003053static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3054{
3055 /* For 4-corner performance improve */
3056 rtl_writephy(tp, 0x1f, 0x0005);
3057 rtl_writephy(tp, 0x05, 0x8b80);
3058 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3059 rtl_writephy(tp, 0x1f, 0x0000);
3060
3061 /* PHY auto speed down */
3062 rtl_writephy(tp, 0x1f, 0x0007);
3063 rtl_writephy(tp, 0x1e, 0x002d);
3064 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3065 rtl_writephy(tp, 0x1f, 0x0000);
3066 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3067
3068 /* Improve 10M EEE waveform */
3069 rtl_writephy(tp, 0x1f, 0x0005);
3070 rtl_writephy(tp, 0x05, 0x8b86);
3071 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3072 rtl_writephy(tp, 0x1f, 0x0000);
3073}
3074
Hayes Wangc2218922011-09-06 16:55:18 +08003075static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3076{
3077 static const struct phy_reg phy_reg_init[] = {
3078 /* Channel estimation fine tune */
3079 { 0x1f, 0x0003 },
3080 { 0x09, 0xa20f },
3081 { 0x1f, 0x0000 },
3082
3083 /* Modify green table for giga & fnet */
3084 { 0x1f, 0x0005 },
3085 { 0x05, 0x8b55 },
3086 { 0x06, 0x0000 },
3087 { 0x05, 0x8b5e },
3088 { 0x06, 0x0000 },
3089 { 0x05, 0x8b67 },
3090 { 0x06, 0x0000 },
3091 { 0x05, 0x8b70 },
3092 { 0x06, 0x0000 },
3093 { 0x1f, 0x0000 },
3094 { 0x1f, 0x0007 },
3095 { 0x1e, 0x0078 },
3096 { 0x17, 0x0000 },
3097 { 0x19, 0x00fb },
3098 { 0x1f, 0x0000 },
3099
3100 /* Modify green table for 10M */
3101 { 0x1f, 0x0005 },
3102 { 0x05, 0x8b79 },
3103 { 0x06, 0xaa00 },
3104 { 0x1f, 0x0000 },
3105
3106 /* Disable hiimpedance detection (RTCT) */
3107 { 0x1f, 0x0003 },
3108 { 0x01, 0x328a },
3109 { 0x1f, 0x0000 }
3110 };
3111
3112 rtl_apply_firmware(tp);
3113
3114 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3115
Hayes Wang5f886e02012-03-30 14:33:03 +08003116 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003117
3118 /* Improve 2-pair detection performance */
3119 rtl_writephy(tp, 0x1f, 0x0005);
3120 rtl_writephy(tp, 0x05, 0x8b85);
3121 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3122 rtl_writephy(tp, 0x1f, 0x0000);
3123}
3124
3125static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3126{
3127 rtl_apply_firmware(tp);
3128
Hayes Wang5f886e02012-03-30 14:33:03 +08003129 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003130}
3131
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003132static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3133{
3134 void __iomem *ioaddr = tp->mmio_addr;
3135 static const struct phy_reg phy_reg_init[] = {
3136 /* Channel estimation fine tune */
3137 { 0x1f, 0x0003 },
3138 { 0x09, 0xa20f },
3139 { 0x1f, 0x0000 },
3140
3141 /* Modify green table for giga & fnet */
3142 { 0x1f, 0x0005 },
3143 { 0x05, 0x8b55 },
3144 { 0x06, 0x0000 },
3145 { 0x05, 0x8b5e },
3146 { 0x06, 0x0000 },
3147 { 0x05, 0x8b67 },
3148 { 0x06, 0x0000 },
3149 { 0x05, 0x8b70 },
3150 { 0x06, 0x0000 },
3151 { 0x1f, 0x0000 },
3152 { 0x1f, 0x0007 },
3153 { 0x1e, 0x0078 },
3154 { 0x17, 0x0000 },
3155 { 0x19, 0x00aa },
3156 { 0x1f, 0x0000 },
3157
3158 /* Modify green table for 10M */
3159 { 0x1f, 0x0005 },
3160 { 0x05, 0x8b79 },
3161 { 0x06, 0xaa00 },
3162 { 0x1f, 0x0000 },
3163
3164 /* Disable hiimpedance detection (RTCT) */
3165 { 0x1f, 0x0003 },
3166 { 0x01, 0x328a },
3167 { 0x1f, 0x0000 }
3168 };
3169
3170
3171 rtl_apply_firmware(tp);
3172
3173 rtl8168f_hw_phy_config(tp);
3174
3175 /* Improve 2-pair detection performance */
3176 rtl_writephy(tp, 0x1f, 0x0005);
3177 rtl_writephy(tp, 0x05, 0x8b85);
3178 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3179 rtl_writephy(tp, 0x1f, 0x0000);
3180
3181 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3182
3183 /* Modify green table for giga */
3184 rtl_writephy(tp, 0x1f, 0x0005);
3185 rtl_writephy(tp, 0x05, 0x8b54);
3186 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3187 rtl_writephy(tp, 0x05, 0x8b5d);
3188 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3189 rtl_writephy(tp, 0x05, 0x8a7c);
3190 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3191 rtl_writephy(tp, 0x05, 0x8a7f);
3192 rtl_w1w0_phy(tp, 0x06, 0x0100, 0x0000);
3193 rtl_writephy(tp, 0x05, 0x8a82);
3194 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3195 rtl_writephy(tp, 0x05, 0x8a85);
3196 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3197 rtl_writephy(tp, 0x05, 0x8a88);
3198 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3199 rtl_writephy(tp, 0x1f, 0x0000);
3200
3201 /* uc same-seed solution */
3202 rtl_writephy(tp, 0x1f, 0x0005);
3203 rtl_writephy(tp, 0x05, 0x8b85);
3204 rtl_w1w0_phy(tp, 0x06, 0x8000, 0x0000);
3205 rtl_writephy(tp, 0x1f, 0x0000);
3206
3207 /* eee setting */
3208 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
3209 rtl_writephy(tp, 0x1f, 0x0005);
3210 rtl_writephy(tp, 0x05, 0x8b85);
3211 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3212 rtl_writephy(tp, 0x1f, 0x0004);
3213 rtl_writephy(tp, 0x1f, 0x0007);
3214 rtl_writephy(tp, 0x1e, 0x0020);
3215 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
3216 rtl_writephy(tp, 0x1f, 0x0000);
3217 rtl_writephy(tp, 0x0d, 0x0007);
3218 rtl_writephy(tp, 0x0e, 0x003c);
3219 rtl_writephy(tp, 0x0d, 0x4007);
3220 rtl_writephy(tp, 0x0e, 0x0000);
3221 rtl_writephy(tp, 0x0d, 0x0000);
3222
3223 /* Green feature */
3224 rtl_writephy(tp, 0x1f, 0x0003);
3225 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3226 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3227 rtl_writephy(tp, 0x1f, 0x0000);
3228}
3229
françois romieu4da19632011-01-03 15:07:55 +00003230static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003231{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003232 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003233 { 0x1f, 0x0003 },
3234 { 0x08, 0x441d },
3235 { 0x01, 0x9100 },
3236 { 0x1f, 0x0000 }
3237 };
3238
françois romieu4da19632011-01-03 15:07:55 +00003239 rtl_writephy(tp, 0x1f, 0x0000);
3240 rtl_patchphy(tp, 0x11, 1 << 12);
3241 rtl_patchphy(tp, 0x19, 1 << 13);
3242 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003243
françois romieu4da19632011-01-03 15:07:55 +00003244 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003245}
3246
Hayes Wang5a5e4442011-02-22 17:26:21 +08003247static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3248{
3249 static const struct phy_reg phy_reg_init[] = {
3250 { 0x1f, 0x0005 },
3251 { 0x1a, 0x0000 },
3252 { 0x1f, 0x0000 },
3253
3254 { 0x1f, 0x0004 },
3255 { 0x1c, 0x0000 },
3256 { 0x1f, 0x0000 },
3257
3258 { 0x1f, 0x0001 },
3259 { 0x15, 0x7701 },
3260 { 0x1f, 0x0000 }
3261 };
3262
3263 /* Disable ALDPS before ram code */
3264 rtl_writephy(tp, 0x1f, 0x0000);
3265 rtl_writephy(tp, 0x18, 0x0310);
3266 msleep(100);
3267
François Romieu953a12c2011-04-24 17:38:48 +02003268 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003269
3270 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3271}
3272
Hayes Wang7e18dca2012-03-30 14:33:02 +08003273static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3274{
3275 void __iomem *ioaddr = tp->mmio_addr;
3276
3277 /* Disable ALDPS before setting firmware */
3278 rtl_writephy(tp, 0x1f, 0x0000);
3279 rtl_writephy(tp, 0x18, 0x0310);
3280 msleep(20);
3281
3282 rtl_apply_firmware(tp);
3283
3284 /* EEE setting */
3285 rtl_eri_write(ioaddr, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3286 rtl_writephy(tp, 0x1f, 0x0004);
3287 rtl_writephy(tp, 0x10, 0x401f);
3288 rtl_writephy(tp, 0x19, 0x7030);
3289 rtl_writephy(tp, 0x1f, 0x0000);
3290}
3291
Hayes Wang5598bfe2012-07-02 17:23:21 +08003292static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3293{
3294 void __iomem *ioaddr = tp->mmio_addr;
3295
3296 static const struct phy_reg phy_reg_init[] = {
3297 { 0x1f, 0x0004 },
3298 { 0x10, 0xc07f },
3299 { 0x19, 0x7030 },
3300 { 0x1f, 0x0000 }
3301 };
3302
3303 /* Disable ALDPS before ram code */
3304 rtl_writephy(tp, 0x1f, 0x0000);
3305 rtl_writephy(tp, 0x18, 0x0310);
3306 msleep(100);
3307
3308 rtl_apply_firmware(tp);
3309
3310 rtl_eri_write(ioaddr, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3311 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3312
3313 rtl_eri_write(ioaddr, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3314}
3315
Francois Romieu5615d9f2007-08-17 17:50:46 +02003316static void rtl_hw_phy_config(struct net_device *dev)
3317{
3318 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003319
3320 rtl8169_print_mac_version(tp);
3321
3322 switch (tp->mac_version) {
3323 case RTL_GIGA_MAC_VER_01:
3324 break;
3325 case RTL_GIGA_MAC_VER_02:
3326 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003327 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003328 break;
3329 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003330 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003331 break;
françois romieu2e9558562009-08-10 19:44:19 +00003332 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003333 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003334 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003335 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003336 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003337 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003338 case RTL_GIGA_MAC_VER_07:
3339 case RTL_GIGA_MAC_VER_08:
3340 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003341 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003342 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003343 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003344 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003345 break;
3346 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003347 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003348 break;
3349 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003350 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003351 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003352 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003353 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003354 break;
3355 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003356 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003357 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003358 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003359 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003360 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003361 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003362 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003363 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003364 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003365 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003366 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003367 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003368 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003369 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003370 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003371 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003372 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003373 break;
3374 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003375 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003376 break;
3377 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003378 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003379 break;
françois romieue6de30d2011-01-03 15:08:37 +00003380 case RTL_GIGA_MAC_VER_28:
3381 rtl8168d_4_hw_phy_config(tp);
3382 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003383 case RTL_GIGA_MAC_VER_29:
3384 case RTL_GIGA_MAC_VER_30:
3385 rtl8105e_hw_phy_config(tp);
3386 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003387 case RTL_GIGA_MAC_VER_31:
3388 /* None. */
3389 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003390 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003391 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003392 rtl8168e_1_hw_phy_config(tp);
3393 break;
3394 case RTL_GIGA_MAC_VER_34:
3395 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003396 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003397 case RTL_GIGA_MAC_VER_35:
3398 rtl8168f_1_hw_phy_config(tp);
3399 break;
3400 case RTL_GIGA_MAC_VER_36:
3401 rtl8168f_2_hw_phy_config(tp);
3402 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003403
Hayes Wang7e18dca2012-03-30 14:33:02 +08003404 case RTL_GIGA_MAC_VER_37:
3405 rtl8402_hw_phy_config(tp);
3406 break;
3407
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003408 case RTL_GIGA_MAC_VER_38:
3409 rtl8411_hw_phy_config(tp);
3410 break;
3411
Hayes Wang5598bfe2012-07-02 17:23:21 +08003412 case RTL_GIGA_MAC_VER_39:
3413 rtl8106e_hw_phy_config(tp);
3414 break;
3415
Francois Romieu5615d9f2007-08-17 17:50:46 +02003416 default:
3417 break;
3418 }
3419}
3420
Francois Romieuda78dbf2012-01-26 14:18:23 +01003421static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 struct timer_list *timer = &tp->timer;
3424 void __iomem *ioaddr = tp->mmio_addr;
3425 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3426
Francois Romieubcf0bf92006-07-26 23:14:13 +02003427 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
françois romieu4da19632011-01-03 15:07:55 +00003429 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003430 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 * A busy loop could burn quite a few cycles on nowadays CPU.
3432 * Let's delay the execution of the timer for a few ticks.
3433 */
3434 timeout = HZ/10;
3435 goto out_mod_timer;
3436 }
3437
3438 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01003439 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
Francois Romieuda78dbf2012-01-26 14:18:23 +01003441 netif_warn(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
françois romieu4da19632011-01-03 15:07:55 +00003443 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
3445out_mod_timer:
3446 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003447}
3448
3449static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3450{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003451 if (!test_and_set_bit(flag, tp->wk.flags))
3452 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003453}
3454
3455static void rtl8169_phy_timer(unsigned long __opaque)
3456{
3457 struct net_device *dev = (struct net_device *)__opaque;
3458 struct rtl8169_private *tp = netdev_priv(dev);
3459
Francois Romieu98ddf982012-01-31 10:47:34 +01003460 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461}
3462
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3464 void __iomem *ioaddr)
3465{
3466 iounmap(ioaddr);
3467 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003468 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 pci_disable_device(pdev);
3470 free_netdev(dev);
3471}
3472
Francois Romieubf793292006-11-01 00:53:05 +01003473static void rtl8169_phy_reset(struct net_device *dev,
3474 struct rtl8169_private *tp)
3475{
Francois Romieu07d3f512007-02-21 22:40:46 +01003476 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003477
françois romieu4da19632011-01-03 15:07:55 +00003478 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003479 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003480 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003481 return;
3482 msleep(1);
3483 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003484 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003485}
3486
David S. Miller8decf862011-09-22 03:23:13 -04003487static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3488{
3489 void __iomem *ioaddr = tp->mmio_addr;
3490
3491 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3492 (RTL_R8(PHYstatus) & TBI_Enable);
3493}
3494
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003495static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003497 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003498
Francois Romieu5615d9f2007-08-17 17:50:46 +02003499 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003500
Marcus Sundberg773328942008-07-10 21:28:08 +02003501 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3502 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3503 RTL_W8(0x82, 0x01);
3504 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003505
Francois Romieu6dccd162007-02-13 23:38:05 +01003506 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3507
3508 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3509 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003510
Francois Romieubcf0bf92006-07-26 23:14:13 +02003511 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003512 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3513 RTL_W8(0x82, 0x01);
3514 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003515 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003516 }
3517
Francois Romieubf793292006-11-01 00:53:05 +01003518 rtl8169_phy_reset(dev, tp);
3519
Oliver Neukum54405cd2011-01-06 21:55:13 +01003520 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003521 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3522 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3523 (tp->mii.supports_gmii ?
3524 ADVERTISED_1000baseT_Half |
3525 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003526
David S. Miller8decf862011-09-22 03:23:13 -04003527 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003528 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003529}
3530
Francois Romieu773d2022007-01-31 23:47:43 +01003531static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3532{
3533 void __iomem *ioaddr = tp->mmio_addr;
3534 u32 high;
3535 u32 low;
3536
3537 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3538 high = addr[4] | (addr[5] << 8);
3539
Francois Romieuda78dbf2012-01-26 14:18:23 +01003540 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003541
3542 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003543
Francois Romieu773d2022007-01-31 23:47:43 +01003544 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00003545 RTL_R32(MAC4);
3546
Francois Romieu78f1cd02010-03-27 19:35:46 -07003547 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00003548 RTL_R32(MAC0);
3549
françois romieuc28aa382011-08-02 03:53:43 +00003550 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3551 const struct exgmac_reg e[] = {
3552 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3553 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3554 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3555 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3556 low >> 16 },
3557 };
3558
3559 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3560 }
3561
Francois Romieu773d2022007-01-31 23:47:43 +01003562 RTL_W8(Cfg9346, Cfg9346_Lock);
3563
Francois Romieuda78dbf2012-01-26 14:18:23 +01003564 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003565}
3566
3567static int rtl_set_mac_address(struct net_device *dev, void *p)
3568{
3569 struct rtl8169_private *tp = netdev_priv(dev);
3570 struct sockaddr *addr = p;
3571
3572 if (!is_valid_ether_addr(addr->sa_data))
3573 return -EADDRNOTAVAIL;
3574
3575 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3576
3577 rtl_rar_set(tp, dev->dev_addr);
3578
3579 return 0;
3580}
3581
Francois Romieu5f787a12006-08-17 13:02:36 +02003582static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3583{
3584 struct rtl8169_private *tp = netdev_priv(dev);
3585 struct mii_ioctl_data *data = if_mii(ifr);
3586
Francois Romieu8b4ab282008-11-19 22:05:25 -08003587 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3588}
Francois Romieu5f787a12006-08-17 13:02:36 +02003589
Francois Romieucecb5fd2011-04-01 10:21:07 +02003590static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3591 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003592{
Francois Romieu5f787a12006-08-17 13:02:36 +02003593 switch (cmd) {
3594 case SIOCGMIIPHY:
3595 data->phy_id = 32; /* Internal PHY */
3596 return 0;
3597
3598 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003599 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003600 return 0;
3601
3602 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003603 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003604 return 0;
3605 }
3606 return -EOPNOTSUPP;
3607}
3608
Francois Romieu8b4ab282008-11-19 22:05:25 -08003609static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3610{
3611 return -EOPNOTSUPP;
3612}
3613
Francois Romieufbac58f2007-10-04 22:51:38 +02003614static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3615{
3616 if (tp->features & RTL_FEATURE_MSI) {
3617 pci_disable_msi(pdev);
3618 tp->features &= ~RTL_FEATURE_MSI;
3619 }
3620}
3621
françois romieuc0e45c12011-01-03 15:08:04 +00003622static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3623{
3624 struct mdio_ops *ops = &tp->mdio_ops;
3625
3626 switch (tp->mac_version) {
3627 case RTL_GIGA_MAC_VER_27:
3628 ops->write = r8168dp_1_mdio_write;
3629 ops->read = r8168dp_1_mdio_read;
3630 break;
françois romieue6de30d2011-01-03 15:08:37 +00003631 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003632 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003633 ops->write = r8168dp_2_mdio_write;
3634 ops->read = r8168dp_2_mdio_read;
3635 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003636 default:
3637 ops->write = r8169_mdio_write;
3638 ops->read = r8169_mdio_read;
3639 break;
3640 }
3641}
3642
David S. Miller1805b2f2011-10-24 18:18:09 -04003643static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3644{
3645 void __iomem *ioaddr = tp->mmio_addr;
3646
3647 switch (tp->mac_version) {
3648 case RTL_GIGA_MAC_VER_29:
3649 case RTL_GIGA_MAC_VER_30:
3650 case RTL_GIGA_MAC_VER_32:
3651 case RTL_GIGA_MAC_VER_33:
3652 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08003653 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003654 case RTL_GIGA_MAC_VER_38:
Hayes Wang5598bfe2012-07-02 17:23:21 +08003655 case RTL_GIGA_MAC_VER_39:
David S. Miller1805b2f2011-10-24 18:18:09 -04003656 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3657 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3658 break;
3659 default:
3660 break;
3661 }
3662}
3663
3664static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3665{
3666 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3667 return false;
3668
3669 rtl_writephy(tp, 0x1f, 0x0000);
3670 rtl_writephy(tp, MII_BMCR, 0x0000);
3671
3672 rtl_wol_suspend_quirk(tp);
3673
3674 return true;
3675}
3676
françois romieu065c27c2011-01-03 15:08:12 +00003677static void r810x_phy_power_down(struct rtl8169_private *tp)
3678{
3679 rtl_writephy(tp, 0x1f, 0x0000);
3680 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3681}
3682
3683static void r810x_phy_power_up(struct rtl8169_private *tp)
3684{
3685 rtl_writephy(tp, 0x1f, 0x0000);
3686 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3687}
3688
3689static void r810x_pll_power_down(struct rtl8169_private *tp)
3690{
Hayes Wang00042992012-03-30 14:33:00 +08003691 void __iomem *ioaddr = tp->mmio_addr;
3692
David S. Miller1805b2f2011-10-24 18:18:09 -04003693 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003694 return;
françois romieu065c27c2011-01-03 15:08:12 +00003695
3696 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003697
3698 switch (tp->mac_version) {
3699 case RTL_GIGA_MAC_VER_07:
3700 case RTL_GIGA_MAC_VER_08:
3701 case RTL_GIGA_MAC_VER_09:
3702 case RTL_GIGA_MAC_VER_10:
3703 case RTL_GIGA_MAC_VER_13:
3704 case RTL_GIGA_MAC_VER_16:
3705 break;
3706 default:
3707 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3708 break;
3709 }
françois romieu065c27c2011-01-03 15:08:12 +00003710}
3711
3712static void r810x_pll_power_up(struct rtl8169_private *tp)
3713{
Hayes Wang00042992012-03-30 14:33:00 +08003714 void __iomem *ioaddr = tp->mmio_addr;
3715
françois romieu065c27c2011-01-03 15:08:12 +00003716 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003717
3718 switch (tp->mac_version) {
3719 case RTL_GIGA_MAC_VER_07:
3720 case RTL_GIGA_MAC_VER_08:
3721 case RTL_GIGA_MAC_VER_09:
3722 case RTL_GIGA_MAC_VER_10:
3723 case RTL_GIGA_MAC_VER_13:
3724 case RTL_GIGA_MAC_VER_16:
3725 break;
3726 default:
3727 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3728 break;
3729 }
françois romieu065c27c2011-01-03 15:08:12 +00003730}
3731
3732static void r8168_phy_power_up(struct rtl8169_private *tp)
3733{
3734 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003735 switch (tp->mac_version) {
3736 case RTL_GIGA_MAC_VER_11:
3737 case RTL_GIGA_MAC_VER_12:
3738 case RTL_GIGA_MAC_VER_17:
3739 case RTL_GIGA_MAC_VER_18:
3740 case RTL_GIGA_MAC_VER_19:
3741 case RTL_GIGA_MAC_VER_20:
3742 case RTL_GIGA_MAC_VER_21:
3743 case RTL_GIGA_MAC_VER_22:
3744 case RTL_GIGA_MAC_VER_23:
3745 case RTL_GIGA_MAC_VER_24:
3746 case RTL_GIGA_MAC_VER_25:
3747 case RTL_GIGA_MAC_VER_26:
3748 case RTL_GIGA_MAC_VER_27:
3749 case RTL_GIGA_MAC_VER_28:
3750 case RTL_GIGA_MAC_VER_31:
3751 rtl_writephy(tp, 0x0e, 0x0000);
3752 break;
3753 default:
3754 break;
3755 }
françois romieu065c27c2011-01-03 15:08:12 +00003756 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3757}
3758
3759static void r8168_phy_power_down(struct rtl8169_private *tp)
3760{
3761 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003762 switch (tp->mac_version) {
3763 case RTL_GIGA_MAC_VER_32:
3764 case RTL_GIGA_MAC_VER_33:
3765 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3766 break;
3767
3768 case RTL_GIGA_MAC_VER_11:
3769 case RTL_GIGA_MAC_VER_12:
3770 case RTL_GIGA_MAC_VER_17:
3771 case RTL_GIGA_MAC_VER_18:
3772 case RTL_GIGA_MAC_VER_19:
3773 case RTL_GIGA_MAC_VER_20:
3774 case RTL_GIGA_MAC_VER_21:
3775 case RTL_GIGA_MAC_VER_22:
3776 case RTL_GIGA_MAC_VER_23:
3777 case RTL_GIGA_MAC_VER_24:
3778 case RTL_GIGA_MAC_VER_25:
3779 case RTL_GIGA_MAC_VER_26:
3780 case RTL_GIGA_MAC_VER_27:
3781 case RTL_GIGA_MAC_VER_28:
3782 case RTL_GIGA_MAC_VER_31:
3783 rtl_writephy(tp, 0x0e, 0x0200);
3784 default:
3785 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3786 break;
3787 }
françois romieu065c27c2011-01-03 15:08:12 +00003788}
3789
3790static void r8168_pll_power_down(struct rtl8169_private *tp)
3791{
3792 void __iomem *ioaddr = tp->mmio_addr;
3793
Francois Romieucecb5fd2011-04-01 10:21:07 +02003794 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3795 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3796 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003797 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003798 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003799 }
françois romieu065c27c2011-01-03 15:08:12 +00003800
Francois Romieucecb5fd2011-04-01 10:21:07 +02003801 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3802 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003803 (RTL_R16(CPlusCmd) & ASF)) {
3804 return;
3805 }
3806
hayeswang01dc7fe2011-03-21 01:50:28 +00003807 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3808 tp->mac_version == RTL_GIGA_MAC_VER_33)
3809 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3810
David S. Miller1805b2f2011-10-24 18:18:09 -04003811 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003812 return;
françois romieu065c27c2011-01-03 15:08:12 +00003813
3814 r8168_phy_power_down(tp);
3815
3816 switch (tp->mac_version) {
3817 case RTL_GIGA_MAC_VER_25:
3818 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003819 case RTL_GIGA_MAC_VER_27:
3820 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003821 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003822 case RTL_GIGA_MAC_VER_32:
3823 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003824 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3825 break;
3826 }
3827}
3828
3829static void r8168_pll_power_up(struct rtl8169_private *tp)
3830{
3831 void __iomem *ioaddr = tp->mmio_addr;
3832
françois romieu065c27c2011-01-03 15:08:12 +00003833 switch (tp->mac_version) {
3834 case RTL_GIGA_MAC_VER_25:
3835 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003836 case RTL_GIGA_MAC_VER_27:
3837 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003838 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003839 case RTL_GIGA_MAC_VER_32:
3840 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003841 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3842 break;
3843 }
3844
3845 r8168_phy_power_up(tp);
3846}
3847
Francois Romieud58d46b2011-05-03 16:38:29 +02003848static void rtl_generic_op(struct rtl8169_private *tp,
3849 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00003850{
3851 if (op)
3852 op(tp);
3853}
3854
3855static void rtl_pll_power_down(struct rtl8169_private *tp)
3856{
Francois Romieud58d46b2011-05-03 16:38:29 +02003857 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00003858}
3859
3860static void rtl_pll_power_up(struct rtl8169_private *tp)
3861{
Francois Romieud58d46b2011-05-03 16:38:29 +02003862 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00003863}
3864
3865static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3866{
3867 struct pll_power_ops *ops = &tp->pll_power_ops;
3868
3869 switch (tp->mac_version) {
3870 case RTL_GIGA_MAC_VER_07:
3871 case RTL_GIGA_MAC_VER_08:
3872 case RTL_GIGA_MAC_VER_09:
3873 case RTL_GIGA_MAC_VER_10:
3874 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003875 case RTL_GIGA_MAC_VER_29:
3876 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08003877 case RTL_GIGA_MAC_VER_37:
Hayes Wang5598bfe2012-07-02 17:23:21 +08003878 case RTL_GIGA_MAC_VER_39:
françois romieu065c27c2011-01-03 15:08:12 +00003879 ops->down = r810x_pll_power_down;
3880 ops->up = r810x_pll_power_up;
3881 break;
3882
3883 case RTL_GIGA_MAC_VER_11:
3884 case RTL_GIGA_MAC_VER_12:
3885 case RTL_GIGA_MAC_VER_17:
3886 case RTL_GIGA_MAC_VER_18:
3887 case RTL_GIGA_MAC_VER_19:
3888 case RTL_GIGA_MAC_VER_20:
3889 case RTL_GIGA_MAC_VER_21:
3890 case RTL_GIGA_MAC_VER_22:
3891 case RTL_GIGA_MAC_VER_23:
3892 case RTL_GIGA_MAC_VER_24:
3893 case RTL_GIGA_MAC_VER_25:
3894 case RTL_GIGA_MAC_VER_26:
3895 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003896 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003897 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003898 case RTL_GIGA_MAC_VER_32:
3899 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003900 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08003901 case RTL_GIGA_MAC_VER_35:
3902 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003903 case RTL_GIGA_MAC_VER_38:
françois romieu065c27c2011-01-03 15:08:12 +00003904 ops->down = r8168_pll_power_down;
3905 ops->up = r8168_pll_power_up;
3906 break;
3907
3908 default:
3909 ops->down = NULL;
3910 ops->up = NULL;
3911 break;
3912 }
3913}
3914
Hayes Wange542a222011-07-06 15:58:04 +08003915static void rtl_init_rxcfg(struct rtl8169_private *tp)
3916{
3917 void __iomem *ioaddr = tp->mmio_addr;
3918
3919 switch (tp->mac_version) {
3920 case RTL_GIGA_MAC_VER_01:
3921 case RTL_GIGA_MAC_VER_02:
3922 case RTL_GIGA_MAC_VER_03:
3923 case RTL_GIGA_MAC_VER_04:
3924 case RTL_GIGA_MAC_VER_05:
3925 case RTL_GIGA_MAC_VER_06:
3926 case RTL_GIGA_MAC_VER_10:
3927 case RTL_GIGA_MAC_VER_11:
3928 case RTL_GIGA_MAC_VER_12:
3929 case RTL_GIGA_MAC_VER_13:
3930 case RTL_GIGA_MAC_VER_14:
3931 case RTL_GIGA_MAC_VER_15:
3932 case RTL_GIGA_MAC_VER_16:
3933 case RTL_GIGA_MAC_VER_17:
3934 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3935 break;
3936 case RTL_GIGA_MAC_VER_18:
3937 case RTL_GIGA_MAC_VER_19:
3938 case RTL_GIGA_MAC_VER_20:
3939 case RTL_GIGA_MAC_VER_21:
3940 case RTL_GIGA_MAC_VER_22:
3941 case RTL_GIGA_MAC_VER_23:
3942 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00003943 case RTL_GIGA_MAC_VER_34:
Hayes Wange542a222011-07-06 15:58:04 +08003944 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3945 break;
3946 default:
3947 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3948 break;
3949 }
3950}
3951
Hayes Wang92fc43b2011-07-06 15:58:03 +08003952static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3953{
3954 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3955}
3956
Francois Romieud58d46b2011-05-03 16:38:29 +02003957static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3958{
françois romieu9c5028e2012-03-02 04:43:14 +00003959 void __iomem *ioaddr = tp->mmio_addr;
3960
3961 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003962 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00003963 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003964}
3965
3966static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3967{
françois romieu9c5028e2012-03-02 04:43:14 +00003968 void __iomem *ioaddr = tp->mmio_addr;
3969
3970 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003971 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00003972 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003973}
3974
3975static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3976{
3977 void __iomem *ioaddr = tp->mmio_addr;
3978
3979 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3980 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3981 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3982}
3983
3984static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3985{
3986 void __iomem *ioaddr = tp->mmio_addr;
3987
3988 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3989 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3990 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3991}
3992
3993static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3994{
3995 void __iomem *ioaddr = tp->mmio_addr;
3996
3997 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3998}
3999
4000static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4001{
4002 void __iomem *ioaddr = tp->mmio_addr;
4003
4004 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4005}
4006
4007static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4008{
4009 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02004010
4011 RTL_W8(MaxTxPacketSize, 0x3f);
4012 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4013 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01004014 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02004015}
4016
4017static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4018{
4019 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02004020
4021 RTL_W8(MaxTxPacketSize, 0x0c);
4022 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4023 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01004024 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02004025}
4026
4027static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4028{
4029 rtl_tx_performance_tweak(tp->pci_dev,
4030 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4031}
4032
4033static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4034{
4035 rtl_tx_performance_tweak(tp->pci_dev,
4036 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4037}
4038
4039static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4040{
4041 void __iomem *ioaddr = tp->mmio_addr;
4042
4043 r8168b_0_hw_jumbo_enable(tp);
4044
4045 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
4046}
4047
4048static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4049{
4050 void __iomem *ioaddr = tp->mmio_addr;
4051
4052 r8168b_0_hw_jumbo_disable(tp);
4053
4054 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4055}
4056
4057static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
4058{
4059 struct jumbo_ops *ops = &tp->jumbo_ops;
4060
4061 switch (tp->mac_version) {
4062 case RTL_GIGA_MAC_VER_11:
4063 ops->disable = r8168b_0_hw_jumbo_disable;
4064 ops->enable = r8168b_0_hw_jumbo_enable;
4065 break;
4066 case RTL_GIGA_MAC_VER_12:
4067 case RTL_GIGA_MAC_VER_17:
4068 ops->disable = r8168b_1_hw_jumbo_disable;
4069 ops->enable = r8168b_1_hw_jumbo_enable;
4070 break;
4071 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4072 case RTL_GIGA_MAC_VER_19:
4073 case RTL_GIGA_MAC_VER_20:
4074 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4075 case RTL_GIGA_MAC_VER_22:
4076 case RTL_GIGA_MAC_VER_23:
4077 case RTL_GIGA_MAC_VER_24:
4078 case RTL_GIGA_MAC_VER_25:
4079 case RTL_GIGA_MAC_VER_26:
4080 ops->disable = r8168c_hw_jumbo_disable;
4081 ops->enable = r8168c_hw_jumbo_enable;
4082 break;
4083 case RTL_GIGA_MAC_VER_27:
4084 case RTL_GIGA_MAC_VER_28:
4085 ops->disable = r8168dp_hw_jumbo_disable;
4086 ops->enable = r8168dp_hw_jumbo_enable;
4087 break;
4088 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4089 case RTL_GIGA_MAC_VER_32:
4090 case RTL_GIGA_MAC_VER_33:
4091 case RTL_GIGA_MAC_VER_34:
4092 ops->disable = r8168e_hw_jumbo_disable;
4093 ops->enable = r8168e_hw_jumbo_enable;
4094 break;
4095
4096 /*
4097 * No action needed for jumbo frames with 8169.
4098 * No jumbo for 810x at all.
4099 */
4100 default:
4101 ops->disable = NULL;
4102 ops->enable = NULL;
4103 break;
4104 }
4105}
4106
Francois Romieu6f43adc2011-04-29 15:05:51 +02004107static void rtl_hw_reset(struct rtl8169_private *tp)
4108{
4109 void __iomem *ioaddr = tp->mmio_addr;
4110 int i;
4111
4112 /* Soft reset the chip. */
4113 RTL_W8(ChipCmd, CmdReset);
4114
4115 /* Check that the chip has finished the reset. */
4116 for (i = 0; i < 100; i++) {
4117 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
4118 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004119 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004120 }
4121}
4122
Francois Romieub6ffd972011-06-17 17:00:05 +02004123static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4124{
4125 struct rtl_fw *rtl_fw;
4126 const char *name;
4127 int rc = -ENOMEM;
4128
4129 name = rtl_lookup_firmware_name(tp);
4130 if (!name)
4131 goto out_no_firmware;
4132
4133 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4134 if (!rtl_fw)
4135 goto err_warn;
4136
4137 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4138 if (rc < 0)
4139 goto err_free;
4140
Francois Romieufd112f22011-06-18 00:10:29 +02004141 rc = rtl_check_firmware(tp, rtl_fw);
4142 if (rc < 0)
4143 goto err_release_firmware;
4144
Francois Romieub6ffd972011-06-17 17:00:05 +02004145 tp->rtl_fw = rtl_fw;
4146out:
4147 return;
4148
Francois Romieufd112f22011-06-18 00:10:29 +02004149err_release_firmware:
4150 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004151err_free:
4152 kfree(rtl_fw);
4153err_warn:
4154 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4155 name, rc);
4156out_no_firmware:
4157 tp->rtl_fw = NULL;
4158 goto out;
4159}
4160
François Romieu953a12c2011-04-24 17:38:48 +02004161static void rtl_request_firmware(struct rtl8169_private *tp)
4162{
Francois Romieub6ffd972011-06-17 17:00:05 +02004163 if (IS_ERR(tp->rtl_fw))
4164 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004165}
4166
Hayes Wang92fc43b2011-07-06 15:58:03 +08004167static void rtl_rx_close(struct rtl8169_private *tp)
4168{
4169 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004170
Francois Romieu1687b562011-07-19 17:21:29 +02004171 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004172}
4173
françois romieue6de30d2011-01-03 15:08:37 +00004174static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175{
françois romieue6de30d2011-01-03 15:08:37 +00004176 void __iomem *ioaddr = tp->mmio_addr;
4177
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004179 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
Hayes Wang92fc43b2011-07-06 15:58:03 +08004181 rtl_rx_close(tp);
4182
Hayes Wang5d2e1952011-02-22 17:26:22 +08004183 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004184 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4185 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00004186 while (RTL_R8(TxPoll) & NPQ)
4187 udelay(20);
Hayes Wangc2218922011-09-06 16:55:18 +08004188 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4189 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
Hayes Wang7e18dca2012-03-30 14:33:02 +08004190 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004191 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
4192 tp->mac_version == RTL_GIGA_MAC_VER_38) {
David S. Miller8decf862011-09-22 03:23:13 -04004193 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08004194 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4195 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004196 } else {
4197 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4198 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004199 }
4200
Hayes Wang92fc43b2011-07-06 15:58:03 +08004201 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202}
4203
Francois Romieu7f796d832007-06-11 23:04:41 +02004204static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004205{
4206 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004207
4208 /* Set DMA burst size and Interframe Gap Time */
4209 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4210 (InterFrameGap << TxInterFrameGapShift));
4211}
4212
Francois Romieu07ce4062007-02-23 23:36:39 +01004213static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214{
4215 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216
Francois Romieu07ce4062007-02-23 23:36:39 +01004217 tp->hw_start(dev);
4218
Francois Romieuda78dbf2012-01-26 14:18:23 +01004219 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004220}
4221
Francois Romieu7f796d832007-06-11 23:04:41 +02004222static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4223 void __iomem *ioaddr)
4224{
4225 /*
4226 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4227 * register to be written before TxDescAddrLow to work.
4228 * Switching from MMIO to I/O access fixes the issue as well.
4229 */
4230 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004231 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004232 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004233 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004234}
4235
4236static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4237{
4238 u16 cmd;
4239
4240 cmd = RTL_R16(CPlusCmd);
4241 RTL_W16(CPlusCmd, cmd);
4242 return cmd;
4243}
4244
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004245static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004246{
4247 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00004248 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004249}
4250
Francois Romieu6dccd162007-02-13 23:38:05 +01004251static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4252{
Francois Romieu37441002011-06-17 22:58:54 +02004253 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004254 u32 mac_version;
4255 u32 clk;
4256 u32 val;
4257 } cfg2_info [] = {
4258 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4259 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4260 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4261 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004262 };
4263 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004264 unsigned int i;
4265 u32 clk;
4266
4267 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004268 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004269 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4270 RTL_W32(0x7c, p->val);
4271 break;
4272 }
4273 }
4274}
4275
Francois Romieue6b763e2012-03-08 09:35:39 +01004276static void rtl_set_rx_mode(struct net_device *dev)
4277{
4278 struct rtl8169_private *tp = netdev_priv(dev);
4279 void __iomem *ioaddr = tp->mmio_addr;
4280 u32 mc_filter[2]; /* Multicast hash filter */
4281 int rx_mode;
4282 u32 tmp = 0;
4283
4284 if (dev->flags & IFF_PROMISC) {
4285 /* Unconditionally log net taps. */
4286 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4287 rx_mode =
4288 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4289 AcceptAllPhys;
4290 mc_filter[1] = mc_filter[0] = 0xffffffff;
4291 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4292 (dev->flags & IFF_ALLMULTI)) {
4293 /* Too many to filter perfectly -- accept all multicasts. */
4294 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4295 mc_filter[1] = mc_filter[0] = 0xffffffff;
4296 } else {
4297 struct netdev_hw_addr *ha;
4298
4299 rx_mode = AcceptBroadcast | AcceptMyPhys;
4300 mc_filter[1] = mc_filter[0] = 0;
4301 netdev_for_each_mc_addr(ha, dev) {
4302 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4303 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4304 rx_mode |= AcceptMulticast;
4305 }
4306 }
4307
4308 if (dev->features & NETIF_F_RXALL)
4309 rx_mode |= (AcceptErr | AcceptRunt);
4310
4311 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4312
4313 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4314 u32 data = mc_filter[0];
4315
4316 mc_filter[0] = swab32(mc_filter[1]);
4317 mc_filter[1] = swab32(data);
4318 }
4319
4320 RTL_W32(MAR0 + 4, mc_filter[1]);
4321 RTL_W32(MAR0 + 0, mc_filter[0]);
4322
4323 RTL_W32(RxConfig, tmp);
4324}
4325
Francois Romieu07ce4062007-02-23 23:36:39 +01004326static void rtl_hw_start_8169(struct net_device *dev)
4327{
4328 struct rtl8169_private *tp = netdev_priv(dev);
4329 void __iomem *ioaddr = tp->mmio_addr;
4330 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004331
Francois Romieu9cb427b2006-11-02 00:10:16 +01004332 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4333 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4334 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4335 }
4336
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004338 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4339 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4340 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4341 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004342 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4343
Hayes Wange542a222011-07-06 15:58:04 +08004344 rtl_init_rxcfg(tp);
4345
françois romieuf0298f82011-01-03 15:07:42 +00004346 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004348 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
Francois Romieucecb5fd2011-04-01 10:21:07 +02004350 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4351 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4352 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4353 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004354 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355
Francois Romieu7f796d832007-06-11 23:04:41 +02004356 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004357
Francois Romieucecb5fd2011-04-01 10:21:07 +02004358 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4359 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004360 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004362 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 }
4364
Francois Romieubcf0bf92006-07-26 23:14:13 +02004365 RTL_W16(CPlusCmd, tp->cp_cmd);
4366
Francois Romieu6dccd162007-02-13 23:38:05 +01004367 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4368
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 /*
4370 * Undocumented corner. Supposedly:
4371 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4372 */
4373 RTL_W16(IntrMitigate, 0x0000);
4374
Francois Romieu7f796d832007-06-11 23:04:41 +02004375 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004376
Francois Romieucecb5fd2011-04-01 10:21:07 +02004377 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4378 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4379 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4380 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004381 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4382 rtl_set_rx_tx_config_registers(tp);
4383 }
4384
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004386
4387 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4388 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389
4390 RTL_W32(RxMissed, 0);
4391
Francois Romieu07ce4062007-02-23 23:36:39 +01004392 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
4394 /* no early-rx interrupts */
4395 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004396}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004398static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
4399{
4400 if (tp->csi_ops.write)
4401 tp->csi_ops.write(tp->mmio_addr, addr, value);
4402}
4403
4404static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
4405{
4406 if (tp->csi_ops.read)
4407 return tp->csi_ops.read(tp->mmio_addr, addr);
4408 else
4409 return ~0;
4410}
4411
4412static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004413{
4414 u32 csi;
4415
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004416 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4417 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00004418}
4419
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004420static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004421{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004422 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00004423}
4424
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004425static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00004426{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004427 rtl_csi_access_enable(tp, 0x27000000);
4428}
4429
4430static void r8169_csi_write(void __iomem *ioaddr, int addr, int value)
4431{
4432 unsigned int i;
4433
4434 RTL_W32(CSIDR, value);
4435 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4436 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4437
4438 for (i = 0; i < 100; i++) {
4439 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
4440 break;
4441 udelay(10);
4442 }
4443}
4444
4445static u32 r8169_csi_read(void __iomem *ioaddr, int addr)
4446{
4447 u32 value = ~0x00;
4448 unsigned int i;
4449
4450 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
4451 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4452
4453 for (i = 0; i < 100; i++) {
4454 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
4455 value = RTL_R32(CSIDR);
4456 break;
4457 }
4458 udelay(10);
4459 }
4460
4461 return value;
4462}
4463
Hayes Wang7e18dca2012-03-30 14:33:02 +08004464static void r8402_csi_write(void __iomem *ioaddr, int addr, int value)
4465{
4466 unsigned int i;
4467
4468 RTL_W32(CSIDR, value);
4469 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4470 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
4471 CSIAR_FUNC_NIC);
4472
4473 for (i = 0; i < 100; i++) {
4474 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
4475 break;
4476 udelay(10);
4477 }
4478}
4479
4480static u32 r8402_csi_read(void __iomem *ioaddr, int addr)
4481{
4482 u32 value = ~0x00;
4483 unsigned int i;
4484
4485 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
4486 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4487
4488 for (i = 0; i < 100; i++) {
4489 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
4490 value = RTL_R32(CSIDR);
4491 break;
4492 }
4493 udelay(10);
4494 }
4495
4496 return value;
4497}
4498
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004499static void __devinit rtl_init_csi_ops(struct rtl8169_private *tp)
4500{
4501 struct csi_ops *ops = &tp->csi_ops;
4502
4503 switch (tp->mac_version) {
4504 case RTL_GIGA_MAC_VER_01:
4505 case RTL_GIGA_MAC_VER_02:
4506 case RTL_GIGA_MAC_VER_03:
4507 case RTL_GIGA_MAC_VER_04:
4508 case RTL_GIGA_MAC_VER_05:
4509 case RTL_GIGA_MAC_VER_06:
4510 case RTL_GIGA_MAC_VER_10:
4511 case RTL_GIGA_MAC_VER_11:
4512 case RTL_GIGA_MAC_VER_12:
4513 case RTL_GIGA_MAC_VER_13:
4514 case RTL_GIGA_MAC_VER_14:
4515 case RTL_GIGA_MAC_VER_15:
4516 case RTL_GIGA_MAC_VER_16:
4517 case RTL_GIGA_MAC_VER_17:
4518 ops->write = NULL;
4519 ops->read = NULL;
4520 break;
4521
Hayes Wang7e18dca2012-03-30 14:33:02 +08004522 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004523 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004524 ops->write = r8402_csi_write;
4525 ops->read = r8402_csi_read;
4526 break;
4527
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004528 default:
4529 ops->write = r8169_csi_write;
4530 ops->read = r8169_csi_read;
4531 break;
4532 }
Francois Romieudacf8152008-08-02 20:44:13 +02004533}
4534
4535struct ephy_info {
4536 unsigned int offset;
4537 u16 mask;
4538 u16 bits;
4539};
4540
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004541static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004542{
4543 u16 w;
4544
4545 while (len-- > 0) {
4546 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4547 rtl_ephy_write(ioaddr, e->offset, w);
4548 e++;
4549 }
4550}
4551
Francois Romieub726e492008-06-28 12:22:59 +02004552static void rtl_disable_clock_request(struct pci_dev *pdev)
4553{
Jon Masone44daad2011-06-27 07:46:31 +00004554 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004555
4556 if (cap) {
4557 u16 ctl;
4558
4559 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4560 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4561 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4562 }
4563}
4564
françois romieue6de30d2011-01-03 15:08:37 +00004565static void rtl_enable_clock_request(struct pci_dev *pdev)
4566{
Jon Masone44daad2011-06-27 07:46:31 +00004567 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004568
4569 if (cap) {
4570 u16 ctl;
4571
4572 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4573 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4574 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4575 }
4576}
4577
Francois Romieub726e492008-06-28 12:22:59 +02004578#define R8168_CPCMD_QUIRK_MASK (\
4579 EnableBist | \
4580 Mac_dbgo_oe | \
4581 Force_half_dup | \
4582 Force_rxflow_en | \
4583 Force_txflow_en | \
4584 Cxpl_dbg_sel | \
4585 ASF | \
4586 PktCntrDisable | \
4587 Mac_dbgo_sel)
4588
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004589static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004590{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004591 void __iomem *ioaddr = tp->mmio_addr;
4592 struct pci_dev *pdev = tp->pci_dev;
4593
Francois Romieub726e492008-06-28 12:22:59 +02004594 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4595
4596 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4597
Francois Romieu2e68ae42008-06-28 12:00:55 +02004598 rtl_tx_performance_tweak(pdev,
4599 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004600}
4601
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004602static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004603{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004604 void __iomem *ioaddr = tp->mmio_addr;
4605
4606 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004607
françois romieuf0298f82011-01-03 15:07:42 +00004608 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004609
4610 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004611}
4612
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004613static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004614{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004615 void __iomem *ioaddr = tp->mmio_addr;
4616 struct pci_dev *pdev = tp->pci_dev;
4617
Francois Romieub726e492008-06-28 12:22:59 +02004618 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4619
4620 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4621
Francois Romieu219a1e92008-06-28 11:58:39 +02004622 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004623
4624 rtl_disable_clock_request(pdev);
4625
4626 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004627}
4628
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004629static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004630{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004631 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004632 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004633 { 0x01, 0, 0x0001 },
4634 { 0x02, 0x0800, 0x1000 },
4635 { 0x03, 0, 0x0042 },
4636 { 0x06, 0x0080, 0x0000 },
4637 { 0x07, 0, 0x2000 }
4638 };
4639
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004640 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004641
4642 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4643
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004644 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004645}
4646
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004647static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02004648{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004649 void __iomem *ioaddr = tp->mmio_addr;
4650 struct pci_dev *pdev = tp->pci_dev;
4651
4652 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004653
4654 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4655
4656 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4657
4658 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4659}
4660
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004661static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004662{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004663 void __iomem *ioaddr = tp->mmio_addr;
4664 struct pci_dev *pdev = tp->pci_dev;
4665
4666 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004667
4668 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4669
4670 /* Magic. */
4671 RTL_W8(DBG_REG, 0x20);
4672
françois romieuf0298f82011-01-03 15:07:42 +00004673 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004674
4675 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4676
4677 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4678}
4679
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004680static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004681{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004682 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004683 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004684 { 0x02, 0x0800, 0x1000 },
4685 { 0x03, 0, 0x0002 },
4686 { 0x06, 0x0080, 0x0000 }
4687 };
4688
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004689 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004690
4691 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4692
4693 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4694
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004695 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004696}
4697
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004698static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004699{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004700 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004701 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004702 { 0x01, 0, 0x0001 },
4703 { 0x03, 0x0400, 0x0220 }
4704 };
4705
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004706 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004707
4708 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4709
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004710 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004711}
4712
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004713static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02004714{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004715 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004716}
4717
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004718static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02004719{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004720 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004721
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004722 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004723}
4724
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004725static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02004726{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004727 void __iomem *ioaddr = tp->mmio_addr;
4728 struct pci_dev *pdev = tp->pci_dev;
4729
4730 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004731
4732 rtl_disable_clock_request(pdev);
4733
françois romieuf0298f82011-01-03 15:07:42 +00004734 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004735
4736 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4737
4738 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4739}
4740
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004741static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00004742{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004743 void __iomem *ioaddr = tp->mmio_addr;
4744 struct pci_dev *pdev = tp->pci_dev;
4745
4746 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004747
4748 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4749
4750 RTL_W8(MaxTxPacketSize, TxPacketMax);
4751
4752 rtl_disable_clock_request(pdev);
4753}
4754
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004755static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004756{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004757 void __iomem *ioaddr = tp->mmio_addr;
4758 struct pci_dev *pdev = tp->pci_dev;
françois romieue6de30d2011-01-03 15:08:37 +00004759 static const struct ephy_info e_info_8168d_4[] = {
4760 { 0x0b, ~0, 0x48 },
4761 { 0x19, 0x20, 0x50 },
4762 { 0x0c, ~0, 0x20 }
4763 };
4764 int i;
4765
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004766 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004767
4768 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4769
4770 RTL_W8(MaxTxPacketSize, TxPacketMax);
4771
4772 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4773 const struct ephy_info *e = e_info_8168d_4 + i;
4774 u16 w;
4775
4776 w = rtl_ephy_read(ioaddr, e->offset);
4777 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4778 }
4779
4780 rtl_enable_clock_request(pdev);
4781}
4782
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004783static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00004784{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004785 void __iomem *ioaddr = tp->mmio_addr;
4786 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08004787 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004788 { 0x00, 0x0200, 0x0100 },
4789 { 0x00, 0x0000, 0x0004 },
4790 { 0x06, 0x0002, 0x0001 },
4791 { 0x06, 0x0000, 0x0030 },
4792 { 0x07, 0x0000, 0x2000 },
4793 { 0x00, 0x0000, 0x0020 },
4794 { 0x03, 0x5800, 0x2000 },
4795 { 0x03, 0x0000, 0x0001 },
4796 { 0x01, 0x0800, 0x1000 },
4797 { 0x07, 0x0000, 0x4000 },
4798 { 0x1e, 0x0000, 0x2000 },
4799 { 0x19, 0xffff, 0xfe6c },
4800 { 0x0a, 0x0000, 0x0040 }
4801 };
4802
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004803 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004804
Hayes Wang70090422011-07-06 15:58:06 +08004805 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004806
4807 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4808
4809 RTL_W8(MaxTxPacketSize, TxPacketMax);
4810
4811 rtl_disable_clock_request(pdev);
4812
4813 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004814 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4815 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004816
Francois Romieucecb5fd2011-04-01 10:21:07 +02004817 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004818}
4819
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004820static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08004821{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004822 void __iomem *ioaddr = tp->mmio_addr;
4823 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08004824 static const struct ephy_info e_info_8168e_2[] = {
4825 { 0x09, 0x0000, 0x0080 },
4826 { 0x19, 0x0000, 0x0224 }
4827 };
4828
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004829 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08004830
4831 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4832
4833 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4834
4835 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4836 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4837 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4838 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4839 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4840 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4841 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4842 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4843 ERIAR_EXGMAC);
4844
Hayes Wang3090bd92011-09-06 16:55:15 +08004845 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004846
4847 rtl_disable_clock_request(pdev);
4848
4849 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4850 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4851
4852 /* Adjust EEE LED frequency */
4853 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4854
4855 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4856 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4857 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4858}
4859
Hayes Wang5f886e02012-03-30 14:33:03 +08004860static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08004861{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004862 void __iomem *ioaddr = tp->mmio_addr;
4863 struct pci_dev *pdev = tp->pci_dev;
Hayes Wangc2218922011-09-06 16:55:18 +08004864
Hayes Wang5f886e02012-03-30 14:33:03 +08004865 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08004866
4867 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4868
4869 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4870 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4871 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4872 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4873 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4874 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4875 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4876 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4877 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4878 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08004879
4880 RTL_W8(MaxTxPacketSize, EarlySize);
4881
4882 rtl_disable_clock_request(pdev);
4883
4884 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4885 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
Hayes Wangc2218922011-09-06 16:55:18 +08004886 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4887 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4888 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4889}
4890
Hayes Wang5f886e02012-03-30 14:33:03 +08004891static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
4892{
4893 void __iomem *ioaddr = tp->mmio_addr;
4894 static const struct ephy_info e_info_8168f_1[] = {
4895 { 0x06, 0x00c0, 0x0020 },
4896 { 0x08, 0x0001, 0x0002 },
4897 { 0x09, 0x0000, 0x0080 },
4898 { 0x19, 0x0000, 0x0224 }
4899 };
4900
4901 rtl_hw_start_8168f(tp);
4902
4903 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4904
4905 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4906 ERIAR_EXGMAC);
4907
4908 /* Adjust EEE LED frequency */
4909 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4910}
4911
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004912static void rtl_hw_start_8411(struct rtl8169_private *tp)
4913{
4914 void __iomem *ioaddr = tp->mmio_addr;
4915 static const struct ephy_info e_info_8168f_1[] = {
4916 { 0x06, 0x00c0, 0x0020 },
4917 { 0x0f, 0xffff, 0x5200 },
4918 { 0x1e, 0x0000, 0x4000 },
4919 { 0x19, 0x0000, 0x0224 }
4920 };
4921
4922 rtl_hw_start_8168f(tp);
4923
4924 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4925
4926 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000,
4927 ERIAR_EXGMAC);
4928}
4929
Francois Romieu07ce4062007-02-23 23:36:39 +01004930static void rtl_hw_start_8168(struct net_device *dev)
4931{
Francois Romieu2dd99532007-06-11 23:22:52 +02004932 struct rtl8169_private *tp = netdev_priv(dev);
4933 void __iomem *ioaddr = tp->mmio_addr;
4934
4935 RTL_W8(Cfg9346, Cfg9346_Unlock);
4936
françois romieuf0298f82011-01-03 15:07:42 +00004937 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004938
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004939 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004940
Francois Romieu0e485152007-02-20 00:00:26 +01004941 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004942
4943 RTL_W16(CPlusCmd, tp->cp_cmd);
4944
Francois Romieu0e485152007-02-20 00:00:26 +01004945 RTL_W16(IntrMitigate, 0x5151);
4946
4947 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00004948 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01004949 tp->event_slow |= RxFIFOOver | PCSTimeout;
4950 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01004951 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004952
4953 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4954
Francois Romieub8363902008-06-01 12:31:57 +02004955 rtl_set_rx_mode(dev);
4956
4957 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4958 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004959
4960 RTL_R8(IntrMask);
4961
Francois Romieu219a1e92008-06-28 11:58:39 +02004962 switch (tp->mac_version) {
4963 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004964 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004965 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004966
4967 case RTL_GIGA_MAC_VER_12:
4968 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004969 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004970 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004971
4972 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004973 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004974 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004975
4976 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004977 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004978 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004979
4980 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004981 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004982 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004983
Francois Romieu197ff762008-06-28 13:16:02 +02004984 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004985 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004986 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004987
Francois Romieu6fb07052008-06-29 11:54:28 +02004988 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004989 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004990 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004991
Francois Romieuef3386f2008-06-29 12:24:30 +02004992 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004993 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004994 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004995
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004996 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004997 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004998 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004999
Francois Romieu5b538df2008-07-20 16:22:45 +02005000 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00005001 case RTL_GIGA_MAC_VER_26:
5002 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005003 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005004 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02005005
françois romieue6de30d2011-01-03 15:08:37 +00005006 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005007 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005008 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02005009
hayeswang4804b3b2011-03-21 01:50:29 +00005010 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005011 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005012 break;
5013
hayeswang01dc7fe2011-03-21 01:50:28 +00005014 case RTL_GIGA_MAC_VER_32:
5015 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005016 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005017 break;
5018 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005019 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005020 break;
françois romieue6de30d2011-01-03 15:08:37 +00005021
Hayes Wangc2218922011-09-06 16:55:18 +08005022 case RTL_GIGA_MAC_VER_35:
5023 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005024 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005025 break;
5026
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005027 case RTL_GIGA_MAC_VER_38:
5028 rtl_hw_start_8411(tp);
5029 break;
5030
Francois Romieu219a1e92008-06-28 11:58:39 +02005031 default:
5032 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
5033 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00005034 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005035 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005036
Francois Romieu0e485152007-02-20 00:00:26 +01005037 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5038
Francois Romieub8363902008-06-01 12:31:57 +02005039 RTL_W8(Cfg9346, Cfg9346_Lock);
5040
Francois Romieu2dd99532007-06-11 23:22:52 +02005041 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005042}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043
Francois Romieu2857ffb2008-08-02 21:08:49 +02005044#define R810X_CPCMD_QUIRK_MASK (\
5045 EnableBist | \
5046 Mac_dbgo_oe | \
5047 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00005048 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02005049 Force_txflow_en | \
5050 Cxpl_dbg_sel | \
5051 ASF | \
5052 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005053 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005054
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005055static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005056{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005057 void __iomem *ioaddr = tp->mmio_addr;
5058 struct pci_dev *pdev = tp->pci_dev;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005059 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02005060 { 0x01, 0, 0x6e65 },
5061 { 0x02, 0, 0x091f },
5062 { 0x03, 0, 0xc2f9 },
5063 { 0x06, 0, 0xafb5 },
5064 { 0x07, 0, 0x0e00 },
5065 { 0x19, 0, 0xec80 },
5066 { 0x01, 0, 0x2e65 },
5067 { 0x01, 0, 0x6e65 }
5068 };
5069 u8 cfg1;
5070
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005071 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005072
5073 RTL_W8(DBG_REG, FIX_NAK_1);
5074
5075 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5076
5077 RTL_W8(Config1,
5078 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5079 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5080
5081 cfg1 = RTL_R8(Config1);
5082 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5083 RTL_W8(Config1, cfg1 & ~LEDS0);
5084
Francois Romieu2857ffb2008-08-02 21:08:49 +02005085 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
5086}
5087
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005088static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005089{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005090 void __iomem *ioaddr = tp->mmio_addr;
5091 struct pci_dev *pdev = tp->pci_dev;
5092
5093 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005094
5095 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5096
5097 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5098 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005099}
5100
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005101static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005102{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005103 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005104
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005105 rtl_ephy_write(tp->mmio_addr, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005106}
5107
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005108static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005109{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005110 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005111 static const struct ephy_info e_info_8105e_1[] = {
5112 { 0x07, 0, 0x4000 },
5113 { 0x19, 0, 0x0200 },
5114 { 0x19, 0, 0x0020 },
5115 { 0x1e, 0, 0x2000 },
5116 { 0x03, 0, 0x0001 },
5117 { 0x19, 0, 0x0100 },
5118 { 0x19, 0, 0x0004 },
5119 { 0x0a, 0, 0x0020 }
5120 };
5121
Francois Romieucecb5fd2011-04-01 10:21:07 +02005122 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005123 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5124
Francois Romieucecb5fd2011-04-01 10:21:07 +02005125 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005126 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5127
5128 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e2011-07-06 15:58:02 +08005129 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005130
5131 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
5132}
5133
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005134static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005135{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005136 void __iomem *ioaddr = tp->mmio_addr;
5137
5138 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005139 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
5140}
5141
Hayes Wang7e18dca2012-03-30 14:33:02 +08005142static void rtl_hw_start_8402(struct rtl8169_private *tp)
5143{
5144 void __iomem *ioaddr = tp->mmio_addr;
5145 static const struct ephy_info e_info_8402[] = {
5146 { 0x19, 0xffff, 0xff64 },
5147 { 0x1e, 0, 0x4000 }
5148 };
5149
5150 rtl_csi_access_enable_2(tp);
5151
5152 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5153 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5154
5155 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5156 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5157
5158 rtl_ephy_init(ioaddr, e_info_8402, ARRAY_SIZE(e_info_8402));
5159
5160 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5161
5162 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5163 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
5164 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5165 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5166 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5167 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5168 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00,
5169 ERIAR_EXGMAC);
5170}
5171
Hayes Wang5598bfe2012-07-02 17:23:21 +08005172static void rtl_hw_start_8106(struct rtl8169_private *tp)
5173{
5174 void __iomem *ioaddr = tp->mmio_addr;
5175
5176 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5177 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5178
5179 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5180 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
5181 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
5182}
5183
Francois Romieu07ce4062007-02-23 23:36:39 +01005184static void rtl_hw_start_8101(struct net_device *dev)
5185{
Francois Romieucdf1a602007-06-11 23:29:50 +02005186 struct rtl8169_private *tp = netdev_priv(dev);
5187 void __iomem *ioaddr = tp->mmio_addr;
5188 struct pci_dev *pdev = tp->pci_dev;
5189
Francois Romieuda78dbf2012-01-26 14:18:23 +01005190 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5191 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00005192
Francois Romieucecb5fd2011-04-01 10:21:07 +02005193 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5194 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00005195 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02005196
5197 if (cap) {
5198 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
5199 PCI_EXP_DEVCTL_NOSNOOP_EN);
5200 }
Francois Romieucdf1a602007-06-11 23:29:50 +02005201 }
5202
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005203 RTL_W8(Cfg9346, Cfg9346_Unlock);
5204
Francois Romieu2857ffb2008-08-02 21:08:49 +02005205 switch (tp->mac_version) {
5206 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005207 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005208 break;
5209
5210 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005211 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005212 break;
5213
5214 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005215 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005216 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005217
5218 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005219 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005220 break;
5221 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005222 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005223 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005224
5225 case RTL_GIGA_MAC_VER_37:
5226 rtl_hw_start_8402(tp);
5227 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08005228
5229 case RTL_GIGA_MAC_VER_39:
5230 rtl_hw_start_8106(tp);
5231 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02005232 }
5233
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005234 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02005235
françois romieuf0298f82011-01-03 15:07:42 +00005236 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02005237
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005238 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02005239
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005240 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02005241 RTL_W16(CPlusCmd, tp->cp_cmd);
5242
5243 RTL_W16(IntrMitigate, 0x0000);
5244
5245 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5246
5247 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5248 rtl_set_rx_tx_config_registers(tp);
5249
Francois Romieucdf1a602007-06-11 23:29:50 +02005250 RTL_R8(IntrMask);
5251
Francois Romieucdf1a602007-06-11 23:29:50 +02005252 rtl_set_rx_mode(dev);
5253
5254 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255}
5256
5257static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5258{
Francois Romieud58d46b2011-05-03 16:38:29 +02005259 struct rtl8169_private *tp = netdev_priv(dev);
5260
5261 if (new_mtu < ETH_ZLEN ||
5262 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 return -EINVAL;
5264
Francois Romieud58d46b2011-05-03 16:38:29 +02005265 if (new_mtu > ETH_DATA_LEN)
5266 rtl_hw_jumbo_enable(tp);
5267 else
5268 rtl_hw_jumbo_disable(tp);
5269
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005271 netdev_update_features(dev);
5272
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005273 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274}
5275
5276static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5277{
Al Viro95e09182007-12-22 18:55:39 +00005278 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5280}
5281
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005282static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5283 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005285 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005286 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005287
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005288 kfree(*data_buff);
5289 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 rtl8169_make_unusable_by_asic(desc);
5291}
5292
5293static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5294{
5295 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5296
5297 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5298}
5299
5300static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5301 u32 rx_buf_sz)
5302{
5303 desc->addr = cpu_to_le64(mapping);
5304 wmb();
5305 rtl8169_mark_to_asic(desc, rx_buf_sz);
5306}
5307
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005308static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005310 return (void *)ALIGN((long)data, 16);
5311}
5312
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005313static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5314 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005315{
5316 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005318 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005319 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005320 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005322 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5323 if (!data)
5324 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005325
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005326 if (rtl8169_align(data) != data) {
5327 kfree(data);
5328 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5329 if (!data)
5330 return NULL;
5331 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005332
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005333 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005334 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005335 if (unlikely(dma_mapping_error(d, mapping))) {
5336 if (net_ratelimit())
5337 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005338 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340
5341 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005342 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005343
5344err_out:
5345 kfree(data);
5346 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347}
5348
5349static void rtl8169_rx_clear(struct rtl8169_private *tp)
5350{
Francois Romieu07d3f512007-02-21 22:40:46 +01005351 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352
5353 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005354 if (tp->Rx_databuff[i]) {
5355 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 tp->RxDescArray + i);
5357 }
5358 }
5359}
5360
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005361static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005363 desc->opts1 |= cpu_to_le32(RingEnd);
5364}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005365
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005366static int rtl8169_rx_fill(struct rtl8169_private *tp)
5367{
5368 unsigned int i;
5369
5370 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005371 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005372
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005373 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005375
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005376 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005377 if (!data) {
5378 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005379 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005380 }
5381 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005384 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5385 return 0;
5386
5387err_out:
5388 rtl8169_rx_clear(tp);
5389 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390}
5391
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392static int rtl8169_init_ring(struct net_device *dev)
5393{
5394 struct rtl8169_private *tp = netdev_priv(dev);
5395
5396 rtl8169_init_ring_indexes(tp);
5397
5398 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005399 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005401 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402}
5403
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005404static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 struct TxDesc *desc)
5406{
5407 unsigned int len = tx_skb->len;
5408
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005409 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5410
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 desc->opts1 = 0x00;
5412 desc->opts2 = 0x00;
5413 desc->addr = 0x00;
5414 tx_skb->len = 0;
5415}
5416
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005417static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5418 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419{
5420 unsigned int i;
5421
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005422 for (i = 0; i < n; i++) {
5423 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 struct ring_info *tx_skb = tp->tx_skb + entry;
5425 unsigned int len = tx_skb->len;
5426
5427 if (len) {
5428 struct sk_buff *skb = tx_skb->skb;
5429
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005430 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 tp->TxDescArray + entry);
5432 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005433 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 dev_kfree_skb(skb);
5435 tx_skb->skb = NULL;
5436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 }
5438 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005439}
5440
5441static void rtl8169_tx_clear(struct rtl8169_private *tp)
5442{
5443 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 tp->cur_tx = tp->dirty_tx = 0;
Igor Maravic036dafa2012-03-05 00:01:25 +01005445 netdev_reset_queue(tp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446}
5447
Francois Romieu4422bcd2012-01-26 11:23:32 +01005448static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449{
David Howellsc4028952006-11-22 14:57:56 +00005450 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005451 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452
Francois Romieuda78dbf2012-01-26 14:18:23 +01005453 napi_disable(&tp->napi);
5454 netif_stop_queue(dev);
5455 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456
françois romieuc7c2c392011-12-04 20:30:52 +00005457 rtl8169_hw_reset(tp);
5458
Francois Romieu56de4142011-03-15 17:29:31 +01005459 for (i = 0; i < NUM_RX_DESC; i++)
5460 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5461
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005463 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464
Francois Romieuda78dbf2012-01-26 14:18:23 +01005465 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01005466 rtl_hw_start(dev);
5467 netif_wake_queue(dev);
5468 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469}
5470
5471static void rtl8169_tx_timeout(struct net_device *dev)
5472{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005473 struct rtl8169_private *tp = netdev_priv(dev);
5474
5475 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476}
5477
5478static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005479 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480{
5481 struct skb_shared_info *info = skb_shinfo(skb);
5482 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005483 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005484 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485
5486 entry = tp->cur_tx;
5487 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005488 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 dma_addr_t mapping;
5490 u32 status, len;
5491 void *addr;
5492
5493 entry = (entry + 1) % NUM_TX_DESC;
5494
5495 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005496 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005497 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005498 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005499 if (unlikely(dma_mapping_error(d, mapping))) {
5500 if (net_ratelimit())
5501 netif_err(tp, drv, tp->dev,
5502 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005503 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505
Francois Romieucecb5fd2011-04-01 10:21:07 +02005506 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005507 status = opts[0] | len |
5508 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509
5510 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005511 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 txd->addr = cpu_to_le64(mapping);
5513
5514 tp->tx_skb[entry].len = len;
5515 }
5516
5517 if (cur_frag) {
5518 tp->tx_skb[entry].skb = skb;
5519 txd->opts1 |= cpu_to_le32(LastFrag);
5520 }
5521
5522 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005523
5524err_out:
5525 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5526 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527}
5528
Francois Romieu2b7b4312011-04-18 22:53:24 -07005529static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5530 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005532 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005533 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005534 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535
Francois Romieu2b7b4312011-04-18 22:53:24 -07005536 if (mss) {
5537 opts[0] |= TD_LSO;
5538 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5539 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005540 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541
5542 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005543 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005545 opts[offset] |= info->checksum.udp;
5546 else
5547 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549}
5550
Stephen Hemminger613573252009-08-31 19:50:58 +00005551static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5552 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553{
5554 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005555 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 struct TxDesc *txd = tp->TxDescArray + entry;
5557 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005558 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 dma_addr_t mapping;
5560 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005561 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005562 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005563
Julien Ducourthial477206a2012-05-09 00:00:06 +02005564 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005565 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005566 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 }
5568
5569 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005570 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005572 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005573 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005574 if (unlikely(dma_mapping_error(d, mapping))) {
5575 if (net_ratelimit())
5576 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005577 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579
5580 tp->tx_skb[entry].len = len;
5581 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582
Francois Romieu2b7b4312011-04-18 22:53:24 -07005583 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5584 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005585
Francois Romieu2b7b4312011-04-18 22:53:24 -07005586 rtl8169_tso_csum(tp, skb, opts);
5587
5588 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005589 if (frags < 0)
5590 goto err_dma_1;
5591 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005592 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005593 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005594 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005595 tp->tx_skb[entry].skb = skb;
5596 }
5597
Francois Romieu2b7b4312011-04-18 22:53:24 -07005598 txd->opts2 = cpu_to_le32(opts[1]);
5599
Igor Maravic036dafa2012-03-05 00:01:25 +01005600 netdev_sent_queue(dev, skb->len);
5601
Richard Cochran5047fb52012-03-10 07:29:42 +00005602 skb_tx_timestamp(skb);
5603
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 wmb();
5605
Francois Romieucecb5fd2011-04-01 10:21:07 +02005606 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005607 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 txd->opts1 = cpu_to_le32(status);
5609
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 tp->cur_tx += frags + 1;
5611
David Dillow4c020a92010-03-03 16:33:10 +00005612 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613
Francois Romieucecb5fd2011-04-01 10:21:07 +02005614 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615
Francois Romieuda78dbf2012-01-26 14:18:23 +01005616 mmiowb();
5617
Julien Ducourthial477206a2012-05-09 00:00:06 +02005618 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01005619 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5620 * not miss a ring update when it notices a stopped queue.
5621 */
5622 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01005624 /* Sync with rtl_tx:
5625 * - publish queue status and cur_tx ring index (write barrier)
5626 * - refresh dirty_tx ring index (read barrier).
5627 * May the current thread have a pessimistic view of the ring
5628 * status and forget to wake up queue, a racing rtl_tx thread
5629 * can't.
5630 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005631 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02005632 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 netif_wake_queue(dev);
5634 }
5635
Stephen Hemminger613573252009-08-31 19:50:58 +00005636 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005638err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005639 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005640err_dma_0:
5641 dev_kfree_skb(skb);
5642 dev->stats.tx_dropped++;
5643 return NETDEV_TX_OK;
5644
5645err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005647 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005648 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649}
5650
5651static void rtl8169_pcierr_interrupt(struct net_device *dev)
5652{
5653 struct rtl8169_private *tp = netdev_priv(dev);
5654 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 u16 pci_status, pci_cmd;
5656
5657 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5658 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5659
Joe Perchesbf82c182010-02-09 11:49:50 +00005660 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5661 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662
5663 /*
5664 * The recovery sequence below admits a very elaborated explanation:
5665 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005666 * - I did not see what else could be done;
5667 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 *
5669 * Feel free to adjust to your needs.
5670 */
Francois Romieua27993f2006-12-18 00:04:19 +01005671 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005672 pci_cmd &= ~PCI_COMMAND_PARITY;
5673 else
5674 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5675
5676 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677
5678 pci_write_config_word(pdev, PCI_STATUS,
5679 pci_status & (PCI_STATUS_DETECTED_PARITY |
5680 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5681 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5682
5683 /* The infamous DAC f*ckup only happens at boot time */
5684 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005685 void __iomem *ioaddr = tp->mmio_addr;
5686
Joe Perchesbf82c182010-02-09 11:49:50 +00005687 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 tp->cp_cmd &= ~PCIDAC;
5689 RTL_W16(CPlusCmd, tp->cp_cmd);
5690 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 }
5692
françois romieue6de30d2011-01-03 15:08:37 +00005693 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005694
Francois Romieu98ddf982012-01-31 10:47:34 +01005695 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696}
5697
Igor Maravic036dafa2012-03-05 00:01:25 +01005698struct rtl_txc {
5699 int packets;
5700 int bytes;
5701};
5702
Francois Romieuda78dbf2012-01-26 14:18:23 +01005703static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704{
Igor Maravic036dafa2012-03-05 00:01:25 +01005705 struct rtl8169_stats *tx_stats = &tp->tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 unsigned int dirty_tx, tx_left;
Igor Maravic036dafa2012-03-05 00:01:25 +01005707 struct rtl_txc txc = { 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 dirty_tx = tp->dirty_tx;
5710 smp_rmb();
5711 tx_left = tp->cur_tx - dirty_tx;
5712
5713 while (tx_left > 0) {
5714 unsigned int entry = dirty_tx % NUM_TX_DESC;
5715 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 u32 status;
5717
5718 rmb();
5719 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5720 if (status & DescOwn)
5721 break;
5722
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005723 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5724 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 if (status & LastFrag) {
Igor Maravic036dafa2012-03-05 00:01:25 +01005726 struct sk_buff *skb = tx_skb->skb;
5727
5728 txc.packets++;
5729 txc.bytes += skb->len;
5730 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 tx_skb->skb = NULL;
5732 }
5733 dirty_tx++;
5734 tx_left--;
5735 }
5736
Igor Maravic036dafa2012-03-05 00:01:25 +01005737 u64_stats_update_begin(&tx_stats->syncp);
5738 tx_stats->packets += txc.packets;
5739 tx_stats->bytes += txc.bytes;
5740 u64_stats_update_end(&tx_stats->syncp);
5741
5742 netdev_completed_queue(dev, txc.packets, txc.bytes);
5743
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 if (tp->dirty_tx != dirty_tx) {
5745 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005746 /* Sync with rtl8169_start_xmit:
5747 * - publish dirty_tx ring index (write barrier)
5748 * - refresh cur_tx ring index and queue status (read barrier)
5749 * May the current thread miss the stopped queue condition,
5750 * a racing xmit thread can only have a right view of the
5751 * ring status.
5752 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005753 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02005755 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 netif_wake_queue(dev);
5757 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005758 /*
5759 * 8168 hack: TxPoll requests are lost when the Tx packets are
5760 * too close. Let's kick an extra TxPoll request when a burst
5761 * of start_xmit activity is detected (if it is not detected,
5762 * it is slow enough). -- FR
5763 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005764 if (tp->cur_tx != dirty_tx) {
5765 void __iomem *ioaddr = tp->mmio_addr;
5766
Francois Romieud78ae2d2007-08-26 20:08:19 +02005767 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 }
5770}
5771
Francois Romieu126fa4b2005-05-12 20:09:17 -04005772static inline int rtl8169_fragmented_frame(u32 status)
5773{
5774 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5775}
5776
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005777static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 u32 status = opts1 & RxProtoMask;
5780
5781 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005782 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 skb->ip_summed = CHECKSUM_UNNECESSARY;
5784 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005785 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786}
5787
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005788static struct sk_buff *rtl8169_try_rx_copy(void *data,
5789 struct rtl8169_private *tp,
5790 int pkt_size,
5791 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005793 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005794 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005796 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005797 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005798 prefetch(data);
5799 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5800 if (skb)
5801 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005802 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5803
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005804 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805}
5806
Francois Romieuda78dbf2012-01-26 14:18:23 +01005807static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808{
5809 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005810 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 cur_rx = tp->cur_rx;
5813 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005814 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005816 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005818 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 u32 status;
5820
5821 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005822 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823
5824 if (status & DescOwn)
5825 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005826 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005827 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5828 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005829 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005831 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005833 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005834 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005835 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005836 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005837 }
Ben Greear6bbe0212012-02-10 15:04:33 +00005838 if ((status & (RxRUNT | RxCRC)) &&
5839 !(status & (RxRWT | RxFOVF)) &&
5840 (dev->features & NETIF_F_RXALL))
5841 goto process_pkt;
5842
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005843 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005845 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00005846 dma_addr_t addr;
5847 int pkt_size;
5848
5849process_pkt:
5850 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00005851 if (likely(!(dev->features & NETIF_F_RXFCS)))
5852 pkt_size = (status & 0x00003fff) - 4;
5853 else
5854 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855
Francois Romieu126fa4b2005-05-12 20:09:17 -04005856 /*
5857 * The driver does not support incoming fragmented
5858 * frames. They are seen as a symptom of over-mtu
5859 * sized frames.
5860 */
5861 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005862 dev->stats.rx_dropped++;
5863 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005864 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005865 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005866 }
5867
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005868 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5869 tp, pkt_size, addr);
5870 rtl8169_mark_to_asic(desc, rx_buf_sz);
5871 if (!skb) {
5872 dev->stats.rx_dropped++;
5873 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 }
5875
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005876 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 skb_put(skb, pkt_size);
5878 skb->protocol = eth_type_trans(skb, dev);
5879
Francois Romieu7a8fc772011-03-01 17:18:33 +01005880 rtl8169_rx_vlan_tag(desc, skb);
5881
Francois Romieu56de4142011-03-15 17:29:31 +01005882 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883
Junchang Wang8027aa22012-03-04 23:30:32 +01005884 u64_stats_update_begin(&tp->rx_stats.syncp);
5885 tp->rx_stats.packets++;
5886 tp->rx_stats.bytes += pkt_size;
5887 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005889
5890 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005891 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005892 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5893 desc->opts2 = 0;
5894 cur_rx++;
5895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 }
5897
5898 count = cur_rx - tp->cur_rx;
5899 tp->cur_rx = cur_rx;
5900
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005901 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902
5903 return count;
5904}
5905
Francois Romieu07d3f512007-02-21 22:40:46 +01005906static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907{
Francois Romieu07d3f512007-02-21 22:40:46 +01005908 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005911 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005913 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005914 if (status && status != 0xffff) {
5915 status &= RTL_EVENT_NAPI | tp->event_slow;
5916 if (status) {
5917 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00005918
Francois Romieuda78dbf2012-01-26 14:18:23 +01005919 rtl_irq_disable(tp);
5920 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 return IRQ_RETVAL(handled);
5924}
5925
Francois Romieuda78dbf2012-01-26 14:18:23 +01005926/*
5927 * Workqueue context.
5928 */
5929static void rtl_slow_event_work(struct rtl8169_private *tp)
5930{
5931 struct net_device *dev = tp->dev;
5932 u16 status;
5933
5934 status = rtl_get_events(tp) & tp->event_slow;
5935 rtl_ack_events(tp, status);
5936
5937 if (unlikely(status & RxFIFOOver)) {
5938 switch (tp->mac_version) {
5939 /* Work around for rx fifo overflow */
5940 case RTL_GIGA_MAC_VER_11:
5941 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01005942 /* XXX - Hack alert. See rtl_task(). */
5943 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005944 default:
5945 break;
5946 }
5947 }
5948
5949 if (unlikely(status & SYSErr))
5950 rtl8169_pcierr_interrupt(dev);
5951
5952 if (status & LinkChg)
5953 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
5954
françois romieu7dbb4912012-06-09 10:53:16 +00005955 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005956}
5957
Francois Romieu4422bcd2012-01-26 11:23:32 +01005958static void rtl_task(struct work_struct *work)
5959{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005960 static const struct {
5961 int bitnr;
5962 void (*action)(struct rtl8169_private *);
5963 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01005964 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005965 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
5966 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
5967 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
5968 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01005969 struct rtl8169_private *tp =
5970 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005971 struct net_device *dev = tp->dev;
5972 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01005973
Francois Romieuda78dbf2012-01-26 14:18:23 +01005974 rtl_lock_work(tp);
5975
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005976 if (!netif_running(dev) ||
5977 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01005978 goto out_unlock;
5979
5980 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
5981 bool pending;
5982
Francois Romieuda78dbf2012-01-26 14:18:23 +01005983 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005984 if (pending)
5985 rtl_work[i].action(tp);
5986 }
5987
5988out_unlock:
5989 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01005990}
5991
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005992static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005994 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5995 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005996 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
5997 int work_done= 0;
5998 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999
Francois Romieuda78dbf2012-01-26 14:18:23 +01006000 status = rtl_get_events(tp);
6001 rtl_ack_events(tp, status & ~tp->event_slow);
6002
6003 if (status & RTL_EVENT_NAPI_RX)
6004 work_done = rtl_rx(dev, tp, (u32) budget);
6005
6006 if (status & RTL_EVENT_NAPI_TX)
6007 rtl_tx(dev, tp);
6008
6009 if (status & tp->event_slow) {
6010 enable_mask &= ~tp->event_slow;
6011
6012 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
6013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006015 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006016 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00006017
Francois Romieuda78dbf2012-01-26 14:18:23 +01006018 rtl_irq_enable(tp, enable_mask);
6019 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020 }
6021
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006022 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024
Francois Romieu523a6092008-09-10 22:28:56 +02006025static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
6026{
6027 struct rtl8169_private *tp = netdev_priv(dev);
6028
6029 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6030 return;
6031
6032 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
6033 RTL_W32(RxMissed, 0);
6034}
6035
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036static void rtl8169_down(struct net_device *dev)
6037{
6038 struct rtl8169_private *tp = netdev_priv(dev);
6039 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040
Francois Romieu4876cc12011-03-11 21:07:11 +01006041 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01006043 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006044 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045
Hayes Wang92fc43b2011-07-06 15:58:03 +08006046 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006047 /*
6048 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01006049 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6050 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006051 */
Francois Romieu523a6092008-09-10 22:28:56 +02006052 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006055 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 rtl8169_tx_clear(tp);
6058
6059 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00006060
6061 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062}
6063
6064static int rtl8169_close(struct net_device *dev)
6065{
6066 struct rtl8169_private *tp = netdev_priv(dev);
6067 struct pci_dev *pdev = tp->pci_dev;
6068
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006069 pm_runtime_get_sync(&pdev->dev);
6070
Francois Romieucecb5fd2011-04-01 10:21:07 +02006071 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08006072 rtl8169_update_counters(dev);
6073
Francois Romieuda78dbf2012-01-26 14:18:23 +01006074 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006075 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006076
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006078 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006080 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00006082 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6083 tp->RxPhyAddr);
6084 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6085 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 tp->TxDescArray = NULL;
6087 tp->RxDescArray = NULL;
6088
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006089 pm_runtime_put_sync(&pdev->dev);
6090
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 return 0;
6092}
6093
Francois Romieudc1c00c2012-03-08 10:06:18 +01006094#ifdef CONFIG_NET_POLL_CONTROLLER
6095static void rtl8169_netpoll(struct net_device *dev)
6096{
6097 struct rtl8169_private *tp = netdev_priv(dev);
6098
6099 rtl8169_interrupt(tp->pci_dev->irq, dev);
6100}
6101#endif
6102
Francois Romieudf43ac72012-03-08 09:48:40 +01006103static int rtl_open(struct net_device *dev)
6104{
6105 struct rtl8169_private *tp = netdev_priv(dev);
6106 void __iomem *ioaddr = tp->mmio_addr;
6107 struct pci_dev *pdev = tp->pci_dev;
6108 int retval = -ENOMEM;
6109
6110 pm_runtime_get_sync(&pdev->dev);
6111
6112 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02006113 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01006114 * dma_alloc_coherent provides more.
6115 */
6116 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6117 &tp->TxPhyAddr, GFP_KERNEL);
6118 if (!tp->TxDescArray)
6119 goto err_pm_runtime_put;
6120
6121 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6122 &tp->RxPhyAddr, GFP_KERNEL);
6123 if (!tp->RxDescArray)
6124 goto err_free_tx_0;
6125
6126 retval = rtl8169_init_ring(dev);
6127 if (retval < 0)
6128 goto err_free_rx_1;
6129
6130 INIT_WORK(&tp->wk.work, rtl_task);
6131
6132 smp_mb();
6133
6134 rtl_request_firmware(tp);
6135
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006136 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01006137 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
6138 dev->name, dev);
6139 if (retval < 0)
6140 goto err_release_fw_2;
6141
6142 rtl_lock_work(tp);
6143
6144 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6145
6146 napi_enable(&tp->napi);
6147
6148 rtl8169_init_phy(dev, tp);
6149
6150 __rtl8169_set_features(dev, dev->features);
6151
6152 rtl_pll_power_up(tp);
6153
6154 rtl_hw_start(dev);
6155
6156 netif_start_queue(dev);
6157
6158 rtl_unlock_work(tp);
6159
6160 tp->saved_wolopts = 0;
6161 pm_runtime_put_noidle(&pdev->dev);
6162
6163 rtl8169_check_link_status(dev, tp, ioaddr);
6164out:
6165 return retval;
6166
6167err_release_fw_2:
6168 rtl_release_firmware(tp);
6169 rtl8169_rx_clear(tp);
6170err_free_rx_1:
6171 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6172 tp->RxPhyAddr);
6173 tp->RxDescArray = NULL;
6174err_free_tx_0:
6175 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6176 tp->TxPhyAddr);
6177 tp->TxDescArray = NULL;
6178err_pm_runtime_put:
6179 pm_runtime_put_noidle(&pdev->dev);
6180 goto out;
6181}
6182
Junchang Wang8027aa22012-03-04 23:30:32 +01006183static struct rtnl_link_stats64 *
6184rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185{
6186 struct rtl8169_private *tp = netdev_priv(dev);
6187 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01006188 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189
Francois Romieuda78dbf2012-01-26 14:18:23 +01006190 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02006191 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02006192
Junchang Wang8027aa22012-03-04 23:30:32 +01006193 do {
6194 start = u64_stats_fetch_begin_bh(&tp->rx_stats.syncp);
6195 stats->rx_packets = tp->rx_stats.packets;
6196 stats->rx_bytes = tp->rx_stats.bytes;
6197 } while (u64_stats_fetch_retry_bh(&tp->rx_stats.syncp, start));
6198
6199
6200 do {
6201 start = u64_stats_fetch_begin_bh(&tp->tx_stats.syncp);
6202 stats->tx_packets = tp->tx_stats.packets;
6203 stats->tx_bytes = tp->tx_stats.bytes;
6204 } while (u64_stats_fetch_retry_bh(&tp->tx_stats.syncp, start));
6205
6206 stats->rx_dropped = dev->stats.rx_dropped;
6207 stats->tx_dropped = dev->stats.tx_dropped;
6208 stats->rx_length_errors = dev->stats.rx_length_errors;
6209 stats->rx_errors = dev->stats.rx_errors;
6210 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6211 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6212 stats->rx_missed_errors = dev->stats.rx_missed_errors;
6213
6214 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215}
6216
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006217static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006218{
françois romieu065c27c2011-01-03 15:08:12 +00006219 struct rtl8169_private *tp = netdev_priv(dev);
6220
Francois Romieu5d06a992006-02-23 00:47:58 +01006221 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006222 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006223
6224 netif_device_detach(dev);
6225 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006226
6227 rtl_lock_work(tp);
6228 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006229 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006230 rtl_unlock_work(tp);
6231
6232 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006233}
Francois Romieu5d06a992006-02-23 00:47:58 +01006234
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006235#ifdef CONFIG_PM
6236
6237static int rtl8169_suspend(struct device *device)
6238{
6239 struct pci_dev *pdev = to_pci_dev(device);
6240 struct net_device *dev = pci_get_drvdata(pdev);
6241
6242 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02006243
Francois Romieu5d06a992006-02-23 00:47:58 +01006244 return 0;
6245}
6246
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006247static void __rtl8169_resume(struct net_device *dev)
6248{
françois romieu065c27c2011-01-03 15:08:12 +00006249 struct rtl8169_private *tp = netdev_priv(dev);
6250
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006251 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006252
6253 rtl_pll_power_up(tp);
6254
Artem Savkovcff4c162012-04-03 10:29:11 +00006255 rtl_lock_work(tp);
6256 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006257 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00006258 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006259
Francois Romieu98ddf982012-01-31 10:47:34 +01006260 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006261}
6262
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006263static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006264{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006265 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01006266 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006267 struct rtl8169_private *tp = netdev_priv(dev);
6268
6269 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01006270
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006271 if (netif_running(dev))
6272 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006273
Francois Romieu5d06a992006-02-23 00:47:58 +01006274 return 0;
6275}
6276
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006277static int rtl8169_runtime_suspend(struct device *device)
6278{
6279 struct pci_dev *pdev = to_pci_dev(device);
6280 struct net_device *dev = pci_get_drvdata(pdev);
6281 struct rtl8169_private *tp = netdev_priv(dev);
6282
6283 if (!tp->TxDescArray)
6284 return 0;
6285
Francois Romieuda78dbf2012-01-26 14:18:23 +01006286 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006287 tp->saved_wolopts = __rtl8169_get_wol(tp);
6288 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006289 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006290
6291 rtl8169_net_suspend(dev);
6292
6293 return 0;
6294}
6295
6296static int rtl8169_runtime_resume(struct device *device)
6297{
6298 struct pci_dev *pdev = to_pci_dev(device);
6299 struct net_device *dev = pci_get_drvdata(pdev);
6300 struct rtl8169_private *tp = netdev_priv(dev);
6301
6302 if (!tp->TxDescArray)
6303 return 0;
6304
Francois Romieuda78dbf2012-01-26 14:18:23 +01006305 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006306 __rtl8169_set_wol(tp, tp->saved_wolopts);
6307 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006308 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006309
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006310 rtl8169_init_phy(dev, tp);
6311
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006312 __rtl8169_resume(dev);
6313
6314 return 0;
6315}
6316
6317static int rtl8169_runtime_idle(struct device *device)
6318{
6319 struct pci_dev *pdev = to_pci_dev(device);
6320 struct net_device *dev = pci_get_drvdata(pdev);
6321 struct rtl8169_private *tp = netdev_priv(dev);
6322
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00006323 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006324}
6325
Alexey Dobriyan47145212009-12-14 18:00:08 -08006326static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006327 .suspend = rtl8169_suspend,
6328 .resume = rtl8169_resume,
6329 .freeze = rtl8169_suspend,
6330 .thaw = rtl8169_resume,
6331 .poweroff = rtl8169_suspend,
6332 .restore = rtl8169_resume,
6333 .runtime_suspend = rtl8169_runtime_suspend,
6334 .runtime_resume = rtl8169_runtime_resume,
6335 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006336};
6337
6338#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6339
6340#else /* !CONFIG_PM */
6341
6342#define RTL8169_PM_OPS NULL
6343
6344#endif /* !CONFIG_PM */
6345
David S. Miller1805b2f2011-10-24 18:18:09 -04006346static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6347{
6348 void __iomem *ioaddr = tp->mmio_addr;
6349
6350 /* WoL fails with 8168b when the receiver is disabled. */
6351 switch (tp->mac_version) {
6352 case RTL_GIGA_MAC_VER_11:
6353 case RTL_GIGA_MAC_VER_12:
6354 case RTL_GIGA_MAC_VER_17:
6355 pci_clear_master(tp->pci_dev);
6356
6357 RTL_W8(ChipCmd, CmdRxEnb);
6358 /* PCI commit */
6359 RTL_R8(ChipCmd);
6360 break;
6361 default:
6362 break;
6363 }
6364}
6365
Francois Romieu1765f952008-09-13 17:21:40 +02006366static void rtl_shutdown(struct pci_dev *pdev)
6367{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006368 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006369 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00006370 struct device *d = &pdev->dev;
6371
6372 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02006373
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006374 rtl8169_net_suspend(dev);
6375
Francois Romieucecb5fd2011-04-01 10:21:07 +02006376 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006377 rtl_rar_set(tp, dev->perm_addr);
6378
Hayes Wang92fc43b2011-07-06 15:58:03 +08006379 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006380
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006381 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006382 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6383 rtl_wol_suspend_quirk(tp);
6384 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006385 }
6386
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006387 pci_wake_from_d3(pdev, true);
6388 pci_set_power_state(pdev, PCI_D3hot);
6389 }
françois romieu2a15cd22012-03-06 01:14:12 +00006390
6391 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006392}
Francois Romieu5d06a992006-02-23 00:47:58 +01006393
Francois Romieue27566e2012-03-08 09:54:01 +01006394static void __devexit rtl_remove_one(struct pci_dev *pdev)
6395{
6396 struct net_device *dev = pci_get_drvdata(pdev);
6397 struct rtl8169_private *tp = netdev_priv(dev);
6398
6399 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6400 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6401 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6402 rtl8168_driver_stop(tp);
6403 }
6404
6405 cancel_work_sync(&tp->wk.work);
6406
Devendra Nagaad1be8d2012-05-31 01:51:20 +00006407 netif_napi_del(&tp->napi);
6408
Francois Romieue27566e2012-03-08 09:54:01 +01006409 unregister_netdev(dev);
6410
6411 rtl_release_firmware(tp);
6412
6413 if (pci_dev_run_wake(pdev))
6414 pm_runtime_get_noresume(&pdev->dev);
6415
6416 /* restore original MAC address */
6417 rtl_rar_set(tp, dev->perm_addr);
6418
6419 rtl_disable_msi(pdev, tp);
6420 rtl8169_release_board(pdev, dev, tp->mmio_addr);
6421 pci_set_drvdata(pdev, NULL);
6422}
6423
Francois Romieufa9c3852012-03-08 10:01:50 +01006424static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01006425 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01006426 .ndo_stop = rtl8169_close,
6427 .ndo_get_stats64 = rtl8169_get_stats64,
6428 .ndo_start_xmit = rtl8169_start_xmit,
6429 .ndo_tx_timeout = rtl8169_tx_timeout,
6430 .ndo_validate_addr = eth_validate_addr,
6431 .ndo_change_mtu = rtl8169_change_mtu,
6432 .ndo_fix_features = rtl8169_fix_features,
6433 .ndo_set_features = rtl8169_set_features,
6434 .ndo_set_mac_address = rtl_set_mac_address,
6435 .ndo_do_ioctl = rtl8169_ioctl,
6436 .ndo_set_rx_mode = rtl_set_rx_mode,
6437#ifdef CONFIG_NET_POLL_CONTROLLER
6438 .ndo_poll_controller = rtl8169_netpoll,
6439#endif
6440
6441};
6442
Francois Romieu31fa8b12012-03-08 10:09:40 +01006443static const struct rtl_cfg_info {
6444 void (*hw_start)(struct net_device *);
6445 unsigned int region;
6446 unsigned int align;
6447 u16 event_slow;
6448 unsigned features;
6449 u8 default_ver;
6450} rtl_cfg_infos [] = {
6451 [RTL_CFG_0] = {
6452 .hw_start = rtl_hw_start_8169,
6453 .region = 1,
6454 .align = 0,
6455 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
6456 .features = RTL_FEATURE_GMII,
6457 .default_ver = RTL_GIGA_MAC_VER_01,
6458 },
6459 [RTL_CFG_1] = {
6460 .hw_start = rtl_hw_start_8168,
6461 .region = 2,
6462 .align = 8,
6463 .event_slow = SYSErr | LinkChg | RxOverflow,
6464 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
6465 .default_ver = RTL_GIGA_MAC_VER_11,
6466 },
6467 [RTL_CFG_2] = {
6468 .hw_start = rtl_hw_start_8101,
6469 .region = 2,
6470 .align = 8,
6471 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
6472 PCSTimeout,
6473 .features = RTL_FEATURE_MSI,
6474 .default_ver = RTL_GIGA_MAC_VER_13,
6475 }
6476};
6477
6478/* Cfg9346_Unlock assumed. */
6479static unsigned rtl_try_msi(struct rtl8169_private *tp,
6480 const struct rtl_cfg_info *cfg)
6481{
6482 void __iomem *ioaddr = tp->mmio_addr;
6483 unsigned msi = 0;
6484 u8 cfg2;
6485
6486 cfg2 = RTL_R8(Config2) & ~MSIEnable;
6487 if (cfg->features & RTL_FEATURE_MSI) {
6488 if (pci_enable_msi(tp->pci_dev)) {
6489 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
6490 } else {
6491 cfg2 |= MSIEnable;
6492 msi = RTL_FEATURE_MSI;
6493 }
6494 }
6495 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6496 RTL_W8(Config2, cfg2);
6497 return msi;
6498}
6499
Francois Romieu3b6cf252012-03-08 09:59:04 +01006500static int __devinit
6501rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6502{
6503 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
6504 const unsigned int region = cfg->region;
6505 struct rtl8169_private *tp;
6506 struct mii_if_info *mii;
6507 struct net_device *dev;
6508 void __iomem *ioaddr;
6509 int chipset, i;
6510 int rc;
6511
6512 if (netif_msg_drv(&debug)) {
6513 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
6514 MODULENAME, RTL8169_VERSION);
6515 }
6516
6517 dev = alloc_etherdev(sizeof (*tp));
6518 if (!dev) {
6519 rc = -ENOMEM;
6520 goto out;
6521 }
6522
6523 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01006524 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006525 tp = netdev_priv(dev);
6526 tp->dev = dev;
6527 tp->pci_dev = pdev;
6528 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
6529
6530 mii = &tp->mii;
6531 mii->dev = dev;
6532 mii->mdio_read = rtl_mdio_read;
6533 mii->mdio_write = rtl_mdio_write;
6534 mii->phy_id_mask = 0x1f;
6535 mii->reg_num_mask = 0x1f;
6536 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
6537
6538 /* disable ASPM completely as that cause random device stop working
6539 * problems as well as full system hangs for some PCIe devices users */
6540 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6541 PCIE_LINK_STATE_CLKPM);
6542
6543 /* enable device (incl. PCI PM wakeup and hotplug setup) */
6544 rc = pci_enable_device(pdev);
6545 if (rc < 0) {
6546 netif_err(tp, probe, dev, "enable failure\n");
6547 goto err_out_free_dev_1;
6548 }
6549
6550 if (pci_set_mwi(pdev) < 0)
6551 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
6552
6553 /* make sure PCI base addr 1 is MMIO */
6554 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
6555 netif_err(tp, probe, dev,
6556 "region #%d not an MMIO resource, aborting\n",
6557 region);
6558 rc = -ENODEV;
6559 goto err_out_mwi_2;
6560 }
6561
6562 /* check for weird/broken PCI region reporting */
6563 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
6564 netif_err(tp, probe, dev,
6565 "Invalid PCI region size(s), aborting\n");
6566 rc = -ENODEV;
6567 goto err_out_mwi_2;
6568 }
6569
6570 rc = pci_request_regions(pdev, MODULENAME);
6571 if (rc < 0) {
6572 netif_err(tp, probe, dev, "could not request regions\n");
6573 goto err_out_mwi_2;
6574 }
6575
6576 tp->cp_cmd = RxChkSum;
6577
6578 if ((sizeof(dma_addr_t) > 4) &&
6579 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
6580 tp->cp_cmd |= PCIDAC;
6581 dev->features |= NETIF_F_HIGHDMA;
6582 } else {
6583 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6584 if (rc < 0) {
6585 netif_err(tp, probe, dev, "DMA configuration failed\n");
6586 goto err_out_free_res_3;
6587 }
6588 }
6589
6590 /* ioremap MMIO region */
6591 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
6592 if (!ioaddr) {
6593 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
6594 rc = -EIO;
6595 goto err_out_free_res_3;
6596 }
6597 tp->mmio_addr = ioaddr;
6598
6599 if (!pci_is_pcie(pdev))
6600 netif_info(tp, probe, dev, "not PCI Express\n");
6601
6602 /* Identify chip attached to board */
6603 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
6604
6605 rtl_init_rxcfg(tp);
6606
6607 rtl_irq_disable(tp);
6608
6609 rtl_hw_reset(tp);
6610
6611 rtl_ack_events(tp, 0xffff);
6612
6613 pci_set_master(pdev);
6614
6615 /*
6616 * Pretend we are using VLANs; This bypasses a nasty bug where
6617 * Interrupts stop flowing on high load on 8110SCd controllers.
6618 */
6619 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6620 tp->cp_cmd |= RxVlan;
6621
6622 rtl_init_mdio_ops(tp);
6623 rtl_init_pll_power_ops(tp);
6624 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006625 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006626
6627 rtl8169_print_mac_version(tp);
6628
6629 chipset = tp->mac_version;
6630 tp->txd_version = rtl_chip_infos[chipset].txd_version;
6631
6632 RTL_W8(Cfg9346, Cfg9346_Unlock);
6633 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
6634 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
6635 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
6636 tp->features |= RTL_FEATURE_WOL;
6637 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
6638 tp->features |= RTL_FEATURE_WOL;
6639 tp->features |= rtl_try_msi(tp, cfg);
6640 RTL_W8(Cfg9346, Cfg9346_Lock);
6641
6642 if (rtl_tbi_enabled(tp)) {
6643 tp->set_speed = rtl8169_set_speed_tbi;
6644 tp->get_settings = rtl8169_gset_tbi;
6645 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
6646 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
6647 tp->link_ok = rtl8169_tbi_link_ok;
6648 tp->do_ioctl = rtl_tbi_ioctl;
6649 } else {
6650 tp->set_speed = rtl8169_set_speed_xmii;
6651 tp->get_settings = rtl8169_gset_xmii;
6652 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
6653 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
6654 tp->link_ok = rtl8169_xmii_link_ok;
6655 tp->do_ioctl = rtl_xmii_ioctl;
6656 }
6657
6658 mutex_init(&tp->wk.mutex);
6659
6660 /* Get MAC address */
6661 for (i = 0; i < ETH_ALEN; i++)
6662 dev->dev_addr[i] = RTL_R8(MAC0 + i);
6663 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
6664
6665 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
6666 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006667
6668 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
6669
6670 /* don't enable SG, IP_CSUM and TSO by default - it might not work
6671 * properly for all devices */
6672 dev->features |= NETIF_F_RXCSUM |
6673 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6674
6675 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6676 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6677 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6678 NETIF_F_HIGHDMA;
6679
6680 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6681 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
6682 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
6683
6684 dev->hw_features |= NETIF_F_RXALL;
6685 dev->hw_features |= NETIF_F_RXFCS;
6686
6687 tp->hw_start = cfg->hw_start;
6688 tp->event_slow = cfg->event_slow;
6689
6690 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
6691 ~(RxBOVF | RxFOVF) : ~0;
6692
6693 init_timer(&tp->timer);
6694 tp->timer.data = (unsigned long) dev;
6695 tp->timer.function = rtl8169_phy_timer;
6696
6697 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
6698
6699 rc = register_netdev(dev);
6700 if (rc < 0)
6701 goto err_out_msi_4;
6702
6703 pci_set_drvdata(pdev, dev);
6704
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006705 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
6706 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
6707 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006708 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
6709 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
6710 "tx checksumming: %s]\n",
6711 rtl_chip_infos[chipset].jumbo_max,
6712 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
6713 }
6714
6715 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6716 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6717 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6718 rtl8168_driver_start(tp);
6719 }
6720
6721 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
6722
6723 if (pci_dev_run_wake(pdev))
6724 pm_runtime_put_noidle(&pdev->dev);
6725
6726 netif_carrier_off(dev);
6727
6728out:
6729 return rc;
6730
6731err_out_msi_4:
Devendra Nagaad1be8d2012-05-31 01:51:20 +00006732 netif_napi_del(&tp->napi);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006733 rtl_disable_msi(pdev, tp);
6734 iounmap(ioaddr);
6735err_out_free_res_3:
6736 pci_release_regions(pdev);
6737err_out_mwi_2:
6738 pci_clear_mwi(pdev);
6739 pci_disable_device(pdev);
6740err_out_free_dev_1:
6741 free_netdev(dev);
6742 goto out;
6743}
6744
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745static struct pci_driver rtl8169_pci_driver = {
6746 .name = MODULENAME,
6747 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01006748 .probe = rtl_init_one,
Francois Romieue27566e2012-03-08 09:54:01 +01006749 .remove = __devexit_p(rtl_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02006750 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006751 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752};
6753
Francois Romieu07d3f512007-02-21 22:40:46 +01006754static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755{
Jeff Garzik29917622006-08-19 17:48:59 -04006756 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757}
6758
Francois Romieu07d3f512007-02-21 22:40:46 +01006759static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760{
6761 pci_unregister_driver(&rtl8169_pci_driver);
6762}
6763
6764module_init(rtl8169_init_module);
6765module_exit(rtl8169_cleanup_module);