blob: 4f74b9762c296b81313660b571ff9774a8535ea1 [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"
françois romieubca03d52011-01-03 15:07:31 +000049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#ifdef RTL8169_DEBUG
51#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020052 if (!(expr)) { \
53 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070054 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020055 }
Joe Perches06fa7352007-10-18 21:15:00 +020056#define dprintk(fmt, args...) \
57 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#else
59#define assert(expr) do {} while (0)
60#define dprintk(fmt, args...) do {} while (0)
61#endif /* RTL8169_DEBUG */
62
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020063#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070064 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020065
Julien Ducourthial477206a2012-05-09 00:00:06 +020066#define TX_SLOTS_AVAIL(tp) \
67 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
68
69/* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
70#define TX_FRAGS_READY_FOR(tp,nr_frags) \
71 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
74 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050075static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Francois Romieu9c14cea2008-07-05 00:21:15 +020077#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
80#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
81
82#define R8169_REGS_SIZE 256
83#define R8169_NAPI_WEIGHT 64
84#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
85#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
86#define RX_BUF_SIZE 1536 /* Rx Buffer size */
87#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
88#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
89
90#define RTL8169_TX_TIMEOUT (6*HZ)
91#define RTL8169_PHY_TIMEOUT (10*HZ)
92
françois romieuea8dbdd2009-03-15 01:10:50 +000093#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
94#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020095#define RTL_EEPROM_SIG_ADDR 0x0000
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/* write/read MMIO register */
98#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
99#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
100#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
101#define RTL_R8(reg) readb (ioaddr + (reg))
102#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +0000103#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200106 RTL_GIGA_MAC_VER_01 = 0,
107 RTL_GIGA_MAC_VER_02,
108 RTL_GIGA_MAC_VER_03,
109 RTL_GIGA_MAC_VER_04,
110 RTL_GIGA_MAC_VER_05,
111 RTL_GIGA_MAC_VER_06,
112 RTL_GIGA_MAC_VER_07,
113 RTL_GIGA_MAC_VER_08,
114 RTL_GIGA_MAC_VER_09,
115 RTL_GIGA_MAC_VER_10,
116 RTL_GIGA_MAC_VER_11,
117 RTL_GIGA_MAC_VER_12,
118 RTL_GIGA_MAC_VER_13,
119 RTL_GIGA_MAC_VER_14,
120 RTL_GIGA_MAC_VER_15,
121 RTL_GIGA_MAC_VER_16,
122 RTL_GIGA_MAC_VER_17,
123 RTL_GIGA_MAC_VER_18,
124 RTL_GIGA_MAC_VER_19,
125 RTL_GIGA_MAC_VER_20,
126 RTL_GIGA_MAC_VER_21,
127 RTL_GIGA_MAC_VER_22,
128 RTL_GIGA_MAC_VER_23,
129 RTL_GIGA_MAC_VER_24,
130 RTL_GIGA_MAC_VER_25,
131 RTL_GIGA_MAC_VER_26,
132 RTL_GIGA_MAC_VER_27,
133 RTL_GIGA_MAC_VER_28,
134 RTL_GIGA_MAC_VER_29,
135 RTL_GIGA_MAC_VER_30,
136 RTL_GIGA_MAC_VER_31,
137 RTL_GIGA_MAC_VER_32,
138 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800139 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800140 RTL_GIGA_MAC_VER_35,
141 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800142 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800143 RTL_GIGA_MAC_VER_38,
Francois Romieu85bffe62011-04-27 08:22:39 +0200144 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145};
146
Francois Romieu2b7b4312011-04-18 22:53:24 -0700147enum rtl_tx_desc_version {
148 RTL_TD_0 = 0,
149 RTL_TD_1 = 1,
150};
151
Francois Romieud58d46b2011-05-03 16:38:29 +0200152#define JUMBO_1K ETH_DATA_LEN
153#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
154#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
155#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
156#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
157
158#define _R(NAME,TD,FW,SZ,B) { \
159 .name = NAME, \
160 .txd_version = TD, \
161 .fw_name = FW, \
162 .jumbo_max = SZ, \
163 .jumbo_tx_csum = B \
164}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800166static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700168 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200169 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200170 u16 jumbo_max;
171 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200172} rtl_chip_infos[] = {
173 /* PCI devices. */
174 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200175 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200176 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200177 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200178 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200179 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200180 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200181 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200182 [RTL_GIGA_MAC_VER_05] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200183 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200184 [RTL_GIGA_MAC_VER_06] =
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 /* PCI-E devices. */
187 [RTL_GIGA_MAC_VER_07] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200188 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200189 [RTL_GIGA_MAC_VER_08] =
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_09] =
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_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200194 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200195 [RTL_GIGA_MAC_VER_11] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200196 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200197 [RTL_GIGA_MAC_VER_12] =
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_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200200 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200201 [RTL_GIGA_MAC_VER_14] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200202 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200203 [RTL_GIGA_MAC_VER_15] =
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_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200206 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200207 [RTL_GIGA_MAC_VER_17] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200208 _R("RTL8168b/8111b", RTL_TD_1, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200209 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200210 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200211 [RTL_GIGA_MAC_VER_19] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200212 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200213 [RTL_GIGA_MAC_VER_20] =
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_21] =
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_22] =
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_23] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200220 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200221 [RTL_GIGA_MAC_VER_24] =
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_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200224 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
225 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200226 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200227 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
228 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200229 [RTL_GIGA_MAC_VER_27] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200230 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200231 [RTL_GIGA_MAC_VER_28] =
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_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200234 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
235 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200236 [RTL_GIGA_MAC_VER_30] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200237 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
238 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200239 [RTL_GIGA_MAC_VER_31] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200240 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200241 [RTL_GIGA_MAC_VER_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200242 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
243 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200244 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200245 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
246 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800247 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200248 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
249 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800250 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200251 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
252 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800253 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200254 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
255 JUMBO_9K, false),
Hayes Wang7e18dca2012-03-30 14:33:02 +0800256 [RTL_GIGA_MAC_VER_37] =
257 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
258 JUMBO_1K, true),
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800259 [RTL_GIGA_MAC_VER_38] =
260 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
261 JUMBO_9K, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262};
263#undef _R
264
Francois Romieubcf0bf92006-07-26 23:14:13 +0200265enum cfg_version {
266 RTL_CFG_0 = 0x00,
267 RTL_CFG_1,
268 RTL_CFG_2
269};
270
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000271static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200272 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200273 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200274 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100275 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200276 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
277 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000278 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200279 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200280 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
281 { PCI_VENDOR_ID_LINKSYS, 0x1032,
282 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100283 { 0x0001, 0x8168,
284 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 {0,},
286};
287
288MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
289
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000290static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700291static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200292static struct {
293 u32 msg_enable;
294} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Francois Romieu07d3f512007-02-21 22:40:46 +0100296enum rtl_registers {
297 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100298 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100299 MAR0 = 8, /* Multicast filter. */
300 CounterAddrLow = 0x10,
301 CounterAddrHigh = 0x14,
302 TxDescStartAddrLow = 0x20,
303 TxDescStartAddrHigh = 0x24,
304 TxHDescStartAddrLow = 0x28,
305 TxHDescStartAddrHigh = 0x2c,
306 FLASH = 0x30,
307 ERSR = 0x36,
308 ChipCmd = 0x37,
309 TxPoll = 0x38,
310 IntrMask = 0x3c,
311 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700312
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800313 TxConfig = 0x40,
314#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
315#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
316
317 RxConfig = 0x44,
318#define RX128_INT_EN (1 << 15) /* 8111c and later */
319#define RX_MULTI_EN (1 << 14) /* 8111c only */
320#define RXCFG_FIFO_SHIFT 13
321 /* No threshold before first PCI xfer */
322#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
323#define RXCFG_DMA_SHIFT 8
324 /* Unlimited maximum PCI burst. */
325#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700326
Francois Romieu07d3f512007-02-21 22:40:46 +0100327 RxMissed = 0x4c,
328 Cfg9346 = 0x50,
329 Config0 = 0x51,
330 Config1 = 0x52,
331 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200332#define PME_SIGNAL (1 << 5) /* 8168c and later */
333
Francois Romieu07d3f512007-02-21 22:40:46 +0100334 Config3 = 0x54,
335 Config4 = 0x55,
336 Config5 = 0x56,
337 MultiIntr = 0x5c,
338 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100339 PHYstatus = 0x6c,
340 RxMaxSize = 0xda,
341 CPlusCmd = 0xe0,
342 IntrMitigate = 0xe2,
343 RxDescAddrLow = 0xe4,
344 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000345 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
346
347#define NoEarlyTx 0x3f /* Max value : no early transmit. */
348
349 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
350
351#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800352#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000353
Francois Romieu07d3f512007-02-21 22:40:46 +0100354 FuncEvent = 0xf0,
355 FuncEventMask = 0xf4,
356 FuncPresetState = 0xf8,
357 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358};
359
Francois Romieuf162a5d2008-06-01 22:37:49 +0200360enum rtl8110_registers {
361 TBICSR = 0x64,
362 TBI_ANAR = 0x68,
363 TBI_LPAR = 0x6a,
364};
365
366enum rtl8168_8101_registers {
367 CSIDR = 0x64,
368 CSIAR = 0x68,
369#define CSIAR_FLAG 0x80000000
370#define CSIAR_WRITE_CMD 0x80000000
371#define CSIAR_BYTE_ENABLE 0x0f
372#define CSIAR_BYTE_ENABLE_SHIFT 12
373#define CSIAR_ADDR_MASK 0x0fff
Hayes Wang7e18dca2012-03-30 14:33:02 +0800374#define CSIAR_FUNC_CARD 0x00000000
375#define CSIAR_FUNC_SDIO 0x00010000
376#define CSIAR_FUNC_NIC 0x00020000
françois romieu065c27c2011-01-03 15:08:12 +0000377 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200378 EPHYAR = 0x80,
379#define EPHYAR_FLAG 0x80000000
380#define EPHYAR_WRITE_CMD 0x80000000
381#define EPHYAR_REG_MASK 0x1f
382#define EPHYAR_REG_SHIFT 16
383#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800384 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800385#define PFM_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200386 DBG_REG = 0xd1,
387#define FIX_NAK_1 (1 << 4)
388#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800389 TWSI = 0xd2,
390 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800391#define NOW_IS_OOB (1 << 7)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800392#define EN_NDP (1 << 3)
393#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000394 EFUSEAR = 0xdc,
395#define EFUSEAR_FLAG 0x80000000
396#define EFUSEAR_WRITE_CMD 0x80000000
397#define EFUSEAR_READ_CMD 0x00000000
398#define EFUSEAR_REG_MASK 0x03ff
399#define EFUSEAR_REG_SHIFT 8
400#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200401};
402
françois romieuc0e45c12011-01-03 15:08:04 +0000403enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800404 LED_FREQ = 0x1a,
405 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000406 ERIDR = 0x70,
407 ERIAR = 0x74,
408#define ERIAR_FLAG 0x80000000
409#define ERIAR_WRITE_CMD 0x80000000
410#define ERIAR_READ_CMD 0x00000000
411#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000412#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800413#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
414#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
415#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
416#define ERIAR_MASK_SHIFT 12
417#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
418#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
419#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000420 EPHY_RXER_NUM = 0x7c,
421 OCPDR = 0xb0, /* OCP GPHY access */
422#define OCPDR_WRITE_CMD 0x80000000
423#define OCPDR_READ_CMD 0x00000000
424#define OCPDR_REG_MASK 0x7f
425#define OCPDR_GPHY_REG_SHIFT 16
426#define OCPDR_DATA_MASK 0xffff
427 OCPAR = 0xb4,
428#define OCPAR_FLAG 0x80000000
429#define OCPAR_GPHY_WRITE_CMD 0x8000f060
430#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000431 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
432 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200433#define TXPLA_RST (1 << 29)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800434#define PWM_EN (1 << 22)
françois romieuc0e45c12011-01-03 15:08:04 +0000435};
436
Francois Romieu07d3f512007-02-21 22:40:46 +0100437enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100439 SYSErr = 0x8000,
440 PCSTimeout = 0x4000,
441 SWInt = 0x0100,
442 TxDescUnavail = 0x0080,
443 RxFIFOOver = 0x0040,
444 LinkChg = 0x0020,
445 RxOverflow = 0x0010,
446 TxErr = 0x0008,
447 TxOK = 0x0004,
448 RxErr = 0x0002,
449 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400452 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200453 RxFOVF = (1 << 23),
454 RxRWT = (1 << 22),
455 RxRES = (1 << 21),
456 RxRUNT = (1 << 20),
457 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800460 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100461 CmdReset = 0x10,
462 CmdRxEnb = 0x08,
463 CmdTxEnb = 0x04,
464 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Francois Romieu275391a2007-02-23 23:50:28 +0100466 /* TXPoll register p.5 */
467 HPQ = 0x80, /* Poll cmd on the high prio queue */
468 NPQ = 0x40, /* Poll cmd on the low prio queue */
469 FSWInt = 0x01, /* Forced software interrupt */
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100472 Cfg9346_Lock = 0x00,
473 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100476 AcceptErr = 0x20,
477 AcceptRunt = 0x10,
478 AcceptBroadcast = 0x08,
479 AcceptMulticast = 0x04,
480 AcceptMyPhys = 0x02,
481 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200482#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 /* TxConfigBits */
485 TxInterFrameGapShift = 24,
486 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
487
Francois Romieu5d06a992006-02-23 00:47:58 +0100488 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200489 LEDS1 = (1 << 7),
490 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200491 Speed_down = (1 << 4),
492 MEMMAP = (1 << 3),
493 IOMAP = (1 << 2),
494 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100495 PMEnable = (1 << 0), /* Power Management Enable */
496
Francois Romieu6dccd162007-02-13 23:38:05 +0100497 /* Config2 register p. 25 */
françois romieu2ca6cf02011-12-15 08:37:43 +0000498 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100499 PCI_Clock_66MHz = 0x01,
500 PCI_Clock_33MHz = 0x00,
501
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100502 /* Config3 register p.25 */
503 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
504 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200505 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200506 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100507
Francois Romieud58d46b2011-05-03 16:38:29 +0200508 /* Config4 register */
509 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
510
Francois Romieu5d06a992006-02-23 00:47:58 +0100511 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100512 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
513 MWF = (1 << 5), /* Accept Multicast wakeup frame */
514 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200515 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100516 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100517 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 /* TBICSR p.28 */
520 TBIReset = 0x80000000,
521 TBILoopback = 0x40000000,
522 TBINwEnable = 0x20000000,
523 TBINwRestart = 0x10000000,
524 TBILinkOk = 0x02000000,
525 TBINwComplete = 0x01000000,
526
527 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200528 EnableBist = (1 << 15), // 8168 8101
529 Mac_dbgo_oe = (1 << 14), // 8168 8101
530 Normal_mode = (1 << 13), // unused
531 Force_half_dup = (1 << 12), // 8168 8101
532 Force_rxflow_en = (1 << 11), // 8168 8101
533 Force_txflow_en = (1 << 10), // 8168 8101
534 Cxpl_dbg_sel = (1 << 9), // 8168 8101
535 ASF = (1 << 8), // 8168 8101
536 PktCntrDisable = (1 << 7), // 8168 8101
537 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 RxVlan = (1 << 6),
539 RxChkSum = (1 << 5),
540 PCIDAC = (1 << 4),
541 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100542 INTT_0 = 0x0000, // 8168
543 INTT_1 = 0x0001, // 8168
544 INTT_2 = 0x0002, // 8168
545 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100548 TBI_Enable = 0x80,
549 TxFlowCtrl = 0x40,
550 RxFlowCtrl = 0x20,
551 _1000bpsF = 0x10,
552 _100bps = 0x08,
553 _10bps = 0x04,
554 LinkStatus = 0x02,
555 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100558 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200559
560 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100561 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562};
563
Francois Romieu2b7b4312011-04-18 22:53:24 -0700564enum rtl_desc_bit {
565 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
567 RingEnd = (1 << 30), /* End of descriptor ring */
568 FirstFrag = (1 << 29), /* First segment of a packet */
569 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700570};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Francois Romieu2b7b4312011-04-18 22:53:24 -0700572/* Generic case. */
573enum rtl_tx_desc_bit {
574 /* First doubleword. */
575 TD_LSO = (1 << 27), /* Large Send Offload */
576#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Francois Romieu2b7b4312011-04-18 22:53:24 -0700578 /* Second doubleword. */
579 TxVlanTag = (1 << 17), /* Add VLAN tag */
580};
581
582/* 8169, 8168b and 810x except 8102e. */
583enum rtl_tx_desc_bit_0 {
584 /* First doubleword. */
585#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
586 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
587 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
588 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
589};
590
591/* 8102e, 8168c and beyond. */
592enum rtl_tx_desc_bit_1 {
593 /* Second doubleword. */
594#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
595 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
596 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
597 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
598};
599
600static const struct rtl_tx_desc_info {
601 struct {
602 u32 udp;
603 u32 tcp;
604 } checksum;
605 u16 mss_shift;
606 u16 opts_offset;
607} tx_desc_info [] = {
608 [RTL_TD_0] = {
609 .checksum = {
610 .udp = TD0_IP_CS | TD0_UDP_CS,
611 .tcp = TD0_IP_CS | TD0_TCP_CS
612 },
613 .mss_shift = TD0_MSS_SHIFT,
614 .opts_offset = 0
615 },
616 [RTL_TD_1] = {
617 .checksum = {
618 .udp = TD1_IP_CS | TD1_UDP_CS,
619 .tcp = TD1_IP_CS | TD1_TCP_CS
620 },
621 .mss_shift = TD1_MSS_SHIFT,
622 .opts_offset = 1
623 }
624};
625
626enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /* Rx private */
628 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
629 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
630
631#define RxProtoUDP (PID1)
632#define RxProtoTCP (PID0)
633#define RxProtoIP (PID1 | PID0)
634#define RxProtoMask RxProtoIP
635
636 IPFail = (1 << 16), /* IP checksum failed */
637 UDPFail = (1 << 15), /* UDP/IP checksum failed */
638 TCPFail = (1 << 14), /* TCP/IP checksum failed */
639 RxVlanTag = (1 << 16), /* VLAN tag available */
640};
641
642#define RsvdMask 0x3fffc000
643
644struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200645 __le32 opts1;
646 __le32 opts2;
647 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648};
649
650struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200651 __le32 opts1;
652 __le32 opts2;
653 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654};
655
656struct ring_info {
657 struct sk_buff *skb;
658 u32 len;
659 u8 __pad[sizeof(void *) - sizeof(u32)];
660};
661
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200662enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200663 RTL_FEATURE_WOL = (1 << 0),
664 RTL_FEATURE_MSI = (1 << 1),
665 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200666};
667
Ivan Vecera355423d2009-02-06 21:49:57 -0800668struct rtl8169_counters {
669 __le64 tx_packets;
670 __le64 rx_packets;
671 __le64 tx_errors;
672 __le32 rx_errors;
673 __le16 rx_missed;
674 __le16 align_errors;
675 __le32 tx_one_collision;
676 __le32 tx_multi_collision;
677 __le64 rx_unicast;
678 __le64 rx_broadcast;
679 __le32 rx_multicast;
680 __le16 tx_aborted;
681 __le16 tx_underun;
682};
683
Francois Romieuda78dbf2012-01-26 14:18:23 +0100684enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100685 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100686 RTL_FLAG_TASK_SLOW_PENDING,
687 RTL_FLAG_TASK_RESET_PENDING,
688 RTL_FLAG_TASK_PHY_PENDING,
689 RTL_FLAG_MAX
690};
691
Junchang Wang8027aa22012-03-04 23:30:32 +0100692struct rtl8169_stats {
693 u64 packets;
694 u64 bytes;
695 struct u64_stats_sync syncp;
696};
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698struct rtl8169_private {
699 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200700 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000701 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700702 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200703 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700704 u16 txd_version;
705 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
707 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
708 u32 dirty_rx;
709 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100710 struct rtl8169_stats rx_stats;
711 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
713 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
714 dma_addr_t TxPhyAddr;
715 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000716 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 struct timer_list timer;
719 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100720
721 u16 event_slow;
françois romieuc0e45c12011-01-03 15:08:04 +0000722
723 struct mdio_ops {
724 void (*write)(void __iomem *, int, int);
725 int (*read)(void __iomem *, int);
726 } mdio_ops;
727
françois romieu065c27c2011-01-03 15:08:12 +0000728 struct pll_power_ops {
729 void (*down)(struct rtl8169_private *);
730 void (*up)(struct rtl8169_private *);
731 } pll_power_ops;
732
Francois Romieud58d46b2011-05-03 16:38:29 +0200733 struct jumbo_ops {
734 void (*enable)(struct rtl8169_private *);
735 void (*disable)(struct rtl8169_private *);
736 } jumbo_ops;
737
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800738 struct csi_ops {
739 void (*write)(void __iomem *, int, int);
740 u32 (*read)(void __iomem *, int);
741 } csi_ops;
742
Oliver Neukum54405cd2011-01-06 21:55:13 +0100743 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200744 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000745 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100746 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000747 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800749 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100750
751 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100752 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
753 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100754 struct work_struct work;
755 } wk;
756
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200757 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200758
759 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800760 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000761 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400762 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000763
Francois Romieub6ffd972011-06-17 17:00:05 +0200764 struct rtl_fw {
765 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200766
767#define RTL_VER_SIZE 32
768
769 char version[RTL_VER_SIZE];
770
771 struct rtl_fw_phy_action {
772 __le32 *code;
773 size_t size;
774 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200775 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300776#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777};
778
Ralf Baechle979b6c12005-06-13 14:30:40 -0700779MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700782MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200783module_param_named(debug, debug.msg_enable, int, 0);
784MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785MODULE_LICENSE("GPL");
786MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000787MODULE_FIRMWARE(FIRMWARE_8168D_1);
788MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000789MODULE_FIRMWARE(FIRMWARE_8168E_1);
790MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400791MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800792MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800793MODULE_FIRMWARE(FIRMWARE_8168F_1);
794MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800795MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800796MODULE_FIRMWARE(FIRMWARE_8411_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Francois Romieuda78dbf2012-01-26 14:18:23 +0100798static void rtl_lock_work(struct rtl8169_private *tp)
799{
800 mutex_lock(&tp->wk.mutex);
801}
802
803static void rtl_unlock_work(struct rtl8169_private *tp)
804{
805 mutex_unlock(&tp->wk.mutex);
806}
807
Francois Romieud58d46b2011-05-03 16:38:29 +0200808static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
809{
810 int cap = pci_pcie_cap(pdev);
811
812 if (cap) {
813 u16 ctl;
814
815 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
816 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
817 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
818 }
819}
820
françois romieub646d902011-01-03 15:08:21 +0000821static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
822{
823 void __iomem *ioaddr = tp->mmio_addr;
824 int i;
825
826 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
827 for (i = 0; i < 20; i++) {
828 udelay(100);
829 if (RTL_R32(OCPAR) & OCPAR_FLAG)
830 break;
831 }
832 return RTL_R32(OCPDR);
833}
834
835static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
836{
837 void __iomem *ioaddr = tp->mmio_addr;
838 int i;
839
840 RTL_W32(OCPDR, data);
841 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
842 for (i = 0; i < 20; i++) {
843 udelay(100);
844 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
845 break;
846 }
847}
848
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800849static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000850{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800851 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000852 int i;
853
854 RTL_W8(ERIDR, cmd);
855 RTL_W32(ERIAR, 0x800010e8);
856 msleep(2);
857 for (i = 0; i < 5; i++) {
858 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200859 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000860 break;
861 }
862
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800863 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000864}
865
866#define OOB_CMD_RESET 0x00
867#define OOB_CMD_DRIVER_START 0x05
868#define OOB_CMD_DRIVER_STOP 0x06
869
Francois Romieucecb5fd2011-04-01 10:21:07 +0200870static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
871{
872 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
873}
874
françois romieub646d902011-01-03 15:08:21 +0000875static void rtl8168_driver_start(struct rtl8169_private *tp)
876{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200877 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000878 int i;
879
880 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
881
Francois Romieucecb5fd2011-04-01 10:21:07 +0200882 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000883
françois romieub646d902011-01-03 15:08:21 +0000884 for (i = 0; i < 10; i++) {
885 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000886 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000887 break;
888 }
889}
890
891static void rtl8168_driver_stop(struct rtl8169_private *tp)
892{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200893 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000894 int i;
895
896 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
897
Francois Romieucecb5fd2011-04-01 10:21:07 +0200898 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000899
françois romieub646d902011-01-03 15:08:21 +0000900 for (i = 0; i < 10; i++) {
901 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000902 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000903 break;
904 }
905}
906
hayeswang4804b3b2011-03-21 01:50:29 +0000907static int r8168dp_check_dash(struct rtl8169_private *tp)
908{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200909 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000910
Francois Romieucecb5fd2011-04-01 10:21:07 +0200911 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000912}
françois romieub646d902011-01-03 15:08:21 +0000913
françois romieu4da19632011-01-03 15:07:55 +0000914static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 int i;
917
Francois Romieua6baf3a2007-11-08 23:23:21 +0100918 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Francois Romieu23714082006-01-29 00:49:09 +0100920 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100921 /*
922 * Check if the RTL8169 has completed writing to the specified
923 * MII register.
924 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200925 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 break;
Francois Romieu23714082006-01-29 00:49:09 +0100927 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700929 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700930 * According to hardware specs a 20us delay is required after write
931 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700932 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700933 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
françois romieu4da19632011-01-03 15:07:55 +0000936static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
938 int i, value = -1;
939
Francois Romieua6baf3a2007-11-08 23:23:21 +0100940 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Francois Romieu23714082006-01-29 00:49:09 +0100942 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100943 /*
944 * Check if the RTL8169 has completed retrieving data from
945 * the specified MII register.
946 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100948 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 break;
950 }
Francois Romieu23714082006-01-29 00:49:09 +0100951 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700953 /*
954 * According to hardware specs a 20us delay is required after read
955 * complete indication, but before sending next command.
956 */
957 udelay(20);
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return value;
960}
961
françois romieuc0e45c12011-01-03 15:08:04 +0000962static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
963{
964 int i;
965
966 RTL_W32(OCPDR, data |
967 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
968 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
969 RTL_W32(EPHY_RXER_NUM, 0);
970
971 for (i = 0; i < 100; i++) {
972 mdelay(1);
973 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
974 break;
975 }
976}
977
978static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
979{
980 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
981 (value & OCPDR_DATA_MASK));
982}
983
984static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
985{
986 int i;
987
988 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
989
990 mdelay(1);
991 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
992 RTL_W32(EPHY_RXER_NUM, 0);
993
994 for (i = 0; i < 100; i++) {
995 mdelay(1);
996 if (RTL_R32(OCPAR) & OCPAR_FLAG)
997 break;
998 }
999
1000 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
1001}
1002
françois romieue6de30d2011-01-03 15:08:37 +00001003#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1004
1005static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1006{
1007 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1008}
1009
1010static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1011{
1012 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1013}
1014
1015static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
1016{
1017 r8168dp_2_mdio_start(ioaddr);
1018
1019 r8169_mdio_write(ioaddr, reg_addr, value);
1020
1021 r8168dp_2_mdio_stop(ioaddr);
1022}
1023
1024static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
1025{
1026 int value;
1027
1028 r8168dp_2_mdio_start(ioaddr);
1029
1030 value = r8169_mdio_read(ioaddr, reg_addr);
1031
1032 r8168dp_2_mdio_stop(ioaddr);
1033
1034 return value;
1035}
1036
françois romieu4da19632011-01-03 15:07:55 +00001037static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001038{
françois romieuc0e45c12011-01-03 15:08:04 +00001039 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001040}
1041
françois romieu4da19632011-01-03 15:07:55 +00001042static int rtl_readphy(struct rtl8169_private *tp, int location)
1043{
françois romieuc0e45c12011-01-03 15:08:04 +00001044 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +00001045}
1046
1047static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1048{
1049 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1050}
1051
1052static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001053{
1054 int val;
1055
françois romieu4da19632011-01-03 15:07:55 +00001056 val = rtl_readphy(tp, reg_addr);
1057 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001058}
1059
Francois Romieuccdffb92008-07-26 14:26:06 +02001060static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1061 int val)
1062{
1063 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001064
françois romieu4da19632011-01-03 15:07:55 +00001065 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001066}
1067
1068static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1069{
1070 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001071
françois romieu4da19632011-01-03 15:07:55 +00001072 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001073}
1074
Francois Romieudacf8152008-08-02 20:44:13 +02001075static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
1076{
1077 unsigned int i;
1078
1079 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1080 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1081
1082 for (i = 0; i < 100; i++) {
1083 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1084 break;
1085 udelay(10);
1086 }
1087}
1088
1089static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1090{
1091 u16 value = 0xffff;
1092 unsigned int i;
1093
1094 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1095
1096 for (i = 0; i < 100; i++) {
1097 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1098 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1099 break;
1100 }
1101 udelay(10);
1102 }
1103
1104 return value;
1105}
1106
Hayes Wang133ac402011-07-06 15:58:05 +08001107static
1108void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1109{
1110 unsigned int i;
1111
1112 BUG_ON((addr & 3) || (mask == 0));
1113 RTL_W32(ERIDR, val);
1114 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1115
1116 for (i = 0; i < 100; i++) {
1117 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1118 break;
1119 udelay(100);
1120 }
1121}
1122
1123static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1124{
1125 u32 value = ~0x00;
1126 unsigned int i;
1127
1128 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1129
1130 for (i = 0; i < 100; i++) {
1131 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1132 value = RTL_R32(ERIDR);
1133 break;
1134 }
1135 udelay(100);
1136 }
1137
1138 return value;
1139}
1140
1141static void
1142rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1143{
1144 u32 val;
1145
1146 val = rtl_eri_read(ioaddr, addr, type);
1147 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1148}
1149
françois romieuc28aa382011-08-02 03:53:43 +00001150struct exgmac_reg {
1151 u16 addr;
1152 u16 mask;
1153 u32 val;
1154};
1155
1156static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1157 const struct exgmac_reg *r, int len)
1158{
1159 while (len-- > 0) {
1160 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1161 r++;
1162 }
1163}
1164
françois romieudaf9df62009-10-07 12:44:20 +00001165static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1166{
1167 u8 value = 0xff;
1168 unsigned int i;
1169
1170 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1171
1172 for (i = 0; i < 300; i++) {
1173 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1174 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1175 break;
1176 }
1177 udelay(100);
1178 }
1179
1180 return value;
1181}
1182
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001183static u16 rtl_get_events(struct rtl8169_private *tp)
1184{
1185 void __iomem *ioaddr = tp->mmio_addr;
1186
1187 return RTL_R16(IntrStatus);
1188}
1189
1190static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1191{
1192 void __iomem *ioaddr = tp->mmio_addr;
1193
1194 RTL_W16(IntrStatus, bits);
1195 mmiowb();
1196}
1197
1198static void rtl_irq_disable(struct rtl8169_private *tp)
1199{
1200 void __iomem *ioaddr = tp->mmio_addr;
1201
1202 RTL_W16(IntrMask, 0);
1203 mmiowb();
1204}
1205
Francois Romieu3e990ff2012-01-26 12:50:01 +01001206static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1207{
1208 void __iomem *ioaddr = tp->mmio_addr;
1209
1210 RTL_W16(IntrMask, bits);
1211}
1212
Francois Romieuda78dbf2012-01-26 14:18:23 +01001213#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1214#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1215#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1216
1217static void rtl_irq_enable_all(struct rtl8169_private *tp)
1218{
1219 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1220}
1221
françois romieu811fd302011-12-04 20:30:45 +00001222static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
françois romieu811fd302011-12-04 20:30:45 +00001224 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001226 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001227 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001228 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
françois romieu4da19632011-01-03 15:07:55 +00001231static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
françois romieu4da19632011-01-03 15:07:55 +00001233 void __iomem *ioaddr = tp->mmio_addr;
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return RTL_R32(TBICSR) & TBIReset;
1236}
1237
françois romieu4da19632011-01-03 15:07:55 +00001238static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
françois romieu4da19632011-01-03 15:07:55 +00001240 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
1243static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1244{
1245 return RTL_R32(TBICSR) & TBILinkOk;
1246}
1247
1248static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1249{
1250 return RTL_R8(PHYstatus) & LinkStatus;
1251}
1252
françois romieu4da19632011-01-03 15:07:55 +00001253static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
françois romieu4da19632011-01-03 15:07:55 +00001255 void __iomem *ioaddr = tp->mmio_addr;
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1258}
1259
françois romieu4da19632011-01-03 15:07:55 +00001260static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 unsigned int val;
1263
françois romieu4da19632011-01-03 15:07:55 +00001264 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1265 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
Hayes Wang70090422011-07-06 15:58:06 +08001268static void rtl_link_chg_patch(struct rtl8169_private *tp)
1269{
1270 void __iomem *ioaddr = tp->mmio_addr;
1271 struct net_device *dev = tp->dev;
1272
1273 if (!netif_running(dev))
1274 return;
1275
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001276 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1277 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Hayes Wang70090422011-07-06 15:58:06 +08001278 if (RTL_R8(PHYstatus) & _1000bpsF) {
1279 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1280 0x00000011, ERIAR_EXGMAC);
1281 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1282 0x00000005, ERIAR_EXGMAC);
1283 } else if (RTL_R8(PHYstatus) & _100bps) {
1284 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1285 0x0000001f, ERIAR_EXGMAC);
1286 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1287 0x00000005, ERIAR_EXGMAC);
1288 } else {
1289 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1290 0x0000001f, ERIAR_EXGMAC);
1291 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1292 0x0000003f, ERIAR_EXGMAC);
1293 }
1294 /* Reset packet filter */
1295 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1296 ERIAR_EXGMAC);
1297 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1298 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001299 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1300 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1301 if (RTL_R8(PHYstatus) & _1000bpsF) {
1302 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1303 0x00000011, ERIAR_EXGMAC);
1304 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1305 0x00000005, ERIAR_EXGMAC);
1306 } else {
1307 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1308 0x0000001f, ERIAR_EXGMAC);
1309 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1310 0x0000003f, ERIAR_EXGMAC);
1311 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001312 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1313 if (RTL_R8(PHYstatus) & _10bps) {
1314 rtl_eri_write(ioaddr, 0x1d0, ERIAR_MASK_0011,
1315 0x4d02, ERIAR_EXGMAC);
1316 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_0011,
1317 0x0060, ERIAR_EXGMAC);
1318 } else {
1319 rtl_eri_write(ioaddr, 0x1d0, ERIAR_MASK_0011,
1320 0x0000, ERIAR_EXGMAC);
1321 }
Hayes Wang70090422011-07-06 15:58:06 +08001322 }
1323}
1324
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001325static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001326 struct rtl8169_private *tp,
1327 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001330 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001331 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001332 if (pm)
1333 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001335 if (net_ratelimit())
1336 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001337 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001339 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001340 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001341 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001345static void rtl8169_check_link_status(struct net_device *dev,
1346 struct rtl8169_private *tp,
1347 void __iomem *ioaddr)
1348{
1349 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1350}
1351
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001352#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1353
1354static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1355{
1356 void __iomem *ioaddr = tp->mmio_addr;
1357 u8 options;
1358 u32 wolopts = 0;
1359
1360 options = RTL_R8(Config1);
1361 if (!(options & PMEnable))
1362 return 0;
1363
1364 options = RTL_R8(Config3);
1365 if (options & LinkUp)
1366 wolopts |= WAKE_PHY;
1367 if (options & MagicPacket)
1368 wolopts |= WAKE_MAGIC;
1369
1370 options = RTL_R8(Config5);
1371 if (options & UWF)
1372 wolopts |= WAKE_UCAST;
1373 if (options & BWF)
1374 wolopts |= WAKE_BCAST;
1375 if (options & MWF)
1376 wolopts |= WAKE_MCAST;
1377
1378 return wolopts;
1379}
1380
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001381static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1382{
1383 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001384
Francois Romieuda78dbf2012-01-26 14:18:23 +01001385 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001386
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001387 wol->supported = WAKE_ANY;
1388 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001389
Francois Romieuda78dbf2012-01-26 14:18:23 +01001390 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001391}
1392
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001393static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001394{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001395 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001396 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001397 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001398 u32 opt;
1399 u16 reg;
1400 u8 mask;
1401 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001402 { WAKE_PHY, Config3, LinkUp },
1403 { WAKE_MAGIC, Config3, MagicPacket },
1404 { WAKE_UCAST, Config5, UWF },
1405 { WAKE_BCAST, Config5, BWF },
1406 { WAKE_MCAST, Config5, MWF },
1407 { WAKE_ANY, Config5, LanWake }
1408 };
Francois Romieu851e6022012-04-17 11:10:11 +02001409 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001410
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001411 RTL_W8(Cfg9346, Cfg9346_Unlock);
1412
1413 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
Francois Romieu851e6022012-04-17 11:10:11 +02001414 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001415 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001416 options |= cfg[i].mask;
1417 RTL_W8(cfg[i].reg, options);
1418 }
1419
Francois Romieu851e6022012-04-17 11:10:11 +02001420 switch (tp->mac_version) {
1421 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1422 options = RTL_R8(Config1) & ~PMEnable;
1423 if (wolopts)
1424 options |= PMEnable;
1425 RTL_W8(Config1, options);
1426 break;
1427 default:
Francois Romieud387b422012-04-17 11:12:01 +02001428 options = RTL_R8(Config2) & ~PME_SIGNAL;
1429 if (wolopts)
1430 options |= PME_SIGNAL;
1431 RTL_W8(Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001432 break;
1433 }
1434
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001435 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001436}
1437
1438static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1439{
1440 struct rtl8169_private *tp = netdev_priv(dev);
1441
Francois Romieuda78dbf2012-01-26 14:18:23 +01001442 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001443
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001444 if (wol->wolopts)
1445 tp->features |= RTL_FEATURE_WOL;
1446 else
1447 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001448 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001449
1450 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001451
françois romieuea809072010-11-08 13:23:58 +00001452 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1453
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001454 return 0;
1455}
1456
Francois Romieu31bd2042011-04-26 18:58:59 +02001457static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1458{
Francois Romieu85bffe62011-04-27 08:22:39 +02001459 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001460}
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462static void rtl8169_get_drvinfo(struct net_device *dev,
1463 struct ethtool_drvinfo *info)
1464{
1465 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001466 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Rick Jones68aad782011-11-07 13:29:27 +00001468 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1469 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1470 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001471 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001472 if (!IS_ERR_OR_NULL(rtl_fw))
1473 strlcpy(info->fw_version, rtl_fw->version,
1474 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
1477static int rtl8169_get_regs_len(struct net_device *dev)
1478{
1479 return R8169_REGS_SIZE;
1480}
1481
1482static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001483 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
1485 struct rtl8169_private *tp = netdev_priv(dev);
1486 void __iomem *ioaddr = tp->mmio_addr;
1487 int ret = 0;
1488 u32 reg;
1489
1490 reg = RTL_R32(TBICSR);
1491 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1492 (duplex == DUPLEX_FULL)) {
1493 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1494 } else if (autoneg == AUTONEG_ENABLE)
1495 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1496 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001497 netif_warn(tp, link, dev,
1498 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 ret = -EOPNOTSUPP;
1500 }
1501
1502 return ret;
1503}
1504
1505static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001506 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
1508 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001509 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001510 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Hayes Wang716b50a2011-02-22 17:26:18 +08001512 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001515 int auto_nego;
1516
françois romieu4da19632011-01-03 15:07:55 +00001517 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001518 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1519 ADVERTISE_100HALF | ADVERTISE_100FULL);
1520
1521 if (adv & ADVERTISED_10baseT_Half)
1522 auto_nego |= ADVERTISE_10HALF;
1523 if (adv & ADVERTISED_10baseT_Full)
1524 auto_nego |= ADVERTISE_10FULL;
1525 if (adv & ADVERTISED_100baseT_Half)
1526 auto_nego |= ADVERTISE_100HALF;
1527 if (adv & ADVERTISED_100baseT_Full)
1528 auto_nego |= ADVERTISE_100FULL;
1529
françois romieu3577aa12009-05-19 10:46:48 +00001530 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1531
françois romieu4da19632011-01-03 15:07:55 +00001532 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001533 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1534
1535 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001536 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001537 if (adv & ADVERTISED_1000baseT_Half)
1538 giga_ctrl |= ADVERTISE_1000HALF;
1539 if (adv & ADVERTISED_1000baseT_Full)
1540 giga_ctrl |= ADVERTISE_1000FULL;
1541 } else if (adv & (ADVERTISED_1000baseT_Half |
1542 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001543 netif_info(tp, link, dev,
1544 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001545 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
françois romieu3577aa12009-05-19 10:46:48 +00001548 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001549
françois romieu4da19632011-01-03 15:07:55 +00001550 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1551 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001552 } else {
1553 giga_ctrl = 0;
1554
1555 if (speed == SPEED_10)
1556 bmcr = 0;
1557 else if (speed == SPEED_100)
1558 bmcr = BMCR_SPEED100;
1559 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001560 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001561
1562 if (duplex == DUPLEX_FULL)
1563 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001564 }
1565
françois romieu4da19632011-01-03 15:07:55 +00001566 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001567
Francois Romieucecb5fd2011-04-01 10:21:07 +02001568 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1569 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001570 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001571 rtl_writephy(tp, 0x17, 0x2138);
1572 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001573 } else {
françois romieu4da19632011-01-03 15:07:55 +00001574 rtl_writephy(tp, 0x17, 0x2108);
1575 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001576 }
1577 }
1578
Oliver Neukum54405cd2011-01-06 21:55:13 +01001579 rc = 0;
1580out:
1581 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
1584static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001585 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
1587 struct rtl8169_private *tp = netdev_priv(dev);
1588 int ret;
1589
Oliver Neukum54405cd2011-01-06 21:55:13 +01001590 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001591 if (ret < 0)
1592 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Francois Romieu4876cc12011-03-11 21:07:11 +01001594 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1595 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001597 }
1598out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 return ret;
1600}
1601
1602static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1603{
1604 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 int ret;
1606
Francois Romieu4876cc12011-03-11 21:07:11 +01001607 del_timer_sync(&tp->timer);
1608
Francois Romieuda78dbf2012-01-26 14:18:23 +01001609 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001610 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001611 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001612 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return ret;
1615}
1616
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001617static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1618 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
Francois Romieud58d46b2011-05-03 16:38:29 +02001620 struct rtl8169_private *tp = netdev_priv(dev);
1621
Francois Romieu2b7b4312011-04-18 22:53:24 -07001622 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001623 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Francois Romieud58d46b2011-05-03 16:38:29 +02001625 if (dev->mtu > JUMBO_1K &&
1626 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1627 features &= ~NETIF_F_IP_CSUM;
1628
Michał Mirosław350fb322011-04-08 06:35:56 +00001629 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630}
1631
Francois Romieuda78dbf2012-01-26 14:18:23 +01001632static void __rtl8169_set_features(struct net_device *dev,
1633 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
1635 struct rtl8169_private *tp = netdev_priv(dev);
Ben Greear6bbe0212012-02-10 15:04:33 +00001636 netdev_features_t changed = features ^ dev->features;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001637 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Ben Greear6bbe0212012-02-10 15:04:33 +00001639 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)))
1640 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Ben Greear6bbe0212012-02-10 15:04:33 +00001642 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)) {
1643 if (features & NETIF_F_RXCSUM)
1644 tp->cp_cmd |= RxChkSum;
1645 else
1646 tp->cp_cmd &= ~RxChkSum;
Michał Mirosław350fb322011-04-08 06:35:56 +00001647
Ben Greear6bbe0212012-02-10 15:04:33 +00001648 if (dev->features & NETIF_F_HW_VLAN_RX)
1649 tp->cp_cmd |= RxVlan;
1650 else
1651 tp->cp_cmd &= ~RxVlan;
1652
1653 RTL_W16(CPlusCmd, tp->cp_cmd);
1654 RTL_R16(CPlusCmd);
1655 }
1656 if (changed & NETIF_F_RXALL) {
1657 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1658 if (features & NETIF_F_RXALL)
1659 tmp |= (AcceptErr | AcceptRunt);
1660 RTL_W32(RxConfig, tmp);
1661 }
Francois Romieuda78dbf2012-01-26 14:18:23 +01001662}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Francois Romieuda78dbf2012-01-26 14:18:23 +01001664static int rtl8169_set_features(struct net_device *dev,
1665 netdev_features_t features)
1666{
1667 struct rtl8169_private *tp = netdev_priv(dev);
1668
1669 rtl_lock_work(tp);
1670 __rtl8169_set_features(dev, features);
1671 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 return 0;
1674}
1675
Francois Romieuda78dbf2012-01-26 14:18:23 +01001676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1678 struct sk_buff *skb)
1679{
Jesse Grosseab6d182010-10-20 13:56:03 +00001680 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1682}
1683
Francois Romieu7a8fc772011-03-01 17:18:33 +01001684static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
1686 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Francois Romieu7a8fc772011-03-01 17:18:33 +01001688 if (opts2 & RxVlanTag)
1689 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692}
1693
Francois Romieuccdffb92008-07-26 14:26:06 +02001694static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
1696 struct rtl8169_private *tp = netdev_priv(dev);
1697 void __iomem *ioaddr = tp->mmio_addr;
1698 u32 status;
1699
1700 cmd->supported =
1701 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1702 cmd->port = PORT_FIBRE;
1703 cmd->transceiver = XCVR_INTERNAL;
1704
1705 status = RTL_R32(TBICSR);
1706 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1707 cmd->autoneg = !!(status & TBINwEnable);
1708
David Decotigny70739492011-04-27 18:32:40 +00001709 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001711
1712 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713}
1714
Francois Romieuccdffb92008-07-26 14:26:06 +02001715static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
1717 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Francois Romieuccdffb92008-07-26 14:26:06 +02001719 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}
1721
1722static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1723{
1724 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001725 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Francois Romieuda78dbf2012-01-26 14:18:23 +01001727 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02001728 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001729 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Francois Romieuccdffb92008-07-26 14:26:06 +02001731 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
1733
1734static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1735 void *p)
1736{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001737 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Francois Romieu5b0384f2006-08-16 16:00:01 +02001739 if (regs->len > R8169_REGS_SIZE)
1740 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Francois Romieuda78dbf2012-01-26 14:18:23 +01001742 rtl_lock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001743 memcpy_fromio(p, tp->mmio_addr, regs->len);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001744 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001747static u32 rtl8169_get_msglevel(struct net_device *dev)
1748{
1749 struct rtl8169_private *tp = netdev_priv(dev);
1750
1751 return tp->msg_enable;
1752}
1753
1754static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1755{
1756 struct rtl8169_private *tp = netdev_priv(dev);
1757
1758 tp->msg_enable = value;
1759}
1760
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001761static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1762 "tx_packets",
1763 "rx_packets",
1764 "tx_errors",
1765 "rx_errors",
1766 "rx_missed",
1767 "align_errors",
1768 "tx_single_collisions",
1769 "tx_multi_collisions",
1770 "unicast",
1771 "broadcast",
1772 "multicast",
1773 "tx_aborted",
1774 "tx_underrun",
1775};
1776
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001777static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001778{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001779 switch (sset) {
1780 case ETH_SS_STATS:
1781 return ARRAY_SIZE(rtl8169_gstrings);
1782 default:
1783 return -EOPNOTSUPP;
1784 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001785}
1786
Ivan Vecera355423d2009-02-06 21:49:57 -08001787static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001788{
1789 struct rtl8169_private *tp = netdev_priv(dev);
1790 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001791 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001792 struct rtl8169_counters *counters;
1793 dma_addr_t paddr;
1794 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001795 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001796
Ivan Vecera355423d2009-02-06 21:49:57 -08001797 /*
1798 * Some chips are unable to dump tally counters when the receiver
1799 * is disabled.
1800 */
1801 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1802 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001803
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001804 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001805 if (!counters)
1806 return;
1807
1808 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001809 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001810 RTL_W32(CounterAddrLow, cmd);
1811 RTL_W32(CounterAddrLow, cmd | CounterDump);
1812
Ivan Vecera355423d2009-02-06 21:49:57 -08001813 while (wait--) {
1814 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001815 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001816 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001817 }
1818 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001819 }
1820
1821 RTL_W32(CounterAddrLow, 0);
1822 RTL_W32(CounterAddrHigh, 0);
1823
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001824 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001825}
1826
Ivan Vecera355423d2009-02-06 21:49:57 -08001827static void rtl8169_get_ethtool_stats(struct net_device *dev,
1828 struct ethtool_stats *stats, u64 *data)
1829{
1830 struct rtl8169_private *tp = netdev_priv(dev);
1831
1832 ASSERT_RTNL();
1833
1834 rtl8169_update_counters(dev);
1835
1836 data[0] = le64_to_cpu(tp->counters.tx_packets);
1837 data[1] = le64_to_cpu(tp->counters.rx_packets);
1838 data[2] = le64_to_cpu(tp->counters.tx_errors);
1839 data[3] = le32_to_cpu(tp->counters.rx_errors);
1840 data[4] = le16_to_cpu(tp->counters.rx_missed);
1841 data[5] = le16_to_cpu(tp->counters.align_errors);
1842 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1843 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1844 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1845 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1846 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1847 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1848 data[12] = le16_to_cpu(tp->counters.tx_underun);
1849}
1850
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001851static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1852{
1853 switch(stringset) {
1854 case ETH_SS_STATS:
1855 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1856 break;
1857 }
1858}
1859
Jeff Garzik7282d492006-09-13 14:30:00 -04001860static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 .get_drvinfo = rtl8169_get_drvinfo,
1862 .get_regs_len = rtl8169_get_regs_len,
1863 .get_link = ethtool_op_get_link,
1864 .get_settings = rtl8169_get_settings,
1865 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001866 .get_msglevel = rtl8169_get_msglevel,
1867 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001869 .get_wol = rtl8169_get_wol,
1870 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001871 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001872 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001873 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00001874 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875};
1876
Francois Romieu07d3f512007-02-21 22:40:46 +01001877static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001878 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Francois Romieu5d320a22011-05-08 17:47:36 +02001880 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001881 /*
1882 * The driver currently handles the 8168Bf and the 8168Be identically
1883 * but they can be identified more specifically through the test below
1884 * if needed:
1885 *
1886 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001887 *
1888 * Same thing for the 8101Eb and the 8101Ec:
1889 *
1890 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001891 */
Francois Romieu37441002011-06-17 22:58:54 +02001892 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001894 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 int mac_version;
1896 } mac_info[] = {
Hayes Wangc2218922011-09-06 16:55:18 +08001897 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001898 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08001899 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1900 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1901
hayeswang01dc7fe2011-03-21 01:50:28 +00001902 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001903 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001904 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1905 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1906 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1907
Francois Romieu5b538df2008-07-20 16:22:45 +02001908 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001909 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1910 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001911 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001912
françois romieue6de30d2011-01-03 15:08:37 +00001913 /* 8168DP family. */
1914 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1915 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001916 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001917
Francois Romieuef808d52008-06-29 13:10:54 +02001918 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001919 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001920 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001921 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001922 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001923 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1924 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001925 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001926 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001927 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001928
1929 /* 8168B family. */
1930 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1931 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1932 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1933 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1934
1935 /* 8101 family. */
Hayes Wang7e18dca2012-03-30 14:33:02 +08001936 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00001937 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001938 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1939 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1940 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001941 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1942 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1943 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1944 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1945 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1946 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001947 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001948 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001949 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001950 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1951 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001952 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1953 /* FIXME: where did these entries come from ? -- FR */
1954 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1955 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1956
1957 /* 8110 family. */
1958 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1959 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1960 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1961 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1962 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1963 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1964
Jean Delvaref21b75e2009-05-26 20:54:48 -07001965 /* Catch-all */
1966 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001967 };
1968 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 u32 reg;
1970
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001971 reg = RTL_R32(TxConfig);
1972 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 p++;
1974 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001975
1976 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1977 netif_notice(tp, probe, dev,
1978 "unknown MAC, using family default\n");
1979 tp->mac_version = default_version;
1980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
1983static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1984{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001985 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986}
1987
Francois Romieu867763c2007-08-17 18:21:58 +02001988struct phy_reg {
1989 u16 reg;
1990 u16 val;
1991};
1992
françois romieu4da19632011-01-03 15:07:55 +00001993static void rtl_writephy_batch(struct rtl8169_private *tp,
1994 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001995{
1996 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001997 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001998 regs++;
1999 }
2000}
2001
françois romieubca03d52011-01-03 15:07:31 +00002002#define PHY_READ 0x00000000
2003#define PHY_DATA_OR 0x10000000
2004#define PHY_DATA_AND 0x20000000
2005#define PHY_BJMPN 0x30000000
2006#define PHY_READ_EFUSE 0x40000000
2007#define PHY_READ_MAC_BYTE 0x50000000
2008#define PHY_WRITE_MAC_BYTE 0x60000000
2009#define PHY_CLEAR_READCOUNT 0x70000000
2010#define PHY_WRITE 0x80000000
2011#define PHY_READCOUNT_EQ_SKIP 0x90000000
2012#define PHY_COMP_EQ_SKIPN 0xa0000000
2013#define PHY_COMP_NEQ_SKIPN 0xb0000000
2014#define PHY_WRITE_PREVIOUS 0xc0000000
2015#define PHY_SKIPN 0xd0000000
2016#define PHY_DELAY_MS 0xe0000000
2017#define PHY_WRITE_ERI_WORD 0xf0000000
2018
Hayes Wang960aee62011-06-18 11:37:48 +02002019struct fw_info {
2020 u32 magic;
2021 char version[RTL_VER_SIZE];
2022 __le32 fw_start;
2023 __le32 fw_len;
2024 u8 chksum;
2025} __packed;
2026
Francois Romieu1c361ef2011-06-17 17:16:24 +02002027#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2028
2029static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002030{
Francois Romieub6ffd972011-06-17 17:00:05 +02002031 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002032 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002033 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2034 char *version = rtl_fw->version;
2035 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002036
Francois Romieu1c361ef2011-06-17 17:16:24 +02002037 if (fw->size < FW_OPCODE_SIZE)
2038 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002039
2040 if (!fw_info->magic) {
2041 size_t i, size, start;
2042 u8 checksum = 0;
2043
2044 if (fw->size < sizeof(*fw_info))
2045 goto out;
2046
2047 for (i = 0; i < fw->size; i++)
2048 checksum += fw->data[i];
2049 if (checksum != 0)
2050 goto out;
2051
2052 start = le32_to_cpu(fw_info->fw_start);
2053 if (start > fw->size)
2054 goto out;
2055
2056 size = le32_to_cpu(fw_info->fw_len);
2057 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2058 goto out;
2059
2060 memcpy(version, fw_info->version, RTL_VER_SIZE);
2061
2062 pa->code = (__le32 *)(fw->data + start);
2063 pa->size = size;
2064 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002065 if (fw->size % FW_OPCODE_SIZE)
2066 goto out;
2067
2068 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2069
2070 pa->code = (__le32 *)fw->data;
2071 pa->size = fw->size / FW_OPCODE_SIZE;
2072 }
2073 version[RTL_VER_SIZE - 1] = 0;
2074
2075 rc = true;
2076out:
2077 return rc;
2078}
2079
Francois Romieufd112f22011-06-18 00:10:29 +02002080static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2081 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002082{
Francois Romieufd112f22011-06-18 00:10:29 +02002083 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002084 size_t index;
2085
Francois Romieu1c361ef2011-06-17 17:16:24 +02002086 for (index = 0; index < pa->size; index++) {
2087 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002088 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002089
hayeswang42b82dc2011-01-10 02:07:25 +00002090 switch(action & 0xf0000000) {
2091 case PHY_READ:
2092 case PHY_DATA_OR:
2093 case PHY_DATA_AND:
2094 case PHY_READ_EFUSE:
2095 case PHY_CLEAR_READCOUNT:
2096 case PHY_WRITE:
2097 case PHY_WRITE_PREVIOUS:
2098 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002099 break;
2100
hayeswang42b82dc2011-01-10 02:07:25 +00002101 case PHY_BJMPN:
2102 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002103 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002104 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002105 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002106 }
2107 break;
2108 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002109 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002110 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002111 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002112 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002113 }
2114 break;
2115 case PHY_COMP_EQ_SKIPN:
2116 case PHY_COMP_NEQ_SKIPN:
2117 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002118 if (index + 1 + regno >= pa->size) {
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
2125 case PHY_READ_MAC_BYTE:
2126 case PHY_WRITE_MAC_BYTE:
2127 case PHY_WRITE_ERI_WORD:
2128 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002129 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002130 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002131 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002132 }
2133 }
Francois Romieufd112f22011-06-18 00:10:29 +02002134 rc = true;
2135out:
2136 return rc;
2137}
françois romieubca03d52011-01-03 15:07:31 +00002138
Francois Romieufd112f22011-06-18 00:10:29 +02002139static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2140{
2141 struct net_device *dev = tp->dev;
2142 int rc = -EINVAL;
2143
2144 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2145 netif_err(tp, ifup, dev, "invalid firwmare\n");
2146 goto out;
2147 }
2148
2149 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2150 rc = 0;
2151out:
2152 return rc;
2153}
2154
2155static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2156{
2157 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2158 u32 predata, count;
2159 size_t index;
2160
2161 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002162
Francois Romieu1c361ef2011-06-17 17:16:24 +02002163 for (index = 0; index < pa->size; ) {
2164 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002165 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002166 u32 regno = (action & 0x0fff0000) >> 16;
2167
2168 if (!action)
2169 break;
françois romieubca03d52011-01-03 15:07:31 +00002170
2171 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002172 case PHY_READ:
2173 predata = rtl_readphy(tp, regno);
2174 count++;
2175 index++;
françois romieubca03d52011-01-03 15:07:31 +00002176 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002177 case PHY_DATA_OR:
2178 predata |= data;
2179 index++;
2180 break;
2181 case PHY_DATA_AND:
2182 predata &= data;
2183 index++;
2184 break;
2185 case PHY_BJMPN:
2186 index -= regno;
2187 break;
2188 case PHY_READ_EFUSE:
2189 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2190 index++;
2191 break;
2192 case PHY_CLEAR_READCOUNT:
2193 count = 0;
2194 index++;
2195 break;
2196 case PHY_WRITE:
2197 rtl_writephy(tp, regno, data);
2198 index++;
2199 break;
2200 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002201 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002202 break;
2203 case PHY_COMP_EQ_SKIPN:
2204 if (predata == data)
2205 index += regno;
2206 index++;
2207 break;
2208 case PHY_COMP_NEQ_SKIPN:
2209 if (predata != data)
2210 index += regno;
2211 index++;
2212 break;
2213 case PHY_WRITE_PREVIOUS:
2214 rtl_writephy(tp, regno, predata);
2215 index++;
2216 break;
2217 case PHY_SKIPN:
2218 index += regno + 1;
2219 break;
2220 case PHY_DELAY_MS:
2221 mdelay(data);
2222 index++;
2223 break;
2224
2225 case PHY_READ_MAC_BYTE:
2226 case PHY_WRITE_MAC_BYTE:
2227 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002228 default:
2229 BUG();
2230 }
2231 }
2232}
2233
françois romieuf1e02ed2011-01-13 13:07:53 +00002234static void rtl_release_firmware(struct rtl8169_private *tp)
2235{
Francois Romieub6ffd972011-06-17 17:00:05 +02002236 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2237 release_firmware(tp->rtl_fw->fw);
2238 kfree(tp->rtl_fw);
2239 }
2240 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002241}
2242
François Romieu953a12c2011-04-24 17:38:48 +02002243static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002244{
Francois Romieub6ffd972011-06-17 17:00:05 +02002245 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002246
2247 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002248 if (!IS_ERR_OR_NULL(rtl_fw))
2249 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002250}
2251
2252static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2253{
2254 if (rtl_readphy(tp, reg) != val)
2255 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2256 else
2257 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002258}
2259
françois romieu4da19632011-01-03 15:07:55 +00002260static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002262 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002263 { 0x1f, 0x0001 },
2264 { 0x06, 0x006e },
2265 { 0x08, 0x0708 },
2266 { 0x15, 0x4000 },
2267 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
françois romieu0b9b5712009-08-10 19:44:56 +00002269 { 0x1f, 0x0001 },
2270 { 0x03, 0x00a1 },
2271 { 0x02, 0x0008 },
2272 { 0x01, 0x0120 },
2273 { 0x00, 0x1000 },
2274 { 0x04, 0x0800 },
2275 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
françois romieu0b9b5712009-08-10 19:44:56 +00002277 { 0x03, 0xff41 },
2278 { 0x02, 0xdf60 },
2279 { 0x01, 0x0140 },
2280 { 0x00, 0x0077 },
2281 { 0x04, 0x7800 },
2282 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
françois romieu0b9b5712009-08-10 19:44:56 +00002284 { 0x03, 0x802f },
2285 { 0x02, 0x4f02 },
2286 { 0x01, 0x0409 },
2287 { 0x00, 0xf0f9 },
2288 { 0x04, 0x9800 },
2289 { 0x04, 0x9000 },
2290
2291 { 0x03, 0xdf01 },
2292 { 0x02, 0xdf20 },
2293 { 0x01, 0xff95 },
2294 { 0x00, 0xba00 },
2295 { 0x04, 0xa800 },
2296 { 0x04, 0xa000 },
2297
2298 { 0x03, 0xff41 },
2299 { 0x02, 0xdf20 },
2300 { 0x01, 0x0140 },
2301 { 0x00, 0x00bb },
2302 { 0x04, 0xb800 },
2303 { 0x04, 0xb000 },
2304
2305 { 0x03, 0xdf41 },
2306 { 0x02, 0xdc60 },
2307 { 0x01, 0x6340 },
2308 { 0x00, 0x007d },
2309 { 0x04, 0xd800 },
2310 { 0x04, 0xd000 },
2311
2312 { 0x03, 0xdf01 },
2313 { 0x02, 0xdf20 },
2314 { 0x01, 0x100a },
2315 { 0x00, 0xa0ff },
2316 { 0x04, 0xf800 },
2317 { 0x04, 0xf000 },
2318
2319 { 0x1f, 0x0000 },
2320 { 0x0b, 0x0000 },
2321 { 0x00, 0x9200 }
2322 };
2323
françois romieu4da19632011-01-03 15:07:55 +00002324 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}
2326
françois romieu4da19632011-01-03 15:07:55 +00002327static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002328{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002329 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002330 { 0x1f, 0x0002 },
2331 { 0x01, 0x90d0 },
2332 { 0x1f, 0x0000 }
2333 };
2334
françois romieu4da19632011-01-03 15:07:55 +00002335 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002336}
2337
françois romieu4da19632011-01-03 15:07:55 +00002338static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002339{
2340 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002341
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002342 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2343 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002344 return;
2345
françois romieu4da19632011-01-03 15:07:55 +00002346 rtl_writephy(tp, 0x1f, 0x0001);
2347 rtl_writephy(tp, 0x10, 0xf01b);
2348 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002349}
2350
françois romieu4da19632011-01-03 15:07:55 +00002351static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002352{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002353 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002354 { 0x1f, 0x0001 },
2355 { 0x04, 0x0000 },
2356 { 0x03, 0x00a1 },
2357 { 0x02, 0x0008 },
2358 { 0x01, 0x0120 },
2359 { 0x00, 0x1000 },
2360 { 0x04, 0x0800 },
2361 { 0x04, 0x9000 },
2362 { 0x03, 0x802f },
2363 { 0x02, 0x4f02 },
2364 { 0x01, 0x0409 },
2365 { 0x00, 0xf099 },
2366 { 0x04, 0x9800 },
2367 { 0x04, 0xa000 },
2368 { 0x03, 0xdf01 },
2369 { 0x02, 0xdf20 },
2370 { 0x01, 0xff95 },
2371 { 0x00, 0xba00 },
2372 { 0x04, 0xa800 },
2373 { 0x04, 0xf000 },
2374 { 0x03, 0xdf01 },
2375 { 0x02, 0xdf20 },
2376 { 0x01, 0x101a },
2377 { 0x00, 0xa0ff },
2378 { 0x04, 0xf800 },
2379 { 0x04, 0x0000 },
2380 { 0x1f, 0x0000 },
2381
2382 { 0x1f, 0x0001 },
2383 { 0x10, 0xf41b },
2384 { 0x14, 0xfb54 },
2385 { 0x18, 0xf5c7 },
2386 { 0x1f, 0x0000 },
2387
2388 { 0x1f, 0x0001 },
2389 { 0x17, 0x0cc0 },
2390 { 0x1f, 0x0000 }
2391 };
2392
françois romieu4da19632011-01-03 15:07:55 +00002393 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002394
françois romieu4da19632011-01-03 15:07:55 +00002395 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002396}
2397
françois romieu4da19632011-01-03 15:07:55 +00002398static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002399{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002400 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002401 { 0x1f, 0x0001 },
2402 { 0x04, 0x0000 },
2403 { 0x03, 0x00a1 },
2404 { 0x02, 0x0008 },
2405 { 0x01, 0x0120 },
2406 { 0x00, 0x1000 },
2407 { 0x04, 0x0800 },
2408 { 0x04, 0x9000 },
2409 { 0x03, 0x802f },
2410 { 0x02, 0x4f02 },
2411 { 0x01, 0x0409 },
2412 { 0x00, 0xf099 },
2413 { 0x04, 0x9800 },
2414 { 0x04, 0xa000 },
2415 { 0x03, 0xdf01 },
2416 { 0x02, 0xdf20 },
2417 { 0x01, 0xff95 },
2418 { 0x00, 0xba00 },
2419 { 0x04, 0xa800 },
2420 { 0x04, 0xf000 },
2421 { 0x03, 0xdf01 },
2422 { 0x02, 0xdf20 },
2423 { 0x01, 0x101a },
2424 { 0x00, 0xa0ff },
2425 { 0x04, 0xf800 },
2426 { 0x04, 0x0000 },
2427 { 0x1f, 0x0000 },
2428
2429 { 0x1f, 0x0001 },
2430 { 0x0b, 0x8480 },
2431 { 0x1f, 0x0000 },
2432
2433 { 0x1f, 0x0001 },
2434 { 0x18, 0x67c7 },
2435 { 0x04, 0x2000 },
2436 { 0x03, 0x002f },
2437 { 0x02, 0x4360 },
2438 { 0x01, 0x0109 },
2439 { 0x00, 0x3022 },
2440 { 0x04, 0x2800 },
2441 { 0x1f, 0x0000 },
2442
2443 { 0x1f, 0x0001 },
2444 { 0x17, 0x0cc0 },
2445 { 0x1f, 0x0000 }
2446 };
2447
françois romieu4da19632011-01-03 15:07:55 +00002448 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002449}
2450
françois romieu4da19632011-01-03 15:07:55 +00002451static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002452{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002453 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002454 { 0x10, 0xf41b },
2455 { 0x1f, 0x0000 }
2456 };
2457
françois romieu4da19632011-01-03 15:07:55 +00002458 rtl_writephy(tp, 0x1f, 0x0001);
2459 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002460
françois romieu4da19632011-01-03 15:07:55 +00002461 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002462}
2463
françois romieu4da19632011-01-03 15:07:55 +00002464static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002465{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002466 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002467 { 0x1f, 0x0001 },
2468 { 0x10, 0xf41b },
2469 { 0x1f, 0x0000 }
2470 };
2471
françois romieu4da19632011-01-03 15:07:55 +00002472 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002473}
2474
françois romieu4da19632011-01-03 15:07:55 +00002475static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002476{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002477 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002478 { 0x1f, 0x0000 },
2479 { 0x1d, 0x0f00 },
2480 { 0x1f, 0x0002 },
2481 { 0x0c, 0x1ec8 },
2482 { 0x1f, 0x0000 }
2483 };
2484
françois romieu4da19632011-01-03 15:07:55 +00002485 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002486}
2487
françois romieu4da19632011-01-03 15:07:55 +00002488static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002489{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002490 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002491 { 0x1f, 0x0001 },
2492 { 0x1d, 0x3d98 },
2493 { 0x1f, 0x0000 }
2494 };
2495
françois romieu4da19632011-01-03 15:07:55 +00002496 rtl_writephy(tp, 0x1f, 0x0000);
2497 rtl_patchphy(tp, 0x14, 1 << 5);
2498 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002499
françois romieu4da19632011-01-03 15:07:55 +00002500 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002501}
2502
françois romieu4da19632011-01-03 15:07:55 +00002503static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002504{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002505 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002506 { 0x1f, 0x0001 },
2507 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002508 { 0x1f, 0x0002 },
2509 { 0x00, 0x88d4 },
2510 { 0x01, 0x82b1 },
2511 { 0x03, 0x7002 },
2512 { 0x08, 0x9e30 },
2513 { 0x09, 0x01f0 },
2514 { 0x0a, 0x5500 },
2515 { 0x0c, 0x00c8 },
2516 { 0x1f, 0x0003 },
2517 { 0x12, 0xc096 },
2518 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002519 { 0x1f, 0x0000 },
2520 { 0x1f, 0x0000 },
2521 { 0x09, 0x2000 },
2522 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002523 };
2524
françois romieu4da19632011-01-03 15:07:55 +00002525 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002526
françois romieu4da19632011-01-03 15:07:55 +00002527 rtl_patchphy(tp, 0x14, 1 << 5);
2528 rtl_patchphy(tp, 0x0d, 1 << 5);
2529 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002530}
2531
françois romieu4da19632011-01-03 15:07:55 +00002532static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002533{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002534 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002535 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002536 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002537 { 0x03, 0x802f },
2538 { 0x02, 0x4f02 },
2539 { 0x01, 0x0409 },
2540 { 0x00, 0xf099 },
2541 { 0x04, 0x9800 },
2542 { 0x04, 0x9000 },
2543 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002544 { 0x1f, 0x0002 },
2545 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002546 { 0x06, 0x0761 },
2547 { 0x1f, 0x0003 },
2548 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002549 { 0x1f, 0x0000 }
2550 };
2551
françois romieu4da19632011-01-03 15:07:55 +00002552 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002553
françois romieu4da19632011-01-03 15:07:55 +00002554 rtl_patchphy(tp, 0x16, 1 << 0);
2555 rtl_patchphy(tp, 0x14, 1 << 5);
2556 rtl_patchphy(tp, 0x0d, 1 << 5);
2557 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002558}
2559
françois romieu4da19632011-01-03 15:07:55 +00002560static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002561{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002562 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002563 { 0x1f, 0x0001 },
2564 { 0x12, 0x2300 },
2565 { 0x1d, 0x3d98 },
2566 { 0x1f, 0x0002 },
2567 { 0x0c, 0x7eb8 },
2568 { 0x06, 0x5461 },
2569 { 0x1f, 0x0003 },
2570 { 0x16, 0x0f0a },
2571 { 0x1f, 0x0000 }
2572 };
2573
françois romieu4da19632011-01-03 15:07:55 +00002574 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002575
françois romieu4da19632011-01-03 15:07:55 +00002576 rtl_patchphy(tp, 0x16, 1 << 0);
2577 rtl_patchphy(tp, 0x14, 1 << 5);
2578 rtl_patchphy(tp, 0x0d, 1 << 5);
2579 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002580}
2581
françois romieu4da19632011-01-03 15:07:55 +00002582static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002583{
françois romieu4da19632011-01-03 15:07:55 +00002584 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002585}
2586
françois romieubca03d52011-01-03 15:07:31 +00002587static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002588{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002589 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002590 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002591 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002592 { 0x06, 0x4064 },
2593 { 0x07, 0x2863 },
2594 { 0x08, 0x059c },
2595 { 0x09, 0x26b4 },
2596 { 0x0a, 0x6a19 },
2597 { 0x0b, 0xdcc8 },
2598 { 0x10, 0xf06d },
2599 { 0x14, 0x7f68 },
2600 { 0x18, 0x7fd9 },
2601 { 0x1c, 0xf0ff },
2602 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002603 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002604 { 0x12, 0xf49f },
2605 { 0x13, 0x070b },
2606 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002607 { 0x14, 0x94c0 },
2608
2609 /*
2610 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002611 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002612 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002613 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002614 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002615 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002616 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002617 { 0x06, 0x5561 },
2618
2619 /*
2620 * Can not link to 1Gbps with bad cable
2621 * Decrease SNR threshold form 21.07dB to 19.04dB
2622 */
2623 { 0x1f, 0x0001 },
2624 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002625
2626 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002627 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002628 };
françois romieubca03d52011-01-03 15:07:31 +00002629 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002630
françois romieu4da19632011-01-03 15:07:55 +00002631 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002632
françois romieubca03d52011-01-03 15:07:31 +00002633 /*
2634 * Rx Error Issue
2635 * Fine Tune Switching regulator parameter
2636 */
françois romieu4da19632011-01-03 15:07:55 +00002637 rtl_writephy(tp, 0x1f, 0x0002);
2638 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2639 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002640
françois romieudaf9df62009-10-07 12:44:20 +00002641 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002642 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002643 { 0x1f, 0x0002 },
2644 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002645 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002646 { 0x05, 0x8330 },
2647 { 0x06, 0x669a },
2648 { 0x1f, 0x0002 }
2649 };
2650 int val;
2651
françois romieu4da19632011-01-03 15:07:55 +00002652 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002653
françois romieu4da19632011-01-03 15:07:55 +00002654 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002655
2656 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002657 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002658 0x0065, 0x0066, 0x0067, 0x0068,
2659 0x0069, 0x006a, 0x006b, 0x006c
2660 };
2661 int i;
2662
françois romieu4da19632011-01-03 15:07:55 +00002663 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002664
2665 val &= 0xff00;
2666 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002667 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002668 }
2669 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002670 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002671 { 0x1f, 0x0002 },
2672 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002673 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002674 { 0x05, 0x8330 },
2675 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002676 };
2677
françois romieu4da19632011-01-03 15:07:55 +00002678 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002679 }
2680
françois romieubca03d52011-01-03 15:07:31 +00002681 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002682 rtl_writephy(tp, 0x1f, 0x0002);
2683 rtl_patchphy(tp, 0x0d, 0x0300);
2684 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002685
françois romieubca03d52011-01-03 15:07:31 +00002686 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002687 rtl_writephy(tp, 0x1f, 0x0002);
2688 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2689 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002690
françois romieu4da19632011-01-03 15:07:55 +00002691 rtl_writephy(tp, 0x1f, 0x0005);
2692 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002693
2694 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002695
françois romieu4da19632011-01-03 15:07:55 +00002696 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002697}
2698
françois romieubca03d52011-01-03 15:07:31 +00002699static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002700{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002701 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002702 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002703 { 0x1f, 0x0001 },
2704 { 0x06, 0x4064 },
2705 { 0x07, 0x2863 },
2706 { 0x08, 0x059c },
2707 { 0x09, 0x26b4 },
2708 { 0x0a, 0x6a19 },
2709 { 0x0b, 0xdcc8 },
2710 { 0x10, 0xf06d },
2711 { 0x14, 0x7f68 },
2712 { 0x18, 0x7fd9 },
2713 { 0x1c, 0xf0ff },
2714 { 0x1d, 0x3d9c },
2715 { 0x1f, 0x0003 },
2716 { 0x12, 0xf49f },
2717 { 0x13, 0x070b },
2718 { 0x1a, 0x05ad },
2719 { 0x14, 0x94c0 },
2720
françois romieubca03d52011-01-03 15:07:31 +00002721 /*
2722 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002723 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002724 */
françois romieudaf9df62009-10-07 12:44:20 +00002725 { 0x1f, 0x0002 },
2726 { 0x06, 0x5561 },
2727 { 0x1f, 0x0005 },
2728 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002729 { 0x06, 0x5561 },
2730
2731 /*
2732 * Can not link to 1Gbps with bad cable
2733 * Decrease SNR threshold form 21.07dB to 19.04dB
2734 */
2735 { 0x1f, 0x0001 },
2736 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002737
2738 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002739 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002740 };
françois romieubca03d52011-01-03 15:07:31 +00002741 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002742
françois romieu4da19632011-01-03 15:07:55 +00002743 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002744
2745 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002746 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002747 { 0x1f, 0x0002 },
2748 { 0x05, 0x669a },
2749 { 0x1f, 0x0005 },
2750 { 0x05, 0x8330 },
2751 { 0x06, 0x669a },
2752
2753 { 0x1f, 0x0002 }
2754 };
2755 int val;
2756
françois romieu4da19632011-01-03 15:07:55 +00002757 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002758
françois romieu4da19632011-01-03 15:07:55 +00002759 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002760 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002761 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002762 0x0065, 0x0066, 0x0067, 0x0068,
2763 0x0069, 0x006a, 0x006b, 0x006c
2764 };
2765 int i;
2766
françois romieu4da19632011-01-03 15:07:55 +00002767 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002768
2769 val &= 0xff00;
2770 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002771 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002772 }
2773 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002774 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002775 { 0x1f, 0x0002 },
2776 { 0x05, 0x2642 },
2777 { 0x1f, 0x0005 },
2778 { 0x05, 0x8330 },
2779 { 0x06, 0x2642 }
2780 };
2781
françois romieu4da19632011-01-03 15:07:55 +00002782 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002783 }
2784
françois romieubca03d52011-01-03 15:07:31 +00002785 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002786 rtl_writephy(tp, 0x1f, 0x0002);
2787 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2788 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002789
françois romieubca03d52011-01-03 15:07:31 +00002790 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002791 rtl_writephy(tp, 0x1f, 0x0002);
2792 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002793
françois romieu4da19632011-01-03 15:07:55 +00002794 rtl_writephy(tp, 0x1f, 0x0005);
2795 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002796
2797 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002798
françois romieu4da19632011-01-03 15:07:55 +00002799 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002800}
2801
françois romieu4da19632011-01-03 15:07:55 +00002802static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002803{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002804 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002805 { 0x1f, 0x0002 },
2806 { 0x10, 0x0008 },
2807 { 0x0d, 0x006c },
2808
2809 { 0x1f, 0x0000 },
2810 { 0x0d, 0xf880 },
2811
2812 { 0x1f, 0x0001 },
2813 { 0x17, 0x0cc0 },
2814
2815 { 0x1f, 0x0001 },
2816 { 0x0b, 0xa4d8 },
2817 { 0x09, 0x281c },
2818 { 0x07, 0x2883 },
2819 { 0x0a, 0x6b35 },
2820 { 0x1d, 0x3da4 },
2821 { 0x1c, 0xeffd },
2822 { 0x14, 0x7f52 },
2823 { 0x18, 0x7fc6 },
2824 { 0x08, 0x0601 },
2825 { 0x06, 0x4063 },
2826 { 0x10, 0xf074 },
2827 { 0x1f, 0x0003 },
2828 { 0x13, 0x0789 },
2829 { 0x12, 0xf4bd },
2830 { 0x1a, 0x04fd },
2831 { 0x14, 0x84b0 },
2832 { 0x1f, 0x0000 },
2833 { 0x00, 0x9200 },
2834
2835 { 0x1f, 0x0005 },
2836 { 0x01, 0x0340 },
2837 { 0x1f, 0x0001 },
2838 { 0x04, 0x4000 },
2839 { 0x03, 0x1d21 },
2840 { 0x02, 0x0c32 },
2841 { 0x01, 0x0200 },
2842 { 0x00, 0x5554 },
2843 { 0x04, 0x4800 },
2844 { 0x04, 0x4000 },
2845 { 0x04, 0xf000 },
2846 { 0x03, 0xdf01 },
2847 { 0x02, 0xdf20 },
2848 { 0x01, 0x101a },
2849 { 0x00, 0xa0ff },
2850 { 0x04, 0xf800 },
2851 { 0x04, 0xf000 },
2852 { 0x1f, 0x0000 },
2853
2854 { 0x1f, 0x0007 },
2855 { 0x1e, 0x0023 },
2856 { 0x16, 0x0000 },
2857 { 0x1f, 0x0000 }
2858 };
2859
françois romieu4da19632011-01-03 15:07:55 +00002860 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002861}
2862
françois romieue6de30d2011-01-03 15:08:37 +00002863static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2864{
2865 static const struct phy_reg phy_reg_init[] = {
2866 { 0x1f, 0x0001 },
2867 { 0x17, 0x0cc0 },
2868
2869 { 0x1f, 0x0007 },
2870 { 0x1e, 0x002d },
2871 { 0x18, 0x0040 },
2872 { 0x1f, 0x0000 }
2873 };
2874
2875 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2876 rtl_patchphy(tp, 0x0d, 1 << 5);
2877}
2878
Hayes Wang70090422011-07-06 15:58:06 +08002879static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002880{
2881 static const struct phy_reg phy_reg_init[] = {
2882 /* Enable Delay cap */
2883 { 0x1f, 0x0005 },
2884 { 0x05, 0x8b80 },
2885 { 0x06, 0xc896 },
2886 { 0x1f, 0x0000 },
2887
2888 /* Channel estimation fine tune */
2889 { 0x1f, 0x0001 },
2890 { 0x0b, 0x6c20 },
2891 { 0x07, 0x2872 },
2892 { 0x1c, 0xefff },
2893 { 0x1f, 0x0003 },
2894 { 0x14, 0x6420 },
2895 { 0x1f, 0x0000 },
2896
2897 /* Update PFM & 10M TX idle timer */
2898 { 0x1f, 0x0007 },
2899 { 0x1e, 0x002f },
2900 { 0x15, 0x1919 },
2901 { 0x1f, 0x0000 },
2902
2903 { 0x1f, 0x0007 },
2904 { 0x1e, 0x00ac },
2905 { 0x18, 0x0006 },
2906 { 0x1f, 0x0000 }
2907 };
2908
Francois Romieu15ecd032011-04-27 13:52:22 -07002909 rtl_apply_firmware(tp);
2910
hayeswang01dc7fe2011-03-21 01:50:28 +00002911 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2912
2913 /* DCO enable for 10M IDLE Power */
2914 rtl_writephy(tp, 0x1f, 0x0007);
2915 rtl_writephy(tp, 0x1e, 0x0023);
2916 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2917 rtl_writephy(tp, 0x1f, 0x0000);
2918
2919 /* For impedance matching */
2920 rtl_writephy(tp, 0x1f, 0x0002);
2921 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002922 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002923
2924 /* PHY auto speed down */
2925 rtl_writephy(tp, 0x1f, 0x0007);
2926 rtl_writephy(tp, 0x1e, 0x002d);
2927 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2928 rtl_writephy(tp, 0x1f, 0x0000);
2929 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2930
2931 rtl_writephy(tp, 0x1f, 0x0005);
2932 rtl_writephy(tp, 0x05, 0x8b86);
2933 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2934 rtl_writephy(tp, 0x1f, 0x0000);
2935
2936 rtl_writephy(tp, 0x1f, 0x0005);
2937 rtl_writephy(tp, 0x05, 0x8b85);
2938 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2939 rtl_writephy(tp, 0x1f, 0x0007);
2940 rtl_writephy(tp, 0x1e, 0x0020);
2941 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2942 rtl_writephy(tp, 0x1f, 0x0006);
2943 rtl_writephy(tp, 0x00, 0x5a00);
2944 rtl_writephy(tp, 0x1f, 0x0000);
2945 rtl_writephy(tp, 0x0d, 0x0007);
2946 rtl_writephy(tp, 0x0e, 0x003c);
2947 rtl_writephy(tp, 0x0d, 0x4007);
2948 rtl_writephy(tp, 0x0e, 0x0000);
2949 rtl_writephy(tp, 0x0d, 0x0000);
2950}
2951
Hayes Wang70090422011-07-06 15:58:06 +08002952static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2953{
2954 static const struct phy_reg phy_reg_init[] = {
2955 /* Enable Delay cap */
2956 { 0x1f, 0x0004 },
2957 { 0x1f, 0x0007 },
2958 { 0x1e, 0x00ac },
2959 { 0x18, 0x0006 },
2960 { 0x1f, 0x0002 },
2961 { 0x1f, 0x0000 },
2962 { 0x1f, 0x0000 },
2963
2964 /* Channel estimation fine tune */
2965 { 0x1f, 0x0003 },
2966 { 0x09, 0xa20f },
2967 { 0x1f, 0x0000 },
2968 { 0x1f, 0x0000 },
2969
2970 /* Green Setting */
2971 { 0x1f, 0x0005 },
2972 { 0x05, 0x8b5b },
2973 { 0x06, 0x9222 },
2974 { 0x05, 0x8b6d },
2975 { 0x06, 0x8000 },
2976 { 0x05, 0x8b76 },
2977 { 0x06, 0x8000 },
2978 { 0x1f, 0x0000 }
2979 };
2980
2981 rtl_apply_firmware(tp);
2982
2983 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2984
2985 /* For 4-corner performance improve */
2986 rtl_writephy(tp, 0x1f, 0x0005);
2987 rtl_writephy(tp, 0x05, 0x8b80);
2988 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2989 rtl_writephy(tp, 0x1f, 0x0000);
2990
2991 /* PHY auto speed down */
2992 rtl_writephy(tp, 0x1f, 0x0004);
2993 rtl_writephy(tp, 0x1f, 0x0007);
2994 rtl_writephy(tp, 0x1e, 0x002d);
2995 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2996 rtl_writephy(tp, 0x1f, 0x0002);
2997 rtl_writephy(tp, 0x1f, 0x0000);
2998 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2999
3000 /* improve 10M EEE waveform */
3001 rtl_writephy(tp, 0x1f, 0x0005);
3002 rtl_writephy(tp, 0x05, 0x8b86);
3003 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3004 rtl_writephy(tp, 0x1f, 0x0000);
3005
3006 /* Improve 2-pair detection performance */
3007 rtl_writephy(tp, 0x1f, 0x0005);
3008 rtl_writephy(tp, 0x05, 0x8b85);
3009 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3010 rtl_writephy(tp, 0x1f, 0x0000);
3011
3012 /* EEE setting */
3013 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
3014 ERIAR_EXGMAC);
3015 rtl_writephy(tp, 0x1f, 0x0005);
3016 rtl_writephy(tp, 0x05, 0x8b85);
3017 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3018 rtl_writephy(tp, 0x1f, 0x0004);
3019 rtl_writephy(tp, 0x1f, 0x0007);
3020 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04003021 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003022 rtl_writephy(tp, 0x1f, 0x0002);
3023 rtl_writephy(tp, 0x1f, 0x0000);
3024 rtl_writephy(tp, 0x0d, 0x0007);
3025 rtl_writephy(tp, 0x0e, 0x003c);
3026 rtl_writephy(tp, 0x0d, 0x4007);
3027 rtl_writephy(tp, 0x0e, 0x0000);
3028 rtl_writephy(tp, 0x0d, 0x0000);
3029
3030 /* Green feature */
3031 rtl_writephy(tp, 0x1f, 0x0003);
3032 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3033 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3034 rtl_writephy(tp, 0x1f, 0x0000);
3035}
3036
Hayes Wang5f886e02012-03-30 14:33:03 +08003037static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3038{
3039 /* For 4-corner performance improve */
3040 rtl_writephy(tp, 0x1f, 0x0005);
3041 rtl_writephy(tp, 0x05, 0x8b80);
3042 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3043 rtl_writephy(tp, 0x1f, 0x0000);
3044
3045 /* PHY auto speed down */
3046 rtl_writephy(tp, 0x1f, 0x0007);
3047 rtl_writephy(tp, 0x1e, 0x002d);
3048 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3049 rtl_writephy(tp, 0x1f, 0x0000);
3050 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3051
3052 /* Improve 10M EEE waveform */
3053 rtl_writephy(tp, 0x1f, 0x0005);
3054 rtl_writephy(tp, 0x05, 0x8b86);
3055 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3056 rtl_writephy(tp, 0x1f, 0x0000);
3057}
3058
Hayes Wangc2218922011-09-06 16:55:18 +08003059static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3060{
3061 static const struct phy_reg phy_reg_init[] = {
3062 /* Channel estimation fine tune */
3063 { 0x1f, 0x0003 },
3064 { 0x09, 0xa20f },
3065 { 0x1f, 0x0000 },
3066
3067 /* Modify green table for giga & fnet */
3068 { 0x1f, 0x0005 },
3069 { 0x05, 0x8b55 },
3070 { 0x06, 0x0000 },
3071 { 0x05, 0x8b5e },
3072 { 0x06, 0x0000 },
3073 { 0x05, 0x8b67 },
3074 { 0x06, 0x0000 },
3075 { 0x05, 0x8b70 },
3076 { 0x06, 0x0000 },
3077 { 0x1f, 0x0000 },
3078 { 0x1f, 0x0007 },
3079 { 0x1e, 0x0078 },
3080 { 0x17, 0x0000 },
3081 { 0x19, 0x00fb },
3082 { 0x1f, 0x0000 },
3083
3084 /* Modify green table for 10M */
3085 { 0x1f, 0x0005 },
3086 { 0x05, 0x8b79 },
3087 { 0x06, 0xaa00 },
3088 { 0x1f, 0x0000 },
3089
3090 /* Disable hiimpedance detection (RTCT) */
3091 { 0x1f, 0x0003 },
3092 { 0x01, 0x328a },
3093 { 0x1f, 0x0000 }
3094 };
3095
3096 rtl_apply_firmware(tp);
3097
3098 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3099
Hayes Wang5f886e02012-03-30 14:33:03 +08003100 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003101
3102 /* Improve 2-pair detection performance */
3103 rtl_writephy(tp, 0x1f, 0x0005);
3104 rtl_writephy(tp, 0x05, 0x8b85);
3105 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3106 rtl_writephy(tp, 0x1f, 0x0000);
3107}
3108
3109static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3110{
3111 rtl_apply_firmware(tp);
3112
Hayes Wang5f886e02012-03-30 14:33:03 +08003113 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003114}
3115
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003116static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3117{
3118 void __iomem *ioaddr = tp->mmio_addr;
3119 static const struct phy_reg phy_reg_init[] = {
3120 /* Channel estimation fine tune */
3121 { 0x1f, 0x0003 },
3122 { 0x09, 0xa20f },
3123 { 0x1f, 0x0000 },
3124
3125 /* Modify green table for giga & fnet */
3126 { 0x1f, 0x0005 },
3127 { 0x05, 0x8b55 },
3128 { 0x06, 0x0000 },
3129 { 0x05, 0x8b5e },
3130 { 0x06, 0x0000 },
3131 { 0x05, 0x8b67 },
3132 { 0x06, 0x0000 },
3133 { 0x05, 0x8b70 },
3134 { 0x06, 0x0000 },
3135 { 0x1f, 0x0000 },
3136 { 0x1f, 0x0007 },
3137 { 0x1e, 0x0078 },
3138 { 0x17, 0x0000 },
3139 { 0x19, 0x00aa },
3140 { 0x1f, 0x0000 },
3141
3142 /* Modify green table for 10M */
3143 { 0x1f, 0x0005 },
3144 { 0x05, 0x8b79 },
3145 { 0x06, 0xaa00 },
3146 { 0x1f, 0x0000 },
3147
3148 /* Disable hiimpedance detection (RTCT) */
3149 { 0x1f, 0x0003 },
3150 { 0x01, 0x328a },
3151 { 0x1f, 0x0000 }
3152 };
3153
3154
3155 rtl_apply_firmware(tp);
3156
3157 rtl8168f_hw_phy_config(tp);
3158
3159 /* Improve 2-pair detection performance */
3160 rtl_writephy(tp, 0x1f, 0x0005);
3161 rtl_writephy(tp, 0x05, 0x8b85);
3162 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3163 rtl_writephy(tp, 0x1f, 0x0000);
3164
3165 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3166
3167 /* Modify green table for giga */
3168 rtl_writephy(tp, 0x1f, 0x0005);
3169 rtl_writephy(tp, 0x05, 0x8b54);
3170 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3171 rtl_writephy(tp, 0x05, 0x8b5d);
3172 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3173 rtl_writephy(tp, 0x05, 0x8a7c);
3174 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3175 rtl_writephy(tp, 0x05, 0x8a7f);
3176 rtl_w1w0_phy(tp, 0x06, 0x0100, 0x0000);
3177 rtl_writephy(tp, 0x05, 0x8a82);
3178 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3179 rtl_writephy(tp, 0x05, 0x8a85);
3180 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3181 rtl_writephy(tp, 0x05, 0x8a88);
3182 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3183 rtl_writephy(tp, 0x1f, 0x0000);
3184
3185 /* uc same-seed solution */
3186 rtl_writephy(tp, 0x1f, 0x0005);
3187 rtl_writephy(tp, 0x05, 0x8b85);
3188 rtl_w1w0_phy(tp, 0x06, 0x8000, 0x0000);
3189 rtl_writephy(tp, 0x1f, 0x0000);
3190
3191 /* eee setting */
3192 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
3193 rtl_writephy(tp, 0x1f, 0x0005);
3194 rtl_writephy(tp, 0x05, 0x8b85);
3195 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3196 rtl_writephy(tp, 0x1f, 0x0004);
3197 rtl_writephy(tp, 0x1f, 0x0007);
3198 rtl_writephy(tp, 0x1e, 0x0020);
3199 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
3200 rtl_writephy(tp, 0x1f, 0x0000);
3201 rtl_writephy(tp, 0x0d, 0x0007);
3202 rtl_writephy(tp, 0x0e, 0x003c);
3203 rtl_writephy(tp, 0x0d, 0x4007);
3204 rtl_writephy(tp, 0x0e, 0x0000);
3205 rtl_writephy(tp, 0x0d, 0x0000);
3206
3207 /* Green feature */
3208 rtl_writephy(tp, 0x1f, 0x0003);
3209 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3210 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3211 rtl_writephy(tp, 0x1f, 0x0000);
3212}
3213
françois romieu4da19632011-01-03 15:07:55 +00003214static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003215{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003216 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003217 { 0x1f, 0x0003 },
3218 { 0x08, 0x441d },
3219 { 0x01, 0x9100 },
3220 { 0x1f, 0x0000 }
3221 };
3222
françois romieu4da19632011-01-03 15:07:55 +00003223 rtl_writephy(tp, 0x1f, 0x0000);
3224 rtl_patchphy(tp, 0x11, 1 << 12);
3225 rtl_patchphy(tp, 0x19, 1 << 13);
3226 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003227
françois romieu4da19632011-01-03 15:07:55 +00003228 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003229}
3230
Hayes Wang5a5e4442011-02-22 17:26:21 +08003231static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3232{
3233 static const struct phy_reg phy_reg_init[] = {
3234 { 0x1f, 0x0005 },
3235 { 0x1a, 0x0000 },
3236 { 0x1f, 0x0000 },
3237
3238 { 0x1f, 0x0004 },
3239 { 0x1c, 0x0000 },
3240 { 0x1f, 0x0000 },
3241
3242 { 0x1f, 0x0001 },
3243 { 0x15, 0x7701 },
3244 { 0x1f, 0x0000 }
3245 };
3246
3247 /* Disable ALDPS before ram code */
3248 rtl_writephy(tp, 0x1f, 0x0000);
3249 rtl_writephy(tp, 0x18, 0x0310);
3250 msleep(100);
3251
François Romieu953a12c2011-04-24 17:38:48 +02003252 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003253
3254 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3255}
3256
Hayes Wang7e18dca2012-03-30 14:33:02 +08003257static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3258{
3259 void __iomem *ioaddr = tp->mmio_addr;
3260
3261 /* Disable ALDPS before setting firmware */
3262 rtl_writephy(tp, 0x1f, 0x0000);
3263 rtl_writephy(tp, 0x18, 0x0310);
3264 msleep(20);
3265
3266 rtl_apply_firmware(tp);
3267
3268 /* EEE setting */
3269 rtl_eri_write(ioaddr, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3270 rtl_writephy(tp, 0x1f, 0x0004);
3271 rtl_writephy(tp, 0x10, 0x401f);
3272 rtl_writephy(tp, 0x19, 0x7030);
3273 rtl_writephy(tp, 0x1f, 0x0000);
3274}
3275
Francois Romieu5615d9f2007-08-17 17:50:46 +02003276static void rtl_hw_phy_config(struct net_device *dev)
3277{
3278 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003279
3280 rtl8169_print_mac_version(tp);
3281
3282 switch (tp->mac_version) {
3283 case RTL_GIGA_MAC_VER_01:
3284 break;
3285 case RTL_GIGA_MAC_VER_02:
3286 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003287 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003288 break;
3289 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003290 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003291 break;
françois romieu2e9558562009-08-10 19:44:19 +00003292 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003293 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003294 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003295 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003296 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003297 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003298 case RTL_GIGA_MAC_VER_07:
3299 case RTL_GIGA_MAC_VER_08:
3300 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003301 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003302 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003303 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003304 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003305 break;
3306 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003307 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003308 break;
3309 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003310 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003311 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003312 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003313 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003314 break;
3315 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003316 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003317 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003318 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003319 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003320 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003321 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003322 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003323 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003324 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003325 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003326 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003327 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003328 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003329 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003330 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003331 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003332 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003333 break;
3334 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003335 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003336 break;
3337 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003338 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003339 break;
françois romieue6de30d2011-01-03 15:08:37 +00003340 case RTL_GIGA_MAC_VER_28:
3341 rtl8168d_4_hw_phy_config(tp);
3342 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003343 case RTL_GIGA_MAC_VER_29:
3344 case RTL_GIGA_MAC_VER_30:
3345 rtl8105e_hw_phy_config(tp);
3346 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003347 case RTL_GIGA_MAC_VER_31:
3348 /* None. */
3349 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003350 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003351 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003352 rtl8168e_1_hw_phy_config(tp);
3353 break;
3354 case RTL_GIGA_MAC_VER_34:
3355 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003356 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003357 case RTL_GIGA_MAC_VER_35:
3358 rtl8168f_1_hw_phy_config(tp);
3359 break;
3360 case RTL_GIGA_MAC_VER_36:
3361 rtl8168f_2_hw_phy_config(tp);
3362 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003363
Hayes Wang7e18dca2012-03-30 14:33:02 +08003364 case RTL_GIGA_MAC_VER_37:
3365 rtl8402_hw_phy_config(tp);
3366 break;
3367
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003368 case RTL_GIGA_MAC_VER_38:
3369 rtl8411_hw_phy_config(tp);
3370 break;
3371
Francois Romieu5615d9f2007-08-17 17:50:46 +02003372 default:
3373 break;
3374 }
3375}
3376
Francois Romieuda78dbf2012-01-26 14:18:23 +01003377static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 struct timer_list *timer = &tp->timer;
3380 void __iomem *ioaddr = tp->mmio_addr;
3381 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3382
Francois Romieubcf0bf92006-07-26 23:14:13 +02003383 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
françois romieu4da19632011-01-03 15:07:55 +00003385 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003386 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 * A busy loop could burn quite a few cycles on nowadays CPU.
3388 * Let's delay the execution of the timer for a few ticks.
3389 */
3390 timeout = HZ/10;
3391 goto out_mod_timer;
3392 }
3393
3394 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01003395 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
Francois Romieuda78dbf2012-01-26 14:18:23 +01003397 netif_warn(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398
françois romieu4da19632011-01-03 15:07:55 +00003399 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
3401out_mod_timer:
3402 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003403}
3404
3405static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3406{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003407 if (!test_and_set_bit(flag, tp->wk.flags))
3408 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003409}
3410
3411static void rtl8169_phy_timer(unsigned long __opaque)
3412{
3413 struct net_device *dev = (struct net_device *)__opaque;
3414 struct rtl8169_private *tp = netdev_priv(dev);
3415
Francois Romieu98ddf982012-01-31 10:47:34 +01003416 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417}
3418
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3420 void __iomem *ioaddr)
3421{
3422 iounmap(ioaddr);
3423 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003424 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 pci_disable_device(pdev);
3426 free_netdev(dev);
3427}
3428
Francois Romieubf793292006-11-01 00:53:05 +01003429static void rtl8169_phy_reset(struct net_device *dev,
3430 struct rtl8169_private *tp)
3431{
Francois Romieu07d3f512007-02-21 22:40:46 +01003432 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003433
françois romieu4da19632011-01-03 15:07:55 +00003434 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003435 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003436 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003437 return;
3438 msleep(1);
3439 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003440 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003441}
3442
David S. Miller8decf862011-09-22 03:23:13 -04003443static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3444{
3445 void __iomem *ioaddr = tp->mmio_addr;
3446
3447 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3448 (RTL_R8(PHYstatus) & TBI_Enable);
3449}
3450
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003451static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003453 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003454
Francois Romieu5615d9f2007-08-17 17:50:46 +02003455 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003456
Marcus Sundberg773328942008-07-10 21:28:08 +02003457 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3458 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3459 RTL_W8(0x82, 0x01);
3460 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003461
Francois Romieu6dccd162007-02-13 23:38:05 +01003462 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3463
3464 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3465 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003466
Francois Romieubcf0bf92006-07-26 23:14:13 +02003467 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003468 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3469 RTL_W8(0x82, 0x01);
3470 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003471 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003472 }
3473
Francois Romieubf793292006-11-01 00:53:05 +01003474 rtl8169_phy_reset(dev, tp);
3475
Oliver Neukum54405cd2011-01-06 21:55:13 +01003476 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003477 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3478 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3479 (tp->mii.supports_gmii ?
3480 ADVERTISED_1000baseT_Half |
3481 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003482
David S. Miller8decf862011-09-22 03:23:13 -04003483 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003484 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003485}
3486
Francois Romieu773d2022007-01-31 23:47:43 +01003487static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3488{
3489 void __iomem *ioaddr = tp->mmio_addr;
3490 u32 high;
3491 u32 low;
3492
3493 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3494 high = addr[4] | (addr[5] << 8);
3495
Francois Romieuda78dbf2012-01-26 14:18:23 +01003496 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003497
3498 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003499
Francois Romieu773d2022007-01-31 23:47:43 +01003500 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00003501 RTL_R32(MAC4);
3502
Francois Romieu78f1cd02010-03-27 19:35:46 -07003503 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00003504 RTL_R32(MAC0);
3505
françois romieuc28aa382011-08-02 03:53:43 +00003506 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3507 const struct exgmac_reg e[] = {
3508 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3509 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3510 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3511 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3512 low >> 16 },
3513 };
3514
3515 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3516 }
3517
Francois Romieu773d2022007-01-31 23:47:43 +01003518 RTL_W8(Cfg9346, Cfg9346_Lock);
3519
Francois Romieuda78dbf2012-01-26 14:18:23 +01003520 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003521}
3522
3523static int rtl_set_mac_address(struct net_device *dev, void *p)
3524{
3525 struct rtl8169_private *tp = netdev_priv(dev);
3526 struct sockaddr *addr = p;
3527
3528 if (!is_valid_ether_addr(addr->sa_data))
3529 return -EADDRNOTAVAIL;
3530
3531 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3532
3533 rtl_rar_set(tp, dev->dev_addr);
3534
3535 return 0;
3536}
3537
Francois Romieu5f787a12006-08-17 13:02:36 +02003538static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3539{
3540 struct rtl8169_private *tp = netdev_priv(dev);
3541 struct mii_ioctl_data *data = if_mii(ifr);
3542
Francois Romieu8b4ab282008-11-19 22:05:25 -08003543 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3544}
Francois Romieu5f787a12006-08-17 13:02:36 +02003545
Francois Romieucecb5fd2011-04-01 10:21:07 +02003546static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3547 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003548{
Francois Romieu5f787a12006-08-17 13:02:36 +02003549 switch (cmd) {
3550 case SIOCGMIIPHY:
3551 data->phy_id = 32; /* Internal PHY */
3552 return 0;
3553
3554 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003555 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003556 return 0;
3557
3558 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003559 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003560 return 0;
3561 }
3562 return -EOPNOTSUPP;
3563}
3564
Francois Romieu8b4ab282008-11-19 22:05:25 -08003565static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3566{
3567 return -EOPNOTSUPP;
3568}
3569
Francois Romieufbac58f2007-10-04 22:51:38 +02003570static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3571{
3572 if (tp->features & RTL_FEATURE_MSI) {
3573 pci_disable_msi(pdev);
3574 tp->features &= ~RTL_FEATURE_MSI;
3575 }
3576}
3577
françois romieuc0e45c12011-01-03 15:08:04 +00003578static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3579{
3580 struct mdio_ops *ops = &tp->mdio_ops;
3581
3582 switch (tp->mac_version) {
3583 case RTL_GIGA_MAC_VER_27:
3584 ops->write = r8168dp_1_mdio_write;
3585 ops->read = r8168dp_1_mdio_read;
3586 break;
françois romieue6de30d2011-01-03 15:08:37 +00003587 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003588 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003589 ops->write = r8168dp_2_mdio_write;
3590 ops->read = r8168dp_2_mdio_read;
3591 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003592 default:
3593 ops->write = r8169_mdio_write;
3594 ops->read = r8169_mdio_read;
3595 break;
3596 }
3597}
3598
David S. Miller1805b2f2011-10-24 18:18:09 -04003599static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3600{
3601 void __iomem *ioaddr = tp->mmio_addr;
3602
3603 switch (tp->mac_version) {
3604 case RTL_GIGA_MAC_VER_29:
3605 case RTL_GIGA_MAC_VER_30:
3606 case RTL_GIGA_MAC_VER_32:
3607 case RTL_GIGA_MAC_VER_33:
3608 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08003609 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003610 case RTL_GIGA_MAC_VER_38:
David S. Miller1805b2f2011-10-24 18:18:09 -04003611 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3612 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3613 break;
3614 default:
3615 break;
3616 }
3617}
3618
3619static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3620{
3621 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3622 return false;
3623
3624 rtl_writephy(tp, 0x1f, 0x0000);
3625 rtl_writephy(tp, MII_BMCR, 0x0000);
3626
3627 rtl_wol_suspend_quirk(tp);
3628
3629 return true;
3630}
3631
françois romieu065c27c2011-01-03 15:08:12 +00003632static void r810x_phy_power_down(struct rtl8169_private *tp)
3633{
3634 rtl_writephy(tp, 0x1f, 0x0000);
3635 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3636}
3637
3638static void r810x_phy_power_up(struct rtl8169_private *tp)
3639{
3640 rtl_writephy(tp, 0x1f, 0x0000);
3641 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3642}
3643
3644static void r810x_pll_power_down(struct rtl8169_private *tp)
3645{
Hayes Wang00042992012-03-30 14:33:00 +08003646 void __iomem *ioaddr = tp->mmio_addr;
3647
David S. Miller1805b2f2011-10-24 18:18:09 -04003648 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003649 return;
françois romieu065c27c2011-01-03 15:08:12 +00003650
3651 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003652
3653 switch (tp->mac_version) {
3654 case RTL_GIGA_MAC_VER_07:
3655 case RTL_GIGA_MAC_VER_08:
3656 case RTL_GIGA_MAC_VER_09:
3657 case RTL_GIGA_MAC_VER_10:
3658 case RTL_GIGA_MAC_VER_13:
3659 case RTL_GIGA_MAC_VER_16:
3660 break;
3661 default:
3662 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3663 break;
3664 }
françois romieu065c27c2011-01-03 15:08:12 +00003665}
3666
3667static void r810x_pll_power_up(struct rtl8169_private *tp)
3668{
Hayes Wang00042992012-03-30 14:33:00 +08003669 void __iomem *ioaddr = tp->mmio_addr;
3670
françois romieu065c27c2011-01-03 15:08:12 +00003671 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003672
3673 switch (tp->mac_version) {
3674 case RTL_GIGA_MAC_VER_07:
3675 case RTL_GIGA_MAC_VER_08:
3676 case RTL_GIGA_MAC_VER_09:
3677 case RTL_GIGA_MAC_VER_10:
3678 case RTL_GIGA_MAC_VER_13:
3679 case RTL_GIGA_MAC_VER_16:
3680 break;
3681 default:
3682 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3683 break;
3684 }
françois romieu065c27c2011-01-03 15:08:12 +00003685}
3686
3687static void r8168_phy_power_up(struct rtl8169_private *tp)
3688{
3689 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003690 switch (tp->mac_version) {
3691 case RTL_GIGA_MAC_VER_11:
3692 case RTL_GIGA_MAC_VER_12:
3693 case RTL_GIGA_MAC_VER_17:
3694 case RTL_GIGA_MAC_VER_18:
3695 case RTL_GIGA_MAC_VER_19:
3696 case RTL_GIGA_MAC_VER_20:
3697 case RTL_GIGA_MAC_VER_21:
3698 case RTL_GIGA_MAC_VER_22:
3699 case RTL_GIGA_MAC_VER_23:
3700 case RTL_GIGA_MAC_VER_24:
3701 case RTL_GIGA_MAC_VER_25:
3702 case RTL_GIGA_MAC_VER_26:
3703 case RTL_GIGA_MAC_VER_27:
3704 case RTL_GIGA_MAC_VER_28:
3705 case RTL_GIGA_MAC_VER_31:
3706 rtl_writephy(tp, 0x0e, 0x0000);
3707 break;
3708 default:
3709 break;
3710 }
françois romieu065c27c2011-01-03 15:08:12 +00003711 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3712}
3713
3714static void r8168_phy_power_down(struct rtl8169_private *tp)
3715{
3716 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003717 switch (tp->mac_version) {
3718 case RTL_GIGA_MAC_VER_32:
3719 case RTL_GIGA_MAC_VER_33:
3720 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3721 break;
3722
3723 case RTL_GIGA_MAC_VER_11:
3724 case RTL_GIGA_MAC_VER_12:
3725 case RTL_GIGA_MAC_VER_17:
3726 case RTL_GIGA_MAC_VER_18:
3727 case RTL_GIGA_MAC_VER_19:
3728 case RTL_GIGA_MAC_VER_20:
3729 case RTL_GIGA_MAC_VER_21:
3730 case RTL_GIGA_MAC_VER_22:
3731 case RTL_GIGA_MAC_VER_23:
3732 case RTL_GIGA_MAC_VER_24:
3733 case RTL_GIGA_MAC_VER_25:
3734 case RTL_GIGA_MAC_VER_26:
3735 case RTL_GIGA_MAC_VER_27:
3736 case RTL_GIGA_MAC_VER_28:
3737 case RTL_GIGA_MAC_VER_31:
3738 rtl_writephy(tp, 0x0e, 0x0200);
3739 default:
3740 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3741 break;
3742 }
françois romieu065c27c2011-01-03 15:08:12 +00003743}
3744
3745static void r8168_pll_power_down(struct rtl8169_private *tp)
3746{
3747 void __iomem *ioaddr = tp->mmio_addr;
3748
Francois Romieucecb5fd2011-04-01 10:21:07 +02003749 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3750 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3751 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003752 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003753 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003754 }
françois romieu065c27c2011-01-03 15:08:12 +00003755
Francois Romieucecb5fd2011-04-01 10:21:07 +02003756 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3757 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003758 (RTL_R16(CPlusCmd) & ASF)) {
3759 return;
3760 }
3761
hayeswang01dc7fe2011-03-21 01:50:28 +00003762 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3763 tp->mac_version == RTL_GIGA_MAC_VER_33)
3764 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3765
David S. Miller1805b2f2011-10-24 18:18:09 -04003766 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003767 return;
françois romieu065c27c2011-01-03 15:08:12 +00003768
3769 r8168_phy_power_down(tp);
3770
3771 switch (tp->mac_version) {
3772 case RTL_GIGA_MAC_VER_25:
3773 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003774 case RTL_GIGA_MAC_VER_27:
3775 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003776 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003777 case RTL_GIGA_MAC_VER_32:
3778 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003779 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3780 break;
3781 }
3782}
3783
3784static void r8168_pll_power_up(struct rtl8169_private *tp)
3785{
3786 void __iomem *ioaddr = tp->mmio_addr;
3787
françois romieu065c27c2011-01-03 15:08:12 +00003788 switch (tp->mac_version) {
3789 case RTL_GIGA_MAC_VER_25:
3790 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003791 case RTL_GIGA_MAC_VER_27:
3792 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003793 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003794 case RTL_GIGA_MAC_VER_32:
3795 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003796 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3797 break;
3798 }
3799
3800 r8168_phy_power_up(tp);
3801}
3802
Francois Romieud58d46b2011-05-03 16:38:29 +02003803static void rtl_generic_op(struct rtl8169_private *tp,
3804 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00003805{
3806 if (op)
3807 op(tp);
3808}
3809
3810static void rtl_pll_power_down(struct rtl8169_private *tp)
3811{
Francois Romieud58d46b2011-05-03 16:38:29 +02003812 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00003813}
3814
3815static void rtl_pll_power_up(struct rtl8169_private *tp)
3816{
Francois Romieud58d46b2011-05-03 16:38:29 +02003817 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00003818}
3819
3820static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3821{
3822 struct pll_power_ops *ops = &tp->pll_power_ops;
3823
3824 switch (tp->mac_version) {
3825 case RTL_GIGA_MAC_VER_07:
3826 case RTL_GIGA_MAC_VER_08:
3827 case RTL_GIGA_MAC_VER_09:
3828 case RTL_GIGA_MAC_VER_10:
3829 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003830 case RTL_GIGA_MAC_VER_29:
3831 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08003832 case RTL_GIGA_MAC_VER_37:
françois romieu065c27c2011-01-03 15:08:12 +00003833 ops->down = r810x_pll_power_down;
3834 ops->up = r810x_pll_power_up;
3835 break;
3836
3837 case RTL_GIGA_MAC_VER_11:
3838 case RTL_GIGA_MAC_VER_12:
3839 case RTL_GIGA_MAC_VER_17:
3840 case RTL_GIGA_MAC_VER_18:
3841 case RTL_GIGA_MAC_VER_19:
3842 case RTL_GIGA_MAC_VER_20:
3843 case RTL_GIGA_MAC_VER_21:
3844 case RTL_GIGA_MAC_VER_22:
3845 case RTL_GIGA_MAC_VER_23:
3846 case RTL_GIGA_MAC_VER_24:
3847 case RTL_GIGA_MAC_VER_25:
3848 case RTL_GIGA_MAC_VER_26:
3849 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003850 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003851 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003852 case RTL_GIGA_MAC_VER_32:
3853 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003854 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08003855 case RTL_GIGA_MAC_VER_35:
3856 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003857 case RTL_GIGA_MAC_VER_38:
françois romieu065c27c2011-01-03 15:08:12 +00003858 ops->down = r8168_pll_power_down;
3859 ops->up = r8168_pll_power_up;
3860 break;
3861
3862 default:
3863 ops->down = NULL;
3864 ops->up = NULL;
3865 break;
3866 }
3867}
3868
Hayes Wange542a222011-07-06 15:58:04 +08003869static void rtl_init_rxcfg(struct rtl8169_private *tp)
3870{
3871 void __iomem *ioaddr = tp->mmio_addr;
3872
3873 switch (tp->mac_version) {
3874 case RTL_GIGA_MAC_VER_01:
3875 case RTL_GIGA_MAC_VER_02:
3876 case RTL_GIGA_MAC_VER_03:
3877 case RTL_GIGA_MAC_VER_04:
3878 case RTL_GIGA_MAC_VER_05:
3879 case RTL_GIGA_MAC_VER_06:
3880 case RTL_GIGA_MAC_VER_10:
3881 case RTL_GIGA_MAC_VER_11:
3882 case RTL_GIGA_MAC_VER_12:
3883 case RTL_GIGA_MAC_VER_13:
3884 case RTL_GIGA_MAC_VER_14:
3885 case RTL_GIGA_MAC_VER_15:
3886 case RTL_GIGA_MAC_VER_16:
3887 case RTL_GIGA_MAC_VER_17:
3888 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3889 break;
3890 case RTL_GIGA_MAC_VER_18:
3891 case RTL_GIGA_MAC_VER_19:
3892 case RTL_GIGA_MAC_VER_20:
3893 case RTL_GIGA_MAC_VER_21:
3894 case RTL_GIGA_MAC_VER_22:
3895 case RTL_GIGA_MAC_VER_23:
3896 case RTL_GIGA_MAC_VER_24:
3897 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3898 break;
3899 default:
3900 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3901 break;
3902 }
3903}
3904
Hayes Wang92fc43b2011-07-06 15:58:03 +08003905static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3906{
3907 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3908}
3909
Francois Romieud58d46b2011-05-03 16:38:29 +02003910static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3911{
françois romieu9c5028e2012-03-02 04:43:14 +00003912 void __iomem *ioaddr = tp->mmio_addr;
3913
3914 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003915 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00003916 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003917}
3918
3919static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3920{
françois romieu9c5028e2012-03-02 04:43:14 +00003921 void __iomem *ioaddr = tp->mmio_addr;
3922
3923 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003924 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00003925 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003926}
3927
3928static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3929{
3930 void __iomem *ioaddr = tp->mmio_addr;
3931
3932 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3933 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3934 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3935}
3936
3937static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3938{
3939 void __iomem *ioaddr = tp->mmio_addr;
3940
3941 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3942 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3943 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3944}
3945
3946static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3947{
3948 void __iomem *ioaddr = tp->mmio_addr;
3949
3950 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3951}
3952
3953static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3954{
3955 void __iomem *ioaddr = tp->mmio_addr;
3956
3957 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3958}
3959
3960static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3961{
3962 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003963
3964 RTL_W8(MaxTxPacketSize, 0x3f);
3965 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3966 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003967 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003968}
3969
3970static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3971{
3972 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003973
3974 RTL_W8(MaxTxPacketSize, 0x0c);
3975 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3976 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003977 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003978}
3979
3980static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3981{
3982 rtl_tx_performance_tweak(tp->pci_dev,
3983 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3984}
3985
3986static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3987{
3988 rtl_tx_performance_tweak(tp->pci_dev,
3989 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3990}
3991
3992static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3993{
3994 void __iomem *ioaddr = tp->mmio_addr;
3995
3996 r8168b_0_hw_jumbo_enable(tp);
3997
3998 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
3999}
4000
4001static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4002{
4003 void __iomem *ioaddr = tp->mmio_addr;
4004
4005 r8168b_0_hw_jumbo_disable(tp);
4006
4007 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4008}
4009
4010static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
4011{
4012 struct jumbo_ops *ops = &tp->jumbo_ops;
4013
4014 switch (tp->mac_version) {
4015 case RTL_GIGA_MAC_VER_11:
4016 ops->disable = r8168b_0_hw_jumbo_disable;
4017 ops->enable = r8168b_0_hw_jumbo_enable;
4018 break;
4019 case RTL_GIGA_MAC_VER_12:
4020 case RTL_GIGA_MAC_VER_17:
4021 ops->disable = r8168b_1_hw_jumbo_disable;
4022 ops->enable = r8168b_1_hw_jumbo_enable;
4023 break;
4024 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4025 case RTL_GIGA_MAC_VER_19:
4026 case RTL_GIGA_MAC_VER_20:
4027 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4028 case RTL_GIGA_MAC_VER_22:
4029 case RTL_GIGA_MAC_VER_23:
4030 case RTL_GIGA_MAC_VER_24:
4031 case RTL_GIGA_MAC_VER_25:
4032 case RTL_GIGA_MAC_VER_26:
4033 ops->disable = r8168c_hw_jumbo_disable;
4034 ops->enable = r8168c_hw_jumbo_enable;
4035 break;
4036 case RTL_GIGA_MAC_VER_27:
4037 case RTL_GIGA_MAC_VER_28:
4038 ops->disable = r8168dp_hw_jumbo_disable;
4039 ops->enable = r8168dp_hw_jumbo_enable;
4040 break;
4041 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4042 case RTL_GIGA_MAC_VER_32:
4043 case RTL_GIGA_MAC_VER_33:
4044 case RTL_GIGA_MAC_VER_34:
4045 ops->disable = r8168e_hw_jumbo_disable;
4046 ops->enable = r8168e_hw_jumbo_enable;
4047 break;
4048
4049 /*
4050 * No action needed for jumbo frames with 8169.
4051 * No jumbo for 810x at all.
4052 */
4053 default:
4054 ops->disable = NULL;
4055 ops->enable = NULL;
4056 break;
4057 }
4058}
4059
Francois Romieu6f43adc2011-04-29 15:05:51 +02004060static void rtl_hw_reset(struct rtl8169_private *tp)
4061{
4062 void __iomem *ioaddr = tp->mmio_addr;
4063 int i;
4064
4065 /* Soft reset the chip. */
4066 RTL_W8(ChipCmd, CmdReset);
4067
4068 /* Check that the chip has finished the reset. */
4069 for (i = 0; i < 100; i++) {
4070 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
4071 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004072 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004073 }
4074}
4075
Francois Romieub6ffd972011-06-17 17:00:05 +02004076static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4077{
4078 struct rtl_fw *rtl_fw;
4079 const char *name;
4080 int rc = -ENOMEM;
4081
4082 name = rtl_lookup_firmware_name(tp);
4083 if (!name)
4084 goto out_no_firmware;
4085
4086 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4087 if (!rtl_fw)
4088 goto err_warn;
4089
4090 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4091 if (rc < 0)
4092 goto err_free;
4093
Francois Romieufd112f22011-06-18 00:10:29 +02004094 rc = rtl_check_firmware(tp, rtl_fw);
4095 if (rc < 0)
4096 goto err_release_firmware;
4097
Francois Romieub6ffd972011-06-17 17:00:05 +02004098 tp->rtl_fw = rtl_fw;
4099out:
4100 return;
4101
Francois Romieufd112f22011-06-18 00:10:29 +02004102err_release_firmware:
4103 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004104err_free:
4105 kfree(rtl_fw);
4106err_warn:
4107 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4108 name, rc);
4109out_no_firmware:
4110 tp->rtl_fw = NULL;
4111 goto out;
4112}
4113
François Romieu953a12c2011-04-24 17:38:48 +02004114static void rtl_request_firmware(struct rtl8169_private *tp)
4115{
Francois Romieub6ffd972011-06-17 17:00:05 +02004116 if (IS_ERR(tp->rtl_fw))
4117 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004118}
4119
Hayes Wang92fc43b2011-07-06 15:58:03 +08004120static void rtl_rx_close(struct rtl8169_private *tp)
4121{
4122 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004123
Francois Romieu1687b562011-07-19 17:21:29 +02004124 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004125}
4126
françois romieue6de30d2011-01-03 15:08:37 +00004127static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128{
françois romieue6de30d2011-01-03 15:08:37 +00004129 void __iomem *ioaddr = tp->mmio_addr;
4130
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004132 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133
Hayes Wang92fc43b2011-07-06 15:58:03 +08004134 rtl_rx_close(tp);
4135
Hayes Wang5d2e1952011-02-22 17:26:22 +08004136 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004137 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4138 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00004139 while (RTL_R8(TxPoll) & NPQ)
4140 udelay(20);
Hayes Wangc2218922011-09-06 16:55:18 +08004141 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4142 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
Hayes Wang7e18dca2012-03-30 14:33:02 +08004143 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004144 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
4145 tp->mac_version == RTL_GIGA_MAC_VER_38) {
David S. Miller8decf862011-09-22 03:23:13 -04004146 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08004147 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4148 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004149 } else {
4150 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4151 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004152 }
4153
Hayes Wang92fc43b2011-07-06 15:58:03 +08004154 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155}
4156
Francois Romieu7f796d832007-06-11 23:04:41 +02004157static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004158{
4159 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004160
4161 /* Set DMA burst size and Interframe Gap Time */
4162 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4163 (InterFrameGap << TxInterFrameGapShift));
4164}
4165
Francois Romieu07ce4062007-02-23 23:36:39 +01004166static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167{
4168 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169
Francois Romieu07ce4062007-02-23 23:36:39 +01004170 tp->hw_start(dev);
4171
Francois Romieuda78dbf2012-01-26 14:18:23 +01004172 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004173}
4174
Francois Romieu7f796d832007-06-11 23:04:41 +02004175static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4176 void __iomem *ioaddr)
4177{
4178 /*
4179 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4180 * register to be written before TxDescAddrLow to work.
4181 * Switching from MMIO to I/O access fixes the issue as well.
4182 */
4183 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004184 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004185 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004186 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004187}
4188
4189static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4190{
4191 u16 cmd;
4192
4193 cmd = RTL_R16(CPlusCmd);
4194 RTL_W16(CPlusCmd, cmd);
4195 return cmd;
4196}
4197
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004198static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004199{
4200 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004201 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004202}
4203
Francois Romieu6dccd162007-02-13 23:38:05 +01004204static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4205{
Francois Romieu37441002011-06-17 22:58:54 +02004206 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004207 u32 mac_version;
4208 u32 clk;
4209 u32 val;
4210 } cfg2_info [] = {
4211 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4212 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4213 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4214 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004215 };
4216 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004217 unsigned int i;
4218 u32 clk;
4219
4220 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004221 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004222 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4223 RTL_W32(0x7c, p->val);
4224 break;
4225 }
4226 }
4227}
4228
Francois Romieue6b763e2012-03-08 09:35:39 +01004229static void rtl_set_rx_mode(struct net_device *dev)
4230{
4231 struct rtl8169_private *tp = netdev_priv(dev);
4232 void __iomem *ioaddr = tp->mmio_addr;
4233 u32 mc_filter[2]; /* Multicast hash filter */
4234 int rx_mode;
4235 u32 tmp = 0;
4236
4237 if (dev->flags & IFF_PROMISC) {
4238 /* Unconditionally log net taps. */
4239 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4240 rx_mode =
4241 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4242 AcceptAllPhys;
4243 mc_filter[1] = mc_filter[0] = 0xffffffff;
4244 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4245 (dev->flags & IFF_ALLMULTI)) {
4246 /* Too many to filter perfectly -- accept all multicasts. */
4247 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4248 mc_filter[1] = mc_filter[0] = 0xffffffff;
4249 } else {
4250 struct netdev_hw_addr *ha;
4251
4252 rx_mode = AcceptBroadcast | AcceptMyPhys;
4253 mc_filter[1] = mc_filter[0] = 0;
4254 netdev_for_each_mc_addr(ha, dev) {
4255 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4256 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4257 rx_mode |= AcceptMulticast;
4258 }
4259 }
4260
4261 if (dev->features & NETIF_F_RXALL)
4262 rx_mode |= (AcceptErr | AcceptRunt);
4263
4264 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4265
4266 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4267 u32 data = mc_filter[0];
4268
4269 mc_filter[0] = swab32(mc_filter[1]);
4270 mc_filter[1] = swab32(data);
4271 }
4272
4273 RTL_W32(MAR0 + 4, mc_filter[1]);
4274 RTL_W32(MAR0 + 0, mc_filter[0]);
4275
4276 RTL_W32(RxConfig, tmp);
4277}
4278
Francois Romieu07ce4062007-02-23 23:36:39 +01004279static void rtl_hw_start_8169(struct net_device *dev)
4280{
4281 struct rtl8169_private *tp = netdev_priv(dev);
4282 void __iomem *ioaddr = tp->mmio_addr;
4283 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004284
Francois Romieu9cb427b2006-11-02 00:10:16 +01004285 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4286 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4287 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4288 }
4289
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004291 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4292 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4293 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4294 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004295 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4296
Hayes Wange542a222011-07-06 15:58:04 +08004297 rtl_init_rxcfg(tp);
4298
françois romieuf0298f82011-01-03 15:07:42 +00004299 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004301 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302
Francois Romieucecb5fd2011-04-01 10:21:07 +02004303 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4304 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4305 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4306 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004307 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308
Francois Romieu7f796d832007-06-11 23:04:41 +02004309 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004310
Francois Romieucecb5fd2011-04-01 10:21:07 +02004311 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4312 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004313 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004315 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 }
4317
Francois Romieubcf0bf92006-07-26 23:14:13 +02004318 RTL_W16(CPlusCmd, tp->cp_cmd);
4319
Francois Romieu6dccd162007-02-13 23:38:05 +01004320 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4321
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 /*
4323 * Undocumented corner. Supposedly:
4324 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4325 */
4326 RTL_W16(IntrMitigate, 0x0000);
4327
Francois Romieu7f796d832007-06-11 23:04:41 +02004328 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004329
Francois Romieucecb5fd2011-04-01 10:21:07 +02004330 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4331 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4332 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4333 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004334 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4335 rtl_set_rx_tx_config_registers(tp);
4336 }
4337
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004339
4340 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4341 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
4343 RTL_W32(RxMissed, 0);
4344
Francois Romieu07ce4062007-02-23 23:36:39 +01004345 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
4347 /* no early-rx interrupts */
4348 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004349}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004351static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
4352{
4353 if (tp->csi_ops.write)
4354 tp->csi_ops.write(tp->mmio_addr, addr, value);
4355}
4356
4357static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
4358{
4359 if (tp->csi_ops.read)
4360 return tp->csi_ops.read(tp->mmio_addr, addr);
4361 else
4362 return ~0;
4363}
4364
4365static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004366{
4367 u32 csi;
4368
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004369 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4370 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00004371}
4372
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004373static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004374{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004375 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00004376}
4377
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004378static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00004379{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004380 rtl_csi_access_enable(tp, 0x27000000);
4381}
4382
4383static void r8169_csi_write(void __iomem *ioaddr, int addr, int value)
4384{
4385 unsigned int i;
4386
4387 RTL_W32(CSIDR, value);
4388 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4389 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4390
4391 for (i = 0; i < 100; i++) {
4392 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
4393 break;
4394 udelay(10);
4395 }
4396}
4397
4398static u32 r8169_csi_read(void __iomem *ioaddr, int addr)
4399{
4400 u32 value = ~0x00;
4401 unsigned int i;
4402
4403 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
4404 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4405
4406 for (i = 0; i < 100; i++) {
4407 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
4408 value = RTL_R32(CSIDR);
4409 break;
4410 }
4411 udelay(10);
4412 }
4413
4414 return value;
4415}
4416
Hayes Wang7e18dca2012-03-30 14:33:02 +08004417static void r8402_csi_write(void __iomem *ioaddr, int addr, int value)
4418{
4419 unsigned int i;
4420
4421 RTL_W32(CSIDR, value);
4422 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4423 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
4424 CSIAR_FUNC_NIC);
4425
4426 for (i = 0; i < 100; i++) {
4427 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
4428 break;
4429 udelay(10);
4430 }
4431}
4432
4433static u32 r8402_csi_read(void __iomem *ioaddr, int addr)
4434{
4435 u32 value = ~0x00;
4436 unsigned int i;
4437
4438 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
4439 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4440
4441 for (i = 0; i < 100; i++) {
4442 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
4443 value = RTL_R32(CSIDR);
4444 break;
4445 }
4446 udelay(10);
4447 }
4448
4449 return value;
4450}
4451
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004452static void __devinit rtl_init_csi_ops(struct rtl8169_private *tp)
4453{
4454 struct csi_ops *ops = &tp->csi_ops;
4455
4456 switch (tp->mac_version) {
4457 case RTL_GIGA_MAC_VER_01:
4458 case RTL_GIGA_MAC_VER_02:
4459 case RTL_GIGA_MAC_VER_03:
4460 case RTL_GIGA_MAC_VER_04:
4461 case RTL_GIGA_MAC_VER_05:
4462 case RTL_GIGA_MAC_VER_06:
4463 case RTL_GIGA_MAC_VER_10:
4464 case RTL_GIGA_MAC_VER_11:
4465 case RTL_GIGA_MAC_VER_12:
4466 case RTL_GIGA_MAC_VER_13:
4467 case RTL_GIGA_MAC_VER_14:
4468 case RTL_GIGA_MAC_VER_15:
4469 case RTL_GIGA_MAC_VER_16:
4470 case RTL_GIGA_MAC_VER_17:
4471 ops->write = NULL;
4472 ops->read = NULL;
4473 break;
4474
Hayes Wang7e18dca2012-03-30 14:33:02 +08004475 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004476 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004477 ops->write = r8402_csi_write;
4478 ops->read = r8402_csi_read;
4479 break;
4480
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004481 default:
4482 ops->write = r8169_csi_write;
4483 ops->read = r8169_csi_read;
4484 break;
4485 }
Francois Romieudacf8152008-08-02 20:44:13 +02004486}
4487
4488struct ephy_info {
4489 unsigned int offset;
4490 u16 mask;
4491 u16 bits;
4492};
4493
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004494static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004495{
4496 u16 w;
4497
4498 while (len-- > 0) {
4499 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4500 rtl_ephy_write(ioaddr, e->offset, w);
4501 e++;
4502 }
4503}
4504
Francois Romieub726e492008-06-28 12:22:59 +02004505static void rtl_disable_clock_request(struct pci_dev *pdev)
4506{
Jon Masone44daad2011-06-27 07:46:31 +00004507 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004508
4509 if (cap) {
4510 u16 ctl;
4511
4512 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4513 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4514 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4515 }
4516}
4517
françois romieue6de30d2011-01-03 15:08:37 +00004518static void rtl_enable_clock_request(struct pci_dev *pdev)
4519{
Jon Masone44daad2011-06-27 07:46:31 +00004520 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004521
4522 if (cap) {
4523 u16 ctl;
4524
4525 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4526 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4527 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4528 }
4529}
4530
Francois Romieub726e492008-06-28 12:22:59 +02004531#define R8168_CPCMD_QUIRK_MASK (\
4532 EnableBist | \
4533 Mac_dbgo_oe | \
4534 Force_half_dup | \
4535 Force_rxflow_en | \
4536 Force_txflow_en | \
4537 Cxpl_dbg_sel | \
4538 ASF | \
4539 PktCntrDisable | \
4540 Mac_dbgo_sel)
4541
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004542static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004543{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004544 void __iomem *ioaddr = tp->mmio_addr;
4545 struct pci_dev *pdev = tp->pci_dev;
4546
Francois Romieub726e492008-06-28 12:22:59 +02004547 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4548
4549 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4550
Francois Romieu2e68ae42008-06-28 12:00:55 +02004551 rtl_tx_performance_tweak(pdev,
4552 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004553}
4554
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004555static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004556{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004557 void __iomem *ioaddr = tp->mmio_addr;
4558
4559 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004560
françois romieuf0298f82011-01-03 15:07:42 +00004561 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004562
4563 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004564}
4565
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004566static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004567{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004568 void __iomem *ioaddr = tp->mmio_addr;
4569 struct pci_dev *pdev = tp->pci_dev;
4570
Francois Romieub726e492008-06-28 12:22:59 +02004571 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4572
4573 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4574
Francois Romieu219a1e92008-06-28 11:58:39 +02004575 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004576
4577 rtl_disable_clock_request(pdev);
4578
4579 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004580}
4581
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004582static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004583{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004584 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004585 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004586 { 0x01, 0, 0x0001 },
4587 { 0x02, 0x0800, 0x1000 },
4588 { 0x03, 0, 0x0042 },
4589 { 0x06, 0x0080, 0x0000 },
4590 { 0x07, 0, 0x2000 }
4591 };
4592
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004593 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004594
4595 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4596
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004597 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004598}
4599
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004600static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02004601{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004602 void __iomem *ioaddr = tp->mmio_addr;
4603 struct pci_dev *pdev = tp->pci_dev;
4604
4605 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004606
4607 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4608
4609 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4610
4611 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4612}
4613
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004614static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004615{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004616 void __iomem *ioaddr = tp->mmio_addr;
4617 struct pci_dev *pdev = tp->pci_dev;
4618
4619 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004620
4621 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4622
4623 /* Magic. */
4624 RTL_W8(DBG_REG, 0x20);
4625
françois romieuf0298f82011-01-03 15:07:42 +00004626 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004627
4628 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4629
4630 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4631}
4632
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004633static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004634{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004635 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004636 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004637 { 0x02, 0x0800, 0x1000 },
4638 { 0x03, 0, 0x0002 },
4639 { 0x06, 0x0080, 0x0000 }
4640 };
4641
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004642 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004643
4644 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4645
4646 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4647
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004648 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004649}
4650
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004651static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004652{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004653 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004654 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004655 { 0x01, 0, 0x0001 },
4656 { 0x03, 0x0400, 0x0220 }
4657 };
4658
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004659 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004660
4661 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4662
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004663 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004664}
4665
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004666static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02004667{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004668 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004669}
4670
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004671static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02004672{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004673 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004674
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004675 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004676}
4677
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004678static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02004679{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004680 void __iomem *ioaddr = tp->mmio_addr;
4681 struct pci_dev *pdev = tp->pci_dev;
4682
4683 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004684
4685 rtl_disable_clock_request(pdev);
4686
françois romieuf0298f82011-01-03 15:07:42 +00004687 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004688
4689 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4690
4691 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4692}
4693
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004694static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00004695{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004696 void __iomem *ioaddr = tp->mmio_addr;
4697 struct pci_dev *pdev = tp->pci_dev;
4698
4699 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004700
4701 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4702
4703 RTL_W8(MaxTxPacketSize, TxPacketMax);
4704
4705 rtl_disable_clock_request(pdev);
4706}
4707
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004708static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004709{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004710 void __iomem *ioaddr = tp->mmio_addr;
4711 struct pci_dev *pdev = tp->pci_dev;
françois romieue6de30d2011-01-03 15:08:37 +00004712 static const struct ephy_info e_info_8168d_4[] = {
4713 { 0x0b, ~0, 0x48 },
4714 { 0x19, 0x20, 0x50 },
4715 { 0x0c, ~0, 0x20 }
4716 };
4717 int i;
4718
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004719 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004720
4721 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4722
4723 RTL_W8(MaxTxPacketSize, TxPacketMax);
4724
4725 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4726 const struct ephy_info *e = e_info_8168d_4 + i;
4727 u16 w;
4728
4729 w = rtl_ephy_read(ioaddr, e->offset);
4730 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4731 }
4732
4733 rtl_enable_clock_request(pdev);
4734}
4735
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004736static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00004737{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004738 void __iomem *ioaddr = tp->mmio_addr;
4739 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08004740 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004741 { 0x00, 0x0200, 0x0100 },
4742 { 0x00, 0x0000, 0x0004 },
4743 { 0x06, 0x0002, 0x0001 },
4744 { 0x06, 0x0000, 0x0030 },
4745 { 0x07, 0x0000, 0x2000 },
4746 { 0x00, 0x0000, 0x0020 },
4747 { 0x03, 0x5800, 0x2000 },
4748 { 0x03, 0x0000, 0x0001 },
4749 { 0x01, 0x0800, 0x1000 },
4750 { 0x07, 0x0000, 0x4000 },
4751 { 0x1e, 0x0000, 0x2000 },
4752 { 0x19, 0xffff, 0xfe6c },
4753 { 0x0a, 0x0000, 0x0040 }
4754 };
4755
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004756 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004757
Hayes Wang70090422011-07-06 15:58:06 +08004758 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004759
4760 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4761
4762 RTL_W8(MaxTxPacketSize, TxPacketMax);
4763
4764 rtl_disable_clock_request(pdev);
4765
4766 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004767 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4768 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004769
Francois Romieucecb5fd2011-04-01 10:21:07 +02004770 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004771}
4772
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004773static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08004774{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004775 void __iomem *ioaddr = tp->mmio_addr;
4776 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08004777 static const struct ephy_info e_info_8168e_2[] = {
4778 { 0x09, 0x0000, 0x0080 },
4779 { 0x19, 0x0000, 0x0224 }
4780 };
4781
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004782 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08004783
4784 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4785
4786 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4787
4788 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4789 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4790 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4791 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4792 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4793 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4794 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4795 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4796 ERIAR_EXGMAC);
4797
Hayes Wang3090bd92011-09-06 16:55:15 +08004798 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004799
4800 rtl_disable_clock_request(pdev);
4801
4802 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4803 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4804
4805 /* Adjust EEE LED frequency */
4806 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4807
4808 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4809 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4810 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4811}
4812
Hayes Wang5f886e02012-03-30 14:33:03 +08004813static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08004814{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004815 void __iomem *ioaddr = tp->mmio_addr;
4816 struct pci_dev *pdev = tp->pci_dev;
Hayes Wangc2218922011-09-06 16:55:18 +08004817
Hayes Wang5f886e02012-03-30 14:33:03 +08004818 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08004819
4820 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4821
4822 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4823 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4824 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4825 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4826 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4827 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4828 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4829 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4830 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4831 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08004832
4833 RTL_W8(MaxTxPacketSize, EarlySize);
4834
4835 rtl_disable_clock_request(pdev);
4836
4837 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4838 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
Hayes Wangc2218922011-09-06 16:55:18 +08004839 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4840 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4841 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4842}
4843
Hayes Wang5f886e02012-03-30 14:33:03 +08004844static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
4845{
4846 void __iomem *ioaddr = tp->mmio_addr;
4847 static const struct ephy_info e_info_8168f_1[] = {
4848 { 0x06, 0x00c0, 0x0020 },
4849 { 0x08, 0x0001, 0x0002 },
4850 { 0x09, 0x0000, 0x0080 },
4851 { 0x19, 0x0000, 0x0224 }
4852 };
4853
4854 rtl_hw_start_8168f(tp);
4855
4856 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4857
4858 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4859 ERIAR_EXGMAC);
4860
4861 /* Adjust EEE LED frequency */
4862 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4863}
4864
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004865static void rtl_hw_start_8411(struct rtl8169_private *tp)
4866{
4867 void __iomem *ioaddr = tp->mmio_addr;
4868 static const struct ephy_info e_info_8168f_1[] = {
4869 { 0x06, 0x00c0, 0x0020 },
4870 { 0x0f, 0xffff, 0x5200 },
4871 { 0x1e, 0x0000, 0x4000 },
4872 { 0x19, 0x0000, 0x0224 }
4873 };
4874
4875 rtl_hw_start_8168f(tp);
4876
4877 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4878
4879 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000,
4880 ERIAR_EXGMAC);
4881}
4882
Francois Romieu07ce4062007-02-23 23:36:39 +01004883static void rtl_hw_start_8168(struct net_device *dev)
4884{
Francois Romieu2dd99532007-06-11 23:22:52 +02004885 struct rtl8169_private *tp = netdev_priv(dev);
4886 void __iomem *ioaddr = tp->mmio_addr;
4887
4888 RTL_W8(Cfg9346, Cfg9346_Unlock);
4889
françois romieuf0298f82011-01-03 15:07:42 +00004890 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004891
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004892 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004893
Francois Romieu0e485152007-02-20 00:00:26 +01004894 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004895
4896 RTL_W16(CPlusCmd, tp->cp_cmd);
4897
Francois Romieu0e485152007-02-20 00:00:26 +01004898 RTL_W16(IntrMitigate, 0x5151);
4899
4900 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00004901 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01004902 tp->event_slow |= RxFIFOOver | PCSTimeout;
4903 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01004904 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004905
4906 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4907
Francois Romieub8363902008-06-01 12:31:57 +02004908 rtl_set_rx_mode(dev);
4909
4910 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4911 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004912
4913 RTL_R8(IntrMask);
4914
Francois Romieu219a1e92008-06-28 11:58:39 +02004915 switch (tp->mac_version) {
4916 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004917 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004918 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004919
4920 case RTL_GIGA_MAC_VER_12:
4921 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004922 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004923 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004924
4925 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004926 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004927 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004928
4929 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004930 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004931 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004932
4933 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004934 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004935 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004936
Francois Romieu197ff762008-06-28 13:16:02 +02004937 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004938 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004939 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004940
Francois Romieu6fb07052008-06-29 11:54:28 +02004941 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004942 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004943 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004944
Francois Romieuef3386f2008-06-29 12:24:30 +02004945 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004946 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004947 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004948
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004949 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004950 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004951 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004952
Francois Romieu5b538df2008-07-20 16:22:45 +02004953 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004954 case RTL_GIGA_MAC_VER_26:
4955 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004956 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004957 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004958
françois romieue6de30d2011-01-03 15:08:37 +00004959 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004960 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004961 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004962
hayeswang4804b3b2011-03-21 01:50:29 +00004963 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004964 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004965 break;
4966
hayeswang01dc7fe2011-03-21 01:50:28 +00004967 case RTL_GIGA_MAC_VER_32:
4968 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004969 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08004970 break;
4971 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004972 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004973 break;
françois romieue6de30d2011-01-03 15:08:37 +00004974
Hayes Wangc2218922011-09-06 16:55:18 +08004975 case RTL_GIGA_MAC_VER_35:
4976 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004977 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08004978 break;
4979
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004980 case RTL_GIGA_MAC_VER_38:
4981 rtl_hw_start_8411(tp);
4982 break;
4983
Francois Romieu219a1e92008-06-28 11:58:39 +02004984 default:
4985 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4986 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004987 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004988 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004989
Francois Romieu0e485152007-02-20 00:00:26 +01004990 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4991
Francois Romieub8363902008-06-01 12:31:57 +02004992 RTL_W8(Cfg9346, Cfg9346_Lock);
4993
Francois Romieu2dd99532007-06-11 23:22:52 +02004994 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004995}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996
Francois Romieu2857ffb2008-08-02 21:08:49 +02004997#define R810X_CPCMD_QUIRK_MASK (\
4998 EnableBist | \
4999 Mac_dbgo_oe | \
5000 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00005001 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02005002 Force_txflow_en | \
5003 Cxpl_dbg_sel | \
5004 ASF | \
5005 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005006 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005007
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005008static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005009{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005010 void __iomem *ioaddr = tp->mmio_addr;
5011 struct pci_dev *pdev = tp->pci_dev;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005012 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02005013 { 0x01, 0, 0x6e65 },
5014 { 0x02, 0, 0x091f },
5015 { 0x03, 0, 0xc2f9 },
5016 { 0x06, 0, 0xafb5 },
5017 { 0x07, 0, 0x0e00 },
5018 { 0x19, 0, 0xec80 },
5019 { 0x01, 0, 0x2e65 },
5020 { 0x01, 0, 0x6e65 }
5021 };
5022 u8 cfg1;
5023
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005024 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005025
5026 RTL_W8(DBG_REG, FIX_NAK_1);
5027
5028 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5029
5030 RTL_W8(Config1,
5031 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5032 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5033
5034 cfg1 = RTL_R8(Config1);
5035 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5036 RTL_W8(Config1, cfg1 & ~LEDS0);
5037
Francois Romieu2857ffb2008-08-02 21:08:49 +02005038 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
5039}
5040
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005041static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005042{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005043 void __iomem *ioaddr = tp->mmio_addr;
5044 struct pci_dev *pdev = tp->pci_dev;
5045
5046 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005047
5048 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5049
5050 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5051 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005052}
5053
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005054static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005055{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005056 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005057
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005058 rtl_ephy_write(tp->mmio_addr, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005059}
5060
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005061static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005062{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005063 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005064 static const struct ephy_info e_info_8105e_1[] = {
5065 { 0x07, 0, 0x4000 },
5066 { 0x19, 0, 0x0200 },
5067 { 0x19, 0, 0x0020 },
5068 { 0x1e, 0, 0x2000 },
5069 { 0x03, 0, 0x0001 },
5070 { 0x19, 0, 0x0100 },
5071 { 0x19, 0, 0x0004 },
5072 { 0x0a, 0, 0x0020 }
5073 };
5074
Francois Romieucecb5fd2011-04-01 10:21:07 +02005075 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005076 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5077
Francois Romieucecb5fd2011-04-01 10:21:07 +02005078 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005079 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5080
5081 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08005082 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005083
5084 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
5085}
5086
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005087static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005088{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005089 void __iomem *ioaddr = tp->mmio_addr;
5090
5091 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005092 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
5093}
5094
Hayes Wang7e18dca2012-03-30 14:33:02 +08005095static void rtl_hw_start_8402(struct rtl8169_private *tp)
5096{
5097 void __iomem *ioaddr = tp->mmio_addr;
5098 static const struct ephy_info e_info_8402[] = {
5099 { 0x19, 0xffff, 0xff64 },
5100 { 0x1e, 0, 0x4000 }
5101 };
5102
5103 rtl_csi_access_enable_2(tp);
5104
5105 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5106 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5107
5108 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5109 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5110
5111 rtl_ephy_init(ioaddr, e_info_8402, ARRAY_SIZE(e_info_8402));
5112
5113 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5114
5115 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5116 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
5117 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5118 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5119 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5120 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5121 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00,
5122 ERIAR_EXGMAC);
5123}
5124
Francois Romieu07ce4062007-02-23 23:36:39 +01005125static void rtl_hw_start_8101(struct net_device *dev)
5126{
Francois Romieucdf1a602007-06-11 23:29:50 +02005127 struct rtl8169_private *tp = netdev_priv(dev);
5128 void __iomem *ioaddr = tp->mmio_addr;
5129 struct pci_dev *pdev = tp->pci_dev;
5130
Francois Romieuda78dbf2012-01-26 14:18:23 +01005131 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5132 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00005133
Francois Romieucecb5fd2011-04-01 10:21:07 +02005134 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5135 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00005136 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02005137
5138 if (cap) {
5139 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
5140 PCI_EXP_DEVCTL_NOSNOOP_EN);
5141 }
Francois Romieucdf1a602007-06-11 23:29:50 +02005142 }
5143
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005144 RTL_W8(Cfg9346, Cfg9346_Unlock);
5145
Francois Romieu2857ffb2008-08-02 21:08:49 +02005146 switch (tp->mac_version) {
5147 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005148 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005149 break;
5150
5151 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005152 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005153 break;
5154
5155 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005156 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005157 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005158
5159 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005160 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005161 break;
5162 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005163 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005164 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005165
5166 case RTL_GIGA_MAC_VER_37:
5167 rtl_hw_start_8402(tp);
5168 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02005169 }
5170
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005171 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02005172
françois romieuf0298f82011-01-03 15:07:42 +00005173 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02005174
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005175 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02005176
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005177 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02005178 RTL_W16(CPlusCmd, tp->cp_cmd);
5179
5180 RTL_W16(IntrMitigate, 0x0000);
5181
5182 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5183
5184 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5185 rtl_set_rx_tx_config_registers(tp);
5186
Francois Romieucdf1a602007-06-11 23:29:50 +02005187 RTL_R8(IntrMask);
5188
Francois Romieucdf1a602007-06-11 23:29:50 +02005189 rtl_set_rx_mode(dev);
5190
5191 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192}
5193
5194static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5195{
Francois Romieud58d46b2011-05-03 16:38:29 +02005196 struct rtl8169_private *tp = netdev_priv(dev);
5197
5198 if (new_mtu < ETH_ZLEN ||
5199 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 return -EINVAL;
5201
Francois Romieud58d46b2011-05-03 16:38:29 +02005202 if (new_mtu > ETH_DATA_LEN)
5203 rtl_hw_jumbo_enable(tp);
5204 else
5205 rtl_hw_jumbo_disable(tp);
5206
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005208 netdev_update_features(dev);
5209
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005210 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211}
5212
5213static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5214{
Al Viro95e09182007-12-22 18:55:39 +00005215 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5217}
5218
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005219static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5220 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005222 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005223 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005224
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005225 kfree(*data_buff);
5226 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 rtl8169_make_unusable_by_asic(desc);
5228}
5229
5230static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5231{
5232 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5233
5234 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5235}
5236
5237static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5238 u32 rx_buf_sz)
5239{
5240 desc->addr = cpu_to_le64(mapping);
5241 wmb();
5242 rtl8169_mark_to_asic(desc, rx_buf_sz);
5243}
5244
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005245static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005247 return (void *)ALIGN((long)data, 16);
5248}
5249
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005250static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5251 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005252{
5253 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005255 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005256 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005257 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005259 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5260 if (!data)
5261 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005262
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005263 if (rtl8169_align(data) != data) {
5264 kfree(data);
5265 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5266 if (!data)
5267 return NULL;
5268 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005269
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005270 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005271 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005272 if (unlikely(dma_mapping_error(d, mapping))) {
5273 if (net_ratelimit())
5274 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005275 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277
5278 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005279 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005280
5281err_out:
5282 kfree(data);
5283 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284}
5285
5286static void rtl8169_rx_clear(struct rtl8169_private *tp)
5287{
Francois Romieu07d3f512007-02-21 22:40:46 +01005288 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289
5290 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005291 if (tp->Rx_databuff[i]) {
5292 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 tp->RxDescArray + i);
5294 }
5295 }
5296}
5297
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005298static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005300 desc->opts1 |= cpu_to_le32(RingEnd);
5301}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005302
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005303static int rtl8169_rx_fill(struct rtl8169_private *tp)
5304{
5305 unsigned int i;
5306
5307 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005308 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005309
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005310 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005312
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005313 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005314 if (!data) {
5315 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005316 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005317 }
5318 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005321 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5322 return 0;
5323
5324err_out:
5325 rtl8169_rx_clear(tp);
5326 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327}
5328
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329static int rtl8169_init_ring(struct net_device *dev)
5330{
5331 struct rtl8169_private *tp = netdev_priv(dev);
5332
5333 rtl8169_init_ring_indexes(tp);
5334
5335 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005336 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005338 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339}
5340
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005341static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 struct TxDesc *desc)
5343{
5344 unsigned int len = tx_skb->len;
5345
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005346 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5347
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 desc->opts1 = 0x00;
5349 desc->opts2 = 0x00;
5350 desc->addr = 0x00;
5351 tx_skb->len = 0;
5352}
5353
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005354static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5355 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356{
5357 unsigned int i;
5358
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005359 for (i = 0; i < n; i++) {
5360 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 struct ring_info *tx_skb = tp->tx_skb + entry;
5362 unsigned int len = tx_skb->len;
5363
5364 if (len) {
5365 struct sk_buff *skb = tx_skb->skb;
5366
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005367 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 tp->TxDescArray + entry);
5369 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005370 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 dev_kfree_skb(skb);
5372 tx_skb->skb = NULL;
5373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 }
5375 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005376}
5377
5378static void rtl8169_tx_clear(struct rtl8169_private *tp)
5379{
5380 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 tp->cur_tx = tp->dirty_tx = 0;
Igor Maravic036dafa2012-03-05 00:01:25 +01005382 netdev_reset_queue(tp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383}
5384
Francois Romieu4422bcd2012-01-26 11:23:32 +01005385static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386{
David Howellsc4028952006-11-22 14:57:56 +00005387 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005388 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389
Francois Romieuda78dbf2012-01-26 14:18:23 +01005390 napi_disable(&tp->napi);
5391 netif_stop_queue(dev);
5392 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393
françois romieuc7c2c392011-12-04 20:30:52 +00005394 rtl8169_hw_reset(tp);
5395
Francois Romieu56de4142011-03-15 17:29:31 +01005396 for (i = 0; i < NUM_RX_DESC; i++)
5397 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5398
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005400 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401
Francois Romieuda78dbf2012-01-26 14:18:23 +01005402 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01005403 rtl_hw_start(dev);
5404 netif_wake_queue(dev);
5405 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406}
5407
5408static void rtl8169_tx_timeout(struct net_device *dev)
5409{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005410 struct rtl8169_private *tp = netdev_priv(dev);
5411
5412 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413}
5414
5415static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005416 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417{
5418 struct skb_shared_info *info = skb_shinfo(skb);
5419 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005420 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005421 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422
5423 entry = tp->cur_tx;
5424 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005425 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 dma_addr_t mapping;
5427 u32 status, len;
5428 void *addr;
5429
5430 entry = (entry + 1) % NUM_TX_DESC;
5431
5432 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005433 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005434 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005435 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005436 if (unlikely(dma_mapping_error(d, mapping))) {
5437 if (net_ratelimit())
5438 netif_err(tp, drv, tp->dev,
5439 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005440 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442
Francois Romieucecb5fd2011-04-01 10:21:07 +02005443 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005444 status = opts[0] | len |
5445 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
5447 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005448 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 txd->addr = cpu_to_le64(mapping);
5450
5451 tp->tx_skb[entry].len = len;
5452 }
5453
5454 if (cur_frag) {
5455 tp->tx_skb[entry].skb = skb;
5456 txd->opts1 |= cpu_to_le32(LastFrag);
5457 }
5458
5459 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005460
5461err_out:
5462 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5463 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464}
5465
Francois Romieu2b7b4312011-04-18 22:53:24 -07005466static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5467 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005469 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005470 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005471 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472
Francois Romieu2b7b4312011-04-18 22:53:24 -07005473 if (mss) {
5474 opts[0] |= TD_LSO;
5475 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5476 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005477 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478
5479 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005480 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005482 opts[offset] |= info->checksum.udp;
5483 else
5484 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486}
5487
Stephen Hemminger613573252009-08-31 19:50:58 +00005488static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5489 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490{
5491 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005492 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 struct TxDesc *txd = tp->TxDescArray + entry;
5494 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005495 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 dma_addr_t mapping;
5497 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005498 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005499 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005500
Julien Ducourthial477206a2012-05-09 00:00:06 +02005501 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005502 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005503 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 }
5505
5506 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005507 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005509 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005510 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005511 if (unlikely(dma_mapping_error(d, mapping))) {
5512 if (net_ratelimit())
5513 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005514 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516
5517 tp->tx_skb[entry].len = len;
5518 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519
Francois Romieu2b7b4312011-04-18 22:53:24 -07005520 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5521 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005522
Francois Romieu2b7b4312011-04-18 22:53:24 -07005523 rtl8169_tso_csum(tp, skb, opts);
5524
5525 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005526 if (frags < 0)
5527 goto err_dma_1;
5528 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005529 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005530 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005531 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005532 tp->tx_skb[entry].skb = skb;
5533 }
5534
Francois Romieu2b7b4312011-04-18 22:53:24 -07005535 txd->opts2 = cpu_to_le32(opts[1]);
5536
Igor Maravic036dafa2012-03-05 00:01:25 +01005537 netdev_sent_queue(dev, skb->len);
5538
Richard Cochran5047fb52012-03-10 07:29:42 +00005539 skb_tx_timestamp(skb);
5540
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 wmb();
5542
Francois Romieucecb5fd2011-04-01 10:21:07 +02005543 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005544 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 txd->opts1 = cpu_to_le32(status);
5546
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 tp->cur_tx += frags + 1;
5548
David Dillow4c020a92010-03-03 16:33:10 +00005549 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550
Francois Romieucecb5fd2011-04-01 10:21:07 +02005551 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552
Francois Romieuda78dbf2012-01-26 14:18:23 +01005553 mmiowb();
5554
Julien Ducourthial477206a2012-05-09 00:00:06 +02005555 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01005556 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5557 * not miss a ring update when it notices a stopped queue.
5558 */
5559 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01005561 /* Sync with rtl_tx:
5562 * - publish queue status and cur_tx ring index (write barrier)
5563 * - refresh dirty_tx ring index (read barrier).
5564 * May the current thread have a pessimistic view of the ring
5565 * status and forget to wake up queue, a racing rtl_tx thread
5566 * can't.
5567 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005568 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02005569 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 netif_wake_queue(dev);
5571 }
5572
Stephen Hemminger613573252009-08-31 19:50:58 +00005573 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005575err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005576 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005577err_dma_0:
5578 dev_kfree_skb(skb);
5579 dev->stats.tx_dropped++;
5580 return NETDEV_TX_OK;
5581
5582err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005584 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005585 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586}
5587
5588static void rtl8169_pcierr_interrupt(struct net_device *dev)
5589{
5590 struct rtl8169_private *tp = netdev_priv(dev);
5591 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 u16 pci_status, pci_cmd;
5593
5594 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5595 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5596
Joe Perchesbf82c182010-02-09 11:49:50 +00005597 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5598 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599
5600 /*
5601 * The recovery sequence below admits a very elaborated explanation:
5602 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005603 * - I did not see what else could be done;
5604 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 *
5606 * Feel free to adjust to your needs.
5607 */
Francois Romieua27993f2006-12-18 00:04:19 +01005608 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005609 pci_cmd &= ~PCI_COMMAND_PARITY;
5610 else
5611 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5612
5613 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614
5615 pci_write_config_word(pdev, PCI_STATUS,
5616 pci_status & (PCI_STATUS_DETECTED_PARITY |
5617 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5618 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5619
5620 /* The infamous DAC f*ckup only happens at boot time */
5621 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005622 void __iomem *ioaddr = tp->mmio_addr;
5623
Joe Perchesbf82c182010-02-09 11:49:50 +00005624 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 tp->cp_cmd &= ~PCIDAC;
5626 RTL_W16(CPlusCmd, tp->cp_cmd);
5627 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 }
5629
françois romieue6de30d2011-01-03 15:08:37 +00005630 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005631
Francois Romieu98ddf982012-01-31 10:47:34 +01005632 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633}
5634
Igor Maravic036dafa2012-03-05 00:01:25 +01005635struct rtl_txc {
5636 int packets;
5637 int bytes;
5638};
5639
Francois Romieuda78dbf2012-01-26 14:18:23 +01005640static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641{
Igor Maravic036dafa2012-03-05 00:01:25 +01005642 struct rtl8169_stats *tx_stats = &tp->tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 unsigned int dirty_tx, tx_left;
Igor Maravic036dafa2012-03-05 00:01:25 +01005644 struct rtl_txc txc = { 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 dirty_tx = tp->dirty_tx;
5647 smp_rmb();
5648 tx_left = tp->cur_tx - dirty_tx;
5649
5650 while (tx_left > 0) {
5651 unsigned int entry = dirty_tx % NUM_TX_DESC;
5652 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 u32 status;
5654
5655 rmb();
5656 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5657 if (status & DescOwn)
5658 break;
5659
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005660 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5661 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 if (status & LastFrag) {
Igor Maravic036dafa2012-03-05 00:01:25 +01005663 struct sk_buff *skb = tx_skb->skb;
5664
5665 txc.packets++;
5666 txc.bytes += skb->len;
5667 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 tx_skb->skb = NULL;
5669 }
5670 dirty_tx++;
5671 tx_left--;
5672 }
5673
Igor Maravic036dafa2012-03-05 00:01:25 +01005674 u64_stats_update_begin(&tx_stats->syncp);
5675 tx_stats->packets += txc.packets;
5676 tx_stats->bytes += txc.bytes;
5677 u64_stats_update_end(&tx_stats->syncp);
5678
5679 netdev_completed_queue(dev, txc.packets, txc.bytes);
5680
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 if (tp->dirty_tx != dirty_tx) {
5682 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005683 /* Sync with rtl8169_start_xmit:
5684 * - publish dirty_tx ring index (write barrier)
5685 * - refresh cur_tx ring index and queue status (read barrier)
5686 * May the current thread miss the stopped queue condition,
5687 * a racing xmit thread can only have a right view of the
5688 * ring status.
5689 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005690 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02005692 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 netif_wake_queue(dev);
5694 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005695 /*
5696 * 8168 hack: TxPoll requests are lost when the Tx packets are
5697 * too close. Let's kick an extra TxPoll request when a burst
5698 * of start_xmit activity is detected (if it is not detected,
5699 * it is slow enough). -- FR
5700 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005701 if (tp->cur_tx != dirty_tx) {
5702 void __iomem *ioaddr = tp->mmio_addr;
5703
Francois Romieud78ae2d2007-08-26 20:08:19 +02005704 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 }
5707}
5708
Francois Romieu126fa4b2005-05-12 20:09:17 -04005709static inline int rtl8169_fragmented_frame(u32 status)
5710{
5711 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5712}
5713
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005714static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 u32 status = opts1 & RxProtoMask;
5717
5718 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005719 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 skb->ip_summed = CHECKSUM_UNNECESSARY;
5721 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005722 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723}
5724
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005725static struct sk_buff *rtl8169_try_rx_copy(void *data,
5726 struct rtl8169_private *tp,
5727 int pkt_size,
5728 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005730 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005731 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005733 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005734 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005735 prefetch(data);
5736 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5737 if (skb)
5738 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005739 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5740
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005741 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742}
5743
Francois Romieuda78dbf2012-01-26 14:18:23 +01005744static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745{
5746 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005747 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 cur_rx = tp->cur_rx;
5750 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005751 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005753 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005755 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 u32 status;
5757
5758 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005759 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760
5761 if (status & DescOwn)
5762 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005763 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005764 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5765 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005766 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005768 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005770 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005771 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005772 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005773 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005774 }
Ben Greear6bbe0212012-02-10 15:04:33 +00005775 if ((status & (RxRUNT | RxCRC)) &&
5776 !(status & (RxRWT | RxFOVF)) &&
5777 (dev->features & NETIF_F_RXALL))
5778 goto process_pkt;
5779
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005780 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005782 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00005783 dma_addr_t addr;
5784 int pkt_size;
5785
5786process_pkt:
5787 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00005788 if (likely(!(dev->features & NETIF_F_RXFCS)))
5789 pkt_size = (status & 0x00003fff) - 4;
5790 else
5791 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792
Francois Romieu126fa4b2005-05-12 20:09:17 -04005793 /*
5794 * The driver does not support incoming fragmented
5795 * frames. They are seen as a symptom of over-mtu
5796 * sized frames.
5797 */
5798 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005799 dev->stats.rx_dropped++;
5800 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005801 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005802 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005803 }
5804
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005805 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5806 tp, pkt_size, addr);
5807 rtl8169_mark_to_asic(desc, rx_buf_sz);
5808 if (!skb) {
5809 dev->stats.rx_dropped++;
5810 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 }
5812
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005813 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 skb_put(skb, pkt_size);
5815 skb->protocol = eth_type_trans(skb, dev);
5816
Francois Romieu7a8fc772011-03-01 17:18:33 +01005817 rtl8169_rx_vlan_tag(desc, skb);
5818
Francois Romieu56de4142011-03-15 17:29:31 +01005819 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820
Junchang Wang8027aa22012-03-04 23:30:32 +01005821 u64_stats_update_begin(&tp->rx_stats.syncp);
5822 tp->rx_stats.packets++;
5823 tp->rx_stats.bytes += pkt_size;
5824 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005826
5827 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005828 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005829 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5830 desc->opts2 = 0;
5831 cur_rx++;
5832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 }
5834
5835 count = cur_rx - tp->cur_rx;
5836 tp->cur_rx = cur_rx;
5837
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005838 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839
5840 return count;
5841}
5842
Francois Romieu07d3f512007-02-21 22:40:46 +01005843static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844{
Francois Romieu07d3f512007-02-21 22:40:46 +01005845 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005848 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005850 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005851 if (status && status != 0xffff) {
5852 status &= RTL_EVENT_NAPI | tp->event_slow;
5853 if (status) {
5854 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00005855
Francois Romieuda78dbf2012-01-26 14:18:23 +01005856 rtl_irq_disable(tp);
5857 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 return IRQ_RETVAL(handled);
5861}
5862
Francois Romieuda78dbf2012-01-26 14:18:23 +01005863/*
5864 * Workqueue context.
5865 */
5866static void rtl_slow_event_work(struct rtl8169_private *tp)
5867{
5868 struct net_device *dev = tp->dev;
5869 u16 status;
5870
5871 status = rtl_get_events(tp) & tp->event_slow;
5872 rtl_ack_events(tp, status);
5873
5874 if (unlikely(status & RxFIFOOver)) {
5875 switch (tp->mac_version) {
5876 /* Work around for rx fifo overflow */
5877 case RTL_GIGA_MAC_VER_11:
5878 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01005879 /* XXX - Hack alert. See rtl_task(). */
5880 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005881 default:
5882 break;
5883 }
5884 }
5885
5886 if (unlikely(status & SYSErr))
5887 rtl8169_pcierr_interrupt(dev);
5888
5889 if (status & LinkChg)
5890 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
5891
5892 napi_disable(&tp->napi);
5893 rtl_irq_disable(tp);
5894
5895 napi_enable(&tp->napi);
5896 napi_schedule(&tp->napi);
5897}
5898
Francois Romieu4422bcd2012-01-26 11:23:32 +01005899static void rtl_task(struct work_struct *work)
5900{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005901 static const struct {
5902 int bitnr;
5903 void (*action)(struct rtl8169_private *);
5904 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01005905 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005906 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
5907 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
5908 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
5909 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01005910 struct rtl8169_private *tp =
5911 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005912 struct net_device *dev = tp->dev;
5913 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01005914
Francois Romieuda78dbf2012-01-26 14:18:23 +01005915 rtl_lock_work(tp);
5916
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005917 if (!netif_running(dev) ||
5918 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01005919 goto out_unlock;
5920
5921 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
5922 bool pending;
5923
Francois Romieuda78dbf2012-01-26 14:18:23 +01005924 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005925 if (pending)
5926 rtl_work[i].action(tp);
5927 }
5928
5929out_unlock:
5930 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01005931}
5932
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005933static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005935 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5936 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005937 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
5938 int work_done= 0;
5939 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940
Francois Romieuda78dbf2012-01-26 14:18:23 +01005941 status = rtl_get_events(tp);
5942 rtl_ack_events(tp, status & ~tp->event_slow);
5943
5944 if (status & RTL_EVENT_NAPI_RX)
5945 work_done = rtl_rx(dev, tp, (u32) budget);
5946
5947 if (status & RTL_EVENT_NAPI_TX)
5948 rtl_tx(dev, tp);
5949
5950 if (status & tp->event_slow) {
5951 enable_mask &= ~tp->event_slow;
5952
5953 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
5954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005956 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005957 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005958
Francois Romieuda78dbf2012-01-26 14:18:23 +01005959 rtl_irq_enable(tp, enable_mask);
5960 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 }
5962
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005963 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965
Francois Romieu523a6092008-09-10 22:28:56 +02005966static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5967{
5968 struct rtl8169_private *tp = netdev_priv(dev);
5969
5970 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5971 return;
5972
5973 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5974 RTL_W32(RxMissed, 0);
5975}
5976
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977static void rtl8169_down(struct net_device *dev)
5978{
5979 struct rtl8169_private *tp = netdev_priv(dev);
5980 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981
Francois Romieu4876cc12011-03-11 21:07:11 +01005982 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005984 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005985 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986
Hayes Wang92fc43b2011-07-06 15:58:03 +08005987 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005988 /*
5989 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01005990 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
5991 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005992 */
Francois Romieu523a6092008-09-10 22:28:56 +02005993 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005996 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 rtl8169_tx_clear(tp);
5999
6000 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00006001
6002 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003}
6004
6005static int rtl8169_close(struct net_device *dev)
6006{
6007 struct rtl8169_private *tp = netdev_priv(dev);
6008 struct pci_dev *pdev = tp->pci_dev;
6009
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006010 pm_runtime_get_sync(&pdev->dev);
6011
Francois Romieucecb5fd2011-04-01 10:21:07 +02006012 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08006013 rtl8169_update_counters(dev);
6014
Francois Romieuda78dbf2012-01-26 14:18:23 +01006015 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006016 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006017
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006019 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006021 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00006023 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6024 tp->RxPhyAddr);
6025 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6026 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 tp->TxDescArray = NULL;
6028 tp->RxDescArray = NULL;
6029
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006030 pm_runtime_put_sync(&pdev->dev);
6031
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 return 0;
6033}
6034
Francois Romieudc1c00c2012-03-08 10:06:18 +01006035#ifdef CONFIG_NET_POLL_CONTROLLER
6036static void rtl8169_netpoll(struct net_device *dev)
6037{
6038 struct rtl8169_private *tp = netdev_priv(dev);
6039
6040 rtl8169_interrupt(tp->pci_dev->irq, dev);
6041}
6042#endif
6043
Francois Romieudf43ac72012-03-08 09:48:40 +01006044static int rtl_open(struct net_device *dev)
6045{
6046 struct rtl8169_private *tp = netdev_priv(dev);
6047 void __iomem *ioaddr = tp->mmio_addr;
6048 struct pci_dev *pdev = tp->pci_dev;
6049 int retval = -ENOMEM;
6050
6051 pm_runtime_get_sync(&pdev->dev);
6052
6053 /*
6054 * Rx and Tx desscriptors needs 256 bytes alignment.
6055 * dma_alloc_coherent provides more.
6056 */
6057 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6058 &tp->TxPhyAddr, GFP_KERNEL);
6059 if (!tp->TxDescArray)
6060 goto err_pm_runtime_put;
6061
6062 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6063 &tp->RxPhyAddr, GFP_KERNEL);
6064 if (!tp->RxDescArray)
6065 goto err_free_tx_0;
6066
6067 retval = rtl8169_init_ring(dev);
6068 if (retval < 0)
6069 goto err_free_rx_1;
6070
6071 INIT_WORK(&tp->wk.work, rtl_task);
6072
6073 smp_mb();
6074
6075 rtl_request_firmware(tp);
6076
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006077 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01006078 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
6079 dev->name, dev);
6080 if (retval < 0)
6081 goto err_release_fw_2;
6082
6083 rtl_lock_work(tp);
6084
6085 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6086
6087 napi_enable(&tp->napi);
6088
6089 rtl8169_init_phy(dev, tp);
6090
6091 __rtl8169_set_features(dev, dev->features);
6092
6093 rtl_pll_power_up(tp);
6094
6095 rtl_hw_start(dev);
6096
6097 netif_start_queue(dev);
6098
6099 rtl_unlock_work(tp);
6100
6101 tp->saved_wolopts = 0;
6102 pm_runtime_put_noidle(&pdev->dev);
6103
6104 rtl8169_check_link_status(dev, tp, ioaddr);
6105out:
6106 return retval;
6107
6108err_release_fw_2:
6109 rtl_release_firmware(tp);
6110 rtl8169_rx_clear(tp);
6111err_free_rx_1:
6112 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6113 tp->RxPhyAddr);
6114 tp->RxDescArray = NULL;
6115err_free_tx_0:
6116 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6117 tp->TxPhyAddr);
6118 tp->TxDescArray = NULL;
6119err_pm_runtime_put:
6120 pm_runtime_put_noidle(&pdev->dev);
6121 goto out;
6122}
6123
Junchang Wang8027aa22012-03-04 23:30:32 +01006124static struct rtnl_link_stats64 *
6125rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126{
6127 struct rtl8169_private *tp = netdev_priv(dev);
6128 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01006129 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130
Francois Romieuda78dbf2012-01-26 14:18:23 +01006131 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02006132 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02006133
Junchang Wang8027aa22012-03-04 23:30:32 +01006134 do {
6135 start = u64_stats_fetch_begin_bh(&tp->rx_stats.syncp);
6136 stats->rx_packets = tp->rx_stats.packets;
6137 stats->rx_bytes = tp->rx_stats.bytes;
6138 } while (u64_stats_fetch_retry_bh(&tp->rx_stats.syncp, start));
6139
6140
6141 do {
6142 start = u64_stats_fetch_begin_bh(&tp->tx_stats.syncp);
6143 stats->tx_packets = tp->tx_stats.packets;
6144 stats->tx_bytes = tp->tx_stats.bytes;
6145 } while (u64_stats_fetch_retry_bh(&tp->tx_stats.syncp, start));
6146
6147 stats->rx_dropped = dev->stats.rx_dropped;
6148 stats->tx_dropped = dev->stats.tx_dropped;
6149 stats->rx_length_errors = dev->stats.rx_length_errors;
6150 stats->rx_errors = dev->stats.rx_errors;
6151 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6152 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6153 stats->rx_missed_errors = dev->stats.rx_missed_errors;
6154
6155 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156}
6157
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006158static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006159{
françois romieu065c27c2011-01-03 15:08:12 +00006160 struct rtl8169_private *tp = netdev_priv(dev);
6161
Francois Romieu5d06a992006-02-23 00:47:58 +01006162 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006163 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006164
6165 netif_device_detach(dev);
6166 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006167
6168 rtl_lock_work(tp);
6169 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006170 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006171 rtl_unlock_work(tp);
6172
6173 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006174}
Francois Romieu5d06a992006-02-23 00:47:58 +01006175
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006176#ifdef CONFIG_PM
6177
6178static int rtl8169_suspend(struct device *device)
6179{
6180 struct pci_dev *pdev = to_pci_dev(device);
6181 struct net_device *dev = pci_get_drvdata(pdev);
6182
6183 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02006184
Francois Romieu5d06a992006-02-23 00:47:58 +01006185 return 0;
6186}
6187
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006188static void __rtl8169_resume(struct net_device *dev)
6189{
françois romieu065c27c2011-01-03 15:08:12 +00006190 struct rtl8169_private *tp = netdev_priv(dev);
6191
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006192 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006193
6194 rtl_pll_power_up(tp);
6195
Artem Savkovcff4c162012-04-03 10:29:11 +00006196 rtl_lock_work(tp);
6197 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006198 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00006199 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006200
Francois Romieu98ddf982012-01-31 10:47:34 +01006201 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006202}
6203
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006204static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006205{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006206 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01006207 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006208 struct rtl8169_private *tp = netdev_priv(dev);
6209
6210 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01006211
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006212 if (netif_running(dev))
6213 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006214
Francois Romieu5d06a992006-02-23 00:47:58 +01006215 return 0;
6216}
6217
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006218static int rtl8169_runtime_suspend(struct device *device)
6219{
6220 struct pci_dev *pdev = to_pci_dev(device);
6221 struct net_device *dev = pci_get_drvdata(pdev);
6222 struct rtl8169_private *tp = netdev_priv(dev);
6223
6224 if (!tp->TxDescArray)
6225 return 0;
6226
Francois Romieuda78dbf2012-01-26 14:18:23 +01006227 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006228 tp->saved_wolopts = __rtl8169_get_wol(tp);
6229 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006230 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006231
6232 rtl8169_net_suspend(dev);
6233
6234 return 0;
6235}
6236
6237static int rtl8169_runtime_resume(struct device *device)
6238{
6239 struct pci_dev *pdev = to_pci_dev(device);
6240 struct net_device *dev = pci_get_drvdata(pdev);
6241 struct rtl8169_private *tp = netdev_priv(dev);
6242
6243 if (!tp->TxDescArray)
6244 return 0;
6245
Francois Romieuda78dbf2012-01-26 14:18:23 +01006246 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006247 __rtl8169_set_wol(tp, tp->saved_wolopts);
6248 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006249 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006250
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006251 rtl8169_init_phy(dev, tp);
6252
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006253 __rtl8169_resume(dev);
6254
6255 return 0;
6256}
6257
6258static int rtl8169_runtime_idle(struct device *device)
6259{
6260 struct pci_dev *pdev = to_pci_dev(device);
6261 struct net_device *dev = pci_get_drvdata(pdev);
6262 struct rtl8169_private *tp = netdev_priv(dev);
6263
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00006264 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006265}
6266
Alexey Dobriyan47145212009-12-14 18:00:08 -08006267static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006268 .suspend = rtl8169_suspend,
6269 .resume = rtl8169_resume,
6270 .freeze = rtl8169_suspend,
6271 .thaw = rtl8169_resume,
6272 .poweroff = rtl8169_suspend,
6273 .restore = rtl8169_resume,
6274 .runtime_suspend = rtl8169_runtime_suspend,
6275 .runtime_resume = rtl8169_runtime_resume,
6276 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006277};
6278
6279#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6280
6281#else /* !CONFIG_PM */
6282
6283#define RTL8169_PM_OPS NULL
6284
6285#endif /* !CONFIG_PM */
6286
David S. Miller1805b2f2011-10-24 18:18:09 -04006287static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6288{
6289 void __iomem *ioaddr = tp->mmio_addr;
6290
6291 /* WoL fails with 8168b when the receiver is disabled. */
6292 switch (tp->mac_version) {
6293 case RTL_GIGA_MAC_VER_11:
6294 case RTL_GIGA_MAC_VER_12:
6295 case RTL_GIGA_MAC_VER_17:
6296 pci_clear_master(tp->pci_dev);
6297
6298 RTL_W8(ChipCmd, CmdRxEnb);
6299 /* PCI commit */
6300 RTL_R8(ChipCmd);
6301 break;
6302 default:
6303 break;
6304 }
6305}
6306
Francois Romieu1765f952008-09-13 17:21:40 +02006307static void rtl_shutdown(struct pci_dev *pdev)
6308{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006309 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006310 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00006311 struct device *d = &pdev->dev;
6312
6313 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02006314
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006315 rtl8169_net_suspend(dev);
6316
Francois Romieucecb5fd2011-04-01 10:21:07 +02006317 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006318 rtl_rar_set(tp, dev->perm_addr);
6319
Hayes Wang92fc43b2011-07-06 15:58:03 +08006320 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006321
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006322 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006323 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6324 rtl_wol_suspend_quirk(tp);
6325 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006326 }
6327
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006328 pci_wake_from_d3(pdev, true);
6329 pci_set_power_state(pdev, PCI_D3hot);
6330 }
françois romieu2a15cd22012-03-06 01:14:12 +00006331
6332 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006333}
Francois Romieu5d06a992006-02-23 00:47:58 +01006334
Francois Romieue27566e2012-03-08 09:54:01 +01006335static void __devexit rtl_remove_one(struct pci_dev *pdev)
6336{
6337 struct net_device *dev = pci_get_drvdata(pdev);
6338 struct rtl8169_private *tp = netdev_priv(dev);
6339
6340 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6341 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6342 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6343 rtl8168_driver_stop(tp);
6344 }
6345
6346 cancel_work_sync(&tp->wk.work);
6347
6348 unregister_netdev(dev);
6349
6350 rtl_release_firmware(tp);
6351
6352 if (pci_dev_run_wake(pdev))
6353 pm_runtime_get_noresume(&pdev->dev);
6354
6355 /* restore original MAC address */
6356 rtl_rar_set(tp, dev->perm_addr);
6357
6358 rtl_disable_msi(pdev, tp);
6359 rtl8169_release_board(pdev, dev, tp->mmio_addr);
6360 pci_set_drvdata(pdev, NULL);
6361}
6362
Francois Romieufa9c3852012-03-08 10:01:50 +01006363static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01006364 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01006365 .ndo_stop = rtl8169_close,
6366 .ndo_get_stats64 = rtl8169_get_stats64,
6367 .ndo_start_xmit = rtl8169_start_xmit,
6368 .ndo_tx_timeout = rtl8169_tx_timeout,
6369 .ndo_validate_addr = eth_validate_addr,
6370 .ndo_change_mtu = rtl8169_change_mtu,
6371 .ndo_fix_features = rtl8169_fix_features,
6372 .ndo_set_features = rtl8169_set_features,
6373 .ndo_set_mac_address = rtl_set_mac_address,
6374 .ndo_do_ioctl = rtl8169_ioctl,
6375 .ndo_set_rx_mode = rtl_set_rx_mode,
6376#ifdef CONFIG_NET_POLL_CONTROLLER
6377 .ndo_poll_controller = rtl8169_netpoll,
6378#endif
6379
6380};
6381
Francois Romieu31fa8b12012-03-08 10:09:40 +01006382static const struct rtl_cfg_info {
6383 void (*hw_start)(struct net_device *);
6384 unsigned int region;
6385 unsigned int align;
6386 u16 event_slow;
6387 unsigned features;
6388 u8 default_ver;
6389} rtl_cfg_infos [] = {
6390 [RTL_CFG_0] = {
6391 .hw_start = rtl_hw_start_8169,
6392 .region = 1,
6393 .align = 0,
6394 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
6395 .features = RTL_FEATURE_GMII,
6396 .default_ver = RTL_GIGA_MAC_VER_01,
6397 },
6398 [RTL_CFG_1] = {
6399 .hw_start = rtl_hw_start_8168,
6400 .region = 2,
6401 .align = 8,
6402 .event_slow = SYSErr | LinkChg | RxOverflow,
6403 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
6404 .default_ver = RTL_GIGA_MAC_VER_11,
6405 },
6406 [RTL_CFG_2] = {
6407 .hw_start = rtl_hw_start_8101,
6408 .region = 2,
6409 .align = 8,
6410 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
6411 PCSTimeout,
6412 .features = RTL_FEATURE_MSI,
6413 .default_ver = RTL_GIGA_MAC_VER_13,
6414 }
6415};
6416
6417/* Cfg9346_Unlock assumed. */
6418static unsigned rtl_try_msi(struct rtl8169_private *tp,
6419 const struct rtl_cfg_info *cfg)
6420{
6421 void __iomem *ioaddr = tp->mmio_addr;
6422 unsigned msi = 0;
6423 u8 cfg2;
6424
6425 cfg2 = RTL_R8(Config2) & ~MSIEnable;
6426 if (cfg->features & RTL_FEATURE_MSI) {
6427 if (pci_enable_msi(tp->pci_dev)) {
6428 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
6429 } else {
6430 cfg2 |= MSIEnable;
6431 msi = RTL_FEATURE_MSI;
6432 }
6433 }
6434 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6435 RTL_W8(Config2, cfg2);
6436 return msi;
6437}
6438
Francois Romieu3b6cf252012-03-08 09:59:04 +01006439static int __devinit
6440rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6441{
6442 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
6443 const unsigned int region = cfg->region;
6444 struct rtl8169_private *tp;
6445 struct mii_if_info *mii;
6446 struct net_device *dev;
6447 void __iomem *ioaddr;
6448 int chipset, i;
6449 int rc;
6450
6451 if (netif_msg_drv(&debug)) {
6452 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
6453 MODULENAME, RTL8169_VERSION);
6454 }
6455
6456 dev = alloc_etherdev(sizeof (*tp));
6457 if (!dev) {
6458 rc = -ENOMEM;
6459 goto out;
6460 }
6461
6462 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01006463 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006464 tp = netdev_priv(dev);
6465 tp->dev = dev;
6466 tp->pci_dev = pdev;
6467 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
6468
6469 mii = &tp->mii;
6470 mii->dev = dev;
6471 mii->mdio_read = rtl_mdio_read;
6472 mii->mdio_write = rtl_mdio_write;
6473 mii->phy_id_mask = 0x1f;
6474 mii->reg_num_mask = 0x1f;
6475 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
6476
6477 /* disable ASPM completely as that cause random device stop working
6478 * problems as well as full system hangs for some PCIe devices users */
6479 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6480 PCIE_LINK_STATE_CLKPM);
6481
6482 /* enable device (incl. PCI PM wakeup and hotplug setup) */
6483 rc = pci_enable_device(pdev);
6484 if (rc < 0) {
6485 netif_err(tp, probe, dev, "enable failure\n");
6486 goto err_out_free_dev_1;
6487 }
6488
6489 if (pci_set_mwi(pdev) < 0)
6490 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
6491
6492 /* make sure PCI base addr 1 is MMIO */
6493 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
6494 netif_err(tp, probe, dev,
6495 "region #%d not an MMIO resource, aborting\n",
6496 region);
6497 rc = -ENODEV;
6498 goto err_out_mwi_2;
6499 }
6500
6501 /* check for weird/broken PCI region reporting */
6502 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
6503 netif_err(tp, probe, dev,
6504 "Invalid PCI region size(s), aborting\n");
6505 rc = -ENODEV;
6506 goto err_out_mwi_2;
6507 }
6508
6509 rc = pci_request_regions(pdev, MODULENAME);
6510 if (rc < 0) {
6511 netif_err(tp, probe, dev, "could not request regions\n");
6512 goto err_out_mwi_2;
6513 }
6514
6515 tp->cp_cmd = RxChkSum;
6516
6517 if ((sizeof(dma_addr_t) > 4) &&
6518 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
6519 tp->cp_cmd |= PCIDAC;
6520 dev->features |= NETIF_F_HIGHDMA;
6521 } else {
6522 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6523 if (rc < 0) {
6524 netif_err(tp, probe, dev, "DMA configuration failed\n");
6525 goto err_out_free_res_3;
6526 }
6527 }
6528
6529 /* ioremap MMIO region */
6530 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
6531 if (!ioaddr) {
6532 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
6533 rc = -EIO;
6534 goto err_out_free_res_3;
6535 }
6536 tp->mmio_addr = ioaddr;
6537
6538 if (!pci_is_pcie(pdev))
6539 netif_info(tp, probe, dev, "not PCI Express\n");
6540
6541 /* Identify chip attached to board */
6542 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
6543
6544 rtl_init_rxcfg(tp);
6545
6546 rtl_irq_disable(tp);
6547
6548 rtl_hw_reset(tp);
6549
6550 rtl_ack_events(tp, 0xffff);
6551
6552 pci_set_master(pdev);
6553
6554 /*
6555 * Pretend we are using VLANs; This bypasses a nasty bug where
6556 * Interrupts stop flowing on high load on 8110SCd controllers.
6557 */
6558 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6559 tp->cp_cmd |= RxVlan;
6560
6561 rtl_init_mdio_ops(tp);
6562 rtl_init_pll_power_ops(tp);
6563 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006564 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006565
6566 rtl8169_print_mac_version(tp);
6567
6568 chipset = tp->mac_version;
6569 tp->txd_version = rtl_chip_infos[chipset].txd_version;
6570
6571 RTL_W8(Cfg9346, Cfg9346_Unlock);
6572 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
6573 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
6574 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
6575 tp->features |= RTL_FEATURE_WOL;
6576 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
6577 tp->features |= RTL_FEATURE_WOL;
6578 tp->features |= rtl_try_msi(tp, cfg);
6579 RTL_W8(Cfg9346, Cfg9346_Lock);
6580
6581 if (rtl_tbi_enabled(tp)) {
6582 tp->set_speed = rtl8169_set_speed_tbi;
6583 tp->get_settings = rtl8169_gset_tbi;
6584 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
6585 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
6586 tp->link_ok = rtl8169_tbi_link_ok;
6587 tp->do_ioctl = rtl_tbi_ioctl;
6588 } else {
6589 tp->set_speed = rtl8169_set_speed_xmii;
6590 tp->get_settings = rtl8169_gset_xmii;
6591 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
6592 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
6593 tp->link_ok = rtl8169_xmii_link_ok;
6594 tp->do_ioctl = rtl_xmii_ioctl;
6595 }
6596
6597 mutex_init(&tp->wk.mutex);
6598
6599 /* Get MAC address */
6600 for (i = 0; i < ETH_ALEN; i++)
6601 dev->dev_addr[i] = RTL_R8(MAC0 + i);
6602 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
6603
6604 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
6605 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006606
6607 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
6608
6609 /* don't enable SG, IP_CSUM and TSO by default - it might not work
6610 * properly for all devices */
6611 dev->features |= NETIF_F_RXCSUM |
6612 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6613
6614 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6615 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6616 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6617 NETIF_F_HIGHDMA;
6618
6619 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6620 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
6621 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
6622
6623 dev->hw_features |= NETIF_F_RXALL;
6624 dev->hw_features |= NETIF_F_RXFCS;
6625
6626 tp->hw_start = cfg->hw_start;
6627 tp->event_slow = cfg->event_slow;
6628
6629 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
6630 ~(RxBOVF | RxFOVF) : ~0;
6631
6632 init_timer(&tp->timer);
6633 tp->timer.data = (unsigned long) dev;
6634 tp->timer.function = rtl8169_phy_timer;
6635
6636 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
6637
6638 rc = register_netdev(dev);
6639 if (rc < 0)
6640 goto err_out_msi_4;
6641
6642 pci_set_drvdata(pdev, dev);
6643
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006644 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
6645 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
6646 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006647 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
6648 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
6649 "tx checksumming: %s]\n",
6650 rtl_chip_infos[chipset].jumbo_max,
6651 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
6652 }
6653
6654 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6655 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6656 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6657 rtl8168_driver_start(tp);
6658 }
6659
6660 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
6661
6662 if (pci_dev_run_wake(pdev))
6663 pm_runtime_put_noidle(&pdev->dev);
6664
6665 netif_carrier_off(dev);
6666
6667out:
6668 return rc;
6669
6670err_out_msi_4:
6671 rtl_disable_msi(pdev, tp);
6672 iounmap(ioaddr);
6673err_out_free_res_3:
6674 pci_release_regions(pdev);
6675err_out_mwi_2:
6676 pci_clear_mwi(pdev);
6677 pci_disable_device(pdev);
6678err_out_free_dev_1:
6679 free_netdev(dev);
6680 goto out;
6681}
6682
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683static struct pci_driver rtl8169_pci_driver = {
6684 .name = MODULENAME,
6685 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01006686 .probe = rtl_init_one,
Francois Romieue27566e2012-03-08 09:54:01 +01006687 .remove = __devexit_p(rtl_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02006688 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006689 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690};
6691
Francois Romieu07d3f512007-02-21 22:40:46 +01006692static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693{
Jeff Garzik29917622006-08-19 17:48:59 -04006694 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695}
6696
Francois Romieu07d3f512007-02-21 22:40:46 +01006697static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698{
6699 pci_unregister_driver(&rtl8169_pci_driver);
6700}
6701
6702module_init(rtl8169_init_module);
6703module_exit(rtl8169_cleanup_module);