blob: e7e7d3735c19665bd39592682d92c42f3f2b0d06 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Francois Romieu07d3f512007-02-21 22:40:46 +01002 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3 *
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
7 *
8 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/delay.h>
17#include <linux/ethtool.h>
18#include <linux/mii.h>
19#include <linux/if_vlan.h>
20#include <linux/crc32.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/tcp.h>
24#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000025#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000027#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000028#include <linux/firmware.h>
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +000029#include <linux/pci-aspm.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040030#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <asm/io.h>
33#include <asm/irq.h>
34
Francois Romieu865c6522008-05-11 14:51:00 +020035#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define MODULENAME "r8169"
37#define PFX MODULENAME ": "
38
françois romieubca03d52011-01-03 15:07:31 +000039#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
40#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000041#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
42#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080043#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080044#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
45#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080046#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
Hayes Wang7e18dca2012-03-30 14:33:02 +080047#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
Hayes Wangb3d7b2f2012-03-30 14:48:06 +080048#define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
Hayes Wang5598bfe2012-07-02 17:23:21 +080049#define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
Hayes Wangc5583862012-07-02 17:23:22 +080050#define FIRMWARE_8168G_1 "rtl_nic/rtl8168g-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#ifdef RTL8169_DEBUG
53#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020054 if (!(expr)) { \
55 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070056 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020057 }
Joe Perches06fa7352007-10-18 21:15:00 +020058#define dprintk(fmt, args...) \
59 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#else
61#define assert(expr) do {} while (0)
62#define dprintk(fmt, args...) do {} while (0)
63#endif /* RTL8169_DEBUG */
64
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020065#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070066 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020067
Julien Ducourthial477206a2012-05-09 00:00:06 +020068#define TX_SLOTS_AVAIL(tp) \
69 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
70
71/* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
72#define TX_FRAGS_READY_FOR(tp,nr_frags) \
73 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
76 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050077static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Francois Romieu9c14cea2008-07-05 00:21:15 +020079#define MAX_READ_REQUEST_SHIFT 12
Michal Schmidtaee77e42012-09-09 13:55:26 +000080#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
82
83#define R8169_REGS_SIZE 256
84#define R8169_NAPI_WEIGHT 64
85#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000086#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#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
93/* write/read MMIO register */
94#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
95#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
96#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
97#define RTL_R8(reg) readb (ioaddr + (reg))
98#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +000099#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200102 RTL_GIGA_MAC_VER_01 = 0,
103 RTL_GIGA_MAC_VER_02,
104 RTL_GIGA_MAC_VER_03,
105 RTL_GIGA_MAC_VER_04,
106 RTL_GIGA_MAC_VER_05,
107 RTL_GIGA_MAC_VER_06,
108 RTL_GIGA_MAC_VER_07,
109 RTL_GIGA_MAC_VER_08,
110 RTL_GIGA_MAC_VER_09,
111 RTL_GIGA_MAC_VER_10,
112 RTL_GIGA_MAC_VER_11,
113 RTL_GIGA_MAC_VER_12,
114 RTL_GIGA_MAC_VER_13,
115 RTL_GIGA_MAC_VER_14,
116 RTL_GIGA_MAC_VER_15,
117 RTL_GIGA_MAC_VER_16,
118 RTL_GIGA_MAC_VER_17,
119 RTL_GIGA_MAC_VER_18,
120 RTL_GIGA_MAC_VER_19,
121 RTL_GIGA_MAC_VER_20,
122 RTL_GIGA_MAC_VER_21,
123 RTL_GIGA_MAC_VER_22,
124 RTL_GIGA_MAC_VER_23,
125 RTL_GIGA_MAC_VER_24,
126 RTL_GIGA_MAC_VER_25,
127 RTL_GIGA_MAC_VER_26,
128 RTL_GIGA_MAC_VER_27,
129 RTL_GIGA_MAC_VER_28,
130 RTL_GIGA_MAC_VER_29,
131 RTL_GIGA_MAC_VER_30,
132 RTL_GIGA_MAC_VER_31,
133 RTL_GIGA_MAC_VER_32,
134 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800135 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800136 RTL_GIGA_MAC_VER_35,
137 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800138 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800139 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800140 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800141 RTL_GIGA_MAC_VER_40,
142 RTL_GIGA_MAC_VER_41,
Francois Romieu85bffe62011-04-27 08:22:39 +0200143 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
Francois Romieu2b7b4312011-04-18 22:53:24 -0700146enum rtl_tx_desc_version {
147 RTL_TD_0 = 0,
148 RTL_TD_1 = 1,
149};
150
Francois Romieud58d46b2011-05-03 16:38:29 +0200151#define JUMBO_1K ETH_DATA_LEN
152#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
153#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
154#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
155#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
156
157#define _R(NAME,TD,FW,SZ,B) { \
158 .name = NAME, \
159 .txd_version = TD, \
160 .fw_name = FW, \
161 .jumbo_max = SZ, \
162 .jumbo_tx_csum = B \
163}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800165static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700167 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200168 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200169 u16 jumbo_max;
170 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200171} rtl_chip_infos[] = {
172 /* PCI devices. */
173 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200174 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200175 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200176 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200177 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200178 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200179 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200180 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200181 [RTL_GIGA_MAC_VER_05] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200182 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200183 [RTL_GIGA_MAC_VER_06] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200184 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200185 /* PCI-E devices. */
186 [RTL_GIGA_MAC_VER_07] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200187 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200188 [RTL_GIGA_MAC_VER_08] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200189 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200190 [RTL_GIGA_MAC_VER_09] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200191 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200192 [RTL_GIGA_MAC_VER_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200193 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200194 [RTL_GIGA_MAC_VER_11] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200195 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200196 [RTL_GIGA_MAC_VER_12] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200197 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200198 [RTL_GIGA_MAC_VER_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200199 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200200 [RTL_GIGA_MAC_VER_14] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200201 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200202 [RTL_GIGA_MAC_VER_15] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200203 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200204 [RTL_GIGA_MAC_VER_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200205 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200206 [RTL_GIGA_MAC_VER_17] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200207 _R("RTL8168b/8111b", RTL_TD_1, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200208 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200209 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200210 [RTL_GIGA_MAC_VER_19] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200211 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200212 [RTL_GIGA_MAC_VER_20] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200213 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200214 [RTL_GIGA_MAC_VER_21] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200215 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200216 [RTL_GIGA_MAC_VER_22] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200217 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200218 [RTL_GIGA_MAC_VER_23] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200219 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200220 [RTL_GIGA_MAC_VER_24] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200221 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200222 [RTL_GIGA_MAC_VER_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200223 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
224 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200225 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200226 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
227 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200228 [RTL_GIGA_MAC_VER_27] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200229 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200230 [RTL_GIGA_MAC_VER_28] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200231 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200232 [RTL_GIGA_MAC_VER_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200233 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
234 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200235 [RTL_GIGA_MAC_VER_30] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200236 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
237 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200238 [RTL_GIGA_MAC_VER_31] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200239 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200240 [RTL_GIGA_MAC_VER_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200241 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
242 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200243 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200244 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
245 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800246 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200247 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
248 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800249 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200250 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
251 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800252 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200253 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
254 JUMBO_9K, false),
Hayes Wang7e18dca2012-03-30 14:33:02 +0800255 [RTL_GIGA_MAC_VER_37] =
256 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
257 JUMBO_1K, true),
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800258 [RTL_GIGA_MAC_VER_38] =
259 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
260 JUMBO_9K, false),
Hayes Wang5598bfe2012-07-02 17:23:21 +0800261 [RTL_GIGA_MAC_VER_39] =
262 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1,
263 JUMBO_1K, true),
Hayes Wangc5583862012-07-02 17:23:22 +0800264 [RTL_GIGA_MAC_VER_40] =
265 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_1,
266 JUMBO_9K, false),
267 [RTL_GIGA_MAC_VER_41] =
268 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269};
270#undef _R
271
Francois Romieubcf0bf92006-07-26 23:14:13 +0200272enum cfg_version {
273 RTL_CFG_0 = 0x00,
274 RTL_CFG_1,
275 RTL_CFG_2
276};
277
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000278static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200279 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200280 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200281 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100282 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200283 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
Francois Romieu2a35cfa2012-08-31 23:06:17 +0200284 { PCI_VENDOR_ID_DLINK, 0x4300,
285 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200286 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000287 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200288 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200289 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
290 { PCI_VENDOR_ID_LINKSYS, 0x1032,
291 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100292 { 0x0001, 0x8168,
293 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 {0,},
295};
296
297MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
298
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000299static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700300static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200301static struct {
302 u32 msg_enable;
303} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Francois Romieu07d3f512007-02-21 22:40:46 +0100305enum rtl_registers {
306 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100307 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100308 MAR0 = 8, /* Multicast filter. */
309 CounterAddrLow = 0x10,
310 CounterAddrHigh = 0x14,
311 TxDescStartAddrLow = 0x20,
312 TxDescStartAddrHigh = 0x24,
313 TxHDescStartAddrLow = 0x28,
314 TxHDescStartAddrHigh = 0x2c,
315 FLASH = 0x30,
316 ERSR = 0x36,
317 ChipCmd = 0x37,
318 TxPoll = 0x38,
319 IntrMask = 0x3c,
320 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700321
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800322 TxConfig = 0x40,
323#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
324#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
325
326 RxConfig = 0x44,
327#define RX128_INT_EN (1 << 15) /* 8111c and later */
328#define RX_MULTI_EN (1 << 14) /* 8111c only */
329#define RXCFG_FIFO_SHIFT 13
330 /* No threshold before first PCI xfer */
331#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
332#define RXCFG_DMA_SHIFT 8
333 /* Unlimited maximum PCI burst. */
334#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700335
Francois Romieu07d3f512007-02-21 22:40:46 +0100336 RxMissed = 0x4c,
337 Cfg9346 = 0x50,
338 Config0 = 0x51,
339 Config1 = 0x52,
340 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200341#define PME_SIGNAL (1 << 5) /* 8168c and later */
342
Francois Romieu07d3f512007-02-21 22:40:46 +0100343 Config3 = 0x54,
344 Config4 = 0x55,
345 Config5 = 0x56,
346 MultiIntr = 0x5c,
347 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100348 PHYstatus = 0x6c,
349 RxMaxSize = 0xda,
350 CPlusCmd = 0xe0,
351 IntrMitigate = 0xe2,
352 RxDescAddrLow = 0xe4,
353 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000354 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
355
356#define NoEarlyTx 0x3f /* Max value : no early transmit. */
357
358 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
359
360#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800361#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000362
Francois Romieu07d3f512007-02-21 22:40:46 +0100363 FuncEvent = 0xf0,
364 FuncEventMask = 0xf4,
365 FuncPresetState = 0xf8,
366 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367};
368
Francois Romieuf162a5d2008-06-01 22:37:49 +0200369enum rtl8110_registers {
370 TBICSR = 0x64,
371 TBI_ANAR = 0x68,
372 TBI_LPAR = 0x6a,
373};
374
375enum rtl8168_8101_registers {
376 CSIDR = 0x64,
377 CSIAR = 0x68,
378#define CSIAR_FLAG 0x80000000
379#define CSIAR_WRITE_CMD 0x80000000
380#define CSIAR_BYTE_ENABLE 0x0f
381#define CSIAR_BYTE_ENABLE_SHIFT 12
382#define CSIAR_ADDR_MASK 0x0fff
Hayes Wang7e18dca2012-03-30 14:33:02 +0800383#define CSIAR_FUNC_CARD 0x00000000
384#define CSIAR_FUNC_SDIO 0x00010000
385#define CSIAR_FUNC_NIC 0x00020000
françois romieu065c27c2011-01-03 15:08:12 +0000386 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200387 EPHYAR = 0x80,
388#define EPHYAR_FLAG 0x80000000
389#define EPHYAR_WRITE_CMD 0x80000000
390#define EPHYAR_REG_MASK 0x1f
391#define EPHYAR_REG_SHIFT 16
392#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800393 DLLPR = 0xd0,
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800394#define PFM_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200395 DBG_REG = 0xd1,
396#define FIX_NAK_1 (1 << 4)
397#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800398 TWSI = 0xd2,
399 MCU = 0xd3,
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800400#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800401#define TX_EMPTY (1 << 5)
402#define RX_EMPTY (1 << 4)
403#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800404#define EN_NDP (1 << 3)
405#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800406#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000407 EFUSEAR = 0xdc,
408#define EFUSEAR_FLAG 0x80000000
409#define EFUSEAR_WRITE_CMD 0x80000000
410#define EFUSEAR_READ_CMD 0x00000000
411#define EFUSEAR_REG_MASK 0x03ff
412#define EFUSEAR_REG_SHIFT 8
413#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200414};
415
françois romieuc0e45c12011-01-03 15:08:04 +0000416enum rtl8168_registers {
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800417 LED_FREQ = 0x1a,
418 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000419 ERIDR = 0x70,
420 ERIAR = 0x74,
421#define ERIAR_FLAG 0x80000000
422#define ERIAR_WRITE_CMD 0x80000000
423#define ERIAR_READ_CMD 0x00000000
424#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000425#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800426#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
427#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
428#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
429#define ERIAR_MASK_SHIFT 12
430#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
431#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800432#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800433#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000434 EPHY_RXER_NUM = 0x7c,
435 OCPDR = 0xb0, /* OCP GPHY access */
436#define OCPDR_WRITE_CMD 0x80000000
437#define OCPDR_READ_CMD 0x00000000
438#define OCPDR_REG_MASK 0x7f
439#define OCPDR_GPHY_REG_SHIFT 16
440#define OCPDR_DATA_MASK 0xffff
441 OCPAR = 0xb4,
442#define OCPAR_FLAG 0x80000000
443#define OCPAR_GPHY_WRITE_CMD 0x8000f060
444#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800445 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000446 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
447 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200448#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800449#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800450#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800451#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800452#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000453};
454
Francois Romieu07d3f512007-02-21 22:40:46 +0100455enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100457 SYSErr = 0x8000,
458 PCSTimeout = 0x4000,
459 SWInt = 0x0100,
460 TxDescUnavail = 0x0080,
461 RxFIFOOver = 0x0040,
462 LinkChg = 0x0020,
463 RxOverflow = 0x0010,
464 TxErr = 0x0008,
465 TxOK = 0x0004,
466 RxErr = 0x0002,
467 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400470 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200471 RxFOVF = (1 << 23),
472 RxRWT = (1 << 22),
473 RxRES = (1 << 21),
474 RxRUNT = (1 << 20),
475 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 /* ChipCmdBits */
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800478 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100479 CmdReset = 0x10,
480 CmdRxEnb = 0x08,
481 CmdTxEnb = 0x04,
482 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Francois Romieu275391a2007-02-23 23:50:28 +0100484 /* TXPoll register p.5 */
485 HPQ = 0x80, /* Poll cmd on the high prio queue */
486 NPQ = 0x40, /* Poll cmd on the low prio queue */
487 FSWInt = 0x01, /* Forced software interrupt */
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100490 Cfg9346_Lock = 0x00,
491 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100494 AcceptErr = 0x20,
495 AcceptRunt = 0x10,
496 AcceptBroadcast = 0x08,
497 AcceptMulticast = 0x04,
498 AcceptMyPhys = 0x02,
499 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200500#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /* TxConfigBits */
503 TxInterFrameGapShift = 24,
504 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
505
Francois Romieu5d06a992006-02-23 00:47:58 +0100506 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200507 LEDS1 = (1 << 7),
508 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200509 Speed_down = (1 << 4),
510 MEMMAP = (1 << 3),
511 IOMAP = (1 << 2),
512 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100513 PMEnable = (1 << 0), /* Power Management Enable */
514
Francois Romieu6dccd162007-02-13 23:38:05 +0100515 /* Config2 register p. 25 */
françois romieu2ca6cf02011-12-15 08:37:43 +0000516 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100517 PCI_Clock_66MHz = 0x01,
518 PCI_Clock_33MHz = 0x00,
519
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100520 /* Config3 register p.25 */
521 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
522 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200523 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200524 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100525
Francois Romieud58d46b2011-05-03 16:38:29 +0200526 /* Config4 register */
527 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
528
Francois Romieu5d06a992006-02-23 00:47:58 +0100529 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100530 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
531 MWF = (1 << 5), /* Accept Multicast wakeup frame */
532 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200533 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100534 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100535 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /* TBICSR p.28 */
538 TBIReset = 0x80000000,
539 TBILoopback = 0x40000000,
540 TBINwEnable = 0x20000000,
541 TBINwRestart = 0x10000000,
542 TBILinkOk = 0x02000000,
543 TBINwComplete = 0x01000000,
544
545 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200546 EnableBist = (1 << 15), // 8168 8101
547 Mac_dbgo_oe = (1 << 14), // 8168 8101
548 Normal_mode = (1 << 13), // unused
549 Force_half_dup = (1 << 12), // 8168 8101
550 Force_rxflow_en = (1 << 11), // 8168 8101
551 Force_txflow_en = (1 << 10), // 8168 8101
552 Cxpl_dbg_sel = (1 << 9), // 8168 8101
553 ASF = (1 << 8), // 8168 8101
554 PktCntrDisable = (1 << 7), // 8168 8101
555 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 RxVlan = (1 << 6),
557 RxChkSum = (1 << 5),
558 PCIDAC = (1 << 4),
559 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100560 INTT_0 = 0x0000, // 8168
561 INTT_1 = 0x0001, // 8168
562 INTT_2 = 0x0002, // 8168
563 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100566 TBI_Enable = 0x80,
567 TxFlowCtrl = 0x40,
568 RxFlowCtrl = 0x20,
569 _1000bpsF = 0x10,
570 _100bps = 0x08,
571 _10bps = 0x04,
572 LinkStatus = 0x02,
573 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100576 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200577
578 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100579 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580};
581
Francois Romieu2b7b4312011-04-18 22:53:24 -0700582enum rtl_desc_bit {
583 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
585 RingEnd = (1 << 30), /* End of descriptor ring */
586 FirstFrag = (1 << 29), /* First segment of a packet */
587 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700588};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Francois Romieu2b7b4312011-04-18 22:53:24 -0700590/* Generic case. */
591enum rtl_tx_desc_bit {
592 /* First doubleword. */
593 TD_LSO = (1 << 27), /* Large Send Offload */
594#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Francois Romieu2b7b4312011-04-18 22:53:24 -0700596 /* Second doubleword. */
597 TxVlanTag = (1 << 17), /* Add VLAN tag */
598};
599
600/* 8169, 8168b and 810x except 8102e. */
601enum rtl_tx_desc_bit_0 {
602 /* First doubleword. */
603#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
604 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
605 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
606 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
607};
608
609/* 8102e, 8168c and beyond. */
610enum rtl_tx_desc_bit_1 {
611 /* Second doubleword. */
612#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
613 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
614 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
615 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
616};
617
618static const struct rtl_tx_desc_info {
619 struct {
620 u32 udp;
621 u32 tcp;
622 } checksum;
623 u16 mss_shift;
624 u16 opts_offset;
625} tx_desc_info [] = {
626 [RTL_TD_0] = {
627 .checksum = {
628 .udp = TD0_IP_CS | TD0_UDP_CS,
629 .tcp = TD0_IP_CS | TD0_TCP_CS
630 },
631 .mss_shift = TD0_MSS_SHIFT,
632 .opts_offset = 0
633 },
634 [RTL_TD_1] = {
635 .checksum = {
636 .udp = TD1_IP_CS | TD1_UDP_CS,
637 .tcp = TD1_IP_CS | TD1_TCP_CS
638 },
639 .mss_shift = TD1_MSS_SHIFT,
640 .opts_offset = 1
641 }
642};
643
644enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /* Rx private */
646 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
647 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
648
649#define RxProtoUDP (PID1)
650#define RxProtoTCP (PID0)
651#define RxProtoIP (PID1 | PID0)
652#define RxProtoMask RxProtoIP
653
654 IPFail = (1 << 16), /* IP checksum failed */
655 UDPFail = (1 << 15), /* UDP/IP checksum failed */
656 TCPFail = (1 << 14), /* TCP/IP checksum failed */
657 RxVlanTag = (1 << 16), /* VLAN tag available */
658};
659
660#define RsvdMask 0x3fffc000
661
662struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200663 __le32 opts1;
664 __le32 opts2;
665 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666};
667
668struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200669 __le32 opts1;
670 __le32 opts2;
671 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672};
673
674struct ring_info {
675 struct sk_buff *skb;
676 u32 len;
677 u8 __pad[sizeof(void *) - sizeof(u32)];
678};
679
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200680enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200681 RTL_FEATURE_WOL = (1 << 0),
682 RTL_FEATURE_MSI = (1 << 1),
683 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200684};
685
Ivan Vecera355423d2009-02-06 21:49:57 -0800686struct rtl8169_counters {
687 __le64 tx_packets;
688 __le64 rx_packets;
689 __le64 tx_errors;
690 __le32 rx_errors;
691 __le16 rx_missed;
692 __le16 align_errors;
693 __le32 tx_one_collision;
694 __le32 tx_multi_collision;
695 __le64 rx_unicast;
696 __le64 rx_broadcast;
697 __le32 rx_multicast;
698 __le16 tx_aborted;
699 __le16 tx_underun;
700};
701
Francois Romieuda78dbf2012-01-26 14:18:23 +0100702enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100703 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100704 RTL_FLAG_TASK_SLOW_PENDING,
705 RTL_FLAG_TASK_RESET_PENDING,
706 RTL_FLAG_TASK_PHY_PENDING,
707 RTL_FLAG_MAX
708};
709
Junchang Wang8027aa22012-03-04 23:30:32 +0100710struct rtl8169_stats {
711 u64 packets;
712 u64 bytes;
713 struct u64_stats_sync syncp;
714};
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716struct rtl8169_private {
717 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200718 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000719 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700720 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200721 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700722 u16 txd_version;
723 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
725 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100727 struct rtl8169_stats rx_stats;
728 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
730 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
731 dma_addr_t TxPhyAddr;
732 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000733 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 struct timer_list timer;
736 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100737
738 u16 event_slow;
françois romieuc0e45c12011-01-03 15:08:04 +0000739
740 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200741 void (*write)(struct rtl8169_private *, int, int);
742 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000743 } mdio_ops;
744
françois romieu065c27c2011-01-03 15:08:12 +0000745 struct pll_power_ops {
746 void (*down)(struct rtl8169_private *);
747 void (*up)(struct rtl8169_private *);
748 } pll_power_ops;
749
Francois Romieud58d46b2011-05-03 16:38:29 +0200750 struct jumbo_ops {
751 void (*enable)(struct rtl8169_private *);
752 void (*disable)(struct rtl8169_private *);
753 } jumbo_ops;
754
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800755 struct csi_ops {
Francois Romieu52989f02012-07-06 13:37:00 +0200756 void (*write)(struct rtl8169_private *, int, int);
757 u32 (*read)(struct rtl8169_private *, int);
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800758 } csi_ops;
759
Oliver Neukum54405cd2011-01-06 21:55:13 +0100760 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200761 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000762 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100763 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000764 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800766 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100767
768 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100769 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
770 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100771 struct work_struct work;
772 } wk;
773
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200774 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200775
776 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800777 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000778 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400779 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000780
Francois Romieub6ffd972011-06-17 17:00:05 +0200781 struct rtl_fw {
782 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200783
784#define RTL_VER_SIZE 32
785
786 char version[RTL_VER_SIZE];
787
788 struct rtl_fw_phy_action {
789 __le32 *code;
790 size_t size;
791 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200792 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300793#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800794
795 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796};
797
Ralf Baechle979b6c12005-06-13 14:30:40 -0700798MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700801MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200802module_param_named(debug, debug.msg_enable, int, 0);
803MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804MODULE_LICENSE("GPL");
805MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000806MODULE_FIRMWARE(FIRMWARE_8168D_1);
807MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000808MODULE_FIRMWARE(FIRMWARE_8168E_1);
809MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400810MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800811MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800812MODULE_FIRMWARE(FIRMWARE_8168F_1);
813MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800814MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800815MODULE_FIRMWARE(FIRMWARE_8411_1);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800816MODULE_FIRMWARE(FIRMWARE_8106E_1);
Hayes Wangc5583862012-07-02 17:23:22 +0800817MODULE_FIRMWARE(FIRMWARE_8168G_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Francois Romieuda78dbf2012-01-26 14:18:23 +0100819static void rtl_lock_work(struct rtl8169_private *tp)
820{
821 mutex_lock(&tp->wk.mutex);
822}
823
824static void rtl_unlock_work(struct rtl8169_private *tp)
825{
826 mutex_unlock(&tp->wk.mutex);
827}
828
Francois Romieud58d46b2011-05-03 16:38:29 +0200829static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
830{
Jiang Liu7d7903b2012-07-24 17:20:16 +0800831 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
832 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200833}
834
Francois Romieuffc46952012-07-06 14:19:23 +0200835struct rtl_cond {
836 bool (*check)(struct rtl8169_private *);
837 const char *msg;
838};
839
840static void rtl_udelay(unsigned int d)
841{
842 udelay(d);
843}
844
845static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
846 void (*delay)(unsigned int), unsigned int d, int n,
847 bool high)
848{
849 int i;
850
851 for (i = 0; i < n; i++) {
852 delay(d);
853 if (c->check(tp) == high)
854 return true;
855 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200856 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
857 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200858 return false;
859}
860
861static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
862 const struct rtl_cond *c,
863 unsigned int d, int n)
864{
865 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
866}
867
868static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
869 const struct rtl_cond *c,
870 unsigned int d, int n)
871{
872 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
873}
874
875static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
876 const struct rtl_cond *c,
877 unsigned int d, int n)
878{
879 return rtl_loop_wait(tp, c, msleep, d, n, true);
880}
881
882static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
883 const struct rtl_cond *c,
884 unsigned int d, int n)
885{
886 return rtl_loop_wait(tp, c, msleep, d, n, false);
887}
888
889#define DECLARE_RTL_COND(name) \
890static bool name ## _check(struct rtl8169_private *); \
891 \
892static const struct rtl_cond name = { \
893 .check = name ## _check, \
894 .msg = #name \
895}; \
896 \
897static bool name ## _check(struct rtl8169_private *tp)
898
899DECLARE_RTL_COND(rtl_ocpar_cond)
900{
901 void __iomem *ioaddr = tp->mmio_addr;
902
903 return RTL_R32(OCPAR) & OCPAR_FLAG;
904}
905
françois romieub646d902011-01-03 15:08:21 +0000906static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
907{
908 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000909
910 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Francois Romieuffc46952012-07-06 14:19:23 +0200911
912 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
913 RTL_R32(OCPDR) : ~0;
françois romieub646d902011-01-03 15:08:21 +0000914}
915
916static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
917{
918 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000919
920 RTL_W32(OCPDR, data);
921 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Francois Romieuffc46952012-07-06 14:19:23 +0200922
923 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
924}
925
926DECLARE_RTL_COND(rtl_eriar_cond)
927{
928 void __iomem *ioaddr = tp->mmio_addr;
929
930 return RTL_R32(ERIAR) & ERIAR_FLAG;
françois romieub646d902011-01-03 15:08:21 +0000931}
932
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800933static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000934{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800935 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000936
937 RTL_W8(ERIDR, cmd);
938 RTL_W32(ERIAR, 0x800010e8);
939 msleep(2);
Francois Romieuffc46952012-07-06 14:19:23 +0200940
941 if (!rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 5))
942 return;
françois romieub646d902011-01-03 15:08:21 +0000943
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800944 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000945}
946
947#define OOB_CMD_RESET 0x00
948#define OOB_CMD_DRIVER_START 0x05
949#define OOB_CMD_DRIVER_STOP 0x06
950
Francois Romieucecb5fd2011-04-01 10:21:07 +0200951static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
952{
953 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
954}
955
Francois Romieuffc46952012-07-06 14:19:23 +0200956DECLARE_RTL_COND(rtl_ocp_read_cond)
françois romieub646d902011-01-03 15:08:21 +0000957{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200958 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000959
Francois Romieucecb5fd2011-04-01 10:21:07 +0200960 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000961
Francois Romieuffc46952012-07-06 14:19:23 +0200962 return ocp_read(tp, 0x0f, reg) & 0x00000800;
963}
964
965static void rtl8168_driver_start(struct rtl8169_private *tp)
966{
967 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
968
969 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
françois romieub646d902011-01-03 15:08:21 +0000970}
971
972static void rtl8168_driver_stop(struct rtl8169_private *tp)
973{
françois romieub646d902011-01-03 15:08:21 +0000974 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
975
Francois Romieuffc46952012-07-06 14:19:23 +0200976 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
françois romieub646d902011-01-03 15:08:21 +0000977}
978
hayeswang4804b3b2011-03-21 01:50:29 +0000979static int r8168dp_check_dash(struct rtl8169_private *tp)
980{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200981 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000982
Francois Romieucecb5fd2011-04-01 10:21:07 +0200983 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000984}
françois romieub646d902011-01-03 15:08:21 +0000985
Hayes Wangc5583862012-07-02 17:23:22 +0800986static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
987{
988 if (reg & 0xffff0001) {
989 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
990 return true;
991 }
992 return false;
993}
994
995DECLARE_RTL_COND(rtl_ocp_gphy_cond)
996{
997 void __iomem *ioaddr = tp->mmio_addr;
998
999 return RTL_R32(GPHY_OCP) & OCPAR_FLAG;
1000}
1001
1002static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1003{
1004 void __iomem *ioaddr = tp->mmio_addr;
1005
1006 if (rtl_ocp_reg_failure(tp, reg))
1007 return;
1008
1009 RTL_W32(GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
1010
1011 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
1012}
1013
1014static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
1015{
1016 void __iomem *ioaddr = tp->mmio_addr;
1017
1018 if (rtl_ocp_reg_failure(tp, reg))
1019 return 0;
1020
1021 RTL_W32(GPHY_OCP, reg << 15);
1022
1023 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
1024 (RTL_R32(GPHY_OCP) & 0xffff) : ~0;
1025}
1026
Hayes Wangc5583862012-07-02 17:23:22 +08001027static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1028{
1029 void __iomem *ioaddr = tp->mmio_addr;
1030
1031 if (rtl_ocp_reg_failure(tp, reg))
1032 return;
1033
1034 RTL_W32(OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +08001035}
1036
1037static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1038{
1039 void __iomem *ioaddr = tp->mmio_addr;
1040
1041 if (rtl_ocp_reg_failure(tp, reg))
1042 return 0;
1043
1044 RTL_W32(OCPDR, reg << 15);
1045
Hayes Wang3a83ad12012-07-11 20:31:56 +08001046 return RTL_R32(OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +08001047}
1048
1049#define OCP_STD_PHY_BASE 0xa400
1050
1051static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1052{
1053 if (reg == 0x1f) {
1054 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1055 return;
1056 }
1057
1058 if (tp->ocp_base != OCP_STD_PHY_BASE)
1059 reg -= 0x10;
1060
1061 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1062}
1063
1064static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1065{
1066 if (tp->ocp_base != OCP_STD_PHY_BASE)
1067 reg -= 0x10;
1068
1069 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1070}
1071
hayeswangeee37862013-04-01 22:23:38 +00001072static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1073{
1074 if (reg == 0x1f) {
1075 tp->ocp_base = value << 4;
1076 return;
1077 }
1078
1079 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1080}
1081
1082static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1083{
1084 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1085}
1086
Francois Romieuffc46952012-07-06 14:19:23 +02001087DECLARE_RTL_COND(rtl_phyar_cond)
1088{
1089 void __iomem *ioaddr = tp->mmio_addr;
1090
1091 return RTL_R32(PHYAR) & 0x80000000;
1092}
1093
Francois Romieu24192212012-07-06 20:19:42 +02001094static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Francois Romieu24192212012-07-06 20:19:42 +02001096 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Francois Romieu24192212012-07-06 20:19:42 +02001098 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Francois Romieuffc46952012-07-06 14:19:23 +02001100 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -07001101 /*
Timo Teräs81a95f02010-06-09 17:31:48 -07001102 * According to hardware specs a 20us delay is required after write
1103 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -07001104 */
Timo Teräs81a95f02010-06-09 17:31:48 -07001105 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
Francois Romieu24192212012-07-06 20:19:42 +02001108static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Francois Romieu24192212012-07-06 20:19:42 +02001110 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieuffc46952012-07-06 14:19:23 +02001111 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Francois Romieu24192212012-07-06 20:19:42 +02001113 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Francois Romieuffc46952012-07-06 14:19:23 +02001115 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1116 RTL_R32(PHYAR) & 0xffff : ~0;
1117
Timo Teräs81a95f02010-06-09 17:31:48 -07001118 /*
1119 * According to hardware specs a 20us delay is required after read
1120 * complete indication, but before sending next command.
1121 */
1122 udelay(20);
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return value;
1125}
1126
Francois Romieu24192212012-07-06 20:19:42 +02001127static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001128{
Francois Romieu24192212012-07-06 20:19:42 +02001129 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001130
Francois Romieu24192212012-07-06 20:19:42 +02001131 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
françois romieuc0e45c12011-01-03 15:08:04 +00001132 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
1133 RTL_W32(EPHY_RXER_NUM, 0);
1134
Francois Romieuffc46952012-07-06 14:19:23 +02001135 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001136}
1137
Francois Romieu24192212012-07-06 20:19:42 +02001138static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001139{
Francois Romieu24192212012-07-06 20:19:42 +02001140 r8168dp_1_mdio_access(tp, reg,
1141 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001142}
1143
Francois Romieu24192212012-07-06 20:19:42 +02001144static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001145{
Francois Romieu24192212012-07-06 20:19:42 +02001146 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001147
Francois Romieu24192212012-07-06 20:19:42 +02001148 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001149
1150 mdelay(1);
1151 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1152 RTL_W32(EPHY_RXER_NUM, 0);
1153
Francois Romieuffc46952012-07-06 14:19:23 +02001154 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1155 RTL_R32(OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001156}
1157
françois romieue6de30d2011-01-03 15:08:37 +00001158#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1159
1160static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1161{
1162 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1163}
1164
1165static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1166{
1167 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1168}
1169
Francois Romieu24192212012-07-06 20:19:42 +02001170static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001171{
Francois Romieu24192212012-07-06 20:19:42 +02001172 void __iomem *ioaddr = tp->mmio_addr;
1173
françois romieue6de30d2011-01-03 15:08:37 +00001174 r8168dp_2_mdio_start(ioaddr);
1175
Francois Romieu24192212012-07-06 20:19:42 +02001176 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001177
1178 r8168dp_2_mdio_stop(ioaddr);
1179}
1180
Francois Romieu24192212012-07-06 20:19:42 +02001181static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001182{
Francois Romieu24192212012-07-06 20:19:42 +02001183 void __iomem *ioaddr = tp->mmio_addr;
françois romieue6de30d2011-01-03 15:08:37 +00001184 int value;
1185
1186 r8168dp_2_mdio_start(ioaddr);
1187
Francois Romieu24192212012-07-06 20:19:42 +02001188 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001189
1190 r8168dp_2_mdio_stop(ioaddr);
1191
1192 return value;
1193}
1194
françois romieu4da19632011-01-03 15:07:55 +00001195static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001196{
Francois Romieu24192212012-07-06 20:19:42 +02001197 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001198}
1199
françois romieu4da19632011-01-03 15:07:55 +00001200static int rtl_readphy(struct rtl8169_private *tp, int location)
1201{
Francois Romieu24192212012-07-06 20:19:42 +02001202 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001203}
1204
1205static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1206{
1207 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1208}
1209
1210static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001211{
1212 int val;
1213
françois romieu4da19632011-01-03 15:07:55 +00001214 val = rtl_readphy(tp, reg_addr);
1215 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001216}
1217
Francois Romieuccdffb92008-07-26 14:26:06 +02001218static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1219 int val)
1220{
1221 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001222
françois romieu4da19632011-01-03 15:07:55 +00001223 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001224}
1225
1226static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1227{
1228 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001229
françois romieu4da19632011-01-03 15:07:55 +00001230 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001231}
1232
Francois Romieuffc46952012-07-06 14:19:23 +02001233DECLARE_RTL_COND(rtl_ephyar_cond)
1234{
1235 void __iomem *ioaddr = tp->mmio_addr;
1236
1237 return RTL_R32(EPHYAR) & EPHYAR_FLAG;
1238}
1239
Francois Romieufdf6fc02012-07-06 22:40:38 +02001240static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001241{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001242 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001243
1244 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1245 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1246
Francois Romieuffc46952012-07-06 14:19:23 +02001247 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1248
1249 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001250}
1251
Francois Romieufdf6fc02012-07-06 22:40:38 +02001252static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001253{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001254 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001255
1256 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1257
Francois Romieuffc46952012-07-06 14:19:23 +02001258 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1259 RTL_R32(EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001260}
1261
Francois Romieufdf6fc02012-07-06 22:40:38 +02001262static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1263 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001264{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001265 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001266
1267 BUG_ON((addr & 3) || (mask == 0));
1268 RTL_W32(ERIDR, val);
1269 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1270
Francois Romieuffc46952012-07-06 14:19:23 +02001271 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001272}
1273
Francois Romieufdf6fc02012-07-06 22:40:38 +02001274static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001275{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001276 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001277
1278 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1279
Francois Romieuffc46952012-07-06 14:19:23 +02001280 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1281 RTL_R32(ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001282}
1283
Francois Romieufdf6fc02012-07-06 22:40:38 +02001284static void rtl_w1w0_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
1285 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001286{
1287 u32 val;
1288
Francois Romieufdf6fc02012-07-06 22:40:38 +02001289 val = rtl_eri_read(tp, addr, type);
1290 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001291}
1292
françois romieuc28aa382011-08-02 03:53:43 +00001293struct exgmac_reg {
1294 u16 addr;
1295 u16 mask;
1296 u32 val;
1297};
1298
Francois Romieufdf6fc02012-07-06 22:40:38 +02001299static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001300 const struct exgmac_reg *r, int len)
1301{
1302 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001303 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001304 r++;
1305 }
1306}
1307
Francois Romieuffc46952012-07-06 14:19:23 +02001308DECLARE_RTL_COND(rtl_efusear_cond)
1309{
1310 void __iomem *ioaddr = tp->mmio_addr;
1311
1312 return RTL_R32(EFUSEAR) & EFUSEAR_FLAG;
1313}
1314
Francois Romieufdf6fc02012-07-06 22:40:38 +02001315static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001316{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001317 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00001318
1319 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1320
Francois Romieuffc46952012-07-06 14:19:23 +02001321 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1322 RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001323}
1324
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001325static u16 rtl_get_events(struct rtl8169_private *tp)
1326{
1327 void __iomem *ioaddr = tp->mmio_addr;
1328
1329 return RTL_R16(IntrStatus);
1330}
1331
1332static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1333{
1334 void __iomem *ioaddr = tp->mmio_addr;
1335
1336 RTL_W16(IntrStatus, bits);
1337 mmiowb();
1338}
1339
1340static void rtl_irq_disable(struct rtl8169_private *tp)
1341{
1342 void __iomem *ioaddr = tp->mmio_addr;
1343
1344 RTL_W16(IntrMask, 0);
1345 mmiowb();
1346}
1347
Francois Romieu3e990ff2012-01-26 12:50:01 +01001348static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1349{
1350 void __iomem *ioaddr = tp->mmio_addr;
1351
1352 RTL_W16(IntrMask, bits);
1353}
1354
Francois Romieuda78dbf2012-01-26 14:18:23 +01001355#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1356#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1357#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1358
1359static void rtl_irq_enable_all(struct rtl8169_private *tp)
1360{
1361 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1362}
1363
françois romieu811fd302011-12-04 20:30:45 +00001364static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
françois romieu811fd302011-12-04 20:30:45 +00001366 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001368 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001369 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001370 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
françois romieu4da19632011-01-03 15:07:55 +00001373static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
françois romieu4da19632011-01-03 15:07:55 +00001375 void __iomem *ioaddr = tp->mmio_addr;
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return RTL_R32(TBICSR) & TBIReset;
1378}
1379
françois romieu4da19632011-01-03 15:07:55 +00001380static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
françois romieu4da19632011-01-03 15:07:55 +00001382 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
1384
1385static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1386{
1387 return RTL_R32(TBICSR) & TBILinkOk;
1388}
1389
1390static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1391{
1392 return RTL_R8(PHYstatus) & LinkStatus;
1393}
1394
françois romieu4da19632011-01-03 15:07:55 +00001395static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
françois romieu4da19632011-01-03 15:07:55 +00001397 void __iomem *ioaddr = tp->mmio_addr;
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1400}
1401
françois romieu4da19632011-01-03 15:07:55 +00001402static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
1404 unsigned int val;
1405
françois romieu4da19632011-01-03 15:07:55 +00001406 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1407 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
Hayes Wang70090422011-07-06 15:58:06 +08001410static void rtl_link_chg_patch(struct rtl8169_private *tp)
1411{
1412 void __iomem *ioaddr = tp->mmio_addr;
1413 struct net_device *dev = tp->dev;
1414
1415 if (!netif_running(dev))
1416 return;
1417
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001418 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1419 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Hayes Wang70090422011-07-06 15:58:06 +08001420 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001421 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1422 ERIAR_EXGMAC);
1423 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1424 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001425 } else if (RTL_R8(PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001426 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1427 ERIAR_EXGMAC);
1428 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1429 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001430 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001431 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1432 ERIAR_EXGMAC);
1433 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1434 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001435 }
1436 /* Reset packet filter */
Francois Romieufdf6fc02012-07-06 22:40:38 +02001437 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001438 ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02001439 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001440 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001441 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1442 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1443 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001444 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1445 ERIAR_EXGMAC);
1446 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1447 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001448 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001449 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1450 ERIAR_EXGMAC);
1451 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1452 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001453 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001454 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1455 if (RTL_R8(PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001456 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1457 ERIAR_EXGMAC);
1458 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1459 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001460 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001461 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1462 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001463 }
Hayes Wang70090422011-07-06 15:58:06 +08001464 }
1465}
1466
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001467static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001468 struct rtl8169_private *tp,
1469 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001472 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001473 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001474 if (pm)
1475 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001477 if (net_ratelimit())
1478 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001479 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001481 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001482 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001483 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001487static void rtl8169_check_link_status(struct net_device *dev,
1488 struct rtl8169_private *tp,
1489 void __iomem *ioaddr)
1490{
1491 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1492}
1493
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001494#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1495
1496static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1497{
1498 void __iomem *ioaddr = tp->mmio_addr;
1499 u8 options;
1500 u32 wolopts = 0;
1501
1502 options = RTL_R8(Config1);
1503 if (!(options & PMEnable))
1504 return 0;
1505
1506 options = RTL_R8(Config3);
1507 if (options & LinkUp)
1508 wolopts |= WAKE_PHY;
1509 if (options & MagicPacket)
1510 wolopts |= WAKE_MAGIC;
1511
1512 options = RTL_R8(Config5);
1513 if (options & UWF)
1514 wolopts |= WAKE_UCAST;
1515 if (options & BWF)
1516 wolopts |= WAKE_BCAST;
1517 if (options & MWF)
1518 wolopts |= WAKE_MCAST;
1519
1520 return wolopts;
1521}
1522
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001523static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1524{
1525 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001526
Francois Romieuda78dbf2012-01-26 14:18:23 +01001527 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001528
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001529 wol->supported = WAKE_ANY;
1530 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001531
Francois Romieuda78dbf2012-01-26 14:18:23 +01001532 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001533}
1534
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001535static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001536{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001537 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001538 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001539 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001540 u32 opt;
1541 u16 reg;
1542 u8 mask;
1543 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001544 { WAKE_PHY, Config3, LinkUp },
1545 { WAKE_MAGIC, Config3, MagicPacket },
1546 { WAKE_UCAST, Config5, UWF },
1547 { WAKE_BCAST, Config5, BWF },
1548 { WAKE_MCAST, Config5, MWF },
1549 { WAKE_ANY, Config5, LanWake }
1550 };
Francois Romieu851e6022012-04-17 11:10:11 +02001551 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001552
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001553 RTL_W8(Cfg9346, Cfg9346_Unlock);
1554
1555 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
Francois Romieu851e6022012-04-17 11:10:11 +02001556 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001557 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001558 options |= cfg[i].mask;
1559 RTL_W8(cfg[i].reg, options);
1560 }
1561
Francois Romieu851e6022012-04-17 11:10:11 +02001562 switch (tp->mac_version) {
1563 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1564 options = RTL_R8(Config1) & ~PMEnable;
1565 if (wolopts)
1566 options |= PMEnable;
1567 RTL_W8(Config1, options);
1568 break;
1569 default:
Francois Romieud387b422012-04-17 11:12:01 +02001570 options = RTL_R8(Config2) & ~PME_SIGNAL;
1571 if (wolopts)
1572 options |= PME_SIGNAL;
1573 RTL_W8(Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001574 break;
1575 }
1576
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001577 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001578}
1579
1580static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1581{
1582 struct rtl8169_private *tp = netdev_priv(dev);
1583
Francois Romieuda78dbf2012-01-26 14:18:23 +01001584 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001585
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001586 if (wol->wolopts)
1587 tp->features |= RTL_FEATURE_WOL;
1588 else
1589 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001590 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001591
1592 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001593
françois romieuea809072010-11-08 13:23:58 +00001594 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1595
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001596 return 0;
1597}
1598
Francois Romieu31bd2042011-04-26 18:58:59 +02001599static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1600{
Francois Romieu85bffe62011-04-27 08:22:39 +02001601 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001602}
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604static void rtl8169_get_drvinfo(struct net_device *dev,
1605 struct ethtool_drvinfo *info)
1606{
1607 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001608 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Rick Jones68aad782011-11-07 13:29:27 +00001610 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1611 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1612 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001613 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001614 if (!IS_ERR_OR_NULL(rtl_fw))
1615 strlcpy(info->fw_version, rtl_fw->version,
1616 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
1619static int rtl8169_get_regs_len(struct net_device *dev)
1620{
1621 return R8169_REGS_SIZE;
1622}
1623
1624static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001625 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
1627 struct rtl8169_private *tp = netdev_priv(dev);
1628 void __iomem *ioaddr = tp->mmio_addr;
1629 int ret = 0;
1630 u32 reg;
1631
1632 reg = RTL_R32(TBICSR);
1633 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1634 (duplex == DUPLEX_FULL)) {
1635 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1636 } else if (autoneg == AUTONEG_ENABLE)
1637 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1638 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001639 netif_warn(tp, link, dev,
1640 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 ret = -EOPNOTSUPP;
1642 }
1643
1644 return ret;
1645}
1646
1647static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001648 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
1650 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001651 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001652 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Hayes Wang716b50a2011-02-22 17:26:18 +08001654 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001657 int auto_nego;
1658
françois romieu4da19632011-01-03 15:07:55 +00001659 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001660 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1661 ADVERTISE_100HALF | ADVERTISE_100FULL);
1662
1663 if (adv & ADVERTISED_10baseT_Half)
1664 auto_nego |= ADVERTISE_10HALF;
1665 if (adv & ADVERTISED_10baseT_Full)
1666 auto_nego |= ADVERTISE_10FULL;
1667 if (adv & ADVERTISED_100baseT_Half)
1668 auto_nego |= ADVERTISE_100HALF;
1669 if (adv & ADVERTISED_100baseT_Full)
1670 auto_nego |= ADVERTISE_100FULL;
1671
françois romieu3577aa12009-05-19 10:46:48 +00001672 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1673
françois romieu4da19632011-01-03 15:07:55 +00001674 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001675 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1676
1677 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001678 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001679 if (adv & ADVERTISED_1000baseT_Half)
1680 giga_ctrl |= ADVERTISE_1000HALF;
1681 if (adv & ADVERTISED_1000baseT_Full)
1682 giga_ctrl |= ADVERTISE_1000FULL;
1683 } else if (adv & (ADVERTISED_1000baseT_Half |
1684 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001685 netif_info(tp, link, dev,
1686 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001687 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
françois romieu3577aa12009-05-19 10:46:48 +00001690 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001691
françois romieu4da19632011-01-03 15:07:55 +00001692 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1693 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001694 } else {
1695 giga_ctrl = 0;
1696
1697 if (speed == SPEED_10)
1698 bmcr = 0;
1699 else if (speed == SPEED_100)
1700 bmcr = BMCR_SPEED100;
1701 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001702 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001703
1704 if (duplex == DUPLEX_FULL)
1705 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001706 }
1707
françois romieu4da19632011-01-03 15:07:55 +00001708 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001709
Francois Romieucecb5fd2011-04-01 10:21:07 +02001710 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1711 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001712 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001713 rtl_writephy(tp, 0x17, 0x2138);
1714 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001715 } else {
françois romieu4da19632011-01-03 15:07:55 +00001716 rtl_writephy(tp, 0x17, 0x2108);
1717 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001718 }
1719 }
1720
Oliver Neukum54405cd2011-01-06 21:55:13 +01001721 rc = 0;
1722out:
1723 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
1726static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001727 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
1729 struct rtl8169_private *tp = netdev_priv(dev);
1730 int ret;
1731
Oliver Neukum54405cd2011-01-06 21:55:13 +01001732 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001733 if (ret < 0)
1734 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Francois Romieu4876cc12011-03-11 21:07:11 +01001736 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1737 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001739 }
1740out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 return ret;
1742}
1743
1744static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1745{
1746 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 int ret;
1748
Francois Romieu4876cc12011-03-11 21:07:11 +01001749 del_timer_sync(&tp->timer);
1750
Francois Romieuda78dbf2012-01-26 14:18:23 +01001751 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001752 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001753 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001754 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return ret;
1757}
1758
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001759static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1760 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
Francois Romieud58d46b2011-05-03 16:38:29 +02001762 struct rtl8169_private *tp = netdev_priv(dev);
1763
Francois Romieu2b7b4312011-04-18 22:53:24 -07001764 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001765 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Francois Romieud58d46b2011-05-03 16:38:29 +02001767 if (dev->mtu > JUMBO_1K &&
1768 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1769 features &= ~NETIF_F_IP_CSUM;
1770
Michał Mirosław350fb322011-04-08 06:35:56 +00001771 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
1773
Francois Romieuda78dbf2012-01-26 14:18:23 +01001774static void __rtl8169_set_features(struct net_device *dev,
1775 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
1777 struct rtl8169_private *tp = netdev_priv(dev);
Ben Greear6bbe0212012-02-10 15:04:33 +00001778 netdev_features_t changed = features ^ dev->features;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001779 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Ben Greear6bbe0212012-02-10 15:04:33 +00001781 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)))
1782 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Ben Greear6bbe0212012-02-10 15:04:33 +00001784 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)) {
1785 if (features & NETIF_F_RXCSUM)
1786 tp->cp_cmd |= RxChkSum;
1787 else
1788 tp->cp_cmd &= ~RxChkSum;
Michał Mirosław350fb322011-04-08 06:35:56 +00001789
Ben Greear6bbe0212012-02-10 15:04:33 +00001790 if (dev->features & NETIF_F_HW_VLAN_RX)
1791 tp->cp_cmd |= RxVlan;
1792 else
1793 tp->cp_cmd &= ~RxVlan;
1794
1795 RTL_W16(CPlusCmd, tp->cp_cmd);
1796 RTL_R16(CPlusCmd);
1797 }
1798 if (changed & NETIF_F_RXALL) {
1799 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1800 if (features & NETIF_F_RXALL)
1801 tmp |= (AcceptErr | AcceptRunt);
1802 RTL_W32(RxConfig, tmp);
1803 }
Francois Romieuda78dbf2012-01-26 14:18:23 +01001804}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Francois Romieuda78dbf2012-01-26 14:18:23 +01001806static int rtl8169_set_features(struct net_device *dev,
1807 netdev_features_t features)
1808{
1809 struct rtl8169_private *tp = netdev_priv(dev);
1810
1811 rtl_lock_work(tp);
1812 __rtl8169_set_features(dev, features);
1813 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815 return 0;
1816}
1817
Francois Romieuda78dbf2012-01-26 14:18:23 +01001818
Kirill Smelkov810f4892012-11-10 21:11:02 +04001819static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
Jesse Grosseab6d182010-10-20 13:56:03 +00001821 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1823}
1824
Francois Romieu7a8fc772011-03-01 17:18:33 +01001825static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
1827 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Francois Romieu7a8fc772011-03-01 17:18:33 +01001829 if (opts2 & RxVlanTag)
1830 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
1832
Francois Romieuccdffb92008-07-26 14:26:06 +02001833static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
1835 struct rtl8169_private *tp = netdev_priv(dev);
1836 void __iomem *ioaddr = tp->mmio_addr;
1837 u32 status;
1838
1839 cmd->supported =
1840 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1841 cmd->port = PORT_FIBRE;
1842 cmd->transceiver = XCVR_INTERNAL;
1843
1844 status = RTL_R32(TBICSR);
1845 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1846 cmd->autoneg = !!(status & TBINwEnable);
1847
David Decotigny70739492011-04-27 18:32:40 +00001848 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001850
1851 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852}
1853
Francois Romieuccdffb92008-07-26 14:26:06 +02001854static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
1856 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Francois Romieuccdffb92008-07-26 14:26:06 +02001858 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
1860
1861static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1862{
1863 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001864 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Francois Romieuda78dbf2012-01-26 14:18:23 +01001866 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02001867 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001868 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Francois Romieuccdffb92008-07-26 14:26:06 +02001870 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871}
1872
1873static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1874 void *p)
1875{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001876 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Francois Romieu5b0384f2006-08-16 16:00:01 +02001878 if (regs->len > R8169_REGS_SIZE)
1879 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Francois Romieuda78dbf2012-01-26 14:18:23 +01001881 rtl_lock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001882 memcpy_fromio(p, tp->mmio_addr, regs->len);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001883 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884}
1885
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001886static u32 rtl8169_get_msglevel(struct net_device *dev)
1887{
1888 struct rtl8169_private *tp = netdev_priv(dev);
1889
1890 return tp->msg_enable;
1891}
1892
1893static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1894{
1895 struct rtl8169_private *tp = netdev_priv(dev);
1896
1897 tp->msg_enable = value;
1898}
1899
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001900static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1901 "tx_packets",
1902 "rx_packets",
1903 "tx_errors",
1904 "rx_errors",
1905 "rx_missed",
1906 "align_errors",
1907 "tx_single_collisions",
1908 "tx_multi_collisions",
1909 "unicast",
1910 "broadcast",
1911 "multicast",
1912 "tx_aborted",
1913 "tx_underrun",
1914};
1915
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001916static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001917{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001918 switch (sset) {
1919 case ETH_SS_STATS:
1920 return ARRAY_SIZE(rtl8169_gstrings);
1921 default:
1922 return -EOPNOTSUPP;
1923 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001924}
1925
Francois Romieuffc46952012-07-06 14:19:23 +02001926DECLARE_RTL_COND(rtl_counters_cond)
1927{
1928 void __iomem *ioaddr = tp->mmio_addr;
1929
1930 return RTL_R32(CounterAddrLow) & CounterDump;
1931}
1932
Ivan Vecera355423d2009-02-06 21:49:57 -08001933static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001934{
1935 struct rtl8169_private *tp = netdev_priv(dev);
1936 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001937 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001938 struct rtl8169_counters *counters;
1939 dma_addr_t paddr;
1940 u32 cmd;
1941
Ivan Vecera355423d2009-02-06 21:49:57 -08001942 /*
1943 * Some chips are unable to dump tally counters when the receiver
1944 * is disabled.
1945 */
1946 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1947 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001948
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001949 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001950 if (!counters)
1951 return;
1952
1953 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001954 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001955 RTL_W32(CounterAddrLow, cmd);
1956 RTL_W32(CounterAddrLow, cmd | CounterDump);
1957
Francois Romieuffc46952012-07-06 14:19:23 +02001958 if (rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000))
1959 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001960
1961 RTL_W32(CounterAddrLow, 0);
1962 RTL_W32(CounterAddrHigh, 0);
1963
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001964 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001965}
1966
Ivan Vecera355423d2009-02-06 21:49:57 -08001967static void rtl8169_get_ethtool_stats(struct net_device *dev,
1968 struct ethtool_stats *stats, u64 *data)
1969{
1970 struct rtl8169_private *tp = netdev_priv(dev);
1971
1972 ASSERT_RTNL();
1973
1974 rtl8169_update_counters(dev);
1975
1976 data[0] = le64_to_cpu(tp->counters.tx_packets);
1977 data[1] = le64_to_cpu(tp->counters.rx_packets);
1978 data[2] = le64_to_cpu(tp->counters.tx_errors);
1979 data[3] = le32_to_cpu(tp->counters.rx_errors);
1980 data[4] = le16_to_cpu(tp->counters.rx_missed);
1981 data[5] = le16_to_cpu(tp->counters.align_errors);
1982 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1983 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1984 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1985 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1986 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1987 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1988 data[12] = le16_to_cpu(tp->counters.tx_underun);
1989}
1990
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001991static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1992{
1993 switch(stringset) {
1994 case ETH_SS_STATS:
1995 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1996 break;
1997 }
1998}
1999
Jeff Garzik7282d492006-09-13 14:30:00 -04002000static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 .get_drvinfo = rtl8169_get_drvinfo,
2002 .get_regs_len = rtl8169_get_regs_len,
2003 .get_link = ethtool_op_get_link,
2004 .get_settings = rtl8169_get_settings,
2005 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002006 .get_msglevel = rtl8169_get_msglevel,
2007 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002009 .get_wol = rtl8169_get_wol,
2010 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002011 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002012 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002013 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002014 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015};
2016
Francois Romieu07d3f512007-02-21 22:40:46 +01002017static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02002018 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
Francois Romieu5d320a22011-05-08 17:47:36 +02002020 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01002021 /*
2022 * The driver currently handles the 8168Bf and the 8168Be identically
2023 * but they can be identified more specifically through the test below
2024 * if needed:
2025 *
2026 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002027 *
2028 * Same thing for the 8101Eb and the 8101Ec:
2029 *
2030 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002031 */
Francois Romieu37441002011-06-17 22:58:54 +02002032 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002034 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 int mac_version;
2036 } mac_info[] = {
Hayes Wangc5583862012-07-02 17:23:22 +08002037 /* 8168G family. */
2038 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2039 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2040
Hayes Wangc2218922011-09-06 16:55:18 +08002041 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002042 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002043 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2044 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2045
hayeswang01dc7fe2011-03-21 01:50:28 +00002046 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002047 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002048 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2049 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2050 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2051
Francois Romieu5b538df2008-07-20 16:22:45 +02002052 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002053 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2054 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002055 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002056
françois romieue6de30d2011-01-03 15:08:37 +00002057 /* 8168DP family. */
2058 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2059 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002060 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002061
Francois Romieuef808d52008-06-29 13:10:54 +02002062 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07002063 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02002064 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002065 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002066 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002067 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2068 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002069 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02002070 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02002071 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002072
2073 /* 8168B family. */
2074 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2075 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2076 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2077 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2078
2079 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002080 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2081 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002082 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00002083 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002084 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2085 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2086 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002087 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2088 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2089 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2090 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2091 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2092 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002093 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002094 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002095 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002096 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2097 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002098 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2099 /* FIXME: where did these entries come from ? -- FR */
2100 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2101 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2102
2103 /* 8110 family. */
2104 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2105 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2106 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2107 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2108 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2109 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2110
Jean Delvaref21b75e2009-05-26 20:54:48 -07002111 /* Catch-all */
2112 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002113 };
2114 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 u32 reg;
2116
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002117 reg = RTL_R32(TxConfig);
2118 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 p++;
2120 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002121
2122 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2123 netif_notice(tp, probe, dev,
2124 "unknown MAC, using family default\n");
2125 tp->mac_version = default_version;
2126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
2128
2129static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2130{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002131 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132}
2133
Francois Romieu867763c2007-08-17 18:21:58 +02002134struct phy_reg {
2135 u16 reg;
2136 u16 val;
2137};
2138
françois romieu4da19632011-01-03 15:07:55 +00002139static void rtl_writephy_batch(struct rtl8169_private *tp,
2140 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002141{
2142 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002143 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002144 regs++;
2145 }
2146}
2147
françois romieubca03d52011-01-03 15:07:31 +00002148#define PHY_READ 0x00000000
2149#define PHY_DATA_OR 0x10000000
2150#define PHY_DATA_AND 0x20000000
2151#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002152#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002153#define PHY_CLEAR_READCOUNT 0x70000000
2154#define PHY_WRITE 0x80000000
2155#define PHY_READCOUNT_EQ_SKIP 0x90000000
2156#define PHY_COMP_EQ_SKIPN 0xa0000000
2157#define PHY_COMP_NEQ_SKIPN 0xb0000000
2158#define PHY_WRITE_PREVIOUS 0xc0000000
2159#define PHY_SKIPN 0xd0000000
2160#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002161
Hayes Wang960aee62011-06-18 11:37:48 +02002162struct fw_info {
2163 u32 magic;
2164 char version[RTL_VER_SIZE];
2165 __le32 fw_start;
2166 __le32 fw_len;
2167 u8 chksum;
2168} __packed;
2169
Francois Romieu1c361ef2011-06-17 17:16:24 +02002170#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2171
2172static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002173{
Francois Romieub6ffd972011-06-17 17:00:05 +02002174 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002175 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002176 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2177 char *version = rtl_fw->version;
2178 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002179
Francois Romieu1c361ef2011-06-17 17:16:24 +02002180 if (fw->size < FW_OPCODE_SIZE)
2181 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002182
2183 if (!fw_info->magic) {
2184 size_t i, size, start;
2185 u8 checksum = 0;
2186
2187 if (fw->size < sizeof(*fw_info))
2188 goto out;
2189
2190 for (i = 0; i < fw->size; i++)
2191 checksum += fw->data[i];
2192 if (checksum != 0)
2193 goto out;
2194
2195 start = le32_to_cpu(fw_info->fw_start);
2196 if (start > fw->size)
2197 goto out;
2198
2199 size = le32_to_cpu(fw_info->fw_len);
2200 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2201 goto out;
2202
2203 memcpy(version, fw_info->version, RTL_VER_SIZE);
2204
2205 pa->code = (__le32 *)(fw->data + start);
2206 pa->size = size;
2207 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002208 if (fw->size % FW_OPCODE_SIZE)
2209 goto out;
2210
2211 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2212
2213 pa->code = (__le32 *)fw->data;
2214 pa->size = fw->size / FW_OPCODE_SIZE;
2215 }
2216 version[RTL_VER_SIZE - 1] = 0;
2217
2218 rc = true;
2219out:
2220 return rc;
2221}
2222
Francois Romieufd112f22011-06-18 00:10:29 +02002223static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2224 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002225{
Francois Romieufd112f22011-06-18 00:10:29 +02002226 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002227 size_t index;
2228
Francois Romieu1c361ef2011-06-17 17:16:24 +02002229 for (index = 0; index < pa->size; index++) {
2230 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002231 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002232
hayeswang42b82dc2011-01-10 02:07:25 +00002233 switch(action & 0xf0000000) {
2234 case PHY_READ:
2235 case PHY_DATA_OR:
2236 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002237 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002238 case PHY_CLEAR_READCOUNT:
2239 case PHY_WRITE:
2240 case PHY_WRITE_PREVIOUS:
2241 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002242 break;
2243
hayeswang42b82dc2011-01-10 02:07:25 +00002244 case PHY_BJMPN:
2245 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002246 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002247 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002248 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002249 }
2250 break;
2251 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002252 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002253 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002254 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002255 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002256 }
2257 break;
2258 case PHY_COMP_EQ_SKIPN:
2259 case PHY_COMP_NEQ_SKIPN:
2260 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002261 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002262 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002263 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002264 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002265 }
2266 break;
2267
hayeswang42b82dc2011-01-10 02:07:25 +00002268 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002269 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002270 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002271 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002272 }
2273 }
Francois Romieufd112f22011-06-18 00:10:29 +02002274 rc = true;
2275out:
2276 return rc;
2277}
françois romieubca03d52011-01-03 15:07:31 +00002278
Francois Romieufd112f22011-06-18 00:10:29 +02002279static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2280{
2281 struct net_device *dev = tp->dev;
2282 int rc = -EINVAL;
2283
2284 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2285 netif_err(tp, ifup, dev, "invalid firwmare\n");
2286 goto out;
2287 }
2288
2289 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2290 rc = 0;
2291out:
2292 return rc;
2293}
2294
2295static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2296{
2297 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002298 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002299 u32 predata, count;
2300 size_t index;
2301
2302 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002303 org.write = ops->write;
2304 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002305
Francois Romieu1c361ef2011-06-17 17:16:24 +02002306 for (index = 0; index < pa->size; ) {
2307 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002308 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002309 u32 regno = (action & 0x0fff0000) >> 16;
2310
2311 if (!action)
2312 break;
françois romieubca03d52011-01-03 15:07:31 +00002313
2314 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002315 case PHY_READ:
2316 predata = rtl_readphy(tp, regno);
2317 count++;
2318 index++;
françois romieubca03d52011-01-03 15:07:31 +00002319 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002320 case PHY_DATA_OR:
2321 predata |= data;
2322 index++;
2323 break;
2324 case PHY_DATA_AND:
2325 predata &= data;
2326 index++;
2327 break;
2328 case PHY_BJMPN:
2329 index -= regno;
2330 break;
hayeswangeee37862013-04-01 22:23:38 +00002331 case PHY_MDIO_CHG:
2332 if (data == 0) {
2333 ops->write = org.write;
2334 ops->read = org.read;
2335 } else if (data == 1) {
2336 ops->write = mac_mcu_write;
2337 ops->read = mac_mcu_read;
2338 }
2339
hayeswang42b82dc2011-01-10 02:07:25 +00002340 index++;
2341 break;
2342 case PHY_CLEAR_READCOUNT:
2343 count = 0;
2344 index++;
2345 break;
2346 case PHY_WRITE:
2347 rtl_writephy(tp, regno, data);
2348 index++;
2349 break;
2350 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002351 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002352 break;
2353 case PHY_COMP_EQ_SKIPN:
2354 if (predata == data)
2355 index += regno;
2356 index++;
2357 break;
2358 case PHY_COMP_NEQ_SKIPN:
2359 if (predata != data)
2360 index += regno;
2361 index++;
2362 break;
2363 case PHY_WRITE_PREVIOUS:
2364 rtl_writephy(tp, regno, predata);
2365 index++;
2366 break;
2367 case PHY_SKIPN:
2368 index += regno + 1;
2369 break;
2370 case PHY_DELAY_MS:
2371 mdelay(data);
2372 index++;
2373 break;
2374
françois romieubca03d52011-01-03 15:07:31 +00002375 default:
2376 BUG();
2377 }
2378 }
hayeswangeee37862013-04-01 22:23:38 +00002379
2380 ops->write = org.write;
2381 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002382}
2383
françois romieuf1e02ed2011-01-13 13:07:53 +00002384static void rtl_release_firmware(struct rtl8169_private *tp)
2385{
Francois Romieub6ffd972011-06-17 17:00:05 +02002386 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2387 release_firmware(tp->rtl_fw->fw);
2388 kfree(tp->rtl_fw);
2389 }
2390 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002391}
2392
François Romieu953a12c2011-04-24 17:38:48 +02002393static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002394{
Francois Romieub6ffd972011-06-17 17:00:05 +02002395 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002396
2397 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002398 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002399 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002400}
2401
2402static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2403{
2404 if (rtl_readphy(tp, reg) != val)
2405 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2406 else
2407 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002408}
2409
françois romieu4da19632011-01-03 15:07:55 +00002410static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002412 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002413 { 0x1f, 0x0001 },
2414 { 0x06, 0x006e },
2415 { 0x08, 0x0708 },
2416 { 0x15, 0x4000 },
2417 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
françois romieu0b9b5712009-08-10 19:44:56 +00002419 { 0x1f, 0x0001 },
2420 { 0x03, 0x00a1 },
2421 { 0x02, 0x0008 },
2422 { 0x01, 0x0120 },
2423 { 0x00, 0x1000 },
2424 { 0x04, 0x0800 },
2425 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
françois romieu0b9b5712009-08-10 19:44:56 +00002427 { 0x03, 0xff41 },
2428 { 0x02, 0xdf60 },
2429 { 0x01, 0x0140 },
2430 { 0x00, 0x0077 },
2431 { 0x04, 0x7800 },
2432 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
françois romieu0b9b5712009-08-10 19:44:56 +00002434 { 0x03, 0x802f },
2435 { 0x02, 0x4f02 },
2436 { 0x01, 0x0409 },
2437 { 0x00, 0xf0f9 },
2438 { 0x04, 0x9800 },
2439 { 0x04, 0x9000 },
2440
2441 { 0x03, 0xdf01 },
2442 { 0x02, 0xdf20 },
2443 { 0x01, 0xff95 },
2444 { 0x00, 0xba00 },
2445 { 0x04, 0xa800 },
2446 { 0x04, 0xa000 },
2447
2448 { 0x03, 0xff41 },
2449 { 0x02, 0xdf20 },
2450 { 0x01, 0x0140 },
2451 { 0x00, 0x00bb },
2452 { 0x04, 0xb800 },
2453 { 0x04, 0xb000 },
2454
2455 { 0x03, 0xdf41 },
2456 { 0x02, 0xdc60 },
2457 { 0x01, 0x6340 },
2458 { 0x00, 0x007d },
2459 { 0x04, 0xd800 },
2460 { 0x04, 0xd000 },
2461
2462 { 0x03, 0xdf01 },
2463 { 0x02, 0xdf20 },
2464 { 0x01, 0x100a },
2465 { 0x00, 0xa0ff },
2466 { 0x04, 0xf800 },
2467 { 0x04, 0xf000 },
2468
2469 { 0x1f, 0x0000 },
2470 { 0x0b, 0x0000 },
2471 { 0x00, 0x9200 }
2472 };
2473
françois romieu4da19632011-01-03 15:07:55 +00002474 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
françois romieu4da19632011-01-03 15:07:55 +00002477static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002478{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002479 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002480 { 0x1f, 0x0002 },
2481 { 0x01, 0x90d0 },
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 Romieu5615d9f2007-08-17 17:50:46 +02002486}
2487
françois romieu4da19632011-01-03 15:07:55 +00002488static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002489{
2490 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002491
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002492 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2493 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002494 return;
2495
françois romieu4da19632011-01-03 15:07:55 +00002496 rtl_writephy(tp, 0x1f, 0x0001);
2497 rtl_writephy(tp, 0x10, 0xf01b);
2498 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002499}
2500
françois romieu4da19632011-01-03 15:07:55 +00002501static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002502{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002503 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002504 { 0x1f, 0x0001 },
2505 { 0x04, 0x0000 },
2506 { 0x03, 0x00a1 },
2507 { 0x02, 0x0008 },
2508 { 0x01, 0x0120 },
2509 { 0x00, 0x1000 },
2510 { 0x04, 0x0800 },
2511 { 0x04, 0x9000 },
2512 { 0x03, 0x802f },
2513 { 0x02, 0x4f02 },
2514 { 0x01, 0x0409 },
2515 { 0x00, 0xf099 },
2516 { 0x04, 0x9800 },
2517 { 0x04, 0xa000 },
2518 { 0x03, 0xdf01 },
2519 { 0x02, 0xdf20 },
2520 { 0x01, 0xff95 },
2521 { 0x00, 0xba00 },
2522 { 0x04, 0xa800 },
2523 { 0x04, 0xf000 },
2524 { 0x03, 0xdf01 },
2525 { 0x02, 0xdf20 },
2526 { 0x01, 0x101a },
2527 { 0x00, 0xa0ff },
2528 { 0x04, 0xf800 },
2529 { 0x04, 0x0000 },
2530 { 0x1f, 0x0000 },
2531
2532 { 0x1f, 0x0001 },
2533 { 0x10, 0xf41b },
2534 { 0x14, 0xfb54 },
2535 { 0x18, 0xf5c7 },
2536 { 0x1f, 0x0000 },
2537
2538 { 0x1f, 0x0001 },
2539 { 0x17, 0x0cc0 },
2540 { 0x1f, 0x0000 }
2541 };
2542
françois romieu4da19632011-01-03 15:07:55 +00002543 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002544
françois romieu4da19632011-01-03 15:07:55 +00002545 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002546}
2547
françois romieu4da19632011-01-03 15:07:55 +00002548static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002549{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002550 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002551 { 0x1f, 0x0001 },
2552 { 0x04, 0x0000 },
2553 { 0x03, 0x00a1 },
2554 { 0x02, 0x0008 },
2555 { 0x01, 0x0120 },
2556 { 0x00, 0x1000 },
2557 { 0x04, 0x0800 },
2558 { 0x04, 0x9000 },
2559 { 0x03, 0x802f },
2560 { 0x02, 0x4f02 },
2561 { 0x01, 0x0409 },
2562 { 0x00, 0xf099 },
2563 { 0x04, 0x9800 },
2564 { 0x04, 0xa000 },
2565 { 0x03, 0xdf01 },
2566 { 0x02, 0xdf20 },
2567 { 0x01, 0xff95 },
2568 { 0x00, 0xba00 },
2569 { 0x04, 0xa800 },
2570 { 0x04, 0xf000 },
2571 { 0x03, 0xdf01 },
2572 { 0x02, 0xdf20 },
2573 { 0x01, 0x101a },
2574 { 0x00, 0xa0ff },
2575 { 0x04, 0xf800 },
2576 { 0x04, 0x0000 },
2577 { 0x1f, 0x0000 },
2578
2579 { 0x1f, 0x0001 },
2580 { 0x0b, 0x8480 },
2581 { 0x1f, 0x0000 },
2582
2583 { 0x1f, 0x0001 },
2584 { 0x18, 0x67c7 },
2585 { 0x04, 0x2000 },
2586 { 0x03, 0x002f },
2587 { 0x02, 0x4360 },
2588 { 0x01, 0x0109 },
2589 { 0x00, 0x3022 },
2590 { 0x04, 0x2800 },
2591 { 0x1f, 0x0000 },
2592
2593 { 0x1f, 0x0001 },
2594 { 0x17, 0x0cc0 },
2595 { 0x1f, 0x0000 }
2596 };
2597
françois romieu4da19632011-01-03 15:07:55 +00002598 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002599}
2600
françois romieu4da19632011-01-03 15:07:55 +00002601static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002602{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002603 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002604 { 0x10, 0xf41b },
2605 { 0x1f, 0x0000 }
2606 };
2607
françois romieu4da19632011-01-03 15:07:55 +00002608 rtl_writephy(tp, 0x1f, 0x0001);
2609 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002610
françois romieu4da19632011-01-03 15:07:55 +00002611 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002612}
2613
françois romieu4da19632011-01-03 15:07:55 +00002614static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002615{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002616 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002617 { 0x1f, 0x0001 },
2618 { 0x10, 0xf41b },
2619 { 0x1f, 0x0000 }
2620 };
2621
françois romieu4da19632011-01-03 15:07:55 +00002622 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002623}
2624
françois romieu4da19632011-01-03 15:07:55 +00002625static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002626{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002627 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002628 { 0x1f, 0x0000 },
2629 { 0x1d, 0x0f00 },
2630 { 0x1f, 0x0002 },
2631 { 0x0c, 0x1ec8 },
2632 { 0x1f, 0x0000 }
2633 };
2634
françois romieu4da19632011-01-03 15:07:55 +00002635 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002636}
2637
françois romieu4da19632011-01-03 15:07:55 +00002638static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002639{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002640 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002641 { 0x1f, 0x0001 },
2642 { 0x1d, 0x3d98 },
2643 { 0x1f, 0x0000 }
2644 };
2645
françois romieu4da19632011-01-03 15:07:55 +00002646 rtl_writephy(tp, 0x1f, 0x0000);
2647 rtl_patchphy(tp, 0x14, 1 << 5);
2648 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002649
françois romieu4da19632011-01-03 15:07:55 +00002650 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002651}
2652
françois romieu4da19632011-01-03 15:07:55 +00002653static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002654{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002655 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002656 { 0x1f, 0x0001 },
2657 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002658 { 0x1f, 0x0002 },
2659 { 0x00, 0x88d4 },
2660 { 0x01, 0x82b1 },
2661 { 0x03, 0x7002 },
2662 { 0x08, 0x9e30 },
2663 { 0x09, 0x01f0 },
2664 { 0x0a, 0x5500 },
2665 { 0x0c, 0x00c8 },
2666 { 0x1f, 0x0003 },
2667 { 0x12, 0xc096 },
2668 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002669 { 0x1f, 0x0000 },
2670 { 0x1f, 0x0000 },
2671 { 0x09, 0x2000 },
2672 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002673 };
2674
françois romieu4da19632011-01-03 15:07:55 +00002675 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002676
françois romieu4da19632011-01-03 15:07:55 +00002677 rtl_patchphy(tp, 0x14, 1 << 5);
2678 rtl_patchphy(tp, 0x0d, 1 << 5);
2679 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002680}
2681
françois romieu4da19632011-01-03 15:07:55 +00002682static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002683{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002684 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002685 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002686 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002687 { 0x03, 0x802f },
2688 { 0x02, 0x4f02 },
2689 { 0x01, 0x0409 },
2690 { 0x00, 0xf099 },
2691 { 0x04, 0x9800 },
2692 { 0x04, 0x9000 },
2693 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002694 { 0x1f, 0x0002 },
2695 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002696 { 0x06, 0x0761 },
2697 { 0x1f, 0x0003 },
2698 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002699 { 0x1f, 0x0000 }
2700 };
2701
françois romieu4da19632011-01-03 15:07:55 +00002702 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002703
françois romieu4da19632011-01-03 15:07:55 +00002704 rtl_patchphy(tp, 0x16, 1 << 0);
2705 rtl_patchphy(tp, 0x14, 1 << 5);
2706 rtl_patchphy(tp, 0x0d, 1 << 5);
2707 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002708}
2709
françois romieu4da19632011-01-03 15:07:55 +00002710static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002711{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002712 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002713 { 0x1f, 0x0001 },
2714 { 0x12, 0x2300 },
2715 { 0x1d, 0x3d98 },
2716 { 0x1f, 0x0002 },
2717 { 0x0c, 0x7eb8 },
2718 { 0x06, 0x5461 },
2719 { 0x1f, 0x0003 },
2720 { 0x16, 0x0f0a },
2721 { 0x1f, 0x0000 }
2722 };
2723
françois romieu4da19632011-01-03 15:07:55 +00002724 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002725
françois romieu4da19632011-01-03 15:07:55 +00002726 rtl_patchphy(tp, 0x16, 1 << 0);
2727 rtl_patchphy(tp, 0x14, 1 << 5);
2728 rtl_patchphy(tp, 0x0d, 1 << 5);
2729 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002730}
2731
françois romieu4da19632011-01-03 15:07:55 +00002732static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002733{
françois romieu4da19632011-01-03 15:07:55 +00002734 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002735}
2736
françois romieubca03d52011-01-03 15:07:31 +00002737static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002738{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002739 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002740 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002741 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002742 { 0x06, 0x4064 },
2743 { 0x07, 0x2863 },
2744 { 0x08, 0x059c },
2745 { 0x09, 0x26b4 },
2746 { 0x0a, 0x6a19 },
2747 { 0x0b, 0xdcc8 },
2748 { 0x10, 0xf06d },
2749 { 0x14, 0x7f68 },
2750 { 0x18, 0x7fd9 },
2751 { 0x1c, 0xf0ff },
2752 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002753 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002754 { 0x12, 0xf49f },
2755 { 0x13, 0x070b },
2756 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002757 { 0x14, 0x94c0 },
2758
2759 /*
2760 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002761 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002762 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002763 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002764 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002765 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002766 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002767 { 0x06, 0x5561 },
2768
2769 /*
2770 * Can not link to 1Gbps with bad cable
2771 * Decrease SNR threshold form 21.07dB to 19.04dB
2772 */
2773 { 0x1f, 0x0001 },
2774 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002775
2776 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002777 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002778 };
2779
françois romieu4da19632011-01-03 15:07:55 +00002780 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002781
françois romieubca03d52011-01-03 15:07:31 +00002782 /*
2783 * Rx Error Issue
2784 * Fine Tune Switching regulator parameter
2785 */
françois romieu4da19632011-01-03 15:07:55 +00002786 rtl_writephy(tp, 0x1f, 0x0002);
2787 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2788 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002789
Francois Romieufdf6fc02012-07-06 22:40:38 +02002790 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002791 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002792 { 0x1f, 0x0002 },
2793 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002794 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002795 { 0x05, 0x8330 },
2796 { 0x06, 0x669a },
2797 { 0x1f, 0x0002 }
2798 };
2799 int val;
2800
françois romieu4da19632011-01-03 15:07:55 +00002801 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002802
françois romieu4da19632011-01-03 15:07:55 +00002803 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002804
2805 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002806 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002807 0x0065, 0x0066, 0x0067, 0x0068,
2808 0x0069, 0x006a, 0x006b, 0x006c
2809 };
2810 int i;
2811
françois romieu4da19632011-01-03 15:07:55 +00002812 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002813
2814 val &= 0xff00;
2815 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002816 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002817 }
2818 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002819 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002820 { 0x1f, 0x0002 },
2821 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002822 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002823 { 0x05, 0x8330 },
2824 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002825 };
2826
françois romieu4da19632011-01-03 15:07:55 +00002827 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002828 }
2829
françois romieubca03d52011-01-03 15:07:31 +00002830 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002831 rtl_writephy(tp, 0x1f, 0x0002);
2832 rtl_patchphy(tp, 0x0d, 0x0300);
2833 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002834
françois romieubca03d52011-01-03 15:07:31 +00002835 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002836 rtl_writephy(tp, 0x1f, 0x0002);
2837 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2838 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002839
françois romieu4da19632011-01-03 15:07:55 +00002840 rtl_writephy(tp, 0x1f, 0x0005);
2841 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002842
2843 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002844
françois romieu4da19632011-01-03 15:07:55 +00002845 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002846}
2847
françois romieubca03d52011-01-03 15:07:31 +00002848static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002849{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002850 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002851 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002852 { 0x1f, 0x0001 },
2853 { 0x06, 0x4064 },
2854 { 0x07, 0x2863 },
2855 { 0x08, 0x059c },
2856 { 0x09, 0x26b4 },
2857 { 0x0a, 0x6a19 },
2858 { 0x0b, 0xdcc8 },
2859 { 0x10, 0xf06d },
2860 { 0x14, 0x7f68 },
2861 { 0x18, 0x7fd9 },
2862 { 0x1c, 0xf0ff },
2863 { 0x1d, 0x3d9c },
2864 { 0x1f, 0x0003 },
2865 { 0x12, 0xf49f },
2866 { 0x13, 0x070b },
2867 { 0x1a, 0x05ad },
2868 { 0x14, 0x94c0 },
2869
françois romieubca03d52011-01-03 15:07:31 +00002870 /*
2871 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002872 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002873 */
françois romieudaf9df62009-10-07 12:44:20 +00002874 { 0x1f, 0x0002 },
2875 { 0x06, 0x5561 },
2876 { 0x1f, 0x0005 },
2877 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002878 { 0x06, 0x5561 },
2879
2880 /*
2881 * Can not link to 1Gbps with bad cable
2882 * Decrease SNR threshold form 21.07dB to 19.04dB
2883 */
2884 { 0x1f, 0x0001 },
2885 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002886
2887 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002888 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002889 };
2890
françois romieu4da19632011-01-03 15:07:55 +00002891 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002892
Francois Romieufdf6fc02012-07-06 22:40:38 +02002893 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002894 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002895 { 0x1f, 0x0002 },
2896 { 0x05, 0x669a },
2897 { 0x1f, 0x0005 },
2898 { 0x05, 0x8330 },
2899 { 0x06, 0x669a },
2900
2901 { 0x1f, 0x0002 }
2902 };
2903 int val;
2904
françois romieu4da19632011-01-03 15:07:55 +00002905 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002906
françois romieu4da19632011-01-03 15:07:55 +00002907 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002908 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002909 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002910 0x0065, 0x0066, 0x0067, 0x0068,
2911 0x0069, 0x006a, 0x006b, 0x006c
2912 };
2913 int i;
2914
françois romieu4da19632011-01-03 15:07:55 +00002915 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002916
2917 val &= 0xff00;
2918 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002919 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002920 }
2921 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002922 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002923 { 0x1f, 0x0002 },
2924 { 0x05, 0x2642 },
2925 { 0x1f, 0x0005 },
2926 { 0x05, 0x8330 },
2927 { 0x06, 0x2642 }
2928 };
2929
françois romieu4da19632011-01-03 15:07:55 +00002930 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002931 }
2932
françois romieubca03d52011-01-03 15:07:31 +00002933 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002934 rtl_writephy(tp, 0x1f, 0x0002);
2935 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2936 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002937
françois romieubca03d52011-01-03 15:07:31 +00002938 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002939 rtl_writephy(tp, 0x1f, 0x0002);
2940 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002941
françois romieu4da19632011-01-03 15:07:55 +00002942 rtl_writephy(tp, 0x1f, 0x0005);
2943 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002944
2945 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002946
françois romieu4da19632011-01-03 15:07:55 +00002947 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002948}
2949
françois romieu4da19632011-01-03 15:07:55 +00002950static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002951{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002952 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002953 { 0x1f, 0x0002 },
2954 { 0x10, 0x0008 },
2955 { 0x0d, 0x006c },
2956
2957 { 0x1f, 0x0000 },
2958 { 0x0d, 0xf880 },
2959
2960 { 0x1f, 0x0001 },
2961 { 0x17, 0x0cc0 },
2962
2963 { 0x1f, 0x0001 },
2964 { 0x0b, 0xa4d8 },
2965 { 0x09, 0x281c },
2966 { 0x07, 0x2883 },
2967 { 0x0a, 0x6b35 },
2968 { 0x1d, 0x3da4 },
2969 { 0x1c, 0xeffd },
2970 { 0x14, 0x7f52 },
2971 { 0x18, 0x7fc6 },
2972 { 0x08, 0x0601 },
2973 { 0x06, 0x4063 },
2974 { 0x10, 0xf074 },
2975 { 0x1f, 0x0003 },
2976 { 0x13, 0x0789 },
2977 { 0x12, 0xf4bd },
2978 { 0x1a, 0x04fd },
2979 { 0x14, 0x84b0 },
2980 { 0x1f, 0x0000 },
2981 { 0x00, 0x9200 },
2982
2983 { 0x1f, 0x0005 },
2984 { 0x01, 0x0340 },
2985 { 0x1f, 0x0001 },
2986 { 0x04, 0x4000 },
2987 { 0x03, 0x1d21 },
2988 { 0x02, 0x0c32 },
2989 { 0x01, 0x0200 },
2990 { 0x00, 0x5554 },
2991 { 0x04, 0x4800 },
2992 { 0x04, 0x4000 },
2993 { 0x04, 0xf000 },
2994 { 0x03, 0xdf01 },
2995 { 0x02, 0xdf20 },
2996 { 0x01, 0x101a },
2997 { 0x00, 0xa0ff },
2998 { 0x04, 0xf800 },
2999 { 0x04, 0xf000 },
3000 { 0x1f, 0x0000 },
3001
3002 { 0x1f, 0x0007 },
3003 { 0x1e, 0x0023 },
3004 { 0x16, 0x0000 },
3005 { 0x1f, 0x0000 }
3006 };
3007
françois romieu4da19632011-01-03 15:07:55 +00003008 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003009}
3010
françois romieue6de30d2011-01-03 15:08:37 +00003011static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3012{
3013 static const struct phy_reg phy_reg_init[] = {
3014 { 0x1f, 0x0001 },
3015 { 0x17, 0x0cc0 },
3016
3017 { 0x1f, 0x0007 },
3018 { 0x1e, 0x002d },
3019 { 0x18, 0x0040 },
3020 { 0x1f, 0x0000 }
3021 };
3022
3023 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3024 rtl_patchphy(tp, 0x0d, 1 << 5);
3025}
3026
Hayes Wang70090422011-07-06 15:58:06 +08003027static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003028{
3029 static const struct phy_reg phy_reg_init[] = {
3030 /* Enable Delay cap */
3031 { 0x1f, 0x0005 },
3032 { 0x05, 0x8b80 },
3033 { 0x06, 0xc896 },
3034 { 0x1f, 0x0000 },
3035
3036 /* Channel estimation fine tune */
3037 { 0x1f, 0x0001 },
3038 { 0x0b, 0x6c20 },
3039 { 0x07, 0x2872 },
3040 { 0x1c, 0xefff },
3041 { 0x1f, 0x0003 },
3042 { 0x14, 0x6420 },
3043 { 0x1f, 0x0000 },
3044
3045 /* Update PFM & 10M TX idle timer */
3046 { 0x1f, 0x0007 },
3047 { 0x1e, 0x002f },
3048 { 0x15, 0x1919 },
3049 { 0x1f, 0x0000 },
3050
3051 { 0x1f, 0x0007 },
3052 { 0x1e, 0x00ac },
3053 { 0x18, 0x0006 },
3054 { 0x1f, 0x0000 }
3055 };
3056
Francois Romieu15ecd032011-04-27 13:52:22 -07003057 rtl_apply_firmware(tp);
3058
hayeswang01dc7fe2011-03-21 01:50:28 +00003059 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3060
3061 /* DCO enable for 10M IDLE Power */
3062 rtl_writephy(tp, 0x1f, 0x0007);
3063 rtl_writephy(tp, 0x1e, 0x0023);
3064 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
3065 rtl_writephy(tp, 0x1f, 0x0000);
3066
3067 /* For impedance matching */
3068 rtl_writephy(tp, 0x1f, 0x0002);
3069 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003070 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003071
3072 /* PHY auto speed down */
3073 rtl_writephy(tp, 0x1f, 0x0007);
3074 rtl_writephy(tp, 0x1e, 0x002d);
3075 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
3076 rtl_writephy(tp, 0x1f, 0x0000);
3077 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3078
3079 rtl_writephy(tp, 0x1f, 0x0005);
3080 rtl_writephy(tp, 0x05, 0x8b86);
3081 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3082 rtl_writephy(tp, 0x1f, 0x0000);
3083
3084 rtl_writephy(tp, 0x1f, 0x0005);
3085 rtl_writephy(tp, 0x05, 0x8b85);
3086 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3087 rtl_writephy(tp, 0x1f, 0x0007);
3088 rtl_writephy(tp, 0x1e, 0x0020);
3089 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
3090 rtl_writephy(tp, 0x1f, 0x0006);
3091 rtl_writephy(tp, 0x00, 0x5a00);
3092 rtl_writephy(tp, 0x1f, 0x0000);
3093 rtl_writephy(tp, 0x0d, 0x0007);
3094 rtl_writephy(tp, 0x0e, 0x003c);
3095 rtl_writephy(tp, 0x0d, 0x4007);
3096 rtl_writephy(tp, 0x0e, 0x0000);
3097 rtl_writephy(tp, 0x0d, 0x0000);
3098}
3099
françois romieu9ecb9aa2012-12-07 11:20:21 +00003100static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3101{
3102 const u16 w[] = {
3103 addr[0] | (addr[1] << 8),
3104 addr[2] | (addr[3] << 8),
3105 addr[4] | (addr[5] << 8)
3106 };
3107 const struct exgmac_reg e[] = {
3108 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3109 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3110 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3111 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3112 };
3113
3114 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3115}
3116
Hayes Wang70090422011-07-06 15:58:06 +08003117static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3118{
3119 static const struct phy_reg phy_reg_init[] = {
3120 /* Enable Delay cap */
3121 { 0x1f, 0x0004 },
3122 { 0x1f, 0x0007 },
3123 { 0x1e, 0x00ac },
3124 { 0x18, 0x0006 },
3125 { 0x1f, 0x0002 },
3126 { 0x1f, 0x0000 },
3127 { 0x1f, 0x0000 },
3128
3129 /* Channel estimation fine tune */
3130 { 0x1f, 0x0003 },
3131 { 0x09, 0xa20f },
3132 { 0x1f, 0x0000 },
3133 { 0x1f, 0x0000 },
3134
3135 /* Green Setting */
3136 { 0x1f, 0x0005 },
3137 { 0x05, 0x8b5b },
3138 { 0x06, 0x9222 },
3139 { 0x05, 0x8b6d },
3140 { 0x06, 0x8000 },
3141 { 0x05, 0x8b76 },
3142 { 0x06, 0x8000 },
3143 { 0x1f, 0x0000 }
3144 };
3145
3146 rtl_apply_firmware(tp);
3147
3148 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3149
3150 /* For 4-corner performance improve */
3151 rtl_writephy(tp, 0x1f, 0x0005);
3152 rtl_writephy(tp, 0x05, 0x8b80);
3153 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
3154 rtl_writephy(tp, 0x1f, 0x0000);
3155
3156 /* PHY auto speed down */
3157 rtl_writephy(tp, 0x1f, 0x0004);
3158 rtl_writephy(tp, 0x1f, 0x0007);
3159 rtl_writephy(tp, 0x1e, 0x002d);
3160 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3161 rtl_writephy(tp, 0x1f, 0x0002);
3162 rtl_writephy(tp, 0x1f, 0x0000);
3163 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3164
3165 /* improve 10M EEE waveform */
3166 rtl_writephy(tp, 0x1f, 0x0005);
3167 rtl_writephy(tp, 0x05, 0x8b86);
3168 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3169 rtl_writephy(tp, 0x1f, 0x0000);
3170
3171 /* Improve 2-pair detection performance */
3172 rtl_writephy(tp, 0x1f, 0x0005);
3173 rtl_writephy(tp, 0x05, 0x8b85);
3174 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3175 rtl_writephy(tp, 0x1f, 0x0000);
3176
3177 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003178 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003179 rtl_writephy(tp, 0x1f, 0x0005);
3180 rtl_writephy(tp, 0x05, 0x8b85);
3181 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3182 rtl_writephy(tp, 0x1f, 0x0004);
3183 rtl_writephy(tp, 0x1f, 0x0007);
3184 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04003185 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003186 rtl_writephy(tp, 0x1f, 0x0002);
3187 rtl_writephy(tp, 0x1f, 0x0000);
3188 rtl_writephy(tp, 0x0d, 0x0007);
3189 rtl_writephy(tp, 0x0e, 0x003c);
3190 rtl_writephy(tp, 0x0d, 0x4007);
3191 rtl_writephy(tp, 0x0e, 0x0000);
3192 rtl_writephy(tp, 0x0d, 0x0000);
3193
3194 /* Green feature */
3195 rtl_writephy(tp, 0x1f, 0x0003);
3196 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3197 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3198 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003199
françois romieu9ecb9aa2012-12-07 11:20:21 +00003200 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3201 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003202}
3203
Hayes Wang5f886e02012-03-30 14:33:03 +08003204static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3205{
3206 /* For 4-corner performance improve */
3207 rtl_writephy(tp, 0x1f, 0x0005);
3208 rtl_writephy(tp, 0x05, 0x8b80);
3209 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3210 rtl_writephy(tp, 0x1f, 0x0000);
3211
3212 /* PHY auto speed down */
3213 rtl_writephy(tp, 0x1f, 0x0007);
3214 rtl_writephy(tp, 0x1e, 0x002d);
3215 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3216 rtl_writephy(tp, 0x1f, 0x0000);
3217 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3218
3219 /* Improve 10M EEE waveform */
3220 rtl_writephy(tp, 0x1f, 0x0005);
3221 rtl_writephy(tp, 0x05, 0x8b86);
3222 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3223 rtl_writephy(tp, 0x1f, 0x0000);
3224}
3225
Hayes Wangc2218922011-09-06 16:55:18 +08003226static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3227{
3228 static const struct phy_reg phy_reg_init[] = {
3229 /* Channel estimation fine tune */
3230 { 0x1f, 0x0003 },
3231 { 0x09, 0xa20f },
3232 { 0x1f, 0x0000 },
3233
3234 /* Modify green table for giga & fnet */
3235 { 0x1f, 0x0005 },
3236 { 0x05, 0x8b55 },
3237 { 0x06, 0x0000 },
3238 { 0x05, 0x8b5e },
3239 { 0x06, 0x0000 },
3240 { 0x05, 0x8b67 },
3241 { 0x06, 0x0000 },
3242 { 0x05, 0x8b70 },
3243 { 0x06, 0x0000 },
3244 { 0x1f, 0x0000 },
3245 { 0x1f, 0x0007 },
3246 { 0x1e, 0x0078 },
3247 { 0x17, 0x0000 },
3248 { 0x19, 0x00fb },
3249 { 0x1f, 0x0000 },
3250
3251 /* Modify green table for 10M */
3252 { 0x1f, 0x0005 },
3253 { 0x05, 0x8b79 },
3254 { 0x06, 0xaa00 },
3255 { 0x1f, 0x0000 },
3256
3257 /* Disable hiimpedance detection (RTCT) */
3258 { 0x1f, 0x0003 },
3259 { 0x01, 0x328a },
3260 { 0x1f, 0x0000 }
3261 };
3262
3263 rtl_apply_firmware(tp);
3264
3265 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3266
Hayes Wang5f886e02012-03-30 14:33:03 +08003267 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003268
3269 /* Improve 2-pair detection performance */
3270 rtl_writephy(tp, 0x1f, 0x0005);
3271 rtl_writephy(tp, 0x05, 0x8b85);
3272 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3273 rtl_writephy(tp, 0x1f, 0x0000);
3274}
3275
3276static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3277{
3278 rtl_apply_firmware(tp);
3279
Hayes Wang5f886e02012-03-30 14:33:03 +08003280 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003281}
3282
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003283static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3284{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003285 static const struct phy_reg phy_reg_init[] = {
3286 /* Channel estimation fine tune */
3287 { 0x1f, 0x0003 },
3288 { 0x09, 0xa20f },
3289 { 0x1f, 0x0000 },
3290
3291 /* Modify green table for giga & fnet */
3292 { 0x1f, 0x0005 },
3293 { 0x05, 0x8b55 },
3294 { 0x06, 0x0000 },
3295 { 0x05, 0x8b5e },
3296 { 0x06, 0x0000 },
3297 { 0x05, 0x8b67 },
3298 { 0x06, 0x0000 },
3299 { 0x05, 0x8b70 },
3300 { 0x06, 0x0000 },
3301 { 0x1f, 0x0000 },
3302 { 0x1f, 0x0007 },
3303 { 0x1e, 0x0078 },
3304 { 0x17, 0x0000 },
3305 { 0x19, 0x00aa },
3306 { 0x1f, 0x0000 },
3307
3308 /* Modify green table for 10M */
3309 { 0x1f, 0x0005 },
3310 { 0x05, 0x8b79 },
3311 { 0x06, 0xaa00 },
3312 { 0x1f, 0x0000 },
3313
3314 /* Disable hiimpedance detection (RTCT) */
3315 { 0x1f, 0x0003 },
3316 { 0x01, 0x328a },
3317 { 0x1f, 0x0000 }
3318 };
3319
3320
3321 rtl_apply_firmware(tp);
3322
3323 rtl8168f_hw_phy_config(tp);
3324
3325 /* Improve 2-pair detection performance */
3326 rtl_writephy(tp, 0x1f, 0x0005);
3327 rtl_writephy(tp, 0x05, 0x8b85);
3328 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3329 rtl_writephy(tp, 0x1f, 0x0000);
3330
3331 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3332
3333 /* Modify green table for giga */
3334 rtl_writephy(tp, 0x1f, 0x0005);
3335 rtl_writephy(tp, 0x05, 0x8b54);
3336 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3337 rtl_writephy(tp, 0x05, 0x8b5d);
3338 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3339 rtl_writephy(tp, 0x05, 0x8a7c);
3340 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3341 rtl_writephy(tp, 0x05, 0x8a7f);
3342 rtl_w1w0_phy(tp, 0x06, 0x0100, 0x0000);
3343 rtl_writephy(tp, 0x05, 0x8a82);
3344 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3345 rtl_writephy(tp, 0x05, 0x8a85);
3346 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3347 rtl_writephy(tp, 0x05, 0x8a88);
3348 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3349 rtl_writephy(tp, 0x1f, 0x0000);
3350
3351 /* uc same-seed solution */
3352 rtl_writephy(tp, 0x1f, 0x0005);
3353 rtl_writephy(tp, 0x05, 0x8b85);
3354 rtl_w1w0_phy(tp, 0x06, 0x8000, 0x0000);
3355 rtl_writephy(tp, 0x1f, 0x0000);
3356
3357 /* eee setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003358 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003359 rtl_writephy(tp, 0x1f, 0x0005);
3360 rtl_writephy(tp, 0x05, 0x8b85);
3361 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3362 rtl_writephy(tp, 0x1f, 0x0004);
3363 rtl_writephy(tp, 0x1f, 0x0007);
3364 rtl_writephy(tp, 0x1e, 0x0020);
3365 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
3366 rtl_writephy(tp, 0x1f, 0x0000);
3367 rtl_writephy(tp, 0x0d, 0x0007);
3368 rtl_writephy(tp, 0x0e, 0x003c);
3369 rtl_writephy(tp, 0x0d, 0x4007);
3370 rtl_writephy(tp, 0x0e, 0x0000);
3371 rtl_writephy(tp, 0x0d, 0x0000);
3372
3373 /* Green feature */
3374 rtl_writephy(tp, 0x1f, 0x0003);
3375 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3376 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3377 rtl_writephy(tp, 0x1f, 0x0000);
3378}
3379
Hayes Wangc5583862012-07-02 17:23:22 +08003380static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3381{
Hayes Wangc5583862012-07-02 17:23:22 +08003382 rtl_apply_firmware(tp);
3383
hayeswang41f44d12013-04-01 22:23:36 +00003384 rtl_writephy(tp, 0x1f, 0x0a46);
3385 if (rtl_readphy(tp, 0x10) & 0x0100) {
3386 rtl_writephy(tp, 0x1f, 0x0bcc);
3387 rtl_w1w0_phy(tp, 0x12, 0x0000, 0x8000);
3388 } else {
3389 rtl_writephy(tp, 0x1f, 0x0bcc);
3390 rtl_w1w0_phy(tp, 0x12, 0x8000, 0x0000);
3391 }
Hayes Wangc5583862012-07-02 17:23:22 +08003392
hayeswang41f44d12013-04-01 22:23:36 +00003393 rtl_writephy(tp, 0x1f, 0x0a46);
3394 if (rtl_readphy(tp, 0x13) & 0x0100) {
3395 rtl_writephy(tp, 0x1f, 0x0c41);
3396 rtl_w1w0_phy(tp, 0x15, 0x0002, 0x0000);
3397 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003398 rtl_writephy(tp, 0x1f, 0x0c41);
3399 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003400 }
Hayes Wangc5583862012-07-02 17:23:22 +08003401
hayeswang41f44d12013-04-01 22:23:36 +00003402 /* Enable PHY auto speed down */
3403 rtl_writephy(tp, 0x1f, 0x0a44);
3404 rtl_w1w0_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003405
hayeswangfe7524c2013-04-01 22:23:37 +00003406 rtl_writephy(tp, 0x1f, 0x0bcc);
3407 rtl_w1w0_phy(tp, 0x14, 0x0100, 0x0000);
3408 rtl_writephy(tp, 0x1f, 0x0a44);
3409 rtl_w1w0_phy(tp, 0x11, 0x00c0, 0x0000);
3410 rtl_writephy(tp, 0x1f, 0x0a43);
3411 rtl_writephy(tp, 0x13, 0x8084);
3412 rtl_w1w0_phy(tp, 0x14, 0x0000, 0x6000);
3413 rtl_w1w0_phy(tp, 0x10, 0x1003, 0x0000);
3414
hayeswang41f44d12013-04-01 22:23:36 +00003415 /* EEE auto-fallback function */
3416 rtl_writephy(tp, 0x1f, 0x0a4b);
3417 rtl_w1w0_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003418
hayeswang41f44d12013-04-01 22:23:36 +00003419 /* Enable UC LPF tune function */
3420 rtl_writephy(tp, 0x1f, 0x0a43);
3421 rtl_writephy(tp, 0x13, 0x8012);
3422 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3423
3424 rtl_writephy(tp, 0x1f, 0x0c42);
3425 rtl_w1w0_phy(tp, 0x11, 0x4000, 0x2000);
3426
hayeswangfe7524c2013-04-01 22:23:37 +00003427 /* Improve SWR Efficiency */
3428 rtl_writephy(tp, 0x1f, 0x0bcd);
3429 rtl_writephy(tp, 0x14, 0x5065);
3430 rtl_writephy(tp, 0x14, 0xd065);
3431 rtl_writephy(tp, 0x1f, 0x0bc8);
3432 rtl_writephy(tp, 0x11, 0x5655);
3433 rtl_writephy(tp, 0x1f, 0x0bcd);
3434 rtl_writephy(tp, 0x14, 0x1065);
3435 rtl_writephy(tp, 0x14, 0x9065);
3436 rtl_writephy(tp, 0x14, 0x1065);
3437
hayeswang41f44d12013-04-01 22:23:36 +00003438 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003439}
3440
françois romieu4da19632011-01-03 15:07:55 +00003441static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003442{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003443 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003444 { 0x1f, 0x0003 },
3445 { 0x08, 0x441d },
3446 { 0x01, 0x9100 },
3447 { 0x1f, 0x0000 }
3448 };
3449
françois romieu4da19632011-01-03 15:07:55 +00003450 rtl_writephy(tp, 0x1f, 0x0000);
3451 rtl_patchphy(tp, 0x11, 1 << 12);
3452 rtl_patchphy(tp, 0x19, 1 << 13);
3453 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003454
françois romieu4da19632011-01-03 15:07:55 +00003455 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003456}
3457
Hayes Wang5a5e4442011-02-22 17:26:21 +08003458static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3459{
3460 static const struct phy_reg phy_reg_init[] = {
3461 { 0x1f, 0x0005 },
3462 { 0x1a, 0x0000 },
3463 { 0x1f, 0x0000 },
3464
3465 { 0x1f, 0x0004 },
3466 { 0x1c, 0x0000 },
3467 { 0x1f, 0x0000 },
3468
3469 { 0x1f, 0x0001 },
3470 { 0x15, 0x7701 },
3471 { 0x1f, 0x0000 }
3472 };
3473
3474 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003475 rtl_writephy(tp, 0x1f, 0x0000);
3476 rtl_writephy(tp, 0x18, 0x0310);
3477 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003478
François Romieu953a12c2011-04-24 17:38:48 +02003479 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003480
3481 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3482}
3483
Hayes Wang7e18dca2012-03-30 14:33:02 +08003484static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3485{
Hayes Wang7e18dca2012-03-30 14:33:02 +08003486 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01003487 rtl_writephy(tp, 0x1f, 0x0000);
3488 rtl_writephy(tp, 0x18, 0x0310);
3489 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003490
3491 rtl_apply_firmware(tp);
3492
3493 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003494 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003495 rtl_writephy(tp, 0x1f, 0x0004);
3496 rtl_writephy(tp, 0x10, 0x401f);
3497 rtl_writephy(tp, 0x19, 0x7030);
3498 rtl_writephy(tp, 0x1f, 0x0000);
3499}
3500
Hayes Wang5598bfe2012-07-02 17:23:21 +08003501static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3502{
Hayes Wang5598bfe2012-07-02 17:23:21 +08003503 static const struct phy_reg phy_reg_init[] = {
3504 { 0x1f, 0x0004 },
3505 { 0x10, 0xc07f },
3506 { 0x19, 0x7030 },
3507 { 0x1f, 0x0000 }
3508 };
3509
3510 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003511 rtl_writephy(tp, 0x1f, 0x0000);
3512 rtl_writephy(tp, 0x18, 0x0310);
3513 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003514
3515 rtl_apply_firmware(tp);
3516
Francois Romieufdf6fc02012-07-06 22:40:38 +02003517 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003518 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3519
Francois Romieufdf6fc02012-07-06 22:40:38 +02003520 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003521}
3522
Francois Romieu5615d9f2007-08-17 17:50:46 +02003523static void rtl_hw_phy_config(struct net_device *dev)
3524{
3525 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003526
3527 rtl8169_print_mac_version(tp);
3528
3529 switch (tp->mac_version) {
3530 case RTL_GIGA_MAC_VER_01:
3531 break;
3532 case RTL_GIGA_MAC_VER_02:
3533 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003534 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003535 break;
3536 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003537 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003538 break;
françois romieu2e9558562009-08-10 19:44:19 +00003539 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003540 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003541 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003542 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003543 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003544 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003545 case RTL_GIGA_MAC_VER_07:
3546 case RTL_GIGA_MAC_VER_08:
3547 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003548 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003549 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003550 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003551 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003552 break;
3553 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003554 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003555 break;
3556 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003557 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003558 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003559 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003560 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003561 break;
3562 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003563 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003564 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003565 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003566 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003567 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003568 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003569 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003570 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003571 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003572 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003573 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003574 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003575 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003576 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003577 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003578 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003579 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003580 break;
3581 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003582 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003583 break;
3584 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003585 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003586 break;
françois romieue6de30d2011-01-03 15:08:37 +00003587 case RTL_GIGA_MAC_VER_28:
3588 rtl8168d_4_hw_phy_config(tp);
3589 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003590 case RTL_GIGA_MAC_VER_29:
3591 case RTL_GIGA_MAC_VER_30:
3592 rtl8105e_hw_phy_config(tp);
3593 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003594 case RTL_GIGA_MAC_VER_31:
3595 /* None. */
3596 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003597 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003598 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003599 rtl8168e_1_hw_phy_config(tp);
3600 break;
3601 case RTL_GIGA_MAC_VER_34:
3602 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003603 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003604 case RTL_GIGA_MAC_VER_35:
3605 rtl8168f_1_hw_phy_config(tp);
3606 break;
3607 case RTL_GIGA_MAC_VER_36:
3608 rtl8168f_2_hw_phy_config(tp);
3609 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003610
Hayes Wang7e18dca2012-03-30 14:33:02 +08003611 case RTL_GIGA_MAC_VER_37:
3612 rtl8402_hw_phy_config(tp);
3613 break;
3614
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003615 case RTL_GIGA_MAC_VER_38:
3616 rtl8411_hw_phy_config(tp);
3617 break;
3618
Hayes Wang5598bfe2012-07-02 17:23:21 +08003619 case RTL_GIGA_MAC_VER_39:
3620 rtl8106e_hw_phy_config(tp);
3621 break;
3622
Hayes Wangc5583862012-07-02 17:23:22 +08003623 case RTL_GIGA_MAC_VER_40:
3624 rtl8168g_1_hw_phy_config(tp);
3625 break;
3626
3627 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02003628 default:
3629 break;
3630 }
3631}
3632
Francois Romieuda78dbf2012-01-26 14:18:23 +01003633static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 struct timer_list *timer = &tp->timer;
3636 void __iomem *ioaddr = tp->mmio_addr;
3637 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3638
Francois Romieubcf0bf92006-07-26 23:14:13 +02003639 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
françois romieu4da19632011-01-03 15:07:55 +00003641 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003642 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 * A busy loop could burn quite a few cycles on nowadays CPU.
3644 * Let's delay the execution of the timer for a few ticks.
3645 */
3646 timeout = HZ/10;
3647 goto out_mod_timer;
3648 }
3649
3650 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01003651 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
Francois Romieuda78dbf2012-01-26 14:18:23 +01003653 netif_warn(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
françois romieu4da19632011-01-03 15:07:55 +00003655 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
3657out_mod_timer:
3658 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003659}
3660
3661static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3662{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003663 if (!test_and_set_bit(flag, tp->wk.flags))
3664 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003665}
3666
3667static void rtl8169_phy_timer(unsigned long __opaque)
3668{
3669 struct net_device *dev = (struct net_device *)__opaque;
3670 struct rtl8169_private *tp = netdev_priv(dev);
3671
Francois Romieu98ddf982012-01-31 10:47:34 +01003672 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673}
3674
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3676 void __iomem *ioaddr)
3677{
3678 iounmap(ioaddr);
3679 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003680 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 pci_disable_device(pdev);
3682 free_netdev(dev);
3683}
3684
Francois Romieuffc46952012-07-06 14:19:23 +02003685DECLARE_RTL_COND(rtl_phy_reset_cond)
3686{
3687 return tp->phy_reset_pending(tp);
3688}
3689
Francois Romieubf793292006-11-01 00:53:05 +01003690static void rtl8169_phy_reset(struct net_device *dev,
3691 struct rtl8169_private *tp)
3692{
françois romieu4da19632011-01-03 15:07:55 +00003693 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02003694 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01003695}
3696
David S. Miller8decf862011-09-22 03:23:13 -04003697static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3698{
3699 void __iomem *ioaddr = tp->mmio_addr;
3700
3701 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3702 (RTL_R8(PHYstatus) & TBI_Enable);
3703}
3704
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003705static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003707 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003708
Francois Romieu5615d9f2007-08-17 17:50:46 +02003709 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003710
Marcus Sundberg773328942008-07-10 21:28:08 +02003711 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3712 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3713 RTL_W8(0x82, 0x01);
3714 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003715
Francois Romieu6dccd162007-02-13 23:38:05 +01003716 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3717
3718 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3719 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003720
Francois Romieubcf0bf92006-07-26 23:14:13 +02003721 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003722 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3723 RTL_W8(0x82, 0x01);
3724 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003725 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003726 }
3727
Francois Romieubf793292006-11-01 00:53:05 +01003728 rtl8169_phy_reset(dev, tp);
3729
Oliver Neukum54405cd2011-01-06 21:55:13 +01003730 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003731 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3732 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3733 (tp->mii.supports_gmii ?
3734 ADVERTISED_1000baseT_Half |
3735 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003736
David S. Miller8decf862011-09-22 03:23:13 -04003737 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003738 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003739}
3740
Francois Romieu773d2022007-01-31 23:47:43 +01003741static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3742{
3743 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu773d2022007-01-31 23:47:43 +01003744
Francois Romieuda78dbf2012-01-26 14:18:23 +01003745 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003746
3747 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003748
françois romieu9ecb9aa2012-12-07 11:20:21 +00003749 RTL_W32(MAC4, addr[4] | addr[5] << 8);
françois romieu908ba2b2010-04-26 11:42:58 +00003750 RTL_R32(MAC4);
3751
françois romieu9ecb9aa2012-12-07 11:20:21 +00003752 RTL_W32(MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
françois romieu908ba2b2010-04-26 11:42:58 +00003753 RTL_R32(MAC0);
3754
françois romieu9ecb9aa2012-12-07 11:20:21 +00003755 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
3756 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00003757
Francois Romieu773d2022007-01-31 23:47:43 +01003758 RTL_W8(Cfg9346, Cfg9346_Lock);
3759
Francois Romieuda78dbf2012-01-26 14:18:23 +01003760 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003761}
3762
3763static int rtl_set_mac_address(struct net_device *dev, void *p)
3764{
3765 struct rtl8169_private *tp = netdev_priv(dev);
3766 struct sockaddr *addr = p;
3767
3768 if (!is_valid_ether_addr(addr->sa_data))
3769 return -EADDRNOTAVAIL;
3770
3771 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3772
3773 rtl_rar_set(tp, dev->dev_addr);
3774
3775 return 0;
3776}
3777
Francois Romieu5f787a12006-08-17 13:02:36 +02003778static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3779{
3780 struct rtl8169_private *tp = netdev_priv(dev);
3781 struct mii_ioctl_data *data = if_mii(ifr);
3782
Francois Romieu8b4ab282008-11-19 22:05:25 -08003783 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3784}
Francois Romieu5f787a12006-08-17 13:02:36 +02003785
Francois Romieucecb5fd2011-04-01 10:21:07 +02003786static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3787 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003788{
Francois Romieu5f787a12006-08-17 13:02:36 +02003789 switch (cmd) {
3790 case SIOCGMIIPHY:
3791 data->phy_id = 32; /* Internal PHY */
3792 return 0;
3793
3794 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003795 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003796 return 0;
3797
3798 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003799 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003800 return 0;
3801 }
3802 return -EOPNOTSUPP;
3803}
3804
Francois Romieu8b4ab282008-11-19 22:05:25 -08003805static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3806{
3807 return -EOPNOTSUPP;
3808}
3809
Francois Romieufbac58f2007-10-04 22:51:38 +02003810static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3811{
3812 if (tp->features & RTL_FEATURE_MSI) {
3813 pci_disable_msi(pdev);
3814 tp->features &= ~RTL_FEATURE_MSI;
3815 }
3816}
3817
Bill Pembertonbaf63292012-12-03 09:23:28 -05003818static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00003819{
3820 struct mdio_ops *ops = &tp->mdio_ops;
3821
3822 switch (tp->mac_version) {
3823 case RTL_GIGA_MAC_VER_27:
3824 ops->write = r8168dp_1_mdio_write;
3825 ops->read = r8168dp_1_mdio_read;
3826 break;
françois romieue6de30d2011-01-03 15:08:37 +00003827 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003828 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003829 ops->write = r8168dp_2_mdio_write;
3830 ops->read = r8168dp_2_mdio_read;
3831 break;
Hayes Wangc5583862012-07-02 17:23:22 +08003832 case RTL_GIGA_MAC_VER_40:
3833 case RTL_GIGA_MAC_VER_41:
3834 ops->write = r8168g_mdio_write;
3835 ops->read = r8168g_mdio_read;
3836 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003837 default:
3838 ops->write = r8169_mdio_write;
3839 ops->read = r8169_mdio_read;
3840 break;
3841 }
3842}
3843
David S. Miller1805b2f2011-10-24 18:18:09 -04003844static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3845{
3846 void __iomem *ioaddr = tp->mmio_addr;
3847
3848 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00003849 case RTL_GIGA_MAC_VER_25:
3850 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04003851 case RTL_GIGA_MAC_VER_29:
3852 case RTL_GIGA_MAC_VER_30:
3853 case RTL_GIGA_MAC_VER_32:
3854 case RTL_GIGA_MAC_VER_33:
3855 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08003856 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003857 case RTL_GIGA_MAC_VER_38:
Hayes Wang5598bfe2012-07-02 17:23:21 +08003858 case RTL_GIGA_MAC_VER_39:
Hayes Wangc5583862012-07-02 17:23:22 +08003859 case RTL_GIGA_MAC_VER_40:
3860 case RTL_GIGA_MAC_VER_41:
David S. Miller1805b2f2011-10-24 18:18:09 -04003861 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3862 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3863 break;
3864 default:
3865 break;
3866 }
3867}
3868
3869static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3870{
3871 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3872 return false;
3873
3874 rtl_writephy(tp, 0x1f, 0x0000);
3875 rtl_writephy(tp, MII_BMCR, 0x0000);
3876
3877 rtl_wol_suspend_quirk(tp);
3878
3879 return true;
3880}
3881
françois romieu065c27c2011-01-03 15:08:12 +00003882static void r810x_phy_power_down(struct rtl8169_private *tp)
3883{
3884 rtl_writephy(tp, 0x1f, 0x0000);
3885 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3886}
3887
3888static void r810x_phy_power_up(struct rtl8169_private *tp)
3889{
3890 rtl_writephy(tp, 0x1f, 0x0000);
3891 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3892}
3893
3894static void r810x_pll_power_down(struct rtl8169_private *tp)
3895{
Hayes Wang00042992012-03-30 14:33:00 +08003896 void __iomem *ioaddr = tp->mmio_addr;
3897
David S. Miller1805b2f2011-10-24 18:18:09 -04003898 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003899 return;
françois romieu065c27c2011-01-03 15:08:12 +00003900
3901 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003902
3903 switch (tp->mac_version) {
3904 case RTL_GIGA_MAC_VER_07:
3905 case RTL_GIGA_MAC_VER_08:
3906 case RTL_GIGA_MAC_VER_09:
3907 case RTL_GIGA_MAC_VER_10:
3908 case RTL_GIGA_MAC_VER_13:
3909 case RTL_GIGA_MAC_VER_16:
3910 break;
3911 default:
3912 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3913 break;
3914 }
françois romieu065c27c2011-01-03 15:08:12 +00003915}
3916
3917static void r810x_pll_power_up(struct rtl8169_private *tp)
3918{
Hayes Wang00042992012-03-30 14:33:00 +08003919 void __iomem *ioaddr = tp->mmio_addr;
3920
françois romieu065c27c2011-01-03 15:08:12 +00003921 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003922
3923 switch (tp->mac_version) {
3924 case RTL_GIGA_MAC_VER_07:
3925 case RTL_GIGA_MAC_VER_08:
3926 case RTL_GIGA_MAC_VER_09:
3927 case RTL_GIGA_MAC_VER_10:
3928 case RTL_GIGA_MAC_VER_13:
3929 case RTL_GIGA_MAC_VER_16:
3930 break;
3931 default:
3932 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3933 break;
3934 }
françois romieu065c27c2011-01-03 15:08:12 +00003935}
3936
3937static void r8168_phy_power_up(struct rtl8169_private *tp)
3938{
3939 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003940 switch (tp->mac_version) {
3941 case RTL_GIGA_MAC_VER_11:
3942 case RTL_GIGA_MAC_VER_12:
3943 case RTL_GIGA_MAC_VER_17:
3944 case RTL_GIGA_MAC_VER_18:
3945 case RTL_GIGA_MAC_VER_19:
3946 case RTL_GIGA_MAC_VER_20:
3947 case RTL_GIGA_MAC_VER_21:
3948 case RTL_GIGA_MAC_VER_22:
3949 case RTL_GIGA_MAC_VER_23:
3950 case RTL_GIGA_MAC_VER_24:
3951 case RTL_GIGA_MAC_VER_25:
3952 case RTL_GIGA_MAC_VER_26:
3953 case RTL_GIGA_MAC_VER_27:
3954 case RTL_GIGA_MAC_VER_28:
3955 case RTL_GIGA_MAC_VER_31:
3956 rtl_writephy(tp, 0x0e, 0x0000);
3957 break;
3958 default:
3959 break;
3960 }
françois romieu065c27c2011-01-03 15:08:12 +00003961 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3962}
3963
3964static void r8168_phy_power_down(struct rtl8169_private *tp)
3965{
3966 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003967 switch (tp->mac_version) {
3968 case RTL_GIGA_MAC_VER_32:
3969 case RTL_GIGA_MAC_VER_33:
3970 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3971 break;
3972
3973 case RTL_GIGA_MAC_VER_11:
3974 case RTL_GIGA_MAC_VER_12:
3975 case RTL_GIGA_MAC_VER_17:
3976 case RTL_GIGA_MAC_VER_18:
3977 case RTL_GIGA_MAC_VER_19:
3978 case RTL_GIGA_MAC_VER_20:
3979 case RTL_GIGA_MAC_VER_21:
3980 case RTL_GIGA_MAC_VER_22:
3981 case RTL_GIGA_MAC_VER_23:
3982 case RTL_GIGA_MAC_VER_24:
3983 case RTL_GIGA_MAC_VER_25:
3984 case RTL_GIGA_MAC_VER_26:
3985 case RTL_GIGA_MAC_VER_27:
3986 case RTL_GIGA_MAC_VER_28:
3987 case RTL_GIGA_MAC_VER_31:
3988 rtl_writephy(tp, 0x0e, 0x0200);
3989 default:
3990 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3991 break;
3992 }
françois romieu065c27c2011-01-03 15:08:12 +00003993}
3994
3995static void r8168_pll_power_down(struct rtl8169_private *tp)
3996{
3997 void __iomem *ioaddr = tp->mmio_addr;
3998
Francois Romieucecb5fd2011-04-01 10:21:07 +02003999 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4000 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4001 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00004002 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00004003 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08004004 }
françois romieu065c27c2011-01-03 15:08:12 +00004005
Francois Romieucecb5fd2011-04-01 10:21:07 +02004006 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4007 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00004008 (RTL_R16(CPlusCmd) & ASF)) {
4009 return;
4010 }
4011
hayeswang01dc7fe2011-03-21 01:50:28 +00004012 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4013 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004014 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004015
David S. Miller1805b2f2011-10-24 18:18:09 -04004016 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004017 return;
françois romieu065c27c2011-01-03 15:08:12 +00004018
4019 r8168_phy_power_down(tp);
4020
4021 switch (tp->mac_version) {
4022 case RTL_GIGA_MAC_VER_25:
4023 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004024 case RTL_GIGA_MAC_VER_27:
4025 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004026 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004027 case RTL_GIGA_MAC_VER_32:
4028 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00004029 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4030 break;
4031 }
4032}
4033
4034static void r8168_pll_power_up(struct rtl8169_private *tp)
4035{
4036 void __iomem *ioaddr = tp->mmio_addr;
4037
françois romieu065c27c2011-01-03 15:08:12 +00004038 switch (tp->mac_version) {
4039 case RTL_GIGA_MAC_VER_25:
4040 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004041 case RTL_GIGA_MAC_VER_27:
4042 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004043 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004044 case RTL_GIGA_MAC_VER_32:
4045 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00004046 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4047 break;
4048 }
4049
4050 r8168_phy_power_up(tp);
4051}
4052
Francois Romieud58d46b2011-05-03 16:38:29 +02004053static void rtl_generic_op(struct rtl8169_private *tp,
4054 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00004055{
4056 if (op)
4057 op(tp);
4058}
4059
4060static void rtl_pll_power_down(struct rtl8169_private *tp)
4061{
Francois Romieud58d46b2011-05-03 16:38:29 +02004062 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00004063}
4064
4065static void rtl_pll_power_up(struct rtl8169_private *tp)
4066{
Francois Romieud58d46b2011-05-03 16:38:29 +02004067 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00004068}
4069
Bill Pembertonbaf63292012-12-03 09:23:28 -05004070static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00004071{
4072 struct pll_power_ops *ops = &tp->pll_power_ops;
4073
4074 switch (tp->mac_version) {
4075 case RTL_GIGA_MAC_VER_07:
4076 case RTL_GIGA_MAC_VER_08:
4077 case RTL_GIGA_MAC_VER_09:
4078 case RTL_GIGA_MAC_VER_10:
4079 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08004080 case RTL_GIGA_MAC_VER_29:
4081 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004082 case RTL_GIGA_MAC_VER_37:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004083 case RTL_GIGA_MAC_VER_39:
françois romieu065c27c2011-01-03 15:08:12 +00004084 ops->down = r810x_pll_power_down;
4085 ops->up = r810x_pll_power_up;
4086 break;
4087
4088 case RTL_GIGA_MAC_VER_11:
4089 case RTL_GIGA_MAC_VER_12:
4090 case RTL_GIGA_MAC_VER_17:
4091 case RTL_GIGA_MAC_VER_18:
4092 case RTL_GIGA_MAC_VER_19:
4093 case RTL_GIGA_MAC_VER_20:
4094 case RTL_GIGA_MAC_VER_21:
4095 case RTL_GIGA_MAC_VER_22:
4096 case RTL_GIGA_MAC_VER_23:
4097 case RTL_GIGA_MAC_VER_24:
4098 case RTL_GIGA_MAC_VER_25:
4099 case RTL_GIGA_MAC_VER_26:
4100 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00004101 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004102 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004103 case RTL_GIGA_MAC_VER_32:
4104 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004105 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08004106 case RTL_GIGA_MAC_VER_35:
4107 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004108 case RTL_GIGA_MAC_VER_38:
Hayes Wangc5583862012-07-02 17:23:22 +08004109 case RTL_GIGA_MAC_VER_40:
4110 case RTL_GIGA_MAC_VER_41:
françois romieu065c27c2011-01-03 15:08:12 +00004111 ops->down = r8168_pll_power_down;
4112 ops->up = r8168_pll_power_up;
4113 break;
4114
4115 default:
4116 ops->down = NULL;
4117 ops->up = NULL;
4118 break;
4119 }
4120}
4121
Hayes Wange542a222011-07-06 15:58:04 +08004122static void rtl_init_rxcfg(struct rtl8169_private *tp)
4123{
4124 void __iomem *ioaddr = tp->mmio_addr;
4125
4126 switch (tp->mac_version) {
4127 case RTL_GIGA_MAC_VER_01:
4128 case RTL_GIGA_MAC_VER_02:
4129 case RTL_GIGA_MAC_VER_03:
4130 case RTL_GIGA_MAC_VER_04:
4131 case RTL_GIGA_MAC_VER_05:
4132 case RTL_GIGA_MAC_VER_06:
4133 case RTL_GIGA_MAC_VER_10:
4134 case RTL_GIGA_MAC_VER_11:
4135 case RTL_GIGA_MAC_VER_12:
4136 case RTL_GIGA_MAC_VER_13:
4137 case RTL_GIGA_MAC_VER_14:
4138 case RTL_GIGA_MAC_VER_15:
4139 case RTL_GIGA_MAC_VER_16:
4140 case RTL_GIGA_MAC_VER_17:
4141 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
4142 break;
4143 case RTL_GIGA_MAC_VER_18:
4144 case RTL_GIGA_MAC_VER_19:
4145 case RTL_GIGA_MAC_VER_20:
4146 case RTL_GIGA_MAC_VER_21:
4147 case RTL_GIGA_MAC_VER_22:
4148 case RTL_GIGA_MAC_VER_23:
4149 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00004150 case RTL_GIGA_MAC_VER_34:
Hayes Wange542a222011-07-06 15:58:04 +08004151 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
4152 break;
4153 default:
4154 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
4155 break;
4156 }
4157}
4158
Hayes Wang92fc43b2011-07-06 15:58:03 +08004159static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4160{
Timo Teräs9fba0812013-01-15 21:01:24 +00004161 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004162}
4163
Francois Romieud58d46b2011-05-03 16:38:29 +02004164static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4165{
françois romieu9c5028e2012-03-02 04:43:14 +00004166 void __iomem *ioaddr = tp->mmio_addr;
4167
4168 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004169 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00004170 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004171}
4172
4173static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4174{
françois romieu9c5028e2012-03-02 04:43:14 +00004175 void __iomem *ioaddr = tp->mmio_addr;
4176
4177 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004178 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00004179 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004180}
4181
4182static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4183{
4184 void __iomem *ioaddr = tp->mmio_addr;
4185
4186 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4187 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
4188 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
4189}
4190
4191static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4192{
4193 void __iomem *ioaddr = tp->mmio_addr;
4194
4195 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4196 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
4197 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
4198}
4199
4200static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4201{
4202 void __iomem *ioaddr = tp->mmio_addr;
4203
4204 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4205}
4206
4207static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4208{
4209 void __iomem *ioaddr = tp->mmio_addr;
4210
4211 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4212}
4213
4214static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4215{
4216 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02004217
4218 RTL_W8(MaxTxPacketSize, 0x3f);
4219 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4220 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01004221 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02004222}
4223
4224static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4225{
4226 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02004227
4228 RTL_W8(MaxTxPacketSize, 0x0c);
4229 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4230 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01004231 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02004232}
4233
4234static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4235{
4236 rtl_tx_performance_tweak(tp->pci_dev,
4237 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4238}
4239
4240static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4241{
4242 rtl_tx_performance_tweak(tp->pci_dev,
4243 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4244}
4245
4246static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4247{
4248 void __iomem *ioaddr = tp->mmio_addr;
4249
4250 r8168b_0_hw_jumbo_enable(tp);
4251
4252 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
4253}
4254
4255static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4256{
4257 void __iomem *ioaddr = tp->mmio_addr;
4258
4259 r8168b_0_hw_jumbo_disable(tp);
4260
4261 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4262}
4263
Bill Pembertonbaf63292012-12-03 09:23:28 -05004264static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02004265{
4266 struct jumbo_ops *ops = &tp->jumbo_ops;
4267
4268 switch (tp->mac_version) {
4269 case RTL_GIGA_MAC_VER_11:
4270 ops->disable = r8168b_0_hw_jumbo_disable;
4271 ops->enable = r8168b_0_hw_jumbo_enable;
4272 break;
4273 case RTL_GIGA_MAC_VER_12:
4274 case RTL_GIGA_MAC_VER_17:
4275 ops->disable = r8168b_1_hw_jumbo_disable;
4276 ops->enable = r8168b_1_hw_jumbo_enable;
4277 break;
4278 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4279 case RTL_GIGA_MAC_VER_19:
4280 case RTL_GIGA_MAC_VER_20:
4281 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4282 case RTL_GIGA_MAC_VER_22:
4283 case RTL_GIGA_MAC_VER_23:
4284 case RTL_GIGA_MAC_VER_24:
4285 case RTL_GIGA_MAC_VER_25:
4286 case RTL_GIGA_MAC_VER_26:
4287 ops->disable = r8168c_hw_jumbo_disable;
4288 ops->enable = r8168c_hw_jumbo_enable;
4289 break;
4290 case RTL_GIGA_MAC_VER_27:
4291 case RTL_GIGA_MAC_VER_28:
4292 ops->disable = r8168dp_hw_jumbo_disable;
4293 ops->enable = r8168dp_hw_jumbo_enable;
4294 break;
4295 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4296 case RTL_GIGA_MAC_VER_32:
4297 case RTL_GIGA_MAC_VER_33:
4298 case RTL_GIGA_MAC_VER_34:
4299 ops->disable = r8168e_hw_jumbo_disable;
4300 ops->enable = r8168e_hw_jumbo_enable;
4301 break;
4302
4303 /*
4304 * No action needed for jumbo frames with 8169.
4305 * No jumbo for 810x at all.
4306 */
Hayes Wangc5583862012-07-02 17:23:22 +08004307 case RTL_GIGA_MAC_VER_40:
4308 case RTL_GIGA_MAC_VER_41:
Francois Romieud58d46b2011-05-03 16:38:29 +02004309 default:
4310 ops->disable = NULL;
4311 ops->enable = NULL;
4312 break;
4313 }
4314}
4315
Francois Romieuffc46952012-07-06 14:19:23 +02004316DECLARE_RTL_COND(rtl_chipcmd_cond)
4317{
4318 void __iomem *ioaddr = tp->mmio_addr;
4319
4320 return RTL_R8(ChipCmd) & CmdReset;
4321}
4322
Francois Romieu6f43adc2011-04-29 15:05:51 +02004323static void rtl_hw_reset(struct rtl8169_private *tp)
4324{
4325 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu6f43adc2011-04-29 15:05:51 +02004326
Francois Romieu6f43adc2011-04-29 15:05:51 +02004327 RTL_W8(ChipCmd, CmdReset);
4328
Francois Romieuffc46952012-07-06 14:19:23 +02004329 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004330}
4331
Francois Romieub6ffd972011-06-17 17:00:05 +02004332static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4333{
4334 struct rtl_fw *rtl_fw;
4335 const char *name;
4336 int rc = -ENOMEM;
4337
4338 name = rtl_lookup_firmware_name(tp);
4339 if (!name)
4340 goto out_no_firmware;
4341
4342 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4343 if (!rtl_fw)
4344 goto err_warn;
4345
4346 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4347 if (rc < 0)
4348 goto err_free;
4349
Francois Romieufd112f22011-06-18 00:10:29 +02004350 rc = rtl_check_firmware(tp, rtl_fw);
4351 if (rc < 0)
4352 goto err_release_firmware;
4353
Francois Romieub6ffd972011-06-17 17:00:05 +02004354 tp->rtl_fw = rtl_fw;
4355out:
4356 return;
4357
Francois Romieufd112f22011-06-18 00:10:29 +02004358err_release_firmware:
4359 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004360err_free:
4361 kfree(rtl_fw);
4362err_warn:
4363 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4364 name, rc);
4365out_no_firmware:
4366 tp->rtl_fw = NULL;
4367 goto out;
4368}
4369
François Romieu953a12c2011-04-24 17:38:48 +02004370static void rtl_request_firmware(struct rtl8169_private *tp)
4371{
Francois Romieub6ffd972011-06-17 17:00:05 +02004372 if (IS_ERR(tp->rtl_fw))
4373 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004374}
4375
Hayes Wang92fc43b2011-07-06 15:58:03 +08004376static void rtl_rx_close(struct rtl8169_private *tp)
4377{
4378 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004379
Francois Romieu1687b562011-07-19 17:21:29 +02004380 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004381}
4382
Francois Romieuffc46952012-07-06 14:19:23 +02004383DECLARE_RTL_COND(rtl_npq_cond)
4384{
4385 void __iomem *ioaddr = tp->mmio_addr;
4386
4387 return RTL_R8(TxPoll) & NPQ;
4388}
4389
4390DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4391{
4392 void __iomem *ioaddr = tp->mmio_addr;
4393
4394 return RTL_R32(TxConfig) & TXCFG_EMPTY;
4395}
4396
françois romieue6de30d2011-01-03 15:08:37 +00004397static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398{
françois romieue6de30d2011-01-03 15:08:37 +00004399 void __iomem *ioaddr = tp->mmio_addr;
4400
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004402 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403
Hayes Wang92fc43b2011-07-06 15:58:03 +08004404 rtl_rx_close(tp);
4405
Hayes Wang5d2e1952011-02-22 17:26:22 +08004406 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004407 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4408 tp->mac_version == RTL_GIGA_MAC_VER_31) {
Francois Romieuffc46952012-07-06 14:19:23 +02004409 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Hayes Wangc2218922011-09-06 16:55:18 +08004410 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4411 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
Hayes Wang7e18dca2012-03-30 14:33:02 +08004412 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004413 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
Hayes Wangc5583862012-07-02 17:23:22 +08004414 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
4415 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004416 tp->mac_version == RTL_GIGA_MAC_VER_38) {
David S. Miller8decf862011-09-22 03:23:13 -04004417 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02004418 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004419 } else {
4420 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4421 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004422 }
4423
Hayes Wang92fc43b2011-07-06 15:58:03 +08004424 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425}
4426
Francois Romieu7f796d832007-06-11 23:04:41 +02004427static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004428{
4429 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004430
4431 /* Set DMA burst size and Interframe Gap Time */
4432 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4433 (InterFrameGap << TxInterFrameGapShift));
4434}
4435
Francois Romieu07ce4062007-02-23 23:36:39 +01004436static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437{
4438 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439
Francois Romieu07ce4062007-02-23 23:36:39 +01004440 tp->hw_start(dev);
4441
Francois Romieuda78dbf2012-01-26 14:18:23 +01004442 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004443}
4444
Francois Romieu7f796d832007-06-11 23:04:41 +02004445static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4446 void __iomem *ioaddr)
4447{
4448 /*
4449 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4450 * register to be written before TxDescAddrLow to work.
4451 * Switching from MMIO to I/O access fixes the issue as well.
4452 */
4453 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004454 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004455 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004456 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004457}
4458
4459static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4460{
4461 u16 cmd;
4462
4463 cmd = RTL_R16(CPlusCmd);
4464 RTL_W16(CPlusCmd, cmd);
4465 return cmd;
4466}
4467
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004468static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004469{
4470 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00004471 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004472}
4473
Francois Romieu6dccd162007-02-13 23:38:05 +01004474static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4475{
Francois Romieu37441002011-06-17 22:58:54 +02004476 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004477 u32 mac_version;
4478 u32 clk;
4479 u32 val;
4480 } cfg2_info [] = {
4481 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4482 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4483 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4484 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004485 };
4486 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004487 unsigned int i;
4488 u32 clk;
4489
4490 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004491 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004492 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4493 RTL_W32(0x7c, p->val);
4494 break;
4495 }
4496 }
4497}
4498
Francois Romieue6b763e2012-03-08 09:35:39 +01004499static void rtl_set_rx_mode(struct net_device *dev)
4500{
4501 struct rtl8169_private *tp = netdev_priv(dev);
4502 void __iomem *ioaddr = tp->mmio_addr;
4503 u32 mc_filter[2]; /* Multicast hash filter */
4504 int rx_mode;
4505 u32 tmp = 0;
4506
4507 if (dev->flags & IFF_PROMISC) {
4508 /* Unconditionally log net taps. */
4509 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4510 rx_mode =
4511 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4512 AcceptAllPhys;
4513 mc_filter[1] = mc_filter[0] = 0xffffffff;
4514 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4515 (dev->flags & IFF_ALLMULTI)) {
4516 /* Too many to filter perfectly -- accept all multicasts. */
4517 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4518 mc_filter[1] = mc_filter[0] = 0xffffffff;
4519 } else {
4520 struct netdev_hw_addr *ha;
4521
4522 rx_mode = AcceptBroadcast | AcceptMyPhys;
4523 mc_filter[1] = mc_filter[0] = 0;
4524 netdev_for_each_mc_addr(ha, dev) {
4525 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4526 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4527 rx_mode |= AcceptMulticast;
4528 }
4529 }
4530
4531 if (dev->features & NETIF_F_RXALL)
4532 rx_mode |= (AcceptErr | AcceptRunt);
4533
4534 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4535
4536 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4537 u32 data = mc_filter[0];
4538
4539 mc_filter[0] = swab32(mc_filter[1]);
4540 mc_filter[1] = swab32(data);
4541 }
4542
Nathan Walp04817762012-11-01 12:08:47 +00004543 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
4544 mc_filter[1] = mc_filter[0] = 0xffffffff;
4545
Francois Romieue6b763e2012-03-08 09:35:39 +01004546 RTL_W32(MAR0 + 4, mc_filter[1]);
4547 RTL_W32(MAR0 + 0, mc_filter[0]);
4548
4549 RTL_W32(RxConfig, tmp);
4550}
4551
Francois Romieu07ce4062007-02-23 23:36:39 +01004552static void rtl_hw_start_8169(struct net_device *dev)
4553{
4554 struct rtl8169_private *tp = netdev_priv(dev);
4555 void __iomem *ioaddr = tp->mmio_addr;
4556 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004557
Francois Romieu9cb427b2006-11-02 00:10:16 +01004558 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4559 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4560 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4561 }
4562
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004564 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4565 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4566 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4567 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004568 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4569
Hayes Wange542a222011-07-06 15:58:04 +08004570 rtl_init_rxcfg(tp);
4571
françois romieuf0298f82011-01-03 15:07:42 +00004572 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004574 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575
Francois Romieucecb5fd2011-04-01 10:21:07 +02004576 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4577 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4578 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4579 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004580 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
Francois Romieu7f796d832007-06-11 23:04:41 +02004582 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004583
Francois Romieucecb5fd2011-04-01 10:21:07 +02004584 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4585 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004586 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004588 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 }
4590
Francois Romieubcf0bf92006-07-26 23:14:13 +02004591 RTL_W16(CPlusCmd, tp->cp_cmd);
4592
Francois Romieu6dccd162007-02-13 23:38:05 +01004593 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4594
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 /*
4596 * Undocumented corner. Supposedly:
4597 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4598 */
4599 RTL_W16(IntrMitigate, 0x0000);
4600
Francois Romieu7f796d832007-06-11 23:04:41 +02004601 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004602
Francois Romieucecb5fd2011-04-01 10:21:07 +02004603 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4604 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4605 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4606 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004607 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4608 rtl_set_rx_tx_config_registers(tp);
4609 }
4610
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004612
4613 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4614 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615
4616 RTL_W32(RxMissed, 0);
4617
Francois Romieu07ce4062007-02-23 23:36:39 +01004618 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619
4620 /* no early-rx interrupts */
4621 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004622}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004624static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
4625{
4626 if (tp->csi_ops.write)
Francois Romieu52989f02012-07-06 13:37:00 +02004627 tp->csi_ops.write(tp, addr, value);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004628}
4629
4630static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
4631{
Francois Romieu52989f02012-07-06 13:37:00 +02004632 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004633}
4634
4635static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004636{
4637 u32 csi;
4638
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004639 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4640 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00004641}
4642
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004643static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004644{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004645 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00004646}
4647
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004648static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00004649{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004650 rtl_csi_access_enable(tp, 0x27000000);
4651}
4652
Francois Romieuffc46952012-07-06 14:19:23 +02004653DECLARE_RTL_COND(rtl_csiar_cond)
4654{
4655 void __iomem *ioaddr = tp->mmio_addr;
4656
4657 return RTL_R32(CSIAR) & CSIAR_FLAG;
4658}
4659
Francois Romieu52989f02012-07-06 13:37:00 +02004660static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004661{
Francois Romieu52989f02012-07-06 13:37:00 +02004662 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004663
4664 RTL_W32(CSIDR, value);
4665 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4666 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4667
Francois Romieuffc46952012-07-06 14:19:23 +02004668 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004669}
4670
Francois Romieu52989f02012-07-06 13:37:00 +02004671static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004672{
Francois Romieu52989f02012-07-06 13:37:00 +02004673 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004674
4675 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
4676 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4677
Francois Romieuffc46952012-07-06 14:19:23 +02004678 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4679 RTL_R32(CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004680}
4681
Francois Romieu52989f02012-07-06 13:37:00 +02004682static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wang7e18dca2012-03-30 14:33:02 +08004683{
Francois Romieu52989f02012-07-06 13:37:00 +02004684 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004685
4686 RTL_W32(CSIDR, value);
4687 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4688 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
4689 CSIAR_FUNC_NIC);
4690
Francois Romieuffc46952012-07-06 14:19:23 +02004691 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004692}
4693
Francois Romieu52989f02012-07-06 13:37:00 +02004694static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wang7e18dca2012-03-30 14:33:02 +08004695{
Francois Romieu52989f02012-07-06 13:37:00 +02004696 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004697
4698 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
4699 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4700
Francois Romieuffc46952012-07-06 14:19:23 +02004701 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4702 RTL_R32(CSIDR) : ~0;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004703}
4704
Bill Pembertonbaf63292012-12-03 09:23:28 -05004705static void rtl_init_csi_ops(struct rtl8169_private *tp)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004706{
4707 struct csi_ops *ops = &tp->csi_ops;
4708
4709 switch (tp->mac_version) {
4710 case RTL_GIGA_MAC_VER_01:
4711 case RTL_GIGA_MAC_VER_02:
4712 case RTL_GIGA_MAC_VER_03:
4713 case RTL_GIGA_MAC_VER_04:
4714 case RTL_GIGA_MAC_VER_05:
4715 case RTL_GIGA_MAC_VER_06:
4716 case RTL_GIGA_MAC_VER_10:
4717 case RTL_GIGA_MAC_VER_11:
4718 case RTL_GIGA_MAC_VER_12:
4719 case RTL_GIGA_MAC_VER_13:
4720 case RTL_GIGA_MAC_VER_14:
4721 case RTL_GIGA_MAC_VER_15:
4722 case RTL_GIGA_MAC_VER_16:
4723 case RTL_GIGA_MAC_VER_17:
4724 ops->write = NULL;
4725 ops->read = NULL;
4726 break;
4727
Hayes Wang7e18dca2012-03-30 14:33:02 +08004728 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004729 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004730 ops->write = r8402_csi_write;
4731 ops->read = r8402_csi_read;
4732 break;
4733
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004734 default:
4735 ops->write = r8169_csi_write;
4736 ops->read = r8169_csi_read;
4737 break;
4738 }
Francois Romieudacf8152008-08-02 20:44:13 +02004739}
4740
4741struct ephy_info {
4742 unsigned int offset;
4743 u16 mask;
4744 u16 bits;
4745};
4746
Francois Romieufdf6fc02012-07-06 22:40:38 +02004747static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
4748 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004749{
4750 u16 w;
4751
4752 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02004753 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4754 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02004755 e++;
4756 }
4757}
4758
Francois Romieub726e492008-06-28 12:22:59 +02004759static void rtl_disable_clock_request(struct pci_dev *pdev)
4760{
Jiang Liu7d7903b2012-07-24 17:20:16 +08004761 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
4762 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02004763}
4764
françois romieue6de30d2011-01-03 15:08:37 +00004765static void rtl_enable_clock_request(struct pci_dev *pdev)
4766{
Jiang Liu7d7903b2012-07-24 17:20:16 +08004767 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
4768 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00004769}
4770
Francois Romieub726e492008-06-28 12:22:59 +02004771#define R8168_CPCMD_QUIRK_MASK (\
4772 EnableBist | \
4773 Mac_dbgo_oe | \
4774 Force_half_dup | \
4775 Force_rxflow_en | \
4776 Force_txflow_en | \
4777 Cxpl_dbg_sel | \
4778 ASF | \
4779 PktCntrDisable | \
4780 Mac_dbgo_sel)
4781
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004782static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004783{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004784 void __iomem *ioaddr = tp->mmio_addr;
4785 struct pci_dev *pdev = tp->pci_dev;
4786
Francois Romieub726e492008-06-28 12:22:59 +02004787 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4788
4789 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4790
françois romieufaf1e782013-02-27 13:01:57 +00004791 if (tp->dev->mtu <= ETH_DATA_LEN) {
4792 rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
4793 PCI_EXP_DEVCTL_NOSNOOP_EN);
4794 }
Francois Romieu219a1e92008-06-28 11:58:39 +02004795}
4796
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004797static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004798{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004799 void __iomem *ioaddr = tp->mmio_addr;
4800
4801 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004802
françois romieuf0298f82011-01-03 15:07:42 +00004803 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004804
4805 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004806}
4807
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004808static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004809{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004810 void __iomem *ioaddr = tp->mmio_addr;
4811 struct pci_dev *pdev = tp->pci_dev;
4812
Francois Romieub726e492008-06-28 12:22:59 +02004813 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4814
4815 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4816
françois romieufaf1e782013-02-27 13:01:57 +00004817 if (tp->dev->mtu <= ETH_DATA_LEN)
4818 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004819
4820 rtl_disable_clock_request(pdev);
4821
4822 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004823}
4824
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004825static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004826{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004827 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004828 { 0x01, 0, 0x0001 },
4829 { 0x02, 0x0800, 0x1000 },
4830 { 0x03, 0, 0x0042 },
4831 { 0x06, 0x0080, 0x0000 },
4832 { 0x07, 0, 0x2000 }
4833 };
4834
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004835 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004836
Francois Romieufdf6fc02012-07-06 22:40:38 +02004837 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02004838
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004839 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004840}
4841
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004842static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02004843{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004844 void __iomem *ioaddr = tp->mmio_addr;
4845 struct pci_dev *pdev = tp->pci_dev;
4846
4847 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004848
4849 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4850
françois romieufaf1e782013-02-27 13:01:57 +00004851 if (tp->dev->mtu <= ETH_DATA_LEN)
4852 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieuef3386f2008-06-29 12:24:30 +02004853
4854 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4855}
4856
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004857static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004858{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004859 void __iomem *ioaddr = tp->mmio_addr;
4860 struct pci_dev *pdev = tp->pci_dev;
4861
4862 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004863
4864 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4865
4866 /* Magic. */
4867 RTL_W8(DBG_REG, 0x20);
4868
françois romieuf0298f82011-01-03 15:07:42 +00004869 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004870
françois romieufaf1e782013-02-27 13:01:57 +00004871 if (tp->dev->mtu <= ETH_DATA_LEN)
4872 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004873
4874 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4875}
4876
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004877static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004878{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004879 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004880 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004881 { 0x02, 0x0800, 0x1000 },
4882 { 0x03, 0, 0x0002 },
4883 { 0x06, 0x0080, 0x0000 }
4884 };
4885
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004886 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004887
4888 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4889
Francois Romieufdf6fc02012-07-06 22:40:38 +02004890 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02004891
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004892 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004893}
4894
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004895static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004896{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004897 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004898 { 0x01, 0, 0x0001 },
4899 { 0x03, 0x0400, 0x0220 }
4900 };
4901
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004902 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004903
Francois Romieufdf6fc02012-07-06 22:40:38 +02004904 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02004905
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004906 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004907}
4908
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004909static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02004910{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004911 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004912}
4913
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004914static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02004915{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004916 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004917
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004918 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004919}
4920
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004921static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02004922{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004923 void __iomem *ioaddr = tp->mmio_addr;
4924 struct pci_dev *pdev = tp->pci_dev;
4925
4926 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004927
4928 rtl_disable_clock_request(pdev);
4929
françois romieuf0298f82011-01-03 15:07:42 +00004930 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004931
françois romieufaf1e782013-02-27 13:01:57 +00004932 if (tp->dev->mtu <= ETH_DATA_LEN)
4933 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu5b538df2008-07-20 16:22:45 +02004934
4935 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4936}
4937
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004938static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00004939{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004940 void __iomem *ioaddr = tp->mmio_addr;
4941 struct pci_dev *pdev = tp->pci_dev;
4942
4943 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004944
françois romieufaf1e782013-02-27 13:01:57 +00004945 if (tp->dev->mtu <= ETH_DATA_LEN)
4946 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang4804b3b2011-03-21 01:50:29 +00004947
4948 RTL_W8(MaxTxPacketSize, TxPacketMax);
4949
4950 rtl_disable_clock_request(pdev);
4951}
4952
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004953static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004954{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004955 void __iomem *ioaddr = tp->mmio_addr;
4956 struct pci_dev *pdev = tp->pci_dev;
françois romieue6de30d2011-01-03 15:08:37 +00004957 static const struct ephy_info e_info_8168d_4[] = {
4958 { 0x0b, ~0, 0x48 },
4959 { 0x19, 0x20, 0x50 },
4960 { 0x0c, ~0, 0x20 }
4961 };
4962 int i;
4963
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004964 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004965
4966 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4967
4968 RTL_W8(MaxTxPacketSize, TxPacketMax);
4969
4970 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4971 const struct ephy_info *e = e_info_8168d_4 + i;
4972 u16 w;
4973
Francois Romieufdf6fc02012-07-06 22:40:38 +02004974 w = rtl_ephy_read(tp, e->offset);
4975 rtl_ephy_write(tp, 0x03, (w & e->mask) | e->bits);
françois romieue6de30d2011-01-03 15:08:37 +00004976 }
4977
4978 rtl_enable_clock_request(pdev);
4979}
4980
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004981static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00004982{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004983 void __iomem *ioaddr = tp->mmio_addr;
4984 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08004985 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004986 { 0x00, 0x0200, 0x0100 },
4987 { 0x00, 0x0000, 0x0004 },
4988 { 0x06, 0x0002, 0x0001 },
4989 { 0x06, 0x0000, 0x0030 },
4990 { 0x07, 0x0000, 0x2000 },
4991 { 0x00, 0x0000, 0x0020 },
4992 { 0x03, 0x5800, 0x2000 },
4993 { 0x03, 0x0000, 0x0001 },
4994 { 0x01, 0x0800, 0x1000 },
4995 { 0x07, 0x0000, 0x4000 },
4996 { 0x1e, 0x0000, 0x2000 },
4997 { 0x19, 0xffff, 0xfe6c },
4998 { 0x0a, 0x0000, 0x0040 }
4999 };
5000
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005001 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005002
Francois Romieufdf6fc02012-07-06 22:40:38 +02005003 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00005004
françois romieufaf1e782013-02-27 13:01:57 +00005005 if (tp->dev->mtu <= ETH_DATA_LEN)
5006 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang01dc7fe2011-03-21 01:50:28 +00005007
5008 RTL_W8(MaxTxPacketSize, TxPacketMax);
5009
5010 rtl_disable_clock_request(pdev);
5011
5012 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02005013 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
5014 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00005015
Francois Romieucecb5fd2011-04-01 10:21:07 +02005016 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00005017}
5018
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005019static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08005020{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005021 void __iomem *ioaddr = tp->mmio_addr;
5022 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08005023 static const struct ephy_info e_info_8168e_2[] = {
5024 { 0x09, 0x0000, 0x0080 },
5025 { 0x19, 0x0000, 0x0224 }
5026 };
5027
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005028 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005029
Francois Romieufdf6fc02012-07-06 22:40:38 +02005030 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08005031
françois romieufaf1e782013-02-27 13:01:57 +00005032 if (tp->dev->mtu <= ETH_DATA_LEN)
5033 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Hayes Wang70090422011-07-06 15:58:06 +08005034
Francois Romieufdf6fc02012-07-06 22:40:38 +02005035 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5036 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5037 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5038 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5039 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5040 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
5041 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5042 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08005043
Hayes Wang3090bd92011-09-06 16:55:15 +08005044 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005045
Francois Romieu4521e1a92012-11-01 16:46:28 +00005046 rtl_disable_clock_request(pdev);
5047
Hayes Wang70090422011-07-06 15:58:06 +08005048 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5049 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5050
5051 /* Adjust EEE LED frequency */
5052 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5053
5054 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5055 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005056 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wang70090422011-07-06 15:58:06 +08005057}
5058
Hayes Wang5f886e02012-03-30 14:33:03 +08005059static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005060{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005061 void __iomem *ioaddr = tp->mmio_addr;
5062 struct pci_dev *pdev = tp->pci_dev;
Hayes Wangc2218922011-09-06 16:55:18 +08005063
Hayes Wang5f886e02012-03-30 14:33:03 +08005064 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005065
5066 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5067
Francois Romieufdf6fc02012-07-06 22:40:38 +02005068 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5069 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5070 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5071 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5072 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5073 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5074 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5075 rtl_w1w0_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5076 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5077 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08005078
5079 RTL_W8(MaxTxPacketSize, EarlySize);
5080
Francois Romieu4521e1a92012-11-01 16:46:28 +00005081 rtl_disable_clock_request(pdev);
5082
Hayes Wangc2218922011-09-06 16:55:18 +08005083 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5084 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
Hayes Wangc2218922011-09-06 16:55:18 +08005085 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005086 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
5087 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08005088}
5089
Hayes Wang5f886e02012-03-30 14:33:03 +08005090static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5091{
5092 void __iomem *ioaddr = tp->mmio_addr;
5093 static const struct ephy_info e_info_8168f_1[] = {
5094 { 0x06, 0x00c0, 0x0020 },
5095 { 0x08, 0x0001, 0x0002 },
5096 { 0x09, 0x0000, 0x0080 },
5097 { 0x19, 0x0000, 0x0224 }
5098 };
5099
5100 rtl_hw_start_8168f(tp);
5101
Francois Romieufdf6fc02012-07-06 22:40:38 +02005102 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08005103
Francois Romieufdf6fc02012-07-06 22:40:38 +02005104 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08005105
5106 /* Adjust EEE LED frequency */
5107 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5108}
5109
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005110static void rtl_hw_start_8411(struct rtl8169_private *tp)
5111{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005112 static const struct ephy_info e_info_8168f_1[] = {
5113 { 0x06, 0x00c0, 0x0020 },
5114 { 0x0f, 0xffff, 0x5200 },
5115 { 0x1e, 0x0000, 0x4000 },
5116 { 0x19, 0x0000, 0x0224 }
5117 };
5118
5119 rtl_hw_start_8168f(tp);
5120
Francois Romieufdf6fc02012-07-06 22:40:38 +02005121 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005122
Francois Romieufdf6fc02012-07-06 22:40:38 +02005123 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005124}
5125
Hayes Wangc5583862012-07-02 17:23:22 +08005126static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5127{
5128 void __iomem *ioaddr = tp->mmio_addr;
5129 struct pci_dev *pdev = tp->pci_dev;
5130
5131 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5132 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5133 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5134 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5135
5136 rtl_csi_access_enable_1(tp);
5137
5138 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5139
5140 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5141 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5142
5143 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005144 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08005145 RTL_W8(MaxTxPacketSize, EarlySize);
5146
5147 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5148 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5149
5150 /* Adjust EEE LED frequency */
5151 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5152
5153 rtl_w1w0_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x02, ERIAR_EXGMAC);
5154}
5155
Francois Romieu07ce4062007-02-23 23:36:39 +01005156static void rtl_hw_start_8168(struct net_device *dev)
5157{
Francois Romieu2dd99532007-06-11 23:22:52 +02005158 struct rtl8169_private *tp = netdev_priv(dev);
5159 void __iomem *ioaddr = tp->mmio_addr;
5160
5161 RTL_W8(Cfg9346, Cfg9346_Unlock);
5162
françois romieuf0298f82011-01-03 15:07:42 +00005163 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02005164
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005165 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02005166
Francois Romieu0e485152007-02-20 00:00:26 +01005167 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02005168
5169 RTL_W16(CPlusCmd, tp->cp_cmd);
5170
Francois Romieu0e485152007-02-20 00:00:26 +01005171 RTL_W16(IntrMitigate, 0x5151);
5172
5173 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00005174 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005175 tp->event_slow |= RxFIFOOver | PCSTimeout;
5176 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01005177 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005178
5179 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5180
Francois Romieub8363902008-06-01 12:31:57 +02005181 rtl_set_rx_mode(dev);
5182
5183 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
5184 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02005185
5186 RTL_R8(IntrMask);
5187
Francois Romieu219a1e92008-06-28 11:58:39 +02005188 switch (tp->mac_version) {
5189 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005190 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005191 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005192
5193 case RTL_GIGA_MAC_VER_12:
5194 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005195 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005196 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005197
5198 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005199 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005200 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005201
5202 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005203 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005204 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005205
5206 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005207 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005208 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005209
Francois Romieu197ff762008-06-28 13:16:02 +02005210 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005211 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005212 break;
Francois Romieu197ff762008-06-28 13:16:02 +02005213
Francois Romieu6fb07052008-06-29 11:54:28 +02005214 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005215 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005216 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02005217
Francois Romieuef3386f2008-06-29 12:24:30 +02005218 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005219 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005220 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02005221
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005222 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005223 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005224 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005225
Francois Romieu5b538df2008-07-20 16:22:45 +02005226 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00005227 case RTL_GIGA_MAC_VER_26:
5228 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005229 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005230 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02005231
françois romieue6de30d2011-01-03 15:08:37 +00005232 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005233 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005234 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02005235
hayeswang4804b3b2011-03-21 01:50:29 +00005236 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005237 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005238 break;
5239
hayeswang01dc7fe2011-03-21 01:50:28 +00005240 case RTL_GIGA_MAC_VER_32:
5241 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005242 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005243 break;
5244 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005245 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005246 break;
françois romieue6de30d2011-01-03 15:08:37 +00005247
Hayes Wangc2218922011-09-06 16:55:18 +08005248 case RTL_GIGA_MAC_VER_35:
5249 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005250 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005251 break;
5252
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005253 case RTL_GIGA_MAC_VER_38:
5254 rtl_hw_start_8411(tp);
5255 break;
5256
Hayes Wangc5583862012-07-02 17:23:22 +08005257 case RTL_GIGA_MAC_VER_40:
5258 case RTL_GIGA_MAC_VER_41:
5259 rtl_hw_start_8168g_1(tp);
5260 break;
5261
Francois Romieu219a1e92008-06-28 11:58:39 +02005262 default:
5263 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
5264 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00005265 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005266 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005267
Francois Romieu0e485152007-02-20 00:00:26 +01005268 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5269
Francois Romieub8363902008-06-01 12:31:57 +02005270 RTL_W8(Cfg9346, Cfg9346_Lock);
5271
Francois Romieu2dd99532007-06-11 23:22:52 +02005272 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005273}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274
Francois Romieu2857ffb2008-08-02 21:08:49 +02005275#define R810X_CPCMD_QUIRK_MASK (\
5276 EnableBist | \
5277 Mac_dbgo_oe | \
5278 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00005279 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02005280 Force_txflow_en | \
5281 Cxpl_dbg_sel | \
5282 ASF | \
5283 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005284 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005285
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005286static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005287{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005288 void __iomem *ioaddr = tp->mmio_addr;
5289 struct pci_dev *pdev = tp->pci_dev;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005290 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02005291 { 0x01, 0, 0x6e65 },
5292 { 0x02, 0, 0x091f },
5293 { 0x03, 0, 0xc2f9 },
5294 { 0x06, 0, 0xafb5 },
5295 { 0x07, 0, 0x0e00 },
5296 { 0x19, 0, 0xec80 },
5297 { 0x01, 0, 0x2e65 },
5298 { 0x01, 0, 0x6e65 }
5299 };
5300 u8 cfg1;
5301
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005302 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005303
5304 RTL_W8(DBG_REG, FIX_NAK_1);
5305
5306 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5307
5308 RTL_W8(Config1,
5309 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5310 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5311
5312 cfg1 = RTL_R8(Config1);
5313 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5314 RTL_W8(Config1, cfg1 & ~LEDS0);
5315
Francois Romieufdf6fc02012-07-06 22:40:38 +02005316 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02005317}
5318
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005319static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005320{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005321 void __iomem *ioaddr = tp->mmio_addr;
5322 struct pci_dev *pdev = tp->pci_dev;
5323
5324 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005325
5326 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5327
5328 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5329 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005330}
5331
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005332static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005333{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005334 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005335
Francois Romieufdf6fc02012-07-06 22:40:38 +02005336 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005337}
5338
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005339static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005340{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005341 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005342 static const struct ephy_info e_info_8105e_1[] = {
5343 { 0x07, 0, 0x4000 },
5344 { 0x19, 0, 0x0200 },
5345 { 0x19, 0, 0x0020 },
5346 { 0x1e, 0, 0x2000 },
5347 { 0x03, 0, 0x0001 },
5348 { 0x19, 0, 0x0100 },
5349 { 0x19, 0, 0x0004 },
5350 { 0x0a, 0, 0x0020 }
5351 };
5352
Francois Romieucecb5fd2011-04-01 10:21:07 +02005353 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005354 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5355
Francois Romieucecb5fd2011-04-01 10:21:07 +02005356 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005357 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5358
5359 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e2011-07-06 15:58:02 +08005360 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005361
Francois Romieufdf6fc02012-07-06 22:40:38 +02005362 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
Hayes Wang5a5e4442011-02-22 17:26:21 +08005363}
5364
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005365static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005366{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005367 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005368 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005369}
5370
Hayes Wang7e18dca2012-03-30 14:33:02 +08005371static void rtl_hw_start_8402(struct rtl8169_private *tp)
5372{
5373 void __iomem *ioaddr = tp->mmio_addr;
5374 static const struct ephy_info e_info_8402[] = {
5375 { 0x19, 0xffff, 0xff64 },
5376 { 0x1e, 0, 0x4000 }
5377 };
5378
5379 rtl_csi_access_enable_2(tp);
5380
5381 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5382 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5383
5384 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5385 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5386
Francois Romieufdf6fc02012-07-06 22:40:38 +02005387 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08005388
5389 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5390
Francois Romieufdf6fc02012-07-06 22:40:38 +02005391 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5392 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
5393 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5394 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5395 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5396 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5397 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005398}
5399
Hayes Wang5598bfe2012-07-02 17:23:21 +08005400static void rtl_hw_start_8106(struct rtl8169_private *tp)
5401{
5402 void __iomem *ioaddr = tp->mmio_addr;
5403
5404 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5405 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5406
Francois Romieu4521e1a92012-11-01 16:46:28 +00005407 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005408 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
5409 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
5410}
5411
Francois Romieu07ce4062007-02-23 23:36:39 +01005412static void rtl_hw_start_8101(struct net_device *dev)
5413{
Francois Romieucdf1a602007-06-11 23:29:50 +02005414 struct rtl8169_private *tp = netdev_priv(dev);
5415 void __iomem *ioaddr = tp->mmio_addr;
5416 struct pci_dev *pdev = tp->pci_dev;
5417
Francois Romieuda78dbf2012-01-26 14:18:23 +01005418 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5419 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00005420
Francois Romieucecb5fd2011-04-01 10:21:07 +02005421 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08005422 tp->mac_version == RTL_GIGA_MAC_VER_16)
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06005423 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
5424 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02005425
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005426 RTL_W8(Cfg9346, Cfg9346_Unlock);
5427
Francois Romieu2857ffb2008-08-02 21:08:49 +02005428 switch (tp->mac_version) {
5429 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005430 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005431 break;
5432
5433 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005434 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005435 break;
5436
5437 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005438 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005439 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005440
5441 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005442 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005443 break;
5444 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005445 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005446 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005447
5448 case RTL_GIGA_MAC_VER_37:
5449 rtl_hw_start_8402(tp);
5450 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08005451
5452 case RTL_GIGA_MAC_VER_39:
5453 rtl_hw_start_8106(tp);
5454 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02005455 }
5456
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005457 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02005458
françois romieuf0298f82011-01-03 15:07:42 +00005459 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02005460
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005461 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02005462
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005463 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02005464 RTL_W16(CPlusCmd, tp->cp_cmd);
5465
5466 RTL_W16(IntrMitigate, 0x0000);
5467
5468 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5469
5470 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5471 rtl_set_rx_tx_config_registers(tp);
5472
Francois Romieucdf1a602007-06-11 23:29:50 +02005473 RTL_R8(IntrMask);
5474
Francois Romieucdf1a602007-06-11 23:29:50 +02005475 rtl_set_rx_mode(dev);
5476
5477 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478}
5479
5480static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5481{
Francois Romieud58d46b2011-05-03 16:38:29 +02005482 struct rtl8169_private *tp = netdev_priv(dev);
5483
5484 if (new_mtu < ETH_ZLEN ||
5485 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 return -EINVAL;
5487
Francois Romieud58d46b2011-05-03 16:38:29 +02005488 if (new_mtu > ETH_DATA_LEN)
5489 rtl_hw_jumbo_enable(tp);
5490 else
5491 rtl_hw_jumbo_disable(tp);
5492
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005494 netdev_update_features(dev);
5495
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497}
5498
5499static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5500{
Al Viro95e09182007-12-22 18:55:39 +00005501 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5503}
5504
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005505static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5506 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005508 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005509 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005510
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005511 kfree(*data_buff);
5512 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 rtl8169_make_unusable_by_asic(desc);
5514}
5515
5516static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5517{
5518 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5519
5520 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5521}
5522
5523static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5524 u32 rx_buf_sz)
5525{
5526 desc->addr = cpu_to_le64(mapping);
5527 wmb();
5528 rtl8169_mark_to_asic(desc, rx_buf_sz);
5529}
5530
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005531static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005533 return (void *)ALIGN((long)data, 16);
5534}
5535
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005536static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5537 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005538{
5539 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005541 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005542 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005543 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005545 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5546 if (!data)
5547 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005548
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005549 if (rtl8169_align(data) != data) {
5550 kfree(data);
5551 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5552 if (!data)
5553 return NULL;
5554 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005555
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005556 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005557 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005558 if (unlikely(dma_mapping_error(d, mapping))) {
5559 if (net_ratelimit())
5560 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005561 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563
5564 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005565 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005566
5567err_out:
5568 kfree(data);
5569 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570}
5571
5572static void rtl8169_rx_clear(struct rtl8169_private *tp)
5573{
Francois Romieu07d3f512007-02-21 22:40:46 +01005574 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575
5576 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005577 if (tp->Rx_databuff[i]) {
5578 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 tp->RxDescArray + i);
5580 }
5581 }
5582}
5583
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005584static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005586 desc->opts1 |= cpu_to_le32(RingEnd);
5587}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005588
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005589static int rtl8169_rx_fill(struct rtl8169_private *tp)
5590{
5591 unsigned int i;
5592
5593 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005594 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005595
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005596 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005598
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005599 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005600 if (!data) {
5601 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005602 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005603 }
5604 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005607 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5608 return 0;
5609
5610err_out:
5611 rtl8169_rx_clear(tp);
5612 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613}
5614
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615static int rtl8169_init_ring(struct net_device *dev)
5616{
5617 struct rtl8169_private *tp = netdev_priv(dev);
5618
5619 rtl8169_init_ring_indexes(tp);
5620
5621 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005622 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005624 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625}
5626
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005627static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 struct TxDesc *desc)
5629{
5630 unsigned int len = tx_skb->len;
5631
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005632 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5633
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 desc->opts1 = 0x00;
5635 desc->opts2 = 0x00;
5636 desc->addr = 0x00;
5637 tx_skb->len = 0;
5638}
5639
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005640static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5641 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642{
5643 unsigned int i;
5644
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005645 for (i = 0; i < n; i++) {
5646 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 struct ring_info *tx_skb = tp->tx_skb + entry;
5648 unsigned int len = tx_skb->len;
5649
5650 if (len) {
5651 struct sk_buff *skb = tx_skb->skb;
5652
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005653 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 tp->TxDescArray + entry);
5655 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005656 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 dev_kfree_skb(skb);
5658 tx_skb->skb = NULL;
5659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 }
5661 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005662}
5663
5664static void rtl8169_tx_clear(struct rtl8169_private *tp)
5665{
5666 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 tp->cur_tx = tp->dirty_tx = 0;
5668}
5669
Francois Romieu4422bcd2012-01-26 11:23:32 +01005670static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671{
David Howellsc4028952006-11-22 14:57:56 +00005672 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005673 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674
Francois Romieuda78dbf2012-01-26 14:18:23 +01005675 napi_disable(&tp->napi);
5676 netif_stop_queue(dev);
5677 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678
françois romieuc7c2c392011-12-04 20:30:52 +00005679 rtl8169_hw_reset(tp);
5680
Francois Romieu56de4142011-03-15 17:29:31 +01005681 for (i = 0; i < NUM_RX_DESC; i++)
5682 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5683
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005685 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686
Francois Romieuda78dbf2012-01-26 14:18:23 +01005687 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01005688 rtl_hw_start(dev);
5689 netif_wake_queue(dev);
5690 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691}
5692
5693static void rtl8169_tx_timeout(struct net_device *dev)
5694{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005695 struct rtl8169_private *tp = netdev_priv(dev);
5696
5697 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698}
5699
5700static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005701 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702{
5703 struct skb_shared_info *info = skb_shinfo(skb);
5704 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005705 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005706 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707
5708 entry = tp->cur_tx;
5709 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005710 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 dma_addr_t mapping;
5712 u32 status, len;
5713 void *addr;
5714
5715 entry = (entry + 1) % NUM_TX_DESC;
5716
5717 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005718 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005719 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005720 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005721 if (unlikely(dma_mapping_error(d, mapping))) {
5722 if (net_ratelimit())
5723 netif_err(tp, drv, tp->dev,
5724 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005725 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727
Francois Romieucecb5fd2011-04-01 10:21:07 +02005728 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005729 status = opts[0] | len |
5730 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731
5732 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005733 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 txd->addr = cpu_to_le64(mapping);
5735
5736 tp->tx_skb[entry].len = len;
5737 }
5738
5739 if (cur_frag) {
5740 tp->tx_skb[entry].skb = skb;
5741 txd->opts1 |= cpu_to_le32(LastFrag);
5742 }
5743
5744 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005745
5746err_out:
5747 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5748 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749}
5750
Francois Romieu2b7b4312011-04-18 22:53:24 -07005751static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5752 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005754 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005755 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005756 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757
Francois Romieu2b7b4312011-04-18 22:53:24 -07005758 if (mss) {
5759 opts[0] |= TD_LSO;
5760 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5761 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005762 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763
5764 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005765 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005767 opts[offset] |= info->checksum.udp;
5768 else
5769 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771}
5772
Stephen Hemminger613573252009-08-31 19:50:58 +00005773static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5774 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775{
5776 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005777 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 struct TxDesc *txd = tp->TxDescArray + entry;
5779 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005780 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 dma_addr_t mapping;
5782 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005783 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005784 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005785
Julien Ducourthial477206a2012-05-09 00:00:06 +02005786 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005787 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005788 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 }
5790
5791 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005792 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005794 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005795 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005796 if (unlikely(dma_mapping_error(d, mapping))) {
5797 if (net_ratelimit())
5798 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005799 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801
5802 tp->tx_skb[entry].len = len;
5803 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804
Kirill Smelkov810f4892012-11-10 21:11:02 +04005805 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
Francois Romieu2b7b4312011-04-18 22:53:24 -07005806 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005807
Francois Romieu2b7b4312011-04-18 22:53:24 -07005808 rtl8169_tso_csum(tp, skb, opts);
5809
5810 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005811 if (frags < 0)
5812 goto err_dma_1;
5813 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005814 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005815 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005816 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005817 tp->tx_skb[entry].skb = skb;
5818 }
5819
Francois Romieu2b7b4312011-04-18 22:53:24 -07005820 txd->opts2 = cpu_to_le32(opts[1]);
5821
Richard Cochran5047fb52012-03-10 07:29:42 +00005822 skb_tx_timestamp(skb);
5823
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 wmb();
5825
Francois Romieucecb5fd2011-04-01 10:21:07 +02005826 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005827 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 txd->opts1 = cpu_to_le32(status);
5829
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 tp->cur_tx += frags + 1;
5831
David Dillow4c020a92010-03-03 16:33:10 +00005832 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833
Francois Romieucecb5fd2011-04-01 10:21:07 +02005834 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835
Francois Romieuda78dbf2012-01-26 14:18:23 +01005836 mmiowb();
5837
Julien Ducourthial477206a2012-05-09 00:00:06 +02005838 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01005839 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5840 * not miss a ring update when it notices a stopped queue.
5841 */
5842 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01005844 /* Sync with rtl_tx:
5845 * - publish queue status and cur_tx ring index (write barrier)
5846 * - refresh dirty_tx ring index (read barrier).
5847 * May the current thread have a pessimistic view of the ring
5848 * status and forget to wake up queue, a racing rtl_tx thread
5849 * can't.
5850 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005851 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02005852 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 netif_wake_queue(dev);
5854 }
5855
Stephen Hemminger613573252009-08-31 19:50:58 +00005856 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005858err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005859 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005860err_dma_0:
5861 dev_kfree_skb(skb);
5862 dev->stats.tx_dropped++;
5863 return NETDEV_TX_OK;
5864
5865err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005867 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005868 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869}
5870
5871static void rtl8169_pcierr_interrupt(struct net_device *dev)
5872{
5873 struct rtl8169_private *tp = netdev_priv(dev);
5874 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 u16 pci_status, pci_cmd;
5876
5877 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5878 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5879
Joe Perchesbf82c182010-02-09 11:49:50 +00005880 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5881 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882
5883 /*
5884 * The recovery sequence below admits a very elaborated explanation:
5885 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005886 * - I did not see what else could be done;
5887 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 *
5889 * Feel free to adjust to your needs.
5890 */
Francois Romieua27993f2006-12-18 00:04:19 +01005891 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005892 pci_cmd &= ~PCI_COMMAND_PARITY;
5893 else
5894 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5895
5896 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897
5898 pci_write_config_word(pdev, PCI_STATUS,
5899 pci_status & (PCI_STATUS_DETECTED_PARITY |
5900 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5901 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5902
5903 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00005904 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005905 void __iomem *ioaddr = tp->mmio_addr;
5906
Joe Perchesbf82c182010-02-09 11:49:50 +00005907 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 tp->cp_cmd &= ~PCIDAC;
5909 RTL_W16(CPlusCmd, tp->cp_cmd);
5910 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 }
5912
françois romieue6de30d2011-01-03 15:08:37 +00005913 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005914
Francois Romieu98ddf982012-01-31 10:47:34 +01005915 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916}
5917
Francois Romieuda78dbf2012-01-26 14:18:23 +01005918static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919{
5920 unsigned int dirty_tx, tx_left;
5921
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 dirty_tx = tp->dirty_tx;
5923 smp_rmb();
5924 tx_left = tp->cur_tx - dirty_tx;
5925
5926 while (tx_left > 0) {
5927 unsigned int entry = dirty_tx % NUM_TX_DESC;
5928 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 u32 status;
5930
5931 rmb();
5932 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5933 if (status & DescOwn)
5934 break;
5935
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005936 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5937 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 if (status & LastFrag) {
Francois Romieu17bcb682012-07-23 22:55:55 +02005939 u64_stats_update_begin(&tp->tx_stats.syncp);
5940 tp->tx_stats.packets++;
5941 tp->tx_stats.bytes += tx_skb->skb->len;
5942 u64_stats_update_end(&tp->tx_stats.syncp);
5943 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 tx_skb->skb = NULL;
5945 }
5946 dirty_tx++;
5947 tx_left--;
5948 }
5949
5950 if (tp->dirty_tx != dirty_tx) {
5951 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005952 /* Sync with rtl8169_start_xmit:
5953 * - publish dirty_tx ring index (write barrier)
5954 * - refresh cur_tx ring index and queue status (read barrier)
5955 * May the current thread miss the stopped queue condition,
5956 * a racing xmit thread can only have a right view of the
5957 * ring status.
5958 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005959 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02005961 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 netif_wake_queue(dev);
5963 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005964 /*
5965 * 8168 hack: TxPoll requests are lost when the Tx packets are
5966 * too close. Let's kick an extra TxPoll request when a burst
5967 * of start_xmit activity is detected (if it is not detected,
5968 * it is slow enough). -- FR
5969 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005970 if (tp->cur_tx != dirty_tx) {
5971 void __iomem *ioaddr = tp->mmio_addr;
5972
Francois Romieud78ae2d2007-08-26 20:08:19 +02005973 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 }
5976}
5977
Francois Romieu126fa4b2005-05-12 20:09:17 -04005978static inline int rtl8169_fragmented_frame(u32 status)
5979{
5980 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5981}
5982
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005983static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985 u32 status = opts1 & RxProtoMask;
5986
5987 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005988 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 skb->ip_summed = CHECKSUM_UNNECESSARY;
5990 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005991 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992}
5993
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005994static struct sk_buff *rtl8169_try_rx_copy(void *data,
5995 struct rtl8169_private *tp,
5996 int pkt_size,
5997 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005999 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006000 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006002 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006003 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006004 prefetch(data);
6005 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
6006 if (skb)
6007 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006008 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6009
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006010 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011}
6012
Francois Romieuda78dbf2012-01-26 14:18:23 +01006013static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014{
6015 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006016 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019
Timo Teräs9fba0812013-01-15 21:01:24 +00006020 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006022 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 u32 status;
6024
6025 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04006026 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027
6028 if (status & DescOwn)
6029 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02006030 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006031 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6032 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006033 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02006035 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02006037 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02006038 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006039 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006040 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02006041 }
Ben Greear6bbe0212012-02-10 15:04:33 +00006042 if ((status & (RxRUNT | RxCRC)) &&
6043 !(status & (RxRWT | RxFOVF)) &&
6044 (dev->features & NETIF_F_RXALL))
6045 goto process_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006047 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00006048 dma_addr_t addr;
6049 int pkt_size;
6050
6051process_pkt:
6052 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00006053 if (likely(!(dev->features & NETIF_F_RXFCS)))
6054 pkt_size = (status & 0x00003fff) - 4;
6055 else
6056 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057
Francois Romieu126fa4b2005-05-12 20:09:17 -04006058 /*
6059 * The driver does not support incoming fragmented
6060 * frames. They are seen as a symptom of over-mtu
6061 * sized frames.
6062 */
6063 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02006064 dev->stats.rx_dropped++;
6065 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00006066 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006067 }
6068
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006069 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6070 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006071 if (!skb) {
6072 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00006073 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 }
6075
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006076 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077 skb_put(skb, pkt_size);
6078 skb->protocol = eth_type_trans(skb, dev);
6079
Francois Romieu7a8fc772011-03-01 17:18:33 +01006080 rtl8169_rx_vlan_tag(desc, skb);
6081
Francois Romieu56de4142011-03-15 17:29:31 +01006082 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083
Junchang Wang8027aa22012-03-04 23:30:32 +01006084 u64_stats_update_begin(&tp->rx_stats.syncp);
6085 tp->rx_stats.packets++;
6086 tp->rx_stats.bytes += pkt_size;
6087 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 }
françois romieuce11ff52013-01-24 13:30:06 +00006089release_descriptor:
6090 desc->opts2 = 0;
6091 wmb();
6092 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 }
6094
6095 count = cur_rx - tp->cur_rx;
6096 tp->cur_rx = cur_rx;
6097
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 return count;
6099}
6100
Francois Romieu07d3f512007-02-21 22:40:46 +01006101static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102{
Francois Romieu07d3f512007-02-21 22:40:46 +01006103 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01006106 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107
Francois Romieu9085cdfa2012-01-26 12:59:08 +01006108 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006109 if (status && status != 0xffff) {
6110 status &= RTL_EVENT_NAPI | tp->event_slow;
6111 if (status) {
6112 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00006113
Francois Romieuda78dbf2012-01-26 14:18:23 +01006114 rtl_irq_disable(tp);
6115 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 return IRQ_RETVAL(handled);
6119}
6120
Francois Romieuda78dbf2012-01-26 14:18:23 +01006121/*
6122 * Workqueue context.
6123 */
6124static void rtl_slow_event_work(struct rtl8169_private *tp)
6125{
6126 struct net_device *dev = tp->dev;
6127 u16 status;
6128
6129 status = rtl_get_events(tp) & tp->event_slow;
6130 rtl_ack_events(tp, status);
6131
6132 if (unlikely(status & RxFIFOOver)) {
6133 switch (tp->mac_version) {
6134 /* Work around for rx fifo overflow */
6135 case RTL_GIGA_MAC_VER_11:
6136 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01006137 /* XXX - Hack alert. See rtl_task(). */
6138 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006139 default:
6140 break;
6141 }
6142 }
6143
6144 if (unlikely(status & SYSErr))
6145 rtl8169_pcierr_interrupt(dev);
6146
6147 if (status & LinkChg)
6148 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
6149
françois romieu7dbb4912012-06-09 10:53:16 +00006150 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006151}
6152
Francois Romieu4422bcd2012-01-26 11:23:32 +01006153static void rtl_task(struct work_struct *work)
6154{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006155 static const struct {
6156 int bitnr;
6157 void (*action)(struct rtl8169_private *);
6158 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01006159 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006160 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
6161 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
6162 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
6163 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01006164 struct rtl8169_private *tp =
6165 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006166 struct net_device *dev = tp->dev;
6167 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01006168
Francois Romieuda78dbf2012-01-26 14:18:23 +01006169 rtl_lock_work(tp);
6170
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006171 if (!netif_running(dev) ||
6172 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01006173 goto out_unlock;
6174
6175 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
6176 bool pending;
6177
Francois Romieuda78dbf2012-01-26 14:18:23 +01006178 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006179 if (pending)
6180 rtl_work[i].action(tp);
6181 }
6182
6183out_unlock:
6184 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01006185}
6186
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006187static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006189 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
6190 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006191 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
6192 int work_done= 0;
6193 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194
Francois Romieuda78dbf2012-01-26 14:18:23 +01006195 status = rtl_get_events(tp);
6196 rtl_ack_events(tp, status & ~tp->event_slow);
6197
6198 if (status & RTL_EVENT_NAPI_RX)
6199 work_done = rtl_rx(dev, tp, (u32) budget);
6200
6201 if (status & RTL_EVENT_NAPI_TX)
6202 rtl_tx(dev, tp);
6203
6204 if (status & tp->event_slow) {
6205 enable_mask &= ~tp->event_slow;
6206
6207 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
6208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006210 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006211 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00006212
Francois Romieuda78dbf2012-01-26 14:18:23 +01006213 rtl_irq_enable(tp, enable_mask);
6214 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215 }
6216
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006217 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219
Francois Romieu523a6092008-09-10 22:28:56 +02006220static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
6221{
6222 struct rtl8169_private *tp = netdev_priv(dev);
6223
6224 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6225 return;
6226
6227 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
6228 RTL_W32(RxMissed, 0);
6229}
6230
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231static void rtl8169_down(struct net_device *dev)
6232{
6233 struct rtl8169_private *tp = netdev_priv(dev);
6234 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235
Francois Romieu4876cc12011-03-11 21:07:11 +01006236 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01006238 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006239 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240
Hayes Wang92fc43b2011-07-06 15:58:03 +08006241 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006242 /*
6243 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01006244 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6245 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006246 */
Francois Romieu523a6092008-09-10 22:28:56 +02006247 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006250 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 rtl8169_tx_clear(tp);
6253
6254 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00006255
6256 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257}
6258
6259static int rtl8169_close(struct net_device *dev)
6260{
6261 struct rtl8169_private *tp = netdev_priv(dev);
6262 struct pci_dev *pdev = tp->pci_dev;
6263
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006264 pm_runtime_get_sync(&pdev->dev);
6265
Francois Romieucecb5fd2011-04-01 10:21:07 +02006266 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08006267 rtl8169_update_counters(dev);
6268
Francois Romieuda78dbf2012-01-26 14:18:23 +01006269 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006270 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006271
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006273 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006275 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00006277 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6278 tp->RxPhyAddr);
6279 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6280 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 tp->TxDescArray = NULL;
6282 tp->RxDescArray = NULL;
6283
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006284 pm_runtime_put_sync(&pdev->dev);
6285
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286 return 0;
6287}
6288
Francois Romieudc1c00c2012-03-08 10:06:18 +01006289#ifdef CONFIG_NET_POLL_CONTROLLER
6290static void rtl8169_netpoll(struct net_device *dev)
6291{
6292 struct rtl8169_private *tp = netdev_priv(dev);
6293
6294 rtl8169_interrupt(tp->pci_dev->irq, dev);
6295}
6296#endif
6297
Francois Romieudf43ac72012-03-08 09:48:40 +01006298static int rtl_open(struct net_device *dev)
6299{
6300 struct rtl8169_private *tp = netdev_priv(dev);
6301 void __iomem *ioaddr = tp->mmio_addr;
6302 struct pci_dev *pdev = tp->pci_dev;
6303 int retval = -ENOMEM;
6304
6305 pm_runtime_get_sync(&pdev->dev);
6306
6307 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02006308 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01006309 * dma_alloc_coherent provides more.
6310 */
6311 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6312 &tp->TxPhyAddr, GFP_KERNEL);
6313 if (!tp->TxDescArray)
6314 goto err_pm_runtime_put;
6315
6316 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6317 &tp->RxPhyAddr, GFP_KERNEL);
6318 if (!tp->RxDescArray)
6319 goto err_free_tx_0;
6320
6321 retval = rtl8169_init_ring(dev);
6322 if (retval < 0)
6323 goto err_free_rx_1;
6324
6325 INIT_WORK(&tp->wk.work, rtl_task);
6326
6327 smp_mb();
6328
6329 rtl_request_firmware(tp);
6330
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006331 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01006332 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
6333 dev->name, dev);
6334 if (retval < 0)
6335 goto err_release_fw_2;
6336
6337 rtl_lock_work(tp);
6338
6339 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6340
6341 napi_enable(&tp->napi);
6342
6343 rtl8169_init_phy(dev, tp);
6344
6345 __rtl8169_set_features(dev, dev->features);
6346
6347 rtl_pll_power_up(tp);
6348
6349 rtl_hw_start(dev);
6350
6351 netif_start_queue(dev);
6352
6353 rtl_unlock_work(tp);
6354
6355 tp->saved_wolopts = 0;
6356 pm_runtime_put_noidle(&pdev->dev);
6357
6358 rtl8169_check_link_status(dev, tp, ioaddr);
6359out:
6360 return retval;
6361
6362err_release_fw_2:
6363 rtl_release_firmware(tp);
6364 rtl8169_rx_clear(tp);
6365err_free_rx_1:
6366 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6367 tp->RxPhyAddr);
6368 tp->RxDescArray = NULL;
6369err_free_tx_0:
6370 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6371 tp->TxPhyAddr);
6372 tp->TxDescArray = NULL;
6373err_pm_runtime_put:
6374 pm_runtime_put_noidle(&pdev->dev);
6375 goto out;
6376}
6377
Junchang Wang8027aa22012-03-04 23:30:32 +01006378static struct rtnl_link_stats64 *
6379rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380{
6381 struct rtl8169_private *tp = netdev_priv(dev);
6382 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01006383 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384
Francois Romieuda78dbf2012-01-26 14:18:23 +01006385 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02006386 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02006387
Junchang Wang8027aa22012-03-04 23:30:32 +01006388 do {
6389 start = u64_stats_fetch_begin_bh(&tp->rx_stats.syncp);
6390 stats->rx_packets = tp->rx_stats.packets;
6391 stats->rx_bytes = tp->rx_stats.bytes;
6392 } while (u64_stats_fetch_retry_bh(&tp->rx_stats.syncp, start));
6393
6394
6395 do {
6396 start = u64_stats_fetch_begin_bh(&tp->tx_stats.syncp);
6397 stats->tx_packets = tp->tx_stats.packets;
6398 stats->tx_bytes = tp->tx_stats.bytes;
6399 } while (u64_stats_fetch_retry_bh(&tp->tx_stats.syncp, start));
6400
6401 stats->rx_dropped = dev->stats.rx_dropped;
6402 stats->tx_dropped = dev->stats.tx_dropped;
6403 stats->rx_length_errors = dev->stats.rx_length_errors;
6404 stats->rx_errors = dev->stats.rx_errors;
6405 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6406 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6407 stats->rx_missed_errors = dev->stats.rx_missed_errors;
6408
6409 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410}
6411
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006412static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006413{
françois romieu065c27c2011-01-03 15:08:12 +00006414 struct rtl8169_private *tp = netdev_priv(dev);
6415
Francois Romieu5d06a992006-02-23 00:47:58 +01006416 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006417 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006418
6419 netif_device_detach(dev);
6420 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006421
6422 rtl_lock_work(tp);
6423 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006424 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006425 rtl_unlock_work(tp);
6426
6427 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006428}
Francois Romieu5d06a992006-02-23 00:47:58 +01006429
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006430#ifdef CONFIG_PM
6431
6432static int rtl8169_suspend(struct device *device)
6433{
6434 struct pci_dev *pdev = to_pci_dev(device);
6435 struct net_device *dev = pci_get_drvdata(pdev);
6436
6437 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02006438
Francois Romieu5d06a992006-02-23 00:47:58 +01006439 return 0;
6440}
6441
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006442static void __rtl8169_resume(struct net_device *dev)
6443{
françois romieu065c27c2011-01-03 15:08:12 +00006444 struct rtl8169_private *tp = netdev_priv(dev);
6445
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006446 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006447
6448 rtl_pll_power_up(tp);
6449
Artem Savkovcff4c162012-04-03 10:29:11 +00006450 rtl_lock_work(tp);
6451 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006452 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00006453 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006454
Francois Romieu98ddf982012-01-31 10:47:34 +01006455 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006456}
6457
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006458static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006459{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006460 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01006461 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006462 struct rtl8169_private *tp = netdev_priv(dev);
6463
6464 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01006465
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006466 if (netif_running(dev))
6467 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006468
Francois Romieu5d06a992006-02-23 00:47:58 +01006469 return 0;
6470}
6471
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006472static int rtl8169_runtime_suspend(struct device *device)
6473{
6474 struct pci_dev *pdev = to_pci_dev(device);
6475 struct net_device *dev = pci_get_drvdata(pdev);
6476 struct rtl8169_private *tp = netdev_priv(dev);
6477
6478 if (!tp->TxDescArray)
6479 return 0;
6480
Francois Romieuda78dbf2012-01-26 14:18:23 +01006481 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006482 tp->saved_wolopts = __rtl8169_get_wol(tp);
6483 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006484 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006485
6486 rtl8169_net_suspend(dev);
6487
6488 return 0;
6489}
6490
6491static int rtl8169_runtime_resume(struct device *device)
6492{
6493 struct pci_dev *pdev = to_pci_dev(device);
6494 struct net_device *dev = pci_get_drvdata(pdev);
6495 struct rtl8169_private *tp = netdev_priv(dev);
6496
6497 if (!tp->TxDescArray)
6498 return 0;
6499
Francois Romieuda78dbf2012-01-26 14:18:23 +01006500 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006501 __rtl8169_set_wol(tp, tp->saved_wolopts);
6502 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006503 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006504
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006505 rtl8169_init_phy(dev, tp);
6506
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006507 __rtl8169_resume(dev);
6508
6509 return 0;
6510}
6511
6512static int rtl8169_runtime_idle(struct device *device)
6513{
6514 struct pci_dev *pdev = to_pci_dev(device);
6515 struct net_device *dev = pci_get_drvdata(pdev);
6516 struct rtl8169_private *tp = netdev_priv(dev);
6517
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00006518 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006519}
6520
Alexey Dobriyan47145212009-12-14 18:00:08 -08006521static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006522 .suspend = rtl8169_suspend,
6523 .resume = rtl8169_resume,
6524 .freeze = rtl8169_suspend,
6525 .thaw = rtl8169_resume,
6526 .poweroff = rtl8169_suspend,
6527 .restore = rtl8169_resume,
6528 .runtime_suspend = rtl8169_runtime_suspend,
6529 .runtime_resume = rtl8169_runtime_resume,
6530 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006531};
6532
6533#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6534
6535#else /* !CONFIG_PM */
6536
6537#define RTL8169_PM_OPS NULL
6538
6539#endif /* !CONFIG_PM */
6540
David S. Miller1805b2f2011-10-24 18:18:09 -04006541static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6542{
6543 void __iomem *ioaddr = tp->mmio_addr;
6544
6545 /* WoL fails with 8168b when the receiver is disabled. */
6546 switch (tp->mac_version) {
6547 case RTL_GIGA_MAC_VER_11:
6548 case RTL_GIGA_MAC_VER_12:
6549 case RTL_GIGA_MAC_VER_17:
6550 pci_clear_master(tp->pci_dev);
6551
6552 RTL_W8(ChipCmd, CmdRxEnb);
6553 /* PCI commit */
6554 RTL_R8(ChipCmd);
6555 break;
6556 default:
6557 break;
6558 }
6559}
6560
Francois Romieu1765f952008-09-13 17:21:40 +02006561static void rtl_shutdown(struct pci_dev *pdev)
6562{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006563 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006564 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00006565 struct device *d = &pdev->dev;
6566
6567 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02006568
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006569 rtl8169_net_suspend(dev);
6570
Francois Romieucecb5fd2011-04-01 10:21:07 +02006571 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006572 rtl_rar_set(tp, dev->perm_addr);
6573
Hayes Wang92fc43b2011-07-06 15:58:03 +08006574 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006575
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006576 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006577 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6578 rtl_wol_suspend_quirk(tp);
6579 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006580 }
6581
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006582 pci_wake_from_d3(pdev, true);
6583 pci_set_power_state(pdev, PCI_D3hot);
6584 }
françois romieu2a15cd22012-03-06 01:14:12 +00006585
6586 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006587}
Francois Romieu5d06a992006-02-23 00:47:58 +01006588
Bill Pembertonbaf63292012-12-03 09:23:28 -05006589static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01006590{
6591 struct net_device *dev = pci_get_drvdata(pdev);
6592 struct rtl8169_private *tp = netdev_priv(dev);
6593
6594 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6595 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6596 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6597 rtl8168_driver_stop(tp);
6598 }
6599
6600 cancel_work_sync(&tp->wk.work);
6601
Devendra Nagaad1be8d2012-05-31 01:51:20 +00006602 netif_napi_del(&tp->napi);
6603
Francois Romieue27566e2012-03-08 09:54:01 +01006604 unregister_netdev(dev);
6605
6606 rtl_release_firmware(tp);
6607
6608 if (pci_dev_run_wake(pdev))
6609 pm_runtime_get_noresume(&pdev->dev);
6610
6611 /* restore original MAC address */
6612 rtl_rar_set(tp, dev->perm_addr);
6613
6614 rtl_disable_msi(pdev, tp);
6615 rtl8169_release_board(pdev, dev, tp->mmio_addr);
6616 pci_set_drvdata(pdev, NULL);
6617}
6618
Francois Romieufa9c3852012-03-08 10:01:50 +01006619static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01006620 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01006621 .ndo_stop = rtl8169_close,
6622 .ndo_get_stats64 = rtl8169_get_stats64,
6623 .ndo_start_xmit = rtl8169_start_xmit,
6624 .ndo_tx_timeout = rtl8169_tx_timeout,
6625 .ndo_validate_addr = eth_validate_addr,
6626 .ndo_change_mtu = rtl8169_change_mtu,
6627 .ndo_fix_features = rtl8169_fix_features,
6628 .ndo_set_features = rtl8169_set_features,
6629 .ndo_set_mac_address = rtl_set_mac_address,
6630 .ndo_do_ioctl = rtl8169_ioctl,
6631 .ndo_set_rx_mode = rtl_set_rx_mode,
6632#ifdef CONFIG_NET_POLL_CONTROLLER
6633 .ndo_poll_controller = rtl8169_netpoll,
6634#endif
6635
6636};
6637
Francois Romieu31fa8b12012-03-08 10:09:40 +01006638static const struct rtl_cfg_info {
6639 void (*hw_start)(struct net_device *);
6640 unsigned int region;
6641 unsigned int align;
6642 u16 event_slow;
6643 unsigned features;
6644 u8 default_ver;
6645} rtl_cfg_infos [] = {
6646 [RTL_CFG_0] = {
6647 .hw_start = rtl_hw_start_8169,
6648 .region = 1,
6649 .align = 0,
6650 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
6651 .features = RTL_FEATURE_GMII,
6652 .default_ver = RTL_GIGA_MAC_VER_01,
6653 },
6654 [RTL_CFG_1] = {
6655 .hw_start = rtl_hw_start_8168,
6656 .region = 2,
6657 .align = 8,
6658 .event_slow = SYSErr | LinkChg | RxOverflow,
6659 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
6660 .default_ver = RTL_GIGA_MAC_VER_11,
6661 },
6662 [RTL_CFG_2] = {
6663 .hw_start = rtl_hw_start_8101,
6664 .region = 2,
6665 .align = 8,
6666 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
6667 PCSTimeout,
6668 .features = RTL_FEATURE_MSI,
6669 .default_ver = RTL_GIGA_MAC_VER_13,
6670 }
6671};
6672
6673/* Cfg9346_Unlock assumed. */
6674static unsigned rtl_try_msi(struct rtl8169_private *tp,
6675 const struct rtl_cfg_info *cfg)
6676{
6677 void __iomem *ioaddr = tp->mmio_addr;
6678 unsigned msi = 0;
6679 u8 cfg2;
6680
6681 cfg2 = RTL_R8(Config2) & ~MSIEnable;
6682 if (cfg->features & RTL_FEATURE_MSI) {
6683 if (pci_enable_msi(tp->pci_dev)) {
6684 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
6685 } else {
6686 cfg2 |= MSIEnable;
6687 msi = RTL_FEATURE_MSI;
6688 }
6689 }
6690 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6691 RTL_W8(Config2, cfg2);
6692 return msi;
6693}
6694
Hayes Wangc5583862012-07-02 17:23:22 +08006695DECLARE_RTL_COND(rtl_link_list_ready_cond)
6696{
6697 void __iomem *ioaddr = tp->mmio_addr;
6698
6699 return RTL_R8(MCU) & LINK_LIST_RDY;
6700}
6701
6702DECLARE_RTL_COND(rtl_rxtx_empty_cond)
6703{
6704 void __iomem *ioaddr = tp->mmio_addr;
6705
6706 return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
6707}
6708
Bill Pembertonbaf63292012-12-03 09:23:28 -05006709static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006710{
6711 void __iomem *ioaddr = tp->mmio_addr;
6712 u32 data;
6713
6714 tp->ocp_base = OCP_STD_PHY_BASE;
6715
6716 RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
6717
6718 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
6719 return;
6720
6721 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
6722 return;
6723
6724 RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
6725 msleep(1);
6726 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6727
Hayes Wang5f8bcce2012-07-10 08:47:05 +02006728 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08006729 data &= ~(1 << 14);
6730 r8168_mac_ocp_write(tp, 0xe8de, data);
6731
6732 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
6733 return;
6734
Hayes Wang5f8bcce2012-07-10 08:47:05 +02006735 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08006736 data |= (1 << 15);
6737 r8168_mac_ocp_write(tp, 0xe8de, data);
6738
6739 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
6740 return;
6741}
6742
Bill Pembertonbaf63292012-12-03 09:23:28 -05006743static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006744{
6745 switch (tp->mac_version) {
6746 case RTL_GIGA_MAC_VER_40:
6747 case RTL_GIGA_MAC_VER_41:
6748 rtl_hw_init_8168g(tp);
6749 break;
6750
6751 default:
6752 break;
6753 }
6754}
6755
Bill Pembertonbaf63292012-12-03 09:23:28 -05006756static int
Francois Romieu3b6cf252012-03-08 09:59:04 +01006757rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6758{
6759 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
6760 const unsigned int region = cfg->region;
6761 struct rtl8169_private *tp;
6762 struct mii_if_info *mii;
6763 struct net_device *dev;
6764 void __iomem *ioaddr;
6765 int chipset, i;
6766 int rc;
6767
6768 if (netif_msg_drv(&debug)) {
6769 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
6770 MODULENAME, RTL8169_VERSION);
6771 }
6772
6773 dev = alloc_etherdev(sizeof (*tp));
6774 if (!dev) {
6775 rc = -ENOMEM;
6776 goto out;
6777 }
6778
6779 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01006780 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006781 tp = netdev_priv(dev);
6782 tp->dev = dev;
6783 tp->pci_dev = pdev;
6784 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
6785
6786 mii = &tp->mii;
6787 mii->dev = dev;
6788 mii->mdio_read = rtl_mdio_read;
6789 mii->mdio_write = rtl_mdio_write;
6790 mii->phy_id_mask = 0x1f;
6791 mii->reg_num_mask = 0x1f;
6792 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
6793
6794 /* disable ASPM completely as that cause random device stop working
6795 * problems as well as full system hangs for some PCIe devices users */
6796 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6797 PCIE_LINK_STATE_CLKPM);
6798
6799 /* enable device (incl. PCI PM wakeup and hotplug setup) */
6800 rc = pci_enable_device(pdev);
6801 if (rc < 0) {
6802 netif_err(tp, probe, dev, "enable failure\n");
6803 goto err_out_free_dev_1;
6804 }
6805
6806 if (pci_set_mwi(pdev) < 0)
6807 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
6808
6809 /* make sure PCI base addr 1 is MMIO */
6810 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
6811 netif_err(tp, probe, dev,
6812 "region #%d not an MMIO resource, aborting\n",
6813 region);
6814 rc = -ENODEV;
6815 goto err_out_mwi_2;
6816 }
6817
6818 /* check for weird/broken PCI region reporting */
6819 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
6820 netif_err(tp, probe, dev,
6821 "Invalid PCI region size(s), aborting\n");
6822 rc = -ENODEV;
6823 goto err_out_mwi_2;
6824 }
6825
6826 rc = pci_request_regions(pdev, MODULENAME);
6827 if (rc < 0) {
6828 netif_err(tp, probe, dev, "could not request regions\n");
6829 goto err_out_mwi_2;
6830 }
6831
6832 tp->cp_cmd = RxChkSum;
6833
6834 if ((sizeof(dma_addr_t) > 4) &&
6835 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
6836 tp->cp_cmd |= PCIDAC;
6837 dev->features |= NETIF_F_HIGHDMA;
6838 } else {
6839 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6840 if (rc < 0) {
6841 netif_err(tp, probe, dev, "DMA configuration failed\n");
6842 goto err_out_free_res_3;
6843 }
6844 }
6845
6846 /* ioremap MMIO region */
6847 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
6848 if (!ioaddr) {
6849 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
6850 rc = -EIO;
6851 goto err_out_free_res_3;
6852 }
6853 tp->mmio_addr = ioaddr;
6854
6855 if (!pci_is_pcie(pdev))
6856 netif_info(tp, probe, dev, "not PCI Express\n");
6857
6858 /* Identify chip attached to board */
6859 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
6860
6861 rtl_init_rxcfg(tp);
6862
6863 rtl_irq_disable(tp);
6864
Hayes Wangc5583862012-07-02 17:23:22 +08006865 rtl_hw_initialize(tp);
6866
Francois Romieu3b6cf252012-03-08 09:59:04 +01006867 rtl_hw_reset(tp);
6868
6869 rtl_ack_events(tp, 0xffff);
6870
6871 pci_set_master(pdev);
6872
6873 /*
6874 * Pretend we are using VLANs; This bypasses a nasty bug where
6875 * Interrupts stop flowing on high load on 8110SCd controllers.
6876 */
6877 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6878 tp->cp_cmd |= RxVlan;
6879
6880 rtl_init_mdio_ops(tp);
6881 rtl_init_pll_power_ops(tp);
6882 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006883 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006884
6885 rtl8169_print_mac_version(tp);
6886
6887 chipset = tp->mac_version;
6888 tp->txd_version = rtl_chip_infos[chipset].txd_version;
6889
6890 RTL_W8(Cfg9346, Cfg9346_Unlock);
6891 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
6892 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
6893 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
6894 tp->features |= RTL_FEATURE_WOL;
6895 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
6896 tp->features |= RTL_FEATURE_WOL;
6897 tp->features |= rtl_try_msi(tp, cfg);
6898 RTL_W8(Cfg9346, Cfg9346_Lock);
6899
6900 if (rtl_tbi_enabled(tp)) {
6901 tp->set_speed = rtl8169_set_speed_tbi;
6902 tp->get_settings = rtl8169_gset_tbi;
6903 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
6904 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
6905 tp->link_ok = rtl8169_tbi_link_ok;
6906 tp->do_ioctl = rtl_tbi_ioctl;
6907 } else {
6908 tp->set_speed = rtl8169_set_speed_xmii;
6909 tp->get_settings = rtl8169_gset_xmii;
6910 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
6911 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
6912 tp->link_ok = rtl8169_xmii_link_ok;
6913 tp->do_ioctl = rtl_xmii_ioctl;
6914 }
6915
6916 mutex_init(&tp->wk.mutex);
6917
6918 /* Get MAC address */
6919 for (i = 0; i < ETH_ALEN; i++)
6920 dev->dev_addr[i] = RTL_R8(MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006921
6922 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
6923 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006924
6925 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
6926
6927 /* don't enable SG, IP_CSUM and TSO by default - it might not work
6928 * properly for all devices */
6929 dev->features |= NETIF_F_RXCSUM |
6930 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6931
6932 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6933 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6934 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6935 NETIF_F_HIGHDMA;
6936
6937 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6938 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
6939 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
6940
6941 dev->hw_features |= NETIF_F_RXALL;
6942 dev->hw_features |= NETIF_F_RXFCS;
6943
6944 tp->hw_start = cfg->hw_start;
6945 tp->event_slow = cfg->event_slow;
6946
6947 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
6948 ~(RxBOVF | RxFOVF) : ~0;
6949
6950 init_timer(&tp->timer);
6951 tp->timer.data = (unsigned long) dev;
6952 tp->timer.function = rtl8169_phy_timer;
6953
6954 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
6955
6956 rc = register_netdev(dev);
6957 if (rc < 0)
6958 goto err_out_msi_4;
6959
6960 pci_set_drvdata(pdev, dev);
6961
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006962 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
6963 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
6964 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006965 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
6966 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
6967 "tx checksumming: %s]\n",
6968 rtl_chip_infos[chipset].jumbo_max,
6969 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
6970 }
6971
6972 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6973 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6974 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6975 rtl8168_driver_start(tp);
6976 }
6977
6978 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
6979
6980 if (pci_dev_run_wake(pdev))
6981 pm_runtime_put_noidle(&pdev->dev);
6982
6983 netif_carrier_off(dev);
6984
6985out:
6986 return rc;
6987
6988err_out_msi_4:
Devendra Nagaad1be8d2012-05-31 01:51:20 +00006989 netif_napi_del(&tp->napi);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006990 rtl_disable_msi(pdev, tp);
6991 iounmap(ioaddr);
6992err_out_free_res_3:
6993 pci_release_regions(pdev);
6994err_out_mwi_2:
6995 pci_clear_mwi(pdev);
6996 pci_disable_device(pdev);
6997err_out_free_dev_1:
6998 free_netdev(dev);
6999 goto out;
7000}
7001
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002static struct pci_driver rtl8169_pci_driver = {
7003 .name = MODULENAME,
7004 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01007005 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05007006 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02007007 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007008 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009};
7010
Devendra Naga3eeb7da2012-10-26 09:27:42 +00007011module_pci_driver(rtl8169_pci_driver);