blob: 0211836f2cb716ff3b4e9fb02e30a07987a7a35d [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"
hayeswangbeb330a2013-04-01 22:23:39 +000050#define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.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] =
hayeswangbeb330a2013-04-01 22:23:39 +0000265 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2,
Hayes Wangc5583862012-07-02 17:23:22 +0800266 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 Wang4f6b00e52011-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)
hayeswangbeb330a2013-04-01 22:23:39 +0000332#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800333#define RXCFG_DMA_SHIFT 8
334 /* Unlimited maximum PCI burst. */
335#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700336
Francois Romieu07d3f512007-02-21 22:40:46 +0100337 RxMissed = 0x4c,
338 Cfg9346 = 0x50,
339 Config0 = 0x51,
340 Config1 = 0x52,
341 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200342#define PME_SIGNAL (1 << 5) /* 8168c and later */
343
Francois Romieu07d3f512007-02-21 22:40:46 +0100344 Config3 = 0x54,
345 Config4 = 0x55,
346 Config5 = 0x56,
347 MultiIntr = 0x5c,
348 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100349 PHYstatus = 0x6c,
350 RxMaxSize = 0xda,
351 CPlusCmd = 0xe0,
352 IntrMitigate = 0xe2,
353 RxDescAddrLow = 0xe4,
354 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000355 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
356
357#define NoEarlyTx 0x3f /* Max value : no early transmit. */
358
359 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
360
361#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800362#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000363
Francois Romieu07d3f512007-02-21 22:40:46 +0100364 FuncEvent = 0xf0,
365 FuncEventMask = 0xf4,
366 FuncPresetState = 0xf8,
367 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368};
369
Francois Romieuf162a5d2008-06-01 22:37:49 +0200370enum rtl8110_registers {
371 TBICSR = 0x64,
372 TBI_ANAR = 0x68,
373 TBI_LPAR = 0x6a,
374};
375
376enum rtl8168_8101_registers {
377 CSIDR = 0x64,
378 CSIAR = 0x68,
379#define CSIAR_FLAG 0x80000000
380#define CSIAR_WRITE_CMD 0x80000000
381#define CSIAR_BYTE_ENABLE 0x0f
382#define CSIAR_BYTE_ENABLE_SHIFT 12
383#define CSIAR_ADDR_MASK 0x0fff
Hayes Wang7e18dca2012-03-30 14:33:02 +0800384#define CSIAR_FUNC_CARD 0x00000000
385#define CSIAR_FUNC_SDIO 0x00010000
386#define CSIAR_FUNC_NIC 0x00020000
françois romieu065c27c2011-01-03 15:08:12 +0000387 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200388 EPHYAR = 0x80,
389#define EPHYAR_FLAG 0x80000000
390#define EPHYAR_WRITE_CMD 0x80000000
391#define EPHYAR_REG_MASK 0x1f
392#define EPHYAR_REG_SHIFT 16
393#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800394 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800395#define PFM_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200396 DBG_REG = 0xd1,
397#define FIX_NAK_1 (1 << 4)
398#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800399 TWSI = 0xd2,
400 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800401#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800402#define TX_EMPTY (1 << 5)
403#define RX_EMPTY (1 << 4)
404#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800405#define EN_NDP (1 << 3)
406#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800407#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000408 EFUSEAR = 0xdc,
409#define EFUSEAR_FLAG 0x80000000
410#define EFUSEAR_WRITE_CMD 0x80000000
411#define EFUSEAR_READ_CMD 0x00000000
412#define EFUSEAR_REG_MASK 0x03ff
413#define EFUSEAR_REG_SHIFT 8
414#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200415};
416
françois romieuc0e45c12011-01-03 15:08:04 +0000417enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800418 LED_FREQ = 0x1a,
419 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000420 ERIDR = 0x70,
421 ERIAR = 0x74,
422#define ERIAR_FLAG 0x80000000
423#define ERIAR_WRITE_CMD 0x80000000
424#define ERIAR_READ_CMD 0x00000000
425#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000426#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800427#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
428#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
429#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
430#define ERIAR_MASK_SHIFT 12
431#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
432#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800433#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800434#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000435 EPHY_RXER_NUM = 0x7c,
436 OCPDR = 0xb0, /* OCP GPHY access */
437#define OCPDR_WRITE_CMD 0x80000000
438#define OCPDR_READ_CMD 0x00000000
439#define OCPDR_REG_MASK 0x7f
440#define OCPDR_GPHY_REG_SHIFT 16
441#define OCPDR_DATA_MASK 0xffff
442 OCPAR = 0xb4,
443#define OCPAR_FLAG 0x80000000
444#define OCPAR_GPHY_WRITE_CMD 0x8000f060
445#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800446 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000447 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
448 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200449#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800450#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800451#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800452#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800453#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000454};
455
Francois Romieu07d3f512007-02-21 22:40:46 +0100456enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100458 SYSErr = 0x8000,
459 PCSTimeout = 0x4000,
460 SWInt = 0x0100,
461 TxDescUnavail = 0x0080,
462 RxFIFOOver = 0x0040,
463 LinkChg = 0x0020,
464 RxOverflow = 0x0010,
465 TxErr = 0x0008,
466 TxOK = 0x0004,
467 RxErr = 0x0002,
468 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400471 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200472 RxFOVF = (1 << 23),
473 RxRWT = (1 << 22),
474 RxRES = (1 << 21),
475 RxRUNT = (1 << 20),
476 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800479 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100480 CmdReset = 0x10,
481 CmdRxEnb = 0x08,
482 CmdTxEnb = 0x04,
483 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Francois Romieu275391a2007-02-23 23:50:28 +0100485 /* TXPoll register p.5 */
486 HPQ = 0x80, /* Poll cmd on the high prio queue */
487 NPQ = 0x40, /* Poll cmd on the low prio queue */
488 FSWInt = 0x01, /* Forced software interrupt */
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100491 Cfg9346_Lock = 0x00,
492 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100495 AcceptErr = 0x20,
496 AcceptRunt = 0x10,
497 AcceptBroadcast = 0x08,
498 AcceptMulticast = 0x04,
499 AcceptMyPhys = 0x02,
500 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200501#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 /* TxConfigBits */
504 TxInterFrameGapShift = 24,
505 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
506
Francois Romieu5d06a992006-02-23 00:47:58 +0100507 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200508 LEDS1 = (1 << 7),
509 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200510 Speed_down = (1 << 4),
511 MEMMAP = (1 << 3),
512 IOMAP = (1 << 2),
513 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100514 PMEnable = (1 << 0), /* Power Management Enable */
515
Francois Romieu6dccd162007-02-13 23:38:05 +0100516 /* Config2 register p. 25 */
françois romieu2ca6cf02011-12-15 08:37:43 +0000517 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100518 PCI_Clock_66MHz = 0x01,
519 PCI_Clock_33MHz = 0x00,
520
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100521 /* Config3 register p.25 */
522 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
523 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200524 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200525 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100526
Francois Romieud58d46b2011-05-03 16:38:29 +0200527 /* Config4 register */
528 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
529
Francois Romieu5d06a992006-02-23 00:47:58 +0100530 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100531 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
532 MWF = (1 << 5), /* Accept Multicast wakeup frame */
533 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200534 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100535 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100536 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /* TBICSR p.28 */
539 TBIReset = 0x80000000,
540 TBILoopback = 0x40000000,
541 TBINwEnable = 0x20000000,
542 TBINwRestart = 0x10000000,
543 TBILinkOk = 0x02000000,
544 TBINwComplete = 0x01000000,
545
546 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200547 EnableBist = (1 << 15), // 8168 8101
548 Mac_dbgo_oe = (1 << 14), // 8168 8101
549 Normal_mode = (1 << 13), // unused
550 Force_half_dup = (1 << 12), // 8168 8101
551 Force_rxflow_en = (1 << 11), // 8168 8101
552 Force_txflow_en = (1 << 10), // 8168 8101
553 Cxpl_dbg_sel = (1 << 9), // 8168 8101
554 ASF = (1 << 8), // 8168 8101
555 PktCntrDisable = (1 << 7), // 8168 8101
556 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 RxVlan = (1 << 6),
558 RxChkSum = (1 << 5),
559 PCIDAC = (1 << 4),
560 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100561 INTT_0 = 0x0000, // 8168
562 INTT_1 = 0x0001, // 8168
563 INTT_2 = 0x0002, // 8168
564 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100567 TBI_Enable = 0x80,
568 TxFlowCtrl = 0x40,
569 RxFlowCtrl = 0x20,
570 _1000bpsF = 0x10,
571 _100bps = 0x08,
572 _10bps = 0x04,
573 LinkStatus = 0x02,
574 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100577 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200578
579 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100580 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581};
582
Francois Romieu2b7b4312011-04-18 22:53:24 -0700583enum rtl_desc_bit {
584 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
586 RingEnd = (1 << 30), /* End of descriptor ring */
587 FirstFrag = (1 << 29), /* First segment of a packet */
588 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700589};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Francois Romieu2b7b4312011-04-18 22:53:24 -0700591/* Generic case. */
592enum rtl_tx_desc_bit {
593 /* First doubleword. */
594 TD_LSO = (1 << 27), /* Large Send Offload */
595#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Francois Romieu2b7b4312011-04-18 22:53:24 -0700597 /* Second doubleword. */
598 TxVlanTag = (1 << 17), /* Add VLAN tag */
599};
600
601/* 8169, 8168b and 810x except 8102e. */
602enum rtl_tx_desc_bit_0 {
603 /* First doubleword. */
604#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
605 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
606 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
607 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
608};
609
610/* 8102e, 8168c and beyond. */
611enum rtl_tx_desc_bit_1 {
612 /* Second doubleword. */
613#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
614 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
615 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
616 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
617};
618
619static const struct rtl_tx_desc_info {
620 struct {
621 u32 udp;
622 u32 tcp;
623 } checksum;
624 u16 mss_shift;
625 u16 opts_offset;
626} tx_desc_info [] = {
627 [RTL_TD_0] = {
628 .checksum = {
629 .udp = TD0_IP_CS | TD0_UDP_CS,
630 .tcp = TD0_IP_CS | TD0_TCP_CS
631 },
632 .mss_shift = TD0_MSS_SHIFT,
633 .opts_offset = 0
634 },
635 [RTL_TD_1] = {
636 .checksum = {
637 .udp = TD1_IP_CS | TD1_UDP_CS,
638 .tcp = TD1_IP_CS | TD1_TCP_CS
639 },
640 .mss_shift = TD1_MSS_SHIFT,
641 .opts_offset = 1
642 }
643};
644
645enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 /* Rx private */
647 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
648 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
649
650#define RxProtoUDP (PID1)
651#define RxProtoTCP (PID0)
652#define RxProtoIP (PID1 | PID0)
653#define RxProtoMask RxProtoIP
654
655 IPFail = (1 << 16), /* IP checksum failed */
656 UDPFail = (1 << 15), /* UDP/IP checksum failed */
657 TCPFail = (1 << 14), /* TCP/IP checksum failed */
658 RxVlanTag = (1 << 16), /* VLAN tag available */
659};
660
661#define RsvdMask 0x3fffc000
662
663struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200664 __le32 opts1;
665 __le32 opts2;
666 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667};
668
669struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200670 __le32 opts1;
671 __le32 opts2;
672 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673};
674
675struct ring_info {
676 struct sk_buff *skb;
677 u32 len;
678 u8 __pad[sizeof(void *) - sizeof(u32)];
679};
680
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200681enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200682 RTL_FEATURE_WOL = (1 << 0),
683 RTL_FEATURE_MSI = (1 << 1),
684 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200685};
686
Ivan Vecera355423d2009-02-06 21:49:57 -0800687struct rtl8169_counters {
688 __le64 tx_packets;
689 __le64 rx_packets;
690 __le64 tx_errors;
691 __le32 rx_errors;
692 __le16 rx_missed;
693 __le16 align_errors;
694 __le32 tx_one_collision;
695 __le32 tx_multi_collision;
696 __le64 rx_unicast;
697 __le64 rx_broadcast;
698 __le32 rx_multicast;
699 __le16 tx_aborted;
700 __le16 tx_underun;
701};
702
Francois Romieuda78dbf2012-01-26 14:18:23 +0100703enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100704 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100705 RTL_FLAG_TASK_SLOW_PENDING,
706 RTL_FLAG_TASK_RESET_PENDING,
707 RTL_FLAG_TASK_PHY_PENDING,
708 RTL_FLAG_MAX
709};
710
Junchang Wang8027aa22012-03-04 23:30:32 +0100711struct rtl8169_stats {
712 u64 packets;
713 u64 bytes;
714 struct u64_stats_sync syncp;
715};
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717struct rtl8169_private {
718 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200719 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000720 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700721 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200722 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700723 u16 txd_version;
724 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
726 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100728 struct rtl8169_stats rx_stats;
729 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
731 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
732 dma_addr_t TxPhyAddr;
733 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000734 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 struct timer_list timer;
737 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100738
739 u16 event_slow;
françois romieuc0e45c12011-01-03 15:08:04 +0000740
741 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200742 void (*write)(struct rtl8169_private *, int, int);
743 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000744 } mdio_ops;
745
françois romieu065c27c2011-01-03 15:08:12 +0000746 struct pll_power_ops {
747 void (*down)(struct rtl8169_private *);
748 void (*up)(struct rtl8169_private *);
749 } pll_power_ops;
750
Francois Romieud58d46b2011-05-03 16:38:29 +0200751 struct jumbo_ops {
752 void (*enable)(struct rtl8169_private *);
753 void (*disable)(struct rtl8169_private *);
754 } jumbo_ops;
755
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800756 struct csi_ops {
Francois Romieu52989f02012-07-06 13:37:00 +0200757 void (*write)(struct rtl8169_private *, int, int);
758 u32 (*read)(struct rtl8169_private *, int);
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800759 } csi_ops;
760
Oliver Neukum54405cd2011-01-06 21:55:13 +0100761 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200762 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000763 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100764 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000765 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800767 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100768
769 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100770 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
771 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100772 struct work_struct work;
773 } wk;
774
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200775 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200776
777 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800778 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000779 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400780 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000781
Francois Romieub6ffd972011-06-17 17:00:05 +0200782 struct rtl_fw {
783 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200784
785#define RTL_VER_SIZE 32
786
787 char version[RTL_VER_SIZE];
788
789 struct rtl_fw_phy_action {
790 __le32 *code;
791 size_t size;
792 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200793 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300794#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800795
796 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797};
798
Ralf Baechle979b6c12005-06-13 14:30:40 -0700799MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700802MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200803module_param_named(debug, debug.msg_enable, int, 0);
804MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805MODULE_LICENSE("GPL");
806MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000807MODULE_FIRMWARE(FIRMWARE_8168D_1);
808MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000809MODULE_FIRMWARE(FIRMWARE_8168E_1);
810MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400811MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800812MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800813MODULE_FIRMWARE(FIRMWARE_8168F_1);
814MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800815MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800816MODULE_FIRMWARE(FIRMWARE_8411_1);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800817MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswangbeb330a2013-04-01 22:23:39 +0000818MODULE_FIRMWARE(FIRMWARE_8168G_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Francois Romieuda78dbf2012-01-26 14:18:23 +0100820static void rtl_lock_work(struct rtl8169_private *tp)
821{
822 mutex_lock(&tp->wk.mutex);
823}
824
825static void rtl_unlock_work(struct rtl8169_private *tp)
826{
827 mutex_unlock(&tp->wk.mutex);
828}
829
Francois Romieud58d46b2011-05-03 16:38:29 +0200830static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
831{
Jiang Liu7d7903b2012-07-24 17:20:16 +0800832 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
833 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200834}
835
Francois Romieuffc46952012-07-06 14:19:23 +0200836struct rtl_cond {
837 bool (*check)(struct rtl8169_private *);
838 const char *msg;
839};
840
841static void rtl_udelay(unsigned int d)
842{
843 udelay(d);
844}
845
846static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
847 void (*delay)(unsigned int), unsigned int d, int n,
848 bool high)
849{
850 int i;
851
852 for (i = 0; i < n; i++) {
853 delay(d);
854 if (c->check(tp) == high)
855 return true;
856 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200857 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
858 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200859 return false;
860}
861
862static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
863 const struct rtl_cond *c,
864 unsigned int d, int n)
865{
866 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
867}
868
869static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
870 const struct rtl_cond *c,
871 unsigned int d, int n)
872{
873 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
874}
875
876static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
877 const struct rtl_cond *c,
878 unsigned int d, int n)
879{
880 return rtl_loop_wait(tp, c, msleep, d, n, true);
881}
882
883static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
884 const struct rtl_cond *c,
885 unsigned int d, int n)
886{
887 return rtl_loop_wait(tp, c, msleep, d, n, false);
888}
889
890#define DECLARE_RTL_COND(name) \
891static bool name ## _check(struct rtl8169_private *); \
892 \
893static const struct rtl_cond name = { \
894 .check = name ## _check, \
895 .msg = #name \
896}; \
897 \
898static bool name ## _check(struct rtl8169_private *tp)
899
900DECLARE_RTL_COND(rtl_ocpar_cond)
901{
902 void __iomem *ioaddr = tp->mmio_addr;
903
904 return RTL_R32(OCPAR) & OCPAR_FLAG;
905}
906
françois romieub646d902011-01-03 15:08:21 +0000907static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
908{
909 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000910
911 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Francois Romieuffc46952012-07-06 14:19:23 +0200912
913 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
914 RTL_R32(OCPDR) : ~0;
françois romieub646d902011-01-03 15:08:21 +0000915}
916
917static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
918{
919 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000920
921 RTL_W32(OCPDR, data);
922 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Francois Romieuffc46952012-07-06 14:19:23 +0200923
924 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
925}
926
927DECLARE_RTL_COND(rtl_eriar_cond)
928{
929 void __iomem *ioaddr = tp->mmio_addr;
930
931 return RTL_R32(ERIAR) & ERIAR_FLAG;
françois romieub646d902011-01-03 15:08:21 +0000932}
933
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800934static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000935{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800936 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000937
938 RTL_W8(ERIDR, cmd);
939 RTL_W32(ERIAR, 0x800010e8);
940 msleep(2);
Francois Romieuffc46952012-07-06 14:19:23 +0200941
942 if (!rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 5))
943 return;
françois romieub646d902011-01-03 15:08:21 +0000944
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800945 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000946}
947
948#define OOB_CMD_RESET 0x00
949#define OOB_CMD_DRIVER_START 0x05
950#define OOB_CMD_DRIVER_STOP 0x06
951
Francois Romieucecb5fd2011-04-01 10:21:07 +0200952static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
953{
954 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
955}
956
Francois Romieuffc46952012-07-06 14:19:23 +0200957DECLARE_RTL_COND(rtl_ocp_read_cond)
françois romieub646d902011-01-03 15:08:21 +0000958{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200959 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000960
Francois Romieucecb5fd2011-04-01 10:21:07 +0200961 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000962
Francois Romieuffc46952012-07-06 14:19:23 +0200963 return ocp_read(tp, 0x0f, reg) & 0x00000800;
964}
965
966static void rtl8168_driver_start(struct rtl8169_private *tp)
967{
968 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
969
970 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
françois romieub646d902011-01-03 15:08:21 +0000971}
972
973static void rtl8168_driver_stop(struct rtl8169_private *tp)
974{
françois romieub646d902011-01-03 15:08:21 +0000975 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
976
Francois Romieuffc46952012-07-06 14:19:23 +0200977 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
françois romieub646d902011-01-03 15:08:21 +0000978}
979
hayeswang4804b3b2011-03-21 01:50:29 +0000980static int r8168dp_check_dash(struct rtl8169_private *tp)
981{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200982 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000983
Francois Romieucecb5fd2011-04-01 10:21:07 +0200984 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000985}
françois romieub646d902011-01-03 15:08:21 +0000986
Hayes Wangc5583862012-07-02 17:23:22 +0800987static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
988{
989 if (reg & 0xffff0001) {
990 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
991 return true;
992 }
993 return false;
994}
995
996DECLARE_RTL_COND(rtl_ocp_gphy_cond)
997{
998 void __iomem *ioaddr = tp->mmio_addr;
999
1000 return RTL_R32(GPHY_OCP) & OCPAR_FLAG;
1001}
1002
1003static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1004{
1005 void __iomem *ioaddr = tp->mmio_addr;
1006
1007 if (rtl_ocp_reg_failure(tp, reg))
1008 return;
1009
1010 RTL_W32(GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
1011
1012 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
1013}
1014
1015static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
1016{
1017 void __iomem *ioaddr = tp->mmio_addr;
1018
1019 if (rtl_ocp_reg_failure(tp, reg))
1020 return 0;
1021
1022 RTL_W32(GPHY_OCP, reg << 15);
1023
1024 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
1025 (RTL_R32(GPHY_OCP) & 0xffff) : ~0;
1026}
1027
Hayes Wangc5583862012-07-02 17:23:22 +08001028static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1029{
1030 void __iomem *ioaddr = tp->mmio_addr;
1031
1032 if (rtl_ocp_reg_failure(tp, reg))
1033 return;
1034
1035 RTL_W32(OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +08001036}
1037
1038static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1039{
1040 void __iomem *ioaddr = tp->mmio_addr;
1041
1042 if (rtl_ocp_reg_failure(tp, reg))
1043 return 0;
1044
1045 RTL_W32(OCPDR, reg << 15);
1046
Hayes Wang3a83ad12012-07-11 20:31:56 +08001047 return RTL_R32(OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +08001048}
1049
1050#define OCP_STD_PHY_BASE 0xa400
1051
1052static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1053{
1054 if (reg == 0x1f) {
1055 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1056 return;
1057 }
1058
1059 if (tp->ocp_base != OCP_STD_PHY_BASE)
1060 reg -= 0x10;
1061
1062 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1063}
1064
1065static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1066{
1067 if (tp->ocp_base != OCP_STD_PHY_BASE)
1068 reg -= 0x10;
1069
1070 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1071}
1072
hayeswangeee37862013-04-01 22:23:38 +00001073static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1074{
1075 if (reg == 0x1f) {
1076 tp->ocp_base = value << 4;
1077 return;
1078 }
1079
1080 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1081}
1082
1083static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1084{
1085 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1086}
1087
Francois Romieuffc46952012-07-06 14:19:23 +02001088DECLARE_RTL_COND(rtl_phyar_cond)
1089{
1090 void __iomem *ioaddr = tp->mmio_addr;
1091
1092 return RTL_R32(PHYAR) & 0x80000000;
1093}
1094
Francois Romieu24192212012-07-06 20:19:42 +02001095static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Francois Romieu24192212012-07-06 20:19:42 +02001097 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Francois Romieu24192212012-07-06 20:19:42 +02001099 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Francois Romieuffc46952012-07-06 14:19:23 +02001101 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -07001102 /*
Timo Teräs81a95f02010-06-09 17:31:48 -07001103 * According to hardware specs a 20us delay is required after write
1104 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -07001105 */
Timo Teräs81a95f02010-06-09 17:31:48 -07001106 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
1108
Francois Romieu24192212012-07-06 20:19:42 +02001109static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
Francois Romieu24192212012-07-06 20:19:42 +02001111 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieuffc46952012-07-06 14:19:23 +02001112 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Francois Romieu24192212012-07-06 20:19:42 +02001114 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Francois Romieuffc46952012-07-06 14:19:23 +02001116 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1117 RTL_R32(PHYAR) & 0xffff : ~0;
1118
Timo Teräs81a95f02010-06-09 17:31:48 -07001119 /*
1120 * According to hardware specs a 20us delay is required after read
1121 * complete indication, but before sending next command.
1122 */
1123 udelay(20);
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return value;
1126}
1127
Francois Romieu24192212012-07-06 20:19:42 +02001128static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001129{
Francois Romieu24192212012-07-06 20:19:42 +02001130 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001131
Francois Romieu24192212012-07-06 20:19:42 +02001132 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
françois romieuc0e45c12011-01-03 15:08:04 +00001133 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
1134 RTL_W32(EPHY_RXER_NUM, 0);
1135
Francois Romieuffc46952012-07-06 14:19:23 +02001136 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001137}
1138
Francois Romieu24192212012-07-06 20:19:42 +02001139static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001140{
Francois Romieu24192212012-07-06 20:19:42 +02001141 r8168dp_1_mdio_access(tp, reg,
1142 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001143}
1144
Francois Romieu24192212012-07-06 20:19:42 +02001145static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001146{
Francois Romieu24192212012-07-06 20:19:42 +02001147 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001148
Francois Romieu24192212012-07-06 20:19:42 +02001149 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001150
1151 mdelay(1);
1152 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1153 RTL_W32(EPHY_RXER_NUM, 0);
1154
Francois Romieuffc46952012-07-06 14:19:23 +02001155 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1156 RTL_R32(OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001157}
1158
françois romieue6de30d2011-01-03 15:08:37 +00001159#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1160
1161static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1162{
1163 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1164}
1165
1166static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1167{
1168 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1169}
1170
Francois Romieu24192212012-07-06 20:19:42 +02001171static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001172{
Francois Romieu24192212012-07-06 20:19:42 +02001173 void __iomem *ioaddr = tp->mmio_addr;
1174
françois romieue6de30d2011-01-03 15:08:37 +00001175 r8168dp_2_mdio_start(ioaddr);
1176
Francois Romieu24192212012-07-06 20:19:42 +02001177 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001178
1179 r8168dp_2_mdio_stop(ioaddr);
1180}
1181
Francois Romieu24192212012-07-06 20:19:42 +02001182static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001183{
Francois Romieu24192212012-07-06 20:19:42 +02001184 void __iomem *ioaddr = tp->mmio_addr;
françois romieue6de30d2011-01-03 15:08:37 +00001185 int value;
1186
1187 r8168dp_2_mdio_start(ioaddr);
1188
Francois Romieu24192212012-07-06 20:19:42 +02001189 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001190
1191 r8168dp_2_mdio_stop(ioaddr);
1192
1193 return value;
1194}
1195
françois romieu4da19632011-01-03 15:07:55 +00001196static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001197{
Francois Romieu24192212012-07-06 20:19:42 +02001198 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001199}
1200
françois romieu4da19632011-01-03 15:07:55 +00001201static int rtl_readphy(struct rtl8169_private *tp, int location)
1202{
Francois Romieu24192212012-07-06 20:19:42 +02001203 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001204}
1205
1206static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1207{
1208 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1209}
1210
1211static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001212{
1213 int val;
1214
françois romieu4da19632011-01-03 15:07:55 +00001215 val = rtl_readphy(tp, reg_addr);
1216 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001217}
1218
Francois Romieuccdffb92008-07-26 14:26:06 +02001219static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1220 int val)
1221{
1222 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001223
françois romieu4da19632011-01-03 15:07:55 +00001224 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001225}
1226
1227static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1228{
1229 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001230
françois romieu4da19632011-01-03 15:07:55 +00001231 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001232}
1233
Francois Romieuffc46952012-07-06 14:19:23 +02001234DECLARE_RTL_COND(rtl_ephyar_cond)
1235{
1236 void __iomem *ioaddr = tp->mmio_addr;
1237
1238 return RTL_R32(EPHYAR) & EPHYAR_FLAG;
1239}
1240
Francois Romieufdf6fc02012-07-06 22:40:38 +02001241static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001242{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001243 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001244
1245 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1246 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1247
Francois Romieuffc46952012-07-06 14:19:23 +02001248 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1249
1250 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001251}
1252
Francois Romieufdf6fc02012-07-06 22:40:38 +02001253static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001254{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001255 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001256
1257 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1258
Francois Romieuffc46952012-07-06 14:19:23 +02001259 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1260 RTL_R32(EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001261}
1262
Francois Romieufdf6fc02012-07-06 22:40:38 +02001263static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1264 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001265{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001266 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001267
1268 BUG_ON((addr & 3) || (mask == 0));
1269 RTL_W32(ERIDR, val);
1270 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1271
Francois Romieuffc46952012-07-06 14:19:23 +02001272 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001273}
1274
Francois Romieufdf6fc02012-07-06 22:40:38 +02001275static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001276{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001277 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001278
1279 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1280
Francois Romieuffc46952012-07-06 14:19:23 +02001281 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1282 RTL_R32(ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001283}
1284
Francois Romieufdf6fc02012-07-06 22:40:38 +02001285static void rtl_w1w0_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
1286 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001287{
1288 u32 val;
1289
Francois Romieufdf6fc02012-07-06 22:40:38 +02001290 val = rtl_eri_read(tp, addr, type);
1291 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001292}
1293
françois romieuc28aa382011-08-02 03:53:43 +00001294struct exgmac_reg {
1295 u16 addr;
1296 u16 mask;
1297 u32 val;
1298};
1299
Francois Romieufdf6fc02012-07-06 22:40:38 +02001300static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001301 const struct exgmac_reg *r, int len)
1302{
1303 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001304 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001305 r++;
1306 }
1307}
1308
Francois Romieuffc46952012-07-06 14:19:23 +02001309DECLARE_RTL_COND(rtl_efusear_cond)
1310{
1311 void __iomem *ioaddr = tp->mmio_addr;
1312
1313 return RTL_R32(EFUSEAR) & EFUSEAR_FLAG;
1314}
1315
Francois Romieufdf6fc02012-07-06 22:40:38 +02001316static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001317{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001318 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00001319
1320 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1321
Francois Romieuffc46952012-07-06 14:19:23 +02001322 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1323 RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001324}
1325
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001326static u16 rtl_get_events(struct rtl8169_private *tp)
1327{
1328 void __iomem *ioaddr = tp->mmio_addr;
1329
1330 return RTL_R16(IntrStatus);
1331}
1332
1333static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1334{
1335 void __iomem *ioaddr = tp->mmio_addr;
1336
1337 RTL_W16(IntrStatus, bits);
1338 mmiowb();
1339}
1340
1341static void rtl_irq_disable(struct rtl8169_private *tp)
1342{
1343 void __iomem *ioaddr = tp->mmio_addr;
1344
1345 RTL_W16(IntrMask, 0);
1346 mmiowb();
1347}
1348
Francois Romieu3e990ff2012-01-26 12:50:01 +01001349static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1350{
1351 void __iomem *ioaddr = tp->mmio_addr;
1352
1353 RTL_W16(IntrMask, bits);
1354}
1355
Francois Romieuda78dbf2012-01-26 14:18:23 +01001356#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1357#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1358#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1359
1360static void rtl_irq_enable_all(struct rtl8169_private *tp)
1361{
1362 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1363}
1364
françois romieu811fd302011-12-04 20:30:45 +00001365static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
françois romieu811fd302011-12-04 20:30:45 +00001367 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001369 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001370 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001371 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372}
1373
françois romieu4da19632011-01-03 15:07:55 +00001374static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
françois romieu4da19632011-01-03 15:07:55 +00001376 void __iomem *ioaddr = tp->mmio_addr;
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return RTL_R32(TBICSR) & TBIReset;
1379}
1380
françois romieu4da19632011-01-03 15:07:55 +00001381static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
françois romieu4da19632011-01-03 15:07:55 +00001383 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
1386static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1387{
1388 return RTL_R32(TBICSR) & TBILinkOk;
1389}
1390
1391static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1392{
1393 return RTL_R8(PHYstatus) & LinkStatus;
1394}
1395
françois romieu4da19632011-01-03 15:07:55 +00001396static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
françois romieu4da19632011-01-03 15:07:55 +00001398 void __iomem *ioaddr = tp->mmio_addr;
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1401}
1402
françois romieu4da19632011-01-03 15:07:55 +00001403static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 unsigned int val;
1406
françois romieu4da19632011-01-03 15:07:55 +00001407 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1408 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
1410
Hayes Wang70090422011-07-06 15:58:06 +08001411static void rtl_link_chg_patch(struct rtl8169_private *tp)
1412{
1413 void __iomem *ioaddr = tp->mmio_addr;
1414 struct net_device *dev = tp->dev;
1415
1416 if (!netif_running(dev))
1417 return;
1418
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001419 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1420 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Hayes Wang70090422011-07-06 15:58:06 +08001421 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001422 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1423 ERIAR_EXGMAC);
1424 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1425 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001426 } else if (RTL_R8(PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001427 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1428 ERIAR_EXGMAC);
1429 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1430 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001431 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001432 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1433 ERIAR_EXGMAC);
1434 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1435 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001436 }
1437 /* Reset packet filter */
Francois Romieufdf6fc02012-07-06 22:40:38 +02001438 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001439 ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02001440 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001441 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001442 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1443 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1444 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001445 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1446 ERIAR_EXGMAC);
1447 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1448 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001449 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001450 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1451 ERIAR_EXGMAC);
1452 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1453 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001454 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001455 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1456 if (RTL_R8(PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001457 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1458 ERIAR_EXGMAC);
1459 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1460 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001461 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001462 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1463 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001464 }
Hayes Wang70090422011-07-06 15:58:06 +08001465 }
1466}
1467
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001468static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001469 struct rtl8169_private *tp,
1470 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001473 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001474 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001475 if (pm)
1476 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001478 if (net_ratelimit())
1479 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001480 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001482 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001483 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001484 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001488static void rtl8169_check_link_status(struct net_device *dev,
1489 struct rtl8169_private *tp,
1490 void __iomem *ioaddr)
1491{
1492 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1493}
1494
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001495#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1496
1497static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1498{
1499 void __iomem *ioaddr = tp->mmio_addr;
1500 u8 options;
1501 u32 wolopts = 0;
1502
1503 options = RTL_R8(Config1);
1504 if (!(options & PMEnable))
1505 return 0;
1506
1507 options = RTL_R8(Config3);
1508 if (options & LinkUp)
1509 wolopts |= WAKE_PHY;
1510 if (options & MagicPacket)
1511 wolopts |= WAKE_MAGIC;
1512
1513 options = RTL_R8(Config5);
1514 if (options & UWF)
1515 wolopts |= WAKE_UCAST;
1516 if (options & BWF)
1517 wolopts |= WAKE_BCAST;
1518 if (options & MWF)
1519 wolopts |= WAKE_MCAST;
1520
1521 return wolopts;
1522}
1523
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001524static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1525{
1526 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001527
Francois Romieuda78dbf2012-01-26 14:18:23 +01001528 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001529
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001530 wol->supported = WAKE_ANY;
1531 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001532
Francois Romieuda78dbf2012-01-26 14:18:23 +01001533 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001534}
1535
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001536static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001537{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001538 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001539 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001540 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001541 u32 opt;
1542 u16 reg;
1543 u8 mask;
1544 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001545 { WAKE_PHY, Config3, LinkUp },
1546 { WAKE_MAGIC, Config3, MagicPacket },
1547 { WAKE_UCAST, Config5, UWF },
1548 { WAKE_BCAST, Config5, BWF },
1549 { WAKE_MCAST, Config5, MWF },
1550 { WAKE_ANY, Config5, LanWake }
1551 };
Francois Romieu851e6022012-04-17 11:10:11 +02001552 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001553
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001554 RTL_W8(Cfg9346, Cfg9346_Unlock);
1555
1556 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
Francois Romieu851e6022012-04-17 11:10:11 +02001557 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001558 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001559 options |= cfg[i].mask;
1560 RTL_W8(cfg[i].reg, options);
1561 }
1562
Francois Romieu851e6022012-04-17 11:10:11 +02001563 switch (tp->mac_version) {
1564 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1565 options = RTL_R8(Config1) & ~PMEnable;
1566 if (wolopts)
1567 options |= PMEnable;
1568 RTL_W8(Config1, options);
1569 break;
1570 default:
Francois Romieud387b422012-04-17 11:12:01 +02001571 options = RTL_R8(Config2) & ~PME_SIGNAL;
1572 if (wolopts)
1573 options |= PME_SIGNAL;
1574 RTL_W8(Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001575 break;
1576 }
1577
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001578 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001579}
1580
1581static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1582{
1583 struct rtl8169_private *tp = netdev_priv(dev);
1584
Francois Romieuda78dbf2012-01-26 14:18:23 +01001585 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001586
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001587 if (wol->wolopts)
1588 tp->features |= RTL_FEATURE_WOL;
1589 else
1590 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001591 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001592
1593 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001594
françois romieuea809072010-11-08 13:23:58 +00001595 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1596
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001597 return 0;
1598}
1599
Francois Romieu31bd2042011-04-26 18:58:59 +02001600static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1601{
Francois Romieu85bffe62011-04-27 08:22:39 +02001602 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001603}
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605static void rtl8169_get_drvinfo(struct net_device *dev,
1606 struct ethtool_drvinfo *info)
1607{
1608 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001609 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Rick Jones68aad782011-11-07 13:29:27 +00001611 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1612 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1613 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001614 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001615 if (!IS_ERR_OR_NULL(rtl_fw))
1616 strlcpy(info->fw_version, rtl_fw->version,
1617 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
1620static int rtl8169_get_regs_len(struct net_device *dev)
1621{
1622 return R8169_REGS_SIZE;
1623}
1624
1625static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001626 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
1628 struct rtl8169_private *tp = netdev_priv(dev);
1629 void __iomem *ioaddr = tp->mmio_addr;
1630 int ret = 0;
1631 u32 reg;
1632
1633 reg = RTL_R32(TBICSR);
1634 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1635 (duplex == DUPLEX_FULL)) {
1636 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1637 } else if (autoneg == AUTONEG_ENABLE)
1638 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1639 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001640 netif_warn(tp, link, dev,
1641 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 ret = -EOPNOTSUPP;
1643 }
1644
1645 return ret;
1646}
1647
1648static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001649 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
1651 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001652 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001653 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Hayes Wang716b50a2011-02-22 17:26:18 +08001655 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001658 int auto_nego;
1659
françois romieu4da19632011-01-03 15:07:55 +00001660 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001661 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1662 ADVERTISE_100HALF | ADVERTISE_100FULL);
1663
1664 if (adv & ADVERTISED_10baseT_Half)
1665 auto_nego |= ADVERTISE_10HALF;
1666 if (adv & ADVERTISED_10baseT_Full)
1667 auto_nego |= ADVERTISE_10FULL;
1668 if (adv & ADVERTISED_100baseT_Half)
1669 auto_nego |= ADVERTISE_100HALF;
1670 if (adv & ADVERTISED_100baseT_Full)
1671 auto_nego |= ADVERTISE_100FULL;
1672
françois romieu3577aa12009-05-19 10:46:48 +00001673 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1674
françois romieu4da19632011-01-03 15:07:55 +00001675 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001676 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1677
1678 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001679 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001680 if (adv & ADVERTISED_1000baseT_Half)
1681 giga_ctrl |= ADVERTISE_1000HALF;
1682 if (adv & ADVERTISED_1000baseT_Full)
1683 giga_ctrl |= ADVERTISE_1000FULL;
1684 } else if (adv & (ADVERTISED_1000baseT_Half |
1685 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001686 netif_info(tp, link, dev,
1687 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001688 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
françois romieu3577aa12009-05-19 10:46:48 +00001691 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001692
françois romieu4da19632011-01-03 15:07:55 +00001693 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1694 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001695 } else {
1696 giga_ctrl = 0;
1697
1698 if (speed == SPEED_10)
1699 bmcr = 0;
1700 else if (speed == SPEED_100)
1701 bmcr = BMCR_SPEED100;
1702 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001703 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001704
1705 if (duplex == DUPLEX_FULL)
1706 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001707 }
1708
françois romieu4da19632011-01-03 15:07:55 +00001709 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001710
Francois Romieucecb5fd2011-04-01 10:21:07 +02001711 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1712 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001713 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001714 rtl_writephy(tp, 0x17, 0x2138);
1715 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001716 } else {
françois romieu4da19632011-01-03 15:07:55 +00001717 rtl_writephy(tp, 0x17, 0x2108);
1718 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001719 }
1720 }
1721
Oliver Neukum54405cd2011-01-06 21:55:13 +01001722 rc = 0;
1723out:
1724 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
1727static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001728 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
1730 struct rtl8169_private *tp = netdev_priv(dev);
1731 int ret;
1732
Oliver Neukum54405cd2011-01-06 21:55:13 +01001733 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001734 if (ret < 0)
1735 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Francois Romieu4876cc12011-03-11 21:07:11 +01001737 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1738 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001740 }
1741out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return ret;
1743}
1744
1745static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1746{
1747 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 int ret;
1749
Francois Romieu4876cc12011-03-11 21:07:11 +01001750 del_timer_sync(&tp->timer);
1751
Francois Romieuda78dbf2012-01-26 14:18:23 +01001752 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001753 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001754 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001755 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 return ret;
1758}
1759
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001760static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1761 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
Francois Romieud58d46b2011-05-03 16:38:29 +02001763 struct rtl8169_private *tp = netdev_priv(dev);
1764
Francois Romieu2b7b4312011-04-18 22:53:24 -07001765 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001766 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Francois Romieud58d46b2011-05-03 16:38:29 +02001768 if (dev->mtu > JUMBO_1K &&
1769 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1770 features &= ~NETIF_F_IP_CSUM;
1771
Michał Mirosław350fb322011-04-08 06:35:56 +00001772 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773}
1774
Francois Romieuda78dbf2012-01-26 14:18:23 +01001775static void __rtl8169_set_features(struct net_device *dev,
1776 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
1778 struct rtl8169_private *tp = netdev_priv(dev);
Ben Greear6bbe0212012-02-10 15:04:33 +00001779 netdev_features_t changed = features ^ dev->features;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001780 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Ben Greear6bbe0212012-02-10 15:04:33 +00001782 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)))
1783 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Ben Greear6bbe0212012-02-10 15:04:33 +00001785 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)) {
1786 if (features & NETIF_F_RXCSUM)
1787 tp->cp_cmd |= RxChkSum;
1788 else
1789 tp->cp_cmd &= ~RxChkSum;
Michał Mirosław350fb322011-04-08 06:35:56 +00001790
Ben Greear6bbe0212012-02-10 15:04:33 +00001791 if (dev->features & NETIF_F_HW_VLAN_RX)
1792 tp->cp_cmd |= RxVlan;
1793 else
1794 tp->cp_cmd &= ~RxVlan;
1795
1796 RTL_W16(CPlusCmd, tp->cp_cmd);
1797 RTL_R16(CPlusCmd);
1798 }
1799 if (changed & NETIF_F_RXALL) {
1800 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1801 if (features & NETIF_F_RXALL)
1802 tmp |= (AcceptErr | AcceptRunt);
1803 RTL_W32(RxConfig, tmp);
1804 }
Francois Romieuda78dbf2012-01-26 14:18:23 +01001805}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Francois Romieuda78dbf2012-01-26 14:18:23 +01001807static int rtl8169_set_features(struct net_device *dev,
1808 netdev_features_t features)
1809{
1810 struct rtl8169_private *tp = netdev_priv(dev);
1811
1812 rtl_lock_work(tp);
1813 __rtl8169_set_features(dev, features);
1814 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 return 0;
1817}
1818
Francois Romieuda78dbf2012-01-26 14:18:23 +01001819
Kirill Smelkov810f4892012-11-10 21:11:02 +04001820static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Jesse Grosseab6d182010-10-20 13:56:03 +00001822 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1824}
1825
Francois Romieu7a8fc772011-03-01 17:18:33 +01001826static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
1828 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Francois Romieu7a8fc772011-03-01 17:18:33 +01001830 if (opts2 & RxVlanTag)
1831 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832}
1833
Francois Romieuccdffb92008-07-26 14:26:06 +02001834static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
1836 struct rtl8169_private *tp = netdev_priv(dev);
1837 void __iomem *ioaddr = tp->mmio_addr;
1838 u32 status;
1839
1840 cmd->supported =
1841 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1842 cmd->port = PORT_FIBRE;
1843 cmd->transceiver = XCVR_INTERNAL;
1844
1845 status = RTL_R32(TBICSR);
1846 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1847 cmd->autoneg = !!(status & TBINwEnable);
1848
David Decotigny70739492011-04-27 18:32:40 +00001849 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001851
1852 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853}
1854
Francois Romieuccdffb92008-07-26 14:26:06 +02001855static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Francois Romieuccdffb92008-07-26 14:26:06 +02001859 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860}
1861
1862static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1863{
1864 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001865 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Francois Romieuda78dbf2012-01-26 14:18:23 +01001867 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02001868 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001869 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Francois Romieuccdffb92008-07-26 14:26:06 +02001871 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872}
1873
1874static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1875 void *p)
1876{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001877 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Francois Romieu5b0384f2006-08-16 16:00:01 +02001879 if (regs->len > R8169_REGS_SIZE)
1880 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Francois Romieuda78dbf2012-01-26 14:18:23 +01001882 rtl_lock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001883 memcpy_fromio(p, tp->mmio_addr, regs->len);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001884 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001887static u32 rtl8169_get_msglevel(struct net_device *dev)
1888{
1889 struct rtl8169_private *tp = netdev_priv(dev);
1890
1891 return tp->msg_enable;
1892}
1893
1894static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1895{
1896 struct rtl8169_private *tp = netdev_priv(dev);
1897
1898 tp->msg_enable = value;
1899}
1900
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001901static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1902 "tx_packets",
1903 "rx_packets",
1904 "tx_errors",
1905 "rx_errors",
1906 "rx_missed",
1907 "align_errors",
1908 "tx_single_collisions",
1909 "tx_multi_collisions",
1910 "unicast",
1911 "broadcast",
1912 "multicast",
1913 "tx_aborted",
1914 "tx_underrun",
1915};
1916
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001917static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001918{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001919 switch (sset) {
1920 case ETH_SS_STATS:
1921 return ARRAY_SIZE(rtl8169_gstrings);
1922 default:
1923 return -EOPNOTSUPP;
1924 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001925}
1926
Francois Romieuffc46952012-07-06 14:19:23 +02001927DECLARE_RTL_COND(rtl_counters_cond)
1928{
1929 void __iomem *ioaddr = tp->mmio_addr;
1930
1931 return RTL_R32(CounterAddrLow) & CounterDump;
1932}
1933
Ivan Vecera355423d2009-02-06 21:49:57 -08001934static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001935{
1936 struct rtl8169_private *tp = netdev_priv(dev);
1937 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001938 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001939 struct rtl8169_counters *counters;
1940 dma_addr_t paddr;
1941 u32 cmd;
1942
Ivan Vecera355423d2009-02-06 21:49:57 -08001943 /*
1944 * Some chips are unable to dump tally counters when the receiver
1945 * is disabled.
1946 */
1947 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1948 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001949
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001950 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001951 if (!counters)
1952 return;
1953
1954 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001955 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001956 RTL_W32(CounterAddrLow, cmd);
1957 RTL_W32(CounterAddrLow, cmd | CounterDump);
1958
Francois Romieuffc46952012-07-06 14:19:23 +02001959 if (rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000))
1960 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001961
1962 RTL_W32(CounterAddrLow, 0);
1963 RTL_W32(CounterAddrHigh, 0);
1964
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001965 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001966}
1967
Ivan Vecera355423d2009-02-06 21:49:57 -08001968static void rtl8169_get_ethtool_stats(struct net_device *dev,
1969 struct ethtool_stats *stats, u64 *data)
1970{
1971 struct rtl8169_private *tp = netdev_priv(dev);
1972
1973 ASSERT_RTNL();
1974
1975 rtl8169_update_counters(dev);
1976
1977 data[0] = le64_to_cpu(tp->counters.tx_packets);
1978 data[1] = le64_to_cpu(tp->counters.rx_packets);
1979 data[2] = le64_to_cpu(tp->counters.tx_errors);
1980 data[3] = le32_to_cpu(tp->counters.rx_errors);
1981 data[4] = le16_to_cpu(tp->counters.rx_missed);
1982 data[5] = le16_to_cpu(tp->counters.align_errors);
1983 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1984 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1985 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1986 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1987 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1988 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1989 data[12] = le16_to_cpu(tp->counters.tx_underun);
1990}
1991
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001992static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1993{
1994 switch(stringset) {
1995 case ETH_SS_STATS:
1996 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1997 break;
1998 }
1999}
2000
Jeff Garzik7282d492006-09-13 14:30:00 -04002001static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 .get_drvinfo = rtl8169_get_drvinfo,
2003 .get_regs_len = rtl8169_get_regs_len,
2004 .get_link = ethtool_op_get_link,
2005 .get_settings = rtl8169_get_settings,
2006 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002007 .get_msglevel = rtl8169_get_msglevel,
2008 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002010 .get_wol = rtl8169_get_wol,
2011 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002012 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002013 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002014 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002015 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016};
2017
Francois Romieu07d3f512007-02-21 22:40:46 +01002018static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02002019 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
Francois Romieu5d320a22011-05-08 17:47:36 +02002021 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01002022 /*
2023 * The driver currently handles the 8168Bf and the 8168Be identically
2024 * but they can be identified more specifically through the test below
2025 * if needed:
2026 *
2027 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002028 *
2029 * Same thing for the 8101Eb and the 8101Ec:
2030 *
2031 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002032 */
Francois Romieu37441002011-06-17 22:58:54 +02002033 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002035 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 int mac_version;
2037 } mac_info[] = {
Hayes Wangc5583862012-07-02 17:23:22 +08002038 /* 8168G family. */
2039 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2040 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2041
Hayes Wangc2218922011-09-06 16:55:18 +08002042 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002043 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002044 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2045 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2046
hayeswang01dc7fe2011-03-21 01:50:28 +00002047 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002048 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002049 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2050 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2051 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2052
Francois Romieu5b538df2008-07-20 16:22:45 +02002053 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002054 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2055 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002056 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002057
françois romieue6de30d2011-01-03 15:08:37 +00002058 /* 8168DP family. */
2059 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2060 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002061 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002062
Francois Romieuef808d52008-06-29 13:10:54 +02002063 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07002064 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02002065 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002066 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002067 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002068 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2069 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002070 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02002071 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02002072 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002073
2074 /* 8168B family. */
2075 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2076 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2077 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2078 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2079
2080 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002081 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2082 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002083 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00002084 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002085 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2086 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2087 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002088 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2089 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2090 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2091 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2092 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2093 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002094 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002095 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002096 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002097 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2098 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002099 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2100 /* FIXME: where did these entries come from ? -- FR */
2101 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2102 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2103
2104 /* 8110 family. */
2105 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2106 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2107 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2108 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2109 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2110 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2111
Jean Delvaref21b75e2009-05-26 20:54:48 -07002112 /* Catch-all */
2113 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002114 };
2115 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 u32 reg;
2117
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002118 reg = RTL_R32(TxConfig);
2119 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 p++;
2121 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002122
2123 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2124 netif_notice(tp, probe, dev,
2125 "unknown MAC, using family default\n");
2126 tp->mac_version = default_version;
2127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
2129
2130static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2131{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002132 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133}
2134
Francois Romieu867763c2007-08-17 18:21:58 +02002135struct phy_reg {
2136 u16 reg;
2137 u16 val;
2138};
2139
françois romieu4da19632011-01-03 15:07:55 +00002140static void rtl_writephy_batch(struct rtl8169_private *tp,
2141 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002142{
2143 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002144 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002145 regs++;
2146 }
2147}
2148
françois romieubca03d52011-01-03 15:07:31 +00002149#define PHY_READ 0x00000000
2150#define PHY_DATA_OR 0x10000000
2151#define PHY_DATA_AND 0x20000000
2152#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002153#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002154#define PHY_CLEAR_READCOUNT 0x70000000
2155#define PHY_WRITE 0x80000000
2156#define PHY_READCOUNT_EQ_SKIP 0x90000000
2157#define PHY_COMP_EQ_SKIPN 0xa0000000
2158#define PHY_COMP_NEQ_SKIPN 0xb0000000
2159#define PHY_WRITE_PREVIOUS 0xc0000000
2160#define PHY_SKIPN 0xd0000000
2161#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002162
Hayes Wang960aee62011-06-18 11:37:48 +02002163struct fw_info {
2164 u32 magic;
2165 char version[RTL_VER_SIZE];
2166 __le32 fw_start;
2167 __le32 fw_len;
2168 u8 chksum;
2169} __packed;
2170
Francois Romieu1c361ef2011-06-17 17:16:24 +02002171#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2172
2173static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002174{
Francois Romieub6ffd972011-06-17 17:00:05 +02002175 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002176 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002177 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2178 char *version = rtl_fw->version;
2179 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002180
Francois Romieu1c361ef2011-06-17 17:16:24 +02002181 if (fw->size < FW_OPCODE_SIZE)
2182 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002183
2184 if (!fw_info->magic) {
2185 size_t i, size, start;
2186 u8 checksum = 0;
2187
2188 if (fw->size < sizeof(*fw_info))
2189 goto out;
2190
2191 for (i = 0; i < fw->size; i++)
2192 checksum += fw->data[i];
2193 if (checksum != 0)
2194 goto out;
2195
2196 start = le32_to_cpu(fw_info->fw_start);
2197 if (start > fw->size)
2198 goto out;
2199
2200 size = le32_to_cpu(fw_info->fw_len);
2201 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2202 goto out;
2203
2204 memcpy(version, fw_info->version, RTL_VER_SIZE);
2205
2206 pa->code = (__le32 *)(fw->data + start);
2207 pa->size = size;
2208 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002209 if (fw->size % FW_OPCODE_SIZE)
2210 goto out;
2211
2212 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2213
2214 pa->code = (__le32 *)fw->data;
2215 pa->size = fw->size / FW_OPCODE_SIZE;
2216 }
2217 version[RTL_VER_SIZE - 1] = 0;
2218
2219 rc = true;
2220out:
2221 return rc;
2222}
2223
Francois Romieufd112f22011-06-18 00:10:29 +02002224static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2225 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002226{
Francois Romieufd112f22011-06-18 00:10:29 +02002227 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002228 size_t index;
2229
Francois Romieu1c361ef2011-06-17 17:16:24 +02002230 for (index = 0; index < pa->size; index++) {
2231 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002232 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002233
hayeswang42b82dc2011-01-10 02:07:25 +00002234 switch(action & 0xf0000000) {
2235 case PHY_READ:
2236 case PHY_DATA_OR:
2237 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002238 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002239 case PHY_CLEAR_READCOUNT:
2240 case PHY_WRITE:
2241 case PHY_WRITE_PREVIOUS:
2242 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002243 break;
2244
hayeswang42b82dc2011-01-10 02:07:25 +00002245 case PHY_BJMPN:
2246 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002247 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002248 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002249 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002250 }
2251 break;
2252 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002253 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002254 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002255 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002256 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002257 }
2258 break;
2259 case PHY_COMP_EQ_SKIPN:
2260 case PHY_COMP_NEQ_SKIPN:
2261 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002262 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002263 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002264 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002265 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002266 }
2267 break;
2268
hayeswang42b82dc2011-01-10 02:07:25 +00002269 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002270 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002271 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002272 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002273 }
2274 }
Francois Romieufd112f22011-06-18 00:10:29 +02002275 rc = true;
2276out:
2277 return rc;
2278}
françois romieubca03d52011-01-03 15:07:31 +00002279
Francois Romieufd112f22011-06-18 00:10:29 +02002280static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2281{
2282 struct net_device *dev = tp->dev;
2283 int rc = -EINVAL;
2284
2285 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2286 netif_err(tp, ifup, dev, "invalid firwmare\n");
2287 goto out;
2288 }
2289
2290 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2291 rc = 0;
2292out:
2293 return rc;
2294}
2295
2296static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2297{
2298 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002299 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002300 u32 predata, count;
2301 size_t index;
2302
2303 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002304 org.write = ops->write;
2305 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002306
Francois Romieu1c361ef2011-06-17 17:16:24 +02002307 for (index = 0; index < pa->size; ) {
2308 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002309 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002310 u32 regno = (action & 0x0fff0000) >> 16;
2311
2312 if (!action)
2313 break;
françois romieubca03d52011-01-03 15:07:31 +00002314
2315 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002316 case PHY_READ:
2317 predata = rtl_readphy(tp, regno);
2318 count++;
2319 index++;
françois romieubca03d52011-01-03 15:07:31 +00002320 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002321 case PHY_DATA_OR:
2322 predata |= data;
2323 index++;
2324 break;
2325 case PHY_DATA_AND:
2326 predata &= data;
2327 index++;
2328 break;
2329 case PHY_BJMPN:
2330 index -= regno;
2331 break;
hayeswangeee37862013-04-01 22:23:38 +00002332 case PHY_MDIO_CHG:
2333 if (data == 0) {
2334 ops->write = org.write;
2335 ops->read = org.read;
2336 } else if (data == 1) {
2337 ops->write = mac_mcu_write;
2338 ops->read = mac_mcu_read;
2339 }
2340
hayeswang42b82dc2011-01-10 02:07:25 +00002341 index++;
2342 break;
2343 case PHY_CLEAR_READCOUNT:
2344 count = 0;
2345 index++;
2346 break;
2347 case PHY_WRITE:
2348 rtl_writephy(tp, regno, data);
2349 index++;
2350 break;
2351 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002352 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002353 break;
2354 case PHY_COMP_EQ_SKIPN:
2355 if (predata == data)
2356 index += regno;
2357 index++;
2358 break;
2359 case PHY_COMP_NEQ_SKIPN:
2360 if (predata != data)
2361 index += regno;
2362 index++;
2363 break;
2364 case PHY_WRITE_PREVIOUS:
2365 rtl_writephy(tp, regno, predata);
2366 index++;
2367 break;
2368 case PHY_SKIPN:
2369 index += regno + 1;
2370 break;
2371 case PHY_DELAY_MS:
2372 mdelay(data);
2373 index++;
2374 break;
2375
françois romieubca03d52011-01-03 15:07:31 +00002376 default:
2377 BUG();
2378 }
2379 }
hayeswangeee37862013-04-01 22:23:38 +00002380
2381 ops->write = org.write;
2382 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002383}
2384
françois romieuf1e02ed2011-01-13 13:07:53 +00002385static void rtl_release_firmware(struct rtl8169_private *tp)
2386{
Francois Romieub6ffd972011-06-17 17:00:05 +02002387 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2388 release_firmware(tp->rtl_fw->fw);
2389 kfree(tp->rtl_fw);
2390 }
2391 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002392}
2393
François Romieu953a12c2011-04-24 17:38:48 +02002394static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002395{
Francois Romieub6ffd972011-06-17 17:00:05 +02002396 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002397
2398 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002399 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002400 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002401}
2402
2403static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2404{
2405 if (rtl_readphy(tp, reg) != val)
2406 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2407 else
2408 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002409}
2410
françois romieu4da19632011-01-03 15:07:55 +00002411static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002413 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002414 { 0x1f, 0x0001 },
2415 { 0x06, 0x006e },
2416 { 0x08, 0x0708 },
2417 { 0x15, 0x4000 },
2418 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
françois romieu0b9b5712009-08-10 19:44:56 +00002420 { 0x1f, 0x0001 },
2421 { 0x03, 0x00a1 },
2422 { 0x02, 0x0008 },
2423 { 0x01, 0x0120 },
2424 { 0x00, 0x1000 },
2425 { 0x04, 0x0800 },
2426 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
françois romieu0b9b5712009-08-10 19:44:56 +00002428 { 0x03, 0xff41 },
2429 { 0x02, 0xdf60 },
2430 { 0x01, 0x0140 },
2431 { 0x00, 0x0077 },
2432 { 0x04, 0x7800 },
2433 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
françois romieu0b9b5712009-08-10 19:44:56 +00002435 { 0x03, 0x802f },
2436 { 0x02, 0x4f02 },
2437 { 0x01, 0x0409 },
2438 { 0x00, 0xf0f9 },
2439 { 0x04, 0x9800 },
2440 { 0x04, 0x9000 },
2441
2442 { 0x03, 0xdf01 },
2443 { 0x02, 0xdf20 },
2444 { 0x01, 0xff95 },
2445 { 0x00, 0xba00 },
2446 { 0x04, 0xa800 },
2447 { 0x04, 0xa000 },
2448
2449 { 0x03, 0xff41 },
2450 { 0x02, 0xdf20 },
2451 { 0x01, 0x0140 },
2452 { 0x00, 0x00bb },
2453 { 0x04, 0xb800 },
2454 { 0x04, 0xb000 },
2455
2456 { 0x03, 0xdf41 },
2457 { 0x02, 0xdc60 },
2458 { 0x01, 0x6340 },
2459 { 0x00, 0x007d },
2460 { 0x04, 0xd800 },
2461 { 0x04, 0xd000 },
2462
2463 { 0x03, 0xdf01 },
2464 { 0x02, 0xdf20 },
2465 { 0x01, 0x100a },
2466 { 0x00, 0xa0ff },
2467 { 0x04, 0xf800 },
2468 { 0x04, 0xf000 },
2469
2470 { 0x1f, 0x0000 },
2471 { 0x0b, 0x0000 },
2472 { 0x00, 0x9200 }
2473 };
2474
françois romieu4da19632011-01-03 15:07:55 +00002475 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476}
2477
françois romieu4da19632011-01-03 15:07:55 +00002478static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002479{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002480 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002481 { 0x1f, 0x0002 },
2482 { 0x01, 0x90d0 },
2483 { 0x1f, 0x0000 }
2484 };
2485
françois romieu4da19632011-01-03 15:07:55 +00002486 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002487}
2488
françois romieu4da19632011-01-03 15:07:55 +00002489static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002490{
2491 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002492
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002493 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2494 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002495 return;
2496
françois romieu4da19632011-01-03 15:07:55 +00002497 rtl_writephy(tp, 0x1f, 0x0001);
2498 rtl_writephy(tp, 0x10, 0xf01b);
2499 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002500}
2501
françois romieu4da19632011-01-03 15:07:55 +00002502static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002503{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002504 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002505 { 0x1f, 0x0001 },
2506 { 0x04, 0x0000 },
2507 { 0x03, 0x00a1 },
2508 { 0x02, 0x0008 },
2509 { 0x01, 0x0120 },
2510 { 0x00, 0x1000 },
2511 { 0x04, 0x0800 },
2512 { 0x04, 0x9000 },
2513 { 0x03, 0x802f },
2514 { 0x02, 0x4f02 },
2515 { 0x01, 0x0409 },
2516 { 0x00, 0xf099 },
2517 { 0x04, 0x9800 },
2518 { 0x04, 0xa000 },
2519 { 0x03, 0xdf01 },
2520 { 0x02, 0xdf20 },
2521 { 0x01, 0xff95 },
2522 { 0x00, 0xba00 },
2523 { 0x04, 0xa800 },
2524 { 0x04, 0xf000 },
2525 { 0x03, 0xdf01 },
2526 { 0x02, 0xdf20 },
2527 { 0x01, 0x101a },
2528 { 0x00, 0xa0ff },
2529 { 0x04, 0xf800 },
2530 { 0x04, 0x0000 },
2531 { 0x1f, 0x0000 },
2532
2533 { 0x1f, 0x0001 },
2534 { 0x10, 0xf41b },
2535 { 0x14, 0xfb54 },
2536 { 0x18, 0xf5c7 },
2537 { 0x1f, 0x0000 },
2538
2539 { 0x1f, 0x0001 },
2540 { 0x17, 0x0cc0 },
2541 { 0x1f, 0x0000 }
2542 };
2543
françois romieu4da19632011-01-03 15:07:55 +00002544 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002545
françois romieu4da19632011-01-03 15:07:55 +00002546 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002547}
2548
françois romieu4da19632011-01-03 15:07:55 +00002549static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002550{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002551 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002552 { 0x1f, 0x0001 },
2553 { 0x04, 0x0000 },
2554 { 0x03, 0x00a1 },
2555 { 0x02, 0x0008 },
2556 { 0x01, 0x0120 },
2557 { 0x00, 0x1000 },
2558 { 0x04, 0x0800 },
2559 { 0x04, 0x9000 },
2560 { 0x03, 0x802f },
2561 { 0x02, 0x4f02 },
2562 { 0x01, 0x0409 },
2563 { 0x00, 0xf099 },
2564 { 0x04, 0x9800 },
2565 { 0x04, 0xa000 },
2566 { 0x03, 0xdf01 },
2567 { 0x02, 0xdf20 },
2568 { 0x01, 0xff95 },
2569 { 0x00, 0xba00 },
2570 { 0x04, 0xa800 },
2571 { 0x04, 0xf000 },
2572 { 0x03, 0xdf01 },
2573 { 0x02, 0xdf20 },
2574 { 0x01, 0x101a },
2575 { 0x00, 0xa0ff },
2576 { 0x04, 0xf800 },
2577 { 0x04, 0x0000 },
2578 { 0x1f, 0x0000 },
2579
2580 { 0x1f, 0x0001 },
2581 { 0x0b, 0x8480 },
2582 { 0x1f, 0x0000 },
2583
2584 { 0x1f, 0x0001 },
2585 { 0x18, 0x67c7 },
2586 { 0x04, 0x2000 },
2587 { 0x03, 0x002f },
2588 { 0x02, 0x4360 },
2589 { 0x01, 0x0109 },
2590 { 0x00, 0x3022 },
2591 { 0x04, 0x2800 },
2592 { 0x1f, 0x0000 },
2593
2594 { 0x1f, 0x0001 },
2595 { 0x17, 0x0cc0 },
2596 { 0x1f, 0x0000 }
2597 };
2598
françois romieu4da19632011-01-03 15:07:55 +00002599 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002600}
2601
françois romieu4da19632011-01-03 15:07:55 +00002602static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002603{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002604 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002605 { 0x10, 0xf41b },
2606 { 0x1f, 0x0000 }
2607 };
2608
françois romieu4da19632011-01-03 15:07:55 +00002609 rtl_writephy(tp, 0x1f, 0x0001);
2610 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002611
françois romieu4da19632011-01-03 15:07:55 +00002612 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002613}
2614
françois romieu4da19632011-01-03 15:07:55 +00002615static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002616{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002617 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002618 { 0x1f, 0x0001 },
2619 { 0x10, 0xf41b },
2620 { 0x1f, 0x0000 }
2621 };
2622
françois romieu4da19632011-01-03 15:07:55 +00002623 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002624}
2625
françois romieu4da19632011-01-03 15:07:55 +00002626static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002627{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002628 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002629 { 0x1f, 0x0000 },
2630 { 0x1d, 0x0f00 },
2631 { 0x1f, 0x0002 },
2632 { 0x0c, 0x1ec8 },
2633 { 0x1f, 0x0000 }
2634 };
2635
françois romieu4da19632011-01-03 15:07:55 +00002636 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002637}
2638
françois romieu4da19632011-01-03 15:07:55 +00002639static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002640{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002641 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002642 { 0x1f, 0x0001 },
2643 { 0x1d, 0x3d98 },
2644 { 0x1f, 0x0000 }
2645 };
2646
françois romieu4da19632011-01-03 15:07:55 +00002647 rtl_writephy(tp, 0x1f, 0x0000);
2648 rtl_patchphy(tp, 0x14, 1 << 5);
2649 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002650
françois romieu4da19632011-01-03 15:07:55 +00002651 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002652}
2653
françois romieu4da19632011-01-03 15:07:55 +00002654static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002655{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002656 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002657 { 0x1f, 0x0001 },
2658 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002659 { 0x1f, 0x0002 },
2660 { 0x00, 0x88d4 },
2661 { 0x01, 0x82b1 },
2662 { 0x03, 0x7002 },
2663 { 0x08, 0x9e30 },
2664 { 0x09, 0x01f0 },
2665 { 0x0a, 0x5500 },
2666 { 0x0c, 0x00c8 },
2667 { 0x1f, 0x0003 },
2668 { 0x12, 0xc096 },
2669 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002670 { 0x1f, 0x0000 },
2671 { 0x1f, 0x0000 },
2672 { 0x09, 0x2000 },
2673 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002674 };
2675
françois romieu4da19632011-01-03 15:07:55 +00002676 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002677
françois romieu4da19632011-01-03 15:07:55 +00002678 rtl_patchphy(tp, 0x14, 1 << 5);
2679 rtl_patchphy(tp, 0x0d, 1 << 5);
2680 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002681}
2682
françois romieu4da19632011-01-03 15:07:55 +00002683static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002684{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002685 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002686 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002687 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002688 { 0x03, 0x802f },
2689 { 0x02, 0x4f02 },
2690 { 0x01, 0x0409 },
2691 { 0x00, 0xf099 },
2692 { 0x04, 0x9800 },
2693 { 0x04, 0x9000 },
2694 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002695 { 0x1f, 0x0002 },
2696 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002697 { 0x06, 0x0761 },
2698 { 0x1f, 0x0003 },
2699 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002700 { 0x1f, 0x0000 }
2701 };
2702
françois romieu4da19632011-01-03 15:07:55 +00002703 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002704
françois romieu4da19632011-01-03 15:07:55 +00002705 rtl_patchphy(tp, 0x16, 1 << 0);
2706 rtl_patchphy(tp, 0x14, 1 << 5);
2707 rtl_patchphy(tp, 0x0d, 1 << 5);
2708 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002709}
2710
françois romieu4da19632011-01-03 15:07:55 +00002711static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002712{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002713 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002714 { 0x1f, 0x0001 },
2715 { 0x12, 0x2300 },
2716 { 0x1d, 0x3d98 },
2717 { 0x1f, 0x0002 },
2718 { 0x0c, 0x7eb8 },
2719 { 0x06, 0x5461 },
2720 { 0x1f, 0x0003 },
2721 { 0x16, 0x0f0a },
2722 { 0x1f, 0x0000 }
2723 };
2724
françois romieu4da19632011-01-03 15:07:55 +00002725 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002726
françois romieu4da19632011-01-03 15:07:55 +00002727 rtl_patchphy(tp, 0x16, 1 << 0);
2728 rtl_patchphy(tp, 0x14, 1 << 5);
2729 rtl_patchphy(tp, 0x0d, 1 << 5);
2730 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002731}
2732
françois romieu4da19632011-01-03 15:07:55 +00002733static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002734{
françois romieu4da19632011-01-03 15:07:55 +00002735 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002736}
2737
françois romieubca03d52011-01-03 15:07:31 +00002738static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002739{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002740 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002741 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002742 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002743 { 0x06, 0x4064 },
2744 { 0x07, 0x2863 },
2745 { 0x08, 0x059c },
2746 { 0x09, 0x26b4 },
2747 { 0x0a, 0x6a19 },
2748 { 0x0b, 0xdcc8 },
2749 { 0x10, 0xf06d },
2750 { 0x14, 0x7f68 },
2751 { 0x18, 0x7fd9 },
2752 { 0x1c, 0xf0ff },
2753 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002754 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002755 { 0x12, 0xf49f },
2756 { 0x13, 0x070b },
2757 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002758 { 0x14, 0x94c0 },
2759
2760 /*
2761 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002762 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002763 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002764 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002765 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002766 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002767 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002768 { 0x06, 0x5561 },
2769
2770 /*
2771 * Can not link to 1Gbps with bad cable
2772 * Decrease SNR threshold form 21.07dB to 19.04dB
2773 */
2774 { 0x1f, 0x0001 },
2775 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002776
2777 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002778 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002779 };
2780
françois romieu4da19632011-01-03 15:07:55 +00002781 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002782
françois romieubca03d52011-01-03 15:07:31 +00002783 /*
2784 * Rx Error Issue
2785 * Fine Tune Switching regulator parameter
2786 */
françois romieu4da19632011-01-03 15:07:55 +00002787 rtl_writephy(tp, 0x1f, 0x0002);
2788 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2789 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002790
Francois Romieufdf6fc02012-07-06 22:40:38 +02002791 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002792 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002793 { 0x1f, 0x0002 },
2794 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002795 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002796 { 0x05, 0x8330 },
2797 { 0x06, 0x669a },
2798 { 0x1f, 0x0002 }
2799 };
2800 int val;
2801
françois romieu4da19632011-01-03 15:07:55 +00002802 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002803
françois romieu4da19632011-01-03 15:07:55 +00002804 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002805
2806 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002807 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002808 0x0065, 0x0066, 0x0067, 0x0068,
2809 0x0069, 0x006a, 0x006b, 0x006c
2810 };
2811 int i;
2812
françois romieu4da19632011-01-03 15:07:55 +00002813 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002814
2815 val &= 0xff00;
2816 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002817 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002818 }
2819 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002820 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002821 { 0x1f, 0x0002 },
2822 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002823 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002824 { 0x05, 0x8330 },
2825 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002826 };
2827
françois romieu4da19632011-01-03 15:07:55 +00002828 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002829 }
2830
françois romieubca03d52011-01-03 15:07:31 +00002831 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002832 rtl_writephy(tp, 0x1f, 0x0002);
2833 rtl_patchphy(tp, 0x0d, 0x0300);
2834 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002835
françois romieubca03d52011-01-03 15:07:31 +00002836 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002837 rtl_writephy(tp, 0x1f, 0x0002);
2838 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2839 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002840
françois romieu4da19632011-01-03 15:07:55 +00002841 rtl_writephy(tp, 0x1f, 0x0005);
2842 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002843
2844 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002845
françois romieu4da19632011-01-03 15:07:55 +00002846 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002847}
2848
françois romieubca03d52011-01-03 15:07:31 +00002849static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002850{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002851 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002852 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002853 { 0x1f, 0x0001 },
2854 { 0x06, 0x4064 },
2855 { 0x07, 0x2863 },
2856 { 0x08, 0x059c },
2857 { 0x09, 0x26b4 },
2858 { 0x0a, 0x6a19 },
2859 { 0x0b, 0xdcc8 },
2860 { 0x10, 0xf06d },
2861 { 0x14, 0x7f68 },
2862 { 0x18, 0x7fd9 },
2863 { 0x1c, 0xf0ff },
2864 { 0x1d, 0x3d9c },
2865 { 0x1f, 0x0003 },
2866 { 0x12, 0xf49f },
2867 { 0x13, 0x070b },
2868 { 0x1a, 0x05ad },
2869 { 0x14, 0x94c0 },
2870
françois romieubca03d52011-01-03 15:07:31 +00002871 /*
2872 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002873 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002874 */
françois romieudaf9df62009-10-07 12:44:20 +00002875 { 0x1f, 0x0002 },
2876 { 0x06, 0x5561 },
2877 { 0x1f, 0x0005 },
2878 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002879 { 0x06, 0x5561 },
2880
2881 /*
2882 * Can not link to 1Gbps with bad cable
2883 * Decrease SNR threshold form 21.07dB to 19.04dB
2884 */
2885 { 0x1f, 0x0001 },
2886 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002887
2888 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002889 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002890 };
2891
françois romieu4da19632011-01-03 15:07:55 +00002892 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002893
Francois Romieufdf6fc02012-07-06 22:40:38 +02002894 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002895 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002896 { 0x1f, 0x0002 },
2897 { 0x05, 0x669a },
2898 { 0x1f, 0x0005 },
2899 { 0x05, 0x8330 },
2900 { 0x06, 0x669a },
2901
2902 { 0x1f, 0x0002 }
2903 };
2904 int val;
2905
françois romieu4da19632011-01-03 15:07:55 +00002906 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002907
françois romieu4da19632011-01-03 15:07:55 +00002908 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002909 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002910 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002911 0x0065, 0x0066, 0x0067, 0x0068,
2912 0x0069, 0x006a, 0x006b, 0x006c
2913 };
2914 int i;
2915
françois romieu4da19632011-01-03 15:07:55 +00002916 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002917
2918 val &= 0xff00;
2919 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002920 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002921 }
2922 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002923 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002924 { 0x1f, 0x0002 },
2925 { 0x05, 0x2642 },
2926 { 0x1f, 0x0005 },
2927 { 0x05, 0x8330 },
2928 { 0x06, 0x2642 }
2929 };
2930
françois romieu4da19632011-01-03 15:07:55 +00002931 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002932 }
2933
françois romieubca03d52011-01-03 15:07:31 +00002934 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002935 rtl_writephy(tp, 0x1f, 0x0002);
2936 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2937 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002938
françois romieubca03d52011-01-03 15:07:31 +00002939 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002940 rtl_writephy(tp, 0x1f, 0x0002);
2941 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002942
françois romieu4da19632011-01-03 15:07:55 +00002943 rtl_writephy(tp, 0x1f, 0x0005);
2944 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002945
2946 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002947
françois romieu4da19632011-01-03 15:07:55 +00002948 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002949}
2950
françois romieu4da19632011-01-03 15:07:55 +00002951static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002952{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002953 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002954 { 0x1f, 0x0002 },
2955 { 0x10, 0x0008 },
2956 { 0x0d, 0x006c },
2957
2958 { 0x1f, 0x0000 },
2959 { 0x0d, 0xf880 },
2960
2961 { 0x1f, 0x0001 },
2962 { 0x17, 0x0cc0 },
2963
2964 { 0x1f, 0x0001 },
2965 { 0x0b, 0xa4d8 },
2966 { 0x09, 0x281c },
2967 { 0x07, 0x2883 },
2968 { 0x0a, 0x6b35 },
2969 { 0x1d, 0x3da4 },
2970 { 0x1c, 0xeffd },
2971 { 0x14, 0x7f52 },
2972 { 0x18, 0x7fc6 },
2973 { 0x08, 0x0601 },
2974 { 0x06, 0x4063 },
2975 { 0x10, 0xf074 },
2976 { 0x1f, 0x0003 },
2977 { 0x13, 0x0789 },
2978 { 0x12, 0xf4bd },
2979 { 0x1a, 0x04fd },
2980 { 0x14, 0x84b0 },
2981 { 0x1f, 0x0000 },
2982 { 0x00, 0x9200 },
2983
2984 { 0x1f, 0x0005 },
2985 { 0x01, 0x0340 },
2986 { 0x1f, 0x0001 },
2987 { 0x04, 0x4000 },
2988 { 0x03, 0x1d21 },
2989 { 0x02, 0x0c32 },
2990 { 0x01, 0x0200 },
2991 { 0x00, 0x5554 },
2992 { 0x04, 0x4800 },
2993 { 0x04, 0x4000 },
2994 { 0x04, 0xf000 },
2995 { 0x03, 0xdf01 },
2996 { 0x02, 0xdf20 },
2997 { 0x01, 0x101a },
2998 { 0x00, 0xa0ff },
2999 { 0x04, 0xf800 },
3000 { 0x04, 0xf000 },
3001 { 0x1f, 0x0000 },
3002
3003 { 0x1f, 0x0007 },
3004 { 0x1e, 0x0023 },
3005 { 0x16, 0x0000 },
3006 { 0x1f, 0x0000 }
3007 };
3008
françois romieu4da19632011-01-03 15:07:55 +00003009 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003010}
3011
françois romieue6de30d2011-01-03 15:08:37 +00003012static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3013{
3014 static const struct phy_reg phy_reg_init[] = {
3015 { 0x1f, 0x0001 },
3016 { 0x17, 0x0cc0 },
3017
3018 { 0x1f, 0x0007 },
3019 { 0x1e, 0x002d },
3020 { 0x18, 0x0040 },
3021 { 0x1f, 0x0000 }
3022 };
3023
3024 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3025 rtl_patchphy(tp, 0x0d, 1 << 5);
3026}
3027
Hayes Wang70090422011-07-06 15:58:06 +08003028static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003029{
3030 static const struct phy_reg phy_reg_init[] = {
3031 /* Enable Delay cap */
3032 { 0x1f, 0x0005 },
3033 { 0x05, 0x8b80 },
3034 { 0x06, 0xc896 },
3035 { 0x1f, 0x0000 },
3036
3037 /* Channel estimation fine tune */
3038 { 0x1f, 0x0001 },
3039 { 0x0b, 0x6c20 },
3040 { 0x07, 0x2872 },
3041 { 0x1c, 0xefff },
3042 { 0x1f, 0x0003 },
3043 { 0x14, 0x6420 },
3044 { 0x1f, 0x0000 },
3045
3046 /* Update PFM & 10M TX idle timer */
3047 { 0x1f, 0x0007 },
3048 { 0x1e, 0x002f },
3049 { 0x15, 0x1919 },
3050 { 0x1f, 0x0000 },
3051
3052 { 0x1f, 0x0007 },
3053 { 0x1e, 0x00ac },
3054 { 0x18, 0x0006 },
3055 { 0x1f, 0x0000 }
3056 };
3057
Francois Romieu15ecd032011-04-27 13:52:22 -07003058 rtl_apply_firmware(tp);
3059
hayeswang01dc7fe2011-03-21 01:50:28 +00003060 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3061
3062 /* DCO enable for 10M IDLE Power */
3063 rtl_writephy(tp, 0x1f, 0x0007);
3064 rtl_writephy(tp, 0x1e, 0x0023);
3065 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
3066 rtl_writephy(tp, 0x1f, 0x0000);
3067
3068 /* For impedance matching */
3069 rtl_writephy(tp, 0x1f, 0x0002);
3070 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003071 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003072
3073 /* PHY auto speed down */
3074 rtl_writephy(tp, 0x1f, 0x0007);
3075 rtl_writephy(tp, 0x1e, 0x002d);
3076 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
3077 rtl_writephy(tp, 0x1f, 0x0000);
3078 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3079
3080 rtl_writephy(tp, 0x1f, 0x0005);
3081 rtl_writephy(tp, 0x05, 0x8b86);
3082 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3083 rtl_writephy(tp, 0x1f, 0x0000);
3084
3085 rtl_writephy(tp, 0x1f, 0x0005);
3086 rtl_writephy(tp, 0x05, 0x8b85);
3087 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3088 rtl_writephy(tp, 0x1f, 0x0007);
3089 rtl_writephy(tp, 0x1e, 0x0020);
3090 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
3091 rtl_writephy(tp, 0x1f, 0x0006);
3092 rtl_writephy(tp, 0x00, 0x5a00);
3093 rtl_writephy(tp, 0x1f, 0x0000);
3094 rtl_writephy(tp, 0x0d, 0x0007);
3095 rtl_writephy(tp, 0x0e, 0x003c);
3096 rtl_writephy(tp, 0x0d, 0x4007);
3097 rtl_writephy(tp, 0x0e, 0x0000);
3098 rtl_writephy(tp, 0x0d, 0x0000);
3099}
3100
françois romieu9ecb9aa2012-12-07 11:20:21 +00003101static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3102{
3103 const u16 w[] = {
3104 addr[0] | (addr[1] << 8),
3105 addr[2] | (addr[3] << 8),
3106 addr[4] | (addr[5] << 8)
3107 };
3108 const struct exgmac_reg e[] = {
3109 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3110 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3111 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3112 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3113 };
3114
3115 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3116}
3117
Hayes Wang70090422011-07-06 15:58:06 +08003118static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3119{
3120 static const struct phy_reg phy_reg_init[] = {
3121 /* Enable Delay cap */
3122 { 0x1f, 0x0004 },
3123 { 0x1f, 0x0007 },
3124 { 0x1e, 0x00ac },
3125 { 0x18, 0x0006 },
3126 { 0x1f, 0x0002 },
3127 { 0x1f, 0x0000 },
3128 { 0x1f, 0x0000 },
3129
3130 /* Channel estimation fine tune */
3131 { 0x1f, 0x0003 },
3132 { 0x09, 0xa20f },
3133 { 0x1f, 0x0000 },
3134 { 0x1f, 0x0000 },
3135
3136 /* Green Setting */
3137 { 0x1f, 0x0005 },
3138 { 0x05, 0x8b5b },
3139 { 0x06, 0x9222 },
3140 { 0x05, 0x8b6d },
3141 { 0x06, 0x8000 },
3142 { 0x05, 0x8b76 },
3143 { 0x06, 0x8000 },
3144 { 0x1f, 0x0000 }
3145 };
3146
3147 rtl_apply_firmware(tp);
3148
3149 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3150
3151 /* For 4-corner performance improve */
3152 rtl_writephy(tp, 0x1f, 0x0005);
3153 rtl_writephy(tp, 0x05, 0x8b80);
3154 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
3155 rtl_writephy(tp, 0x1f, 0x0000);
3156
3157 /* PHY auto speed down */
3158 rtl_writephy(tp, 0x1f, 0x0004);
3159 rtl_writephy(tp, 0x1f, 0x0007);
3160 rtl_writephy(tp, 0x1e, 0x002d);
3161 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3162 rtl_writephy(tp, 0x1f, 0x0002);
3163 rtl_writephy(tp, 0x1f, 0x0000);
3164 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3165
3166 /* improve 10M EEE waveform */
3167 rtl_writephy(tp, 0x1f, 0x0005);
3168 rtl_writephy(tp, 0x05, 0x8b86);
3169 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3170 rtl_writephy(tp, 0x1f, 0x0000);
3171
3172 /* Improve 2-pair detection performance */
3173 rtl_writephy(tp, 0x1f, 0x0005);
3174 rtl_writephy(tp, 0x05, 0x8b85);
3175 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3176 rtl_writephy(tp, 0x1f, 0x0000);
3177
3178 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003179 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003180 rtl_writephy(tp, 0x1f, 0x0005);
3181 rtl_writephy(tp, 0x05, 0x8b85);
3182 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3183 rtl_writephy(tp, 0x1f, 0x0004);
3184 rtl_writephy(tp, 0x1f, 0x0007);
3185 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04003186 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003187 rtl_writephy(tp, 0x1f, 0x0002);
3188 rtl_writephy(tp, 0x1f, 0x0000);
3189 rtl_writephy(tp, 0x0d, 0x0007);
3190 rtl_writephy(tp, 0x0e, 0x003c);
3191 rtl_writephy(tp, 0x0d, 0x4007);
3192 rtl_writephy(tp, 0x0e, 0x0000);
3193 rtl_writephy(tp, 0x0d, 0x0000);
3194
3195 /* Green feature */
3196 rtl_writephy(tp, 0x1f, 0x0003);
3197 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3198 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3199 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003200
françois romieu9ecb9aa2012-12-07 11:20:21 +00003201 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3202 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003203}
3204
Hayes Wang5f886e02012-03-30 14:33:03 +08003205static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3206{
3207 /* For 4-corner performance improve */
3208 rtl_writephy(tp, 0x1f, 0x0005);
3209 rtl_writephy(tp, 0x05, 0x8b80);
3210 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3211 rtl_writephy(tp, 0x1f, 0x0000);
3212
3213 /* PHY auto speed down */
3214 rtl_writephy(tp, 0x1f, 0x0007);
3215 rtl_writephy(tp, 0x1e, 0x002d);
3216 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3217 rtl_writephy(tp, 0x1f, 0x0000);
3218 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3219
3220 /* Improve 10M EEE waveform */
3221 rtl_writephy(tp, 0x1f, 0x0005);
3222 rtl_writephy(tp, 0x05, 0x8b86);
3223 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3224 rtl_writephy(tp, 0x1f, 0x0000);
3225}
3226
Hayes Wangc2218922011-09-06 16:55:18 +08003227static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3228{
3229 static const struct phy_reg phy_reg_init[] = {
3230 /* Channel estimation fine tune */
3231 { 0x1f, 0x0003 },
3232 { 0x09, 0xa20f },
3233 { 0x1f, 0x0000 },
3234
3235 /* Modify green table for giga & fnet */
3236 { 0x1f, 0x0005 },
3237 { 0x05, 0x8b55 },
3238 { 0x06, 0x0000 },
3239 { 0x05, 0x8b5e },
3240 { 0x06, 0x0000 },
3241 { 0x05, 0x8b67 },
3242 { 0x06, 0x0000 },
3243 { 0x05, 0x8b70 },
3244 { 0x06, 0x0000 },
3245 { 0x1f, 0x0000 },
3246 { 0x1f, 0x0007 },
3247 { 0x1e, 0x0078 },
3248 { 0x17, 0x0000 },
3249 { 0x19, 0x00fb },
3250 { 0x1f, 0x0000 },
3251
3252 /* Modify green table for 10M */
3253 { 0x1f, 0x0005 },
3254 { 0x05, 0x8b79 },
3255 { 0x06, 0xaa00 },
3256 { 0x1f, 0x0000 },
3257
3258 /* Disable hiimpedance detection (RTCT) */
3259 { 0x1f, 0x0003 },
3260 { 0x01, 0x328a },
3261 { 0x1f, 0x0000 }
3262 };
3263
3264 rtl_apply_firmware(tp);
3265
3266 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3267
Hayes Wang5f886e02012-03-30 14:33:03 +08003268 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003269
3270 /* Improve 2-pair detection performance */
3271 rtl_writephy(tp, 0x1f, 0x0005);
3272 rtl_writephy(tp, 0x05, 0x8b85);
3273 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3274 rtl_writephy(tp, 0x1f, 0x0000);
3275}
3276
3277static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3278{
3279 rtl_apply_firmware(tp);
3280
Hayes Wang5f886e02012-03-30 14:33:03 +08003281 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003282}
3283
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003284static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3285{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003286 static const struct phy_reg phy_reg_init[] = {
3287 /* Channel estimation fine tune */
3288 { 0x1f, 0x0003 },
3289 { 0x09, 0xa20f },
3290 { 0x1f, 0x0000 },
3291
3292 /* Modify green table for giga & fnet */
3293 { 0x1f, 0x0005 },
3294 { 0x05, 0x8b55 },
3295 { 0x06, 0x0000 },
3296 { 0x05, 0x8b5e },
3297 { 0x06, 0x0000 },
3298 { 0x05, 0x8b67 },
3299 { 0x06, 0x0000 },
3300 { 0x05, 0x8b70 },
3301 { 0x06, 0x0000 },
3302 { 0x1f, 0x0000 },
3303 { 0x1f, 0x0007 },
3304 { 0x1e, 0x0078 },
3305 { 0x17, 0x0000 },
3306 { 0x19, 0x00aa },
3307 { 0x1f, 0x0000 },
3308
3309 /* Modify green table for 10M */
3310 { 0x1f, 0x0005 },
3311 { 0x05, 0x8b79 },
3312 { 0x06, 0xaa00 },
3313 { 0x1f, 0x0000 },
3314
3315 /* Disable hiimpedance detection (RTCT) */
3316 { 0x1f, 0x0003 },
3317 { 0x01, 0x328a },
3318 { 0x1f, 0x0000 }
3319 };
3320
3321
3322 rtl_apply_firmware(tp);
3323
3324 rtl8168f_hw_phy_config(tp);
3325
3326 /* Improve 2-pair detection performance */
3327 rtl_writephy(tp, 0x1f, 0x0005);
3328 rtl_writephy(tp, 0x05, 0x8b85);
3329 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3330 rtl_writephy(tp, 0x1f, 0x0000);
3331
3332 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3333
3334 /* Modify green table for giga */
3335 rtl_writephy(tp, 0x1f, 0x0005);
3336 rtl_writephy(tp, 0x05, 0x8b54);
3337 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3338 rtl_writephy(tp, 0x05, 0x8b5d);
3339 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3340 rtl_writephy(tp, 0x05, 0x8a7c);
3341 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3342 rtl_writephy(tp, 0x05, 0x8a7f);
3343 rtl_w1w0_phy(tp, 0x06, 0x0100, 0x0000);
3344 rtl_writephy(tp, 0x05, 0x8a82);
3345 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3346 rtl_writephy(tp, 0x05, 0x8a85);
3347 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3348 rtl_writephy(tp, 0x05, 0x8a88);
3349 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3350 rtl_writephy(tp, 0x1f, 0x0000);
3351
3352 /* uc same-seed solution */
3353 rtl_writephy(tp, 0x1f, 0x0005);
3354 rtl_writephy(tp, 0x05, 0x8b85);
3355 rtl_w1w0_phy(tp, 0x06, 0x8000, 0x0000);
3356 rtl_writephy(tp, 0x1f, 0x0000);
3357
3358 /* eee setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003359 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003360 rtl_writephy(tp, 0x1f, 0x0005);
3361 rtl_writephy(tp, 0x05, 0x8b85);
3362 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3363 rtl_writephy(tp, 0x1f, 0x0004);
3364 rtl_writephy(tp, 0x1f, 0x0007);
3365 rtl_writephy(tp, 0x1e, 0x0020);
3366 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
3367 rtl_writephy(tp, 0x1f, 0x0000);
3368 rtl_writephy(tp, 0x0d, 0x0007);
3369 rtl_writephy(tp, 0x0e, 0x003c);
3370 rtl_writephy(tp, 0x0d, 0x4007);
3371 rtl_writephy(tp, 0x0e, 0x0000);
3372 rtl_writephy(tp, 0x0d, 0x0000);
3373
3374 /* Green feature */
3375 rtl_writephy(tp, 0x1f, 0x0003);
3376 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3377 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3378 rtl_writephy(tp, 0x1f, 0x0000);
3379}
3380
Hayes Wangc5583862012-07-02 17:23:22 +08003381static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3382{
Hayes Wangc5583862012-07-02 17:23:22 +08003383 rtl_apply_firmware(tp);
3384
hayeswang41f44d12013-04-01 22:23:36 +00003385 rtl_writephy(tp, 0x1f, 0x0a46);
3386 if (rtl_readphy(tp, 0x10) & 0x0100) {
3387 rtl_writephy(tp, 0x1f, 0x0bcc);
3388 rtl_w1w0_phy(tp, 0x12, 0x0000, 0x8000);
3389 } else {
3390 rtl_writephy(tp, 0x1f, 0x0bcc);
3391 rtl_w1w0_phy(tp, 0x12, 0x8000, 0x0000);
3392 }
Hayes Wangc5583862012-07-02 17:23:22 +08003393
hayeswang41f44d12013-04-01 22:23:36 +00003394 rtl_writephy(tp, 0x1f, 0x0a46);
3395 if (rtl_readphy(tp, 0x13) & 0x0100) {
3396 rtl_writephy(tp, 0x1f, 0x0c41);
3397 rtl_w1w0_phy(tp, 0x15, 0x0002, 0x0000);
3398 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003399 rtl_writephy(tp, 0x1f, 0x0c41);
3400 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003401 }
Hayes Wangc5583862012-07-02 17:23:22 +08003402
hayeswang41f44d12013-04-01 22:23:36 +00003403 /* Enable PHY auto speed down */
3404 rtl_writephy(tp, 0x1f, 0x0a44);
3405 rtl_w1w0_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003406
hayeswangfe7524c2013-04-01 22:23:37 +00003407 rtl_writephy(tp, 0x1f, 0x0bcc);
3408 rtl_w1w0_phy(tp, 0x14, 0x0100, 0x0000);
3409 rtl_writephy(tp, 0x1f, 0x0a44);
3410 rtl_w1w0_phy(tp, 0x11, 0x00c0, 0x0000);
3411 rtl_writephy(tp, 0x1f, 0x0a43);
3412 rtl_writephy(tp, 0x13, 0x8084);
3413 rtl_w1w0_phy(tp, 0x14, 0x0000, 0x6000);
3414 rtl_w1w0_phy(tp, 0x10, 0x1003, 0x0000);
3415
hayeswang41f44d12013-04-01 22:23:36 +00003416 /* EEE auto-fallback function */
3417 rtl_writephy(tp, 0x1f, 0x0a4b);
3418 rtl_w1w0_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003419
hayeswang41f44d12013-04-01 22:23:36 +00003420 /* Enable UC LPF tune function */
3421 rtl_writephy(tp, 0x1f, 0x0a43);
3422 rtl_writephy(tp, 0x13, 0x8012);
3423 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3424
3425 rtl_writephy(tp, 0x1f, 0x0c42);
3426 rtl_w1w0_phy(tp, 0x11, 0x4000, 0x2000);
3427
hayeswangfe7524c2013-04-01 22:23:37 +00003428 /* Improve SWR Efficiency */
3429 rtl_writephy(tp, 0x1f, 0x0bcd);
3430 rtl_writephy(tp, 0x14, 0x5065);
3431 rtl_writephy(tp, 0x14, 0xd065);
3432 rtl_writephy(tp, 0x1f, 0x0bc8);
3433 rtl_writephy(tp, 0x11, 0x5655);
3434 rtl_writephy(tp, 0x1f, 0x0bcd);
3435 rtl_writephy(tp, 0x14, 0x1065);
3436 rtl_writephy(tp, 0x14, 0x9065);
3437 rtl_writephy(tp, 0x14, 0x1065);
3438
hayeswang41f44d12013-04-01 22:23:36 +00003439 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003440}
3441
françois romieu4da19632011-01-03 15:07:55 +00003442static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003443{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003444 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003445 { 0x1f, 0x0003 },
3446 { 0x08, 0x441d },
3447 { 0x01, 0x9100 },
3448 { 0x1f, 0x0000 }
3449 };
3450
françois romieu4da19632011-01-03 15:07:55 +00003451 rtl_writephy(tp, 0x1f, 0x0000);
3452 rtl_patchphy(tp, 0x11, 1 << 12);
3453 rtl_patchphy(tp, 0x19, 1 << 13);
3454 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003455
françois romieu4da19632011-01-03 15:07:55 +00003456 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003457}
3458
Hayes Wang5a5e4442011-02-22 17:26:21 +08003459static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3460{
3461 static const struct phy_reg phy_reg_init[] = {
3462 { 0x1f, 0x0005 },
3463 { 0x1a, 0x0000 },
3464 { 0x1f, 0x0000 },
3465
3466 { 0x1f, 0x0004 },
3467 { 0x1c, 0x0000 },
3468 { 0x1f, 0x0000 },
3469
3470 { 0x1f, 0x0001 },
3471 { 0x15, 0x7701 },
3472 { 0x1f, 0x0000 }
3473 };
3474
3475 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003476 rtl_writephy(tp, 0x1f, 0x0000);
3477 rtl_writephy(tp, 0x18, 0x0310);
3478 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003479
François Romieu953a12c2011-04-24 17:38:48 +02003480 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003481
3482 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3483}
3484
Hayes Wang7e18dca2012-03-30 14:33:02 +08003485static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3486{
Hayes Wang7e18dca2012-03-30 14:33:02 +08003487 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01003488 rtl_writephy(tp, 0x1f, 0x0000);
3489 rtl_writephy(tp, 0x18, 0x0310);
3490 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003491
3492 rtl_apply_firmware(tp);
3493
3494 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003495 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003496 rtl_writephy(tp, 0x1f, 0x0004);
3497 rtl_writephy(tp, 0x10, 0x401f);
3498 rtl_writephy(tp, 0x19, 0x7030);
3499 rtl_writephy(tp, 0x1f, 0x0000);
3500}
3501
Hayes Wang5598bfe2012-07-02 17:23:21 +08003502static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3503{
Hayes Wang5598bfe2012-07-02 17:23:21 +08003504 static const struct phy_reg phy_reg_init[] = {
3505 { 0x1f, 0x0004 },
3506 { 0x10, 0xc07f },
3507 { 0x19, 0x7030 },
3508 { 0x1f, 0x0000 }
3509 };
3510
3511 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003512 rtl_writephy(tp, 0x1f, 0x0000);
3513 rtl_writephy(tp, 0x18, 0x0310);
3514 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003515
3516 rtl_apply_firmware(tp);
3517
Francois Romieufdf6fc02012-07-06 22:40:38 +02003518 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003519 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3520
Francois Romieufdf6fc02012-07-06 22:40:38 +02003521 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003522}
3523
Francois Romieu5615d9f2007-08-17 17:50:46 +02003524static void rtl_hw_phy_config(struct net_device *dev)
3525{
3526 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003527
3528 rtl8169_print_mac_version(tp);
3529
3530 switch (tp->mac_version) {
3531 case RTL_GIGA_MAC_VER_01:
3532 break;
3533 case RTL_GIGA_MAC_VER_02:
3534 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003535 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003536 break;
3537 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003538 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003539 break;
françois romieu2e9558562009-08-10 19:44:19 +00003540 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003541 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003542 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003543 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003544 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003545 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003546 case RTL_GIGA_MAC_VER_07:
3547 case RTL_GIGA_MAC_VER_08:
3548 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003549 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003550 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003551 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003552 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003553 break;
3554 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003555 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003556 break;
3557 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003558 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003559 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003560 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003561 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003562 break;
3563 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003564 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003565 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003566 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003567 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003568 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003569 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003570 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003571 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003572 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003573 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003574 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003575 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003576 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003577 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003578 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003579 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003580 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003581 break;
3582 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003583 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003584 break;
3585 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003586 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003587 break;
françois romieue6de30d2011-01-03 15:08:37 +00003588 case RTL_GIGA_MAC_VER_28:
3589 rtl8168d_4_hw_phy_config(tp);
3590 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003591 case RTL_GIGA_MAC_VER_29:
3592 case RTL_GIGA_MAC_VER_30:
3593 rtl8105e_hw_phy_config(tp);
3594 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003595 case RTL_GIGA_MAC_VER_31:
3596 /* None. */
3597 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003598 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003599 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003600 rtl8168e_1_hw_phy_config(tp);
3601 break;
3602 case RTL_GIGA_MAC_VER_34:
3603 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003604 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003605 case RTL_GIGA_MAC_VER_35:
3606 rtl8168f_1_hw_phy_config(tp);
3607 break;
3608 case RTL_GIGA_MAC_VER_36:
3609 rtl8168f_2_hw_phy_config(tp);
3610 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003611
Hayes Wang7e18dca2012-03-30 14:33:02 +08003612 case RTL_GIGA_MAC_VER_37:
3613 rtl8402_hw_phy_config(tp);
3614 break;
3615
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003616 case RTL_GIGA_MAC_VER_38:
3617 rtl8411_hw_phy_config(tp);
3618 break;
3619
Hayes Wang5598bfe2012-07-02 17:23:21 +08003620 case RTL_GIGA_MAC_VER_39:
3621 rtl8106e_hw_phy_config(tp);
3622 break;
3623
Hayes Wangc5583862012-07-02 17:23:22 +08003624 case RTL_GIGA_MAC_VER_40:
3625 rtl8168g_1_hw_phy_config(tp);
3626 break;
3627
3628 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02003629 default:
3630 break;
3631 }
3632}
3633
Francois Romieuda78dbf2012-01-26 14:18:23 +01003634static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 struct timer_list *timer = &tp->timer;
3637 void __iomem *ioaddr = tp->mmio_addr;
3638 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3639
Francois Romieubcf0bf92006-07-26 23:14:13 +02003640 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
françois romieu4da19632011-01-03 15:07:55 +00003642 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003643 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 * A busy loop could burn quite a few cycles on nowadays CPU.
3645 * Let's delay the execution of the timer for a few ticks.
3646 */
3647 timeout = HZ/10;
3648 goto out_mod_timer;
3649 }
3650
3651 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01003652 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
Francois Romieuda78dbf2012-01-26 14:18:23 +01003654 netif_warn(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655
françois romieu4da19632011-01-03 15:07:55 +00003656 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657
3658out_mod_timer:
3659 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003660}
3661
3662static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3663{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003664 if (!test_and_set_bit(flag, tp->wk.flags))
3665 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003666}
3667
3668static void rtl8169_phy_timer(unsigned long __opaque)
3669{
3670 struct net_device *dev = (struct net_device *)__opaque;
3671 struct rtl8169_private *tp = netdev_priv(dev);
3672
Francois Romieu98ddf982012-01-31 10:47:34 +01003673 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674}
3675
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3677 void __iomem *ioaddr)
3678{
3679 iounmap(ioaddr);
3680 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003681 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 pci_disable_device(pdev);
3683 free_netdev(dev);
3684}
3685
Francois Romieuffc46952012-07-06 14:19:23 +02003686DECLARE_RTL_COND(rtl_phy_reset_cond)
3687{
3688 return tp->phy_reset_pending(tp);
3689}
3690
Francois Romieubf793292006-11-01 00:53:05 +01003691static void rtl8169_phy_reset(struct net_device *dev,
3692 struct rtl8169_private *tp)
3693{
françois romieu4da19632011-01-03 15:07:55 +00003694 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02003695 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01003696}
3697
David S. Miller8decf862011-09-22 03:23:13 -04003698static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3699{
3700 void __iomem *ioaddr = tp->mmio_addr;
3701
3702 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3703 (RTL_R8(PHYstatus) & TBI_Enable);
3704}
3705
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003706static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003708 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003709
Francois Romieu5615d9f2007-08-17 17:50:46 +02003710 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003711
Marcus Sundberg773328942008-07-10 21:28:08 +02003712 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3713 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3714 RTL_W8(0x82, 0x01);
3715 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003716
Francois Romieu6dccd162007-02-13 23:38:05 +01003717 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3718
3719 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3720 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003721
Francois Romieubcf0bf92006-07-26 23:14:13 +02003722 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003723 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3724 RTL_W8(0x82, 0x01);
3725 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003726 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003727 }
3728
Francois Romieubf793292006-11-01 00:53:05 +01003729 rtl8169_phy_reset(dev, tp);
3730
Oliver Neukum54405cd2011-01-06 21:55:13 +01003731 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003732 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3733 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3734 (tp->mii.supports_gmii ?
3735 ADVERTISED_1000baseT_Half |
3736 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003737
David S. Miller8decf862011-09-22 03:23:13 -04003738 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003739 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003740}
3741
Francois Romieu773d2022007-01-31 23:47:43 +01003742static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3743{
3744 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu773d2022007-01-31 23:47:43 +01003745
Francois Romieuda78dbf2012-01-26 14:18:23 +01003746 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003747
3748 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003749
françois romieu9ecb9aa2012-12-07 11:20:21 +00003750 RTL_W32(MAC4, addr[4] | addr[5] << 8);
françois romieu908ba2b2010-04-26 11:42:58 +00003751 RTL_R32(MAC4);
3752
françois romieu9ecb9aa2012-12-07 11:20:21 +00003753 RTL_W32(MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
françois romieu908ba2b2010-04-26 11:42:58 +00003754 RTL_R32(MAC0);
3755
françois romieu9ecb9aa2012-12-07 11:20:21 +00003756 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
3757 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00003758
Francois Romieu773d2022007-01-31 23:47:43 +01003759 RTL_W8(Cfg9346, Cfg9346_Lock);
3760
Francois Romieuda78dbf2012-01-26 14:18:23 +01003761 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003762}
3763
3764static int rtl_set_mac_address(struct net_device *dev, void *p)
3765{
3766 struct rtl8169_private *tp = netdev_priv(dev);
3767 struct sockaddr *addr = p;
3768
3769 if (!is_valid_ether_addr(addr->sa_data))
3770 return -EADDRNOTAVAIL;
3771
3772 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3773
3774 rtl_rar_set(tp, dev->dev_addr);
3775
3776 return 0;
3777}
3778
Francois Romieu5f787a12006-08-17 13:02:36 +02003779static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3780{
3781 struct rtl8169_private *tp = netdev_priv(dev);
3782 struct mii_ioctl_data *data = if_mii(ifr);
3783
Francois Romieu8b4ab282008-11-19 22:05:25 -08003784 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3785}
Francois Romieu5f787a12006-08-17 13:02:36 +02003786
Francois Romieucecb5fd2011-04-01 10:21:07 +02003787static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3788 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003789{
Francois Romieu5f787a12006-08-17 13:02:36 +02003790 switch (cmd) {
3791 case SIOCGMIIPHY:
3792 data->phy_id = 32; /* Internal PHY */
3793 return 0;
3794
3795 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003796 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003797 return 0;
3798
3799 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003800 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003801 return 0;
3802 }
3803 return -EOPNOTSUPP;
3804}
3805
Francois Romieu8b4ab282008-11-19 22:05:25 -08003806static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3807{
3808 return -EOPNOTSUPP;
3809}
3810
Francois Romieufbac58f2007-10-04 22:51:38 +02003811static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3812{
3813 if (tp->features & RTL_FEATURE_MSI) {
3814 pci_disable_msi(pdev);
3815 tp->features &= ~RTL_FEATURE_MSI;
3816 }
3817}
3818
Bill Pembertonbaf63292012-12-03 09:23:28 -05003819static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00003820{
3821 struct mdio_ops *ops = &tp->mdio_ops;
3822
3823 switch (tp->mac_version) {
3824 case RTL_GIGA_MAC_VER_27:
3825 ops->write = r8168dp_1_mdio_write;
3826 ops->read = r8168dp_1_mdio_read;
3827 break;
françois romieue6de30d2011-01-03 15:08:37 +00003828 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003829 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003830 ops->write = r8168dp_2_mdio_write;
3831 ops->read = r8168dp_2_mdio_read;
3832 break;
Hayes Wangc5583862012-07-02 17:23:22 +08003833 case RTL_GIGA_MAC_VER_40:
3834 case RTL_GIGA_MAC_VER_41:
3835 ops->write = r8168g_mdio_write;
3836 ops->read = r8168g_mdio_read;
3837 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003838 default:
3839 ops->write = r8169_mdio_write;
3840 ops->read = r8169_mdio_read;
3841 break;
3842 }
3843}
3844
David S. Miller1805b2f2011-10-24 18:18:09 -04003845static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3846{
3847 void __iomem *ioaddr = tp->mmio_addr;
3848
3849 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00003850 case RTL_GIGA_MAC_VER_25:
3851 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04003852 case RTL_GIGA_MAC_VER_29:
3853 case RTL_GIGA_MAC_VER_30:
3854 case RTL_GIGA_MAC_VER_32:
3855 case RTL_GIGA_MAC_VER_33:
3856 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08003857 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003858 case RTL_GIGA_MAC_VER_38:
Hayes Wang5598bfe2012-07-02 17:23:21 +08003859 case RTL_GIGA_MAC_VER_39:
Hayes Wangc5583862012-07-02 17:23:22 +08003860 case RTL_GIGA_MAC_VER_40:
3861 case RTL_GIGA_MAC_VER_41:
David S. Miller1805b2f2011-10-24 18:18:09 -04003862 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3863 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3864 break;
3865 default:
3866 break;
3867 }
3868}
3869
3870static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3871{
3872 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3873 return false;
3874
3875 rtl_writephy(tp, 0x1f, 0x0000);
3876 rtl_writephy(tp, MII_BMCR, 0x0000);
3877
3878 rtl_wol_suspend_quirk(tp);
3879
3880 return true;
3881}
3882
françois romieu065c27c2011-01-03 15:08:12 +00003883static void r810x_phy_power_down(struct rtl8169_private *tp)
3884{
3885 rtl_writephy(tp, 0x1f, 0x0000);
3886 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3887}
3888
3889static void r810x_phy_power_up(struct rtl8169_private *tp)
3890{
3891 rtl_writephy(tp, 0x1f, 0x0000);
3892 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3893}
3894
3895static void r810x_pll_power_down(struct rtl8169_private *tp)
3896{
Hayes Wang00042992012-03-30 14:33:00 +08003897 void __iomem *ioaddr = tp->mmio_addr;
3898
David S. Miller1805b2f2011-10-24 18:18:09 -04003899 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003900 return;
françois romieu065c27c2011-01-03 15:08:12 +00003901
3902 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003903
3904 switch (tp->mac_version) {
3905 case RTL_GIGA_MAC_VER_07:
3906 case RTL_GIGA_MAC_VER_08:
3907 case RTL_GIGA_MAC_VER_09:
3908 case RTL_GIGA_MAC_VER_10:
3909 case RTL_GIGA_MAC_VER_13:
3910 case RTL_GIGA_MAC_VER_16:
3911 break;
3912 default:
3913 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3914 break;
3915 }
françois romieu065c27c2011-01-03 15:08:12 +00003916}
3917
3918static void r810x_pll_power_up(struct rtl8169_private *tp)
3919{
Hayes Wang00042992012-03-30 14:33:00 +08003920 void __iomem *ioaddr = tp->mmio_addr;
3921
françois romieu065c27c2011-01-03 15:08:12 +00003922 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003923
3924 switch (tp->mac_version) {
3925 case RTL_GIGA_MAC_VER_07:
3926 case RTL_GIGA_MAC_VER_08:
3927 case RTL_GIGA_MAC_VER_09:
3928 case RTL_GIGA_MAC_VER_10:
3929 case RTL_GIGA_MAC_VER_13:
3930 case RTL_GIGA_MAC_VER_16:
3931 break;
3932 default:
3933 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3934 break;
3935 }
françois romieu065c27c2011-01-03 15:08:12 +00003936}
3937
3938static void r8168_phy_power_up(struct rtl8169_private *tp)
3939{
3940 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003941 switch (tp->mac_version) {
3942 case RTL_GIGA_MAC_VER_11:
3943 case RTL_GIGA_MAC_VER_12:
3944 case RTL_GIGA_MAC_VER_17:
3945 case RTL_GIGA_MAC_VER_18:
3946 case RTL_GIGA_MAC_VER_19:
3947 case RTL_GIGA_MAC_VER_20:
3948 case RTL_GIGA_MAC_VER_21:
3949 case RTL_GIGA_MAC_VER_22:
3950 case RTL_GIGA_MAC_VER_23:
3951 case RTL_GIGA_MAC_VER_24:
3952 case RTL_GIGA_MAC_VER_25:
3953 case RTL_GIGA_MAC_VER_26:
3954 case RTL_GIGA_MAC_VER_27:
3955 case RTL_GIGA_MAC_VER_28:
3956 case RTL_GIGA_MAC_VER_31:
3957 rtl_writephy(tp, 0x0e, 0x0000);
3958 break;
3959 default:
3960 break;
3961 }
françois romieu065c27c2011-01-03 15:08:12 +00003962 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3963}
3964
3965static void r8168_phy_power_down(struct rtl8169_private *tp)
3966{
3967 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003968 switch (tp->mac_version) {
3969 case RTL_GIGA_MAC_VER_32:
3970 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00003971 case RTL_GIGA_MAC_VER_40:
3972 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00003973 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3974 break;
3975
3976 case RTL_GIGA_MAC_VER_11:
3977 case RTL_GIGA_MAC_VER_12:
3978 case RTL_GIGA_MAC_VER_17:
3979 case RTL_GIGA_MAC_VER_18:
3980 case RTL_GIGA_MAC_VER_19:
3981 case RTL_GIGA_MAC_VER_20:
3982 case RTL_GIGA_MAC_VER_21:
3983 case RTL_GIGA_MAC_VER_22:
3984 case RTL_GIGA_MAC_VER_23:
3985 case RTL_GIGA_MAC_VER_24:
3986 case RTL_GIGA_MAC_VER_25:
3987 case RTL_GIGA_MAC_VER_26:
3988 case RTL_GIGA_MAC_VER_27:
3989 case RTL_GIGA_MAC_VER_28:
3990 case RTL_GIGA_MAC_VER_31:
3991 rtl_writephy(tp, 0x0e, 0x0200);
3992 default:
3993 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3994 break;
3995 }
françois romieu065c27c2011-01-03 15:08:12 +00003996}
3997
3998static void r8168_pll_power_down(struct rtl8169_private *tp)
3999{
4000 void __iomem *ioaddr = tp->mmio_addr;
4001
Francois Romieucecb5fd2011-04-01 10:21:07 +02004002 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4003 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4004 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00004005 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00004006 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08004007 }
françois romieu065c27c2011-01-03 15:08:12 +00004008
Francois Romieucecb5fd2011-04-01 10:21:07 +02004009 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4010 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00004011 (RTL_R16(CPlusCmd) & ASF)) {
4012 return;
4013 }
4014
hayeswang01dc7fe2011-03-21 01:50:28 +00004015 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4016 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004017 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004018
David S. Miller1805b2f2011-10-24 18:18:09 -04004019 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004020 return;
françois romieu065c27c2011-01-03 15:08:12 +00004021
4022 r8168_phy_power_down(tp);
4023
4024 switch (tp->mac_version) {
4025 case RTL_GIGA_MAC_VER_25:
4026 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004027 case RTL_GIGA_MAC_VER_27:
4028 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004029 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004030 case RTL_GIGA_MAC_VER_32:
4031 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00004032 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4033 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004034 case RTL_GIGA_MAC_VER_40:
4035 case RTL_GIGA_MAC_VER_41:
4036 rtl_w1w0_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
4037 0xfc000000, ERIAR_EXGMAC);
4038 break;
françois romieu065c27c2011-01-03 15:08:12 +00004039 }
4040}
4041
4042static void r8168_pll_power_up(struct rtl8169_private *tp)
4043{
4044 void __iomem *ioaddr = tp->mmio_addr;
4045
françois romieu065c27c2011-01-03 15:08:12 +00004046 switch (tp->mac_version) {
4047 case RTL_GIGA_MAC_VER_25:
4048 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004049 case RTL_GIGA_MAC_VER_27:
4050 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004051 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004052 case RTL_GIGA_MAC_VER_32:
4053 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00004054 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4055 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004056 case RTL_GIGA_MAC_VER_40:
4057 case RTL_GIGA_MAC_VER_41:
4058 rtl_w1w0_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
4059 0x00000000, ERIAR_EXGMAC);
4060 break;
françois romieu065c27c2011-01-03 15:08:12 +00004061 }
4062
4063 r8168_phy_power_up(tp);
4064}
4065
Francois Romieud58d46b2011-05-03 16:38:29 +02004066static void rtl_generic_op(struct rtl8169_private *tp,
4067 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00004068{
4069 if (op)
4070 op(tp);
4071}
4072
4073static void rtl_pll_power_down(struct rtl8169_private *tp)
4074{
Francois Romieud58d46b2011-05-03 16:38:29 +02004075 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00004076}
4077
4078static void rtl_pll_power_up(struct rtl8169_private *tp)
4079{
Francois Romieud58d46b2011-05-03 16:38:29 +02004080 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00004081}
4082
Bill Pembertonbaf63292012-12-03 09:23:28 -05004083static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00004084{
4085 struct pll_power_ops *ops = &tp->pll_power_ops;
4086
4087 switch (tp->mac_version) {
4088 case RTL_GIGA_MAC_VER_07:
4089 case RTL_GIGA_MAC_VER_08:
4090 case RTL_GIGA_MAC_VER_09:
4091 case RTL_GIGA_MAC_VER_10:
4092 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08004093 case RTL_GIGA_MAC_VER_29:
4094 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004095 case RTL_GIGA_MAC_VER_37:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004096 case RTL_GIGA_MAC_VER_39:
françois romieu065c27c2011-01-03 15:08:12 +00004097 ops->down = r810x_pll_power_down;
4098 ops->up = r810x_pll_power_up;
4099 break;
4100
4101 case RTL_GIGA_MAC_VER_11:
4102 case RTL_GIGA_MAC_VER_12:
4103 case RTL_GIGA_MAC_VER_17:
4104 case RTL_GIGA_MAC_VER_18:
4105 case RTL_GIGA_MAC_VER_19:
4106 case RTL_GIGA_MAC_VER_20:
4107 case RTL_GIGA_MAC_VER_21:
4108 case RTL_GIGA_MAC_VER_22:
4109 case RTL_GIGA_MAC_VER_23:
4110 case RTL_GIGA_MAC_VER_24:
4111 case RTL_GIGA_MAC_VER_25:
4112 case RTL_GIGA_MAC_VER_26:
4113 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00004114 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004115 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004116 case RTL_GIGA_MAC_VER_32:
4117 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004118 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08004119 case RTL_GIGA_MAC_VER_35:
4120 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004121 case RTL_GIGA_MAC_VER_38:
Hayes Wangc5583862012-07-02 17:23:22 +08004122 case RTL_GIGA_MAC_VER_40:
4123 case RTL_GIGA_MAC_VER_41:
françois romieu065c27c2011-01-03 15:08:12 +00004124 ops->down = r8168_pll_power_down;
4125 ops->up = r8168_pll_power_up;
4126 break;
4127
4128 default:
4129 ops->down = NULL;
4130 ops->up = NULL;
4131 break;
4132 }
4133}
4134
Hayes Wange542a222011-07-06 15:58:04 +08004135static void rtl_init_rxcfg(struct rtl8169_private *tp)
4136{
4137 void __iomem *ioaddr = tp->mmio_addr;
4138
4139 switch (tp->mac_version) {
4140 case RTL_GIGA_MAC_VER_01:
4141 case RTL_GIGA_MAC_VER_02:
4142 case RTL_GIGA_MAC_VER_03:
4143 case RTL_GIGA_MAC_VER_04:
4144 case RTL_GIGA_MAC_VER_05:
4145 case RTL_GIGA_MAC_VER_06:
4146 case RTL_GIGA_MAC_VER_10:
4147 case RTL_GIGA_MAC_VER_11:
4148 case RTL_GIGA_MAC_VER_12:
4149 case RTL_GIGA_MAC_VER_13:
4150 case RTL_GIGA_MAC_VER_14:
4151 case RTL_GIGA_MAC_VER_15:
4152 case RTL_GIGA_MAC_VER_16:
4153 case RTL_GIGA_MAC_VER_17:
4154 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
4155 break;
4156 case RTL_GIGA_MAC_VER_18:
4157 case RTL_GIGA_MAC_VER_19:
4158 case RTL_GIGA_MAC_VER_20:
4159 case RTL_GIGA_MAC_VER_21:
4160 case RTL_GIGA_MAC_VER_22:
4161 case RTL_GIGA_MAC_VER_23:
4162 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00004163 case RTL_GIGA_MAC_VER_34:
Hayes Wange542a222011-07-06 15:58:04 +08004164 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
4165 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004166 case RTL_GIGA_MAC_VER_40:
4167 case RTL_GIGA_MAC_VER_41:
4168 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST | RX_EARLY_OFF);
4169 break;
Hayes Wange542a222011-07-06 15:58:04 +08004170 default:
4171 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
4172 break;
4173 }
4174}
4175
Hayes Wang92fc43b2011-07-06 15:58:03 +08004176static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4177{
Timo Teräs9fba0812013-01-15 21:01:24 +00004178 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004179}
4180
Francois Romieud58d46b2011-05-03 16:38:29 +02004181static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4182{
françois romieu9c5028e2012-03-02 04:43:14 +00004183 void __iomem *ioaddr = tp->mmio_addr;
4184
4185 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004186 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00004187 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004188}
4189
4190static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4191{
françois romieu9c5028e2012-03-02 04:43:14 +00004192 void __iomem *ioaddr = tp->mmio_addr;
4193
4194 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004195 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00004196 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004197}
4198
4199static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4200{
4201 void __iomem *ioaddr = tp->mmio_addr;
4202
4203 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4204 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
4205 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
4206}
4207
4208static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4209{
4210 void __iomem *ioaddr = tp->mmio_addr;
4211
4212 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4213 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
4214 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
4215}
4216
4217static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4218{
4219 void __iomem *ioaddr = tp->mmio_addr;
4220
4221 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4222}
4223
4224static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4225{
4226 void __iomem *ioaddr = tp->mmio_addr;
4227
4228 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4229}
4230
4231static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4232{
4233 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02004234
4235 RTL_W8(MaxTxPacketSize, 0x3f);
4236 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4237 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01004238 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02004239}
4240
4241static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4242{
4243 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02004244
4245 RTL_W8(MaxTxPacketSize, 0x0c);
4246 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4247 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01004248 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02004249}
4250
4251static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4252{
4253 rtl_tx_performance_tweak(tp->pci_dev,
4254 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4255}
4256
4257static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4258{
4259 rtl_tx_performance_tweak(tp->pci_dev,
4260 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4261}
4262
4263static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4264{
4265 void __iomem *ioaddr = tp->mmio_addr;
4266
4267 r8168b_0_hw_jumbo_enable(tp);
4268
4269 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
4270}
4271
4272static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4273{
4274 void __iomem *ioaddr = tp->mmio_addr;
4275
4276 r8168b_0_hw_jumbo_disable(tp);
4277
4278 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4279}
4280
Bill Pembertonbaf63292012-12-03 09:23:28 -05004281static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02004282{
4283 struct jumbo_ops *ops = &tp->jumbo_ops;
4284
4285 switch (tp->mac_version) {
4286 case RTL_GIGA_MAC_VER_11:
4287 ops->disable = r8168b_0_hw_jumbo_disable;
4288 ops->enable = r8168b_0_hw_jumbo_enable;
4289 break;
4290 case RTL_GIGA_MAC_VER_12:
4291 case RTL_GIGA_MAC_VER_17:
4292 ops->disable = r8168b_1_hw_jumbo_disable;
4293 ops->enable = r8168b_1_hw_jumbo_enable;
4294 break;
4295 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4296 case RTL_GIGA_MAC_VER_19:
4297 case RTL_GIGA_MAC_VER_20:
4298 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4299 case RTL_GIGA_MAC_VER_22:
4300 case RTL_GIGA_MAC_VER_23:
4301 case RTL_GIGA_MAC_VER_24:
4302 case RTL_GIGA_MAC_VER_25:
4303 case RTL_GIGA_MAC_VER_26:
4304 ops->disable = r8168c_hw_jumbo_disable;
4305 ops->enable = r8168c_hw_jumbo_enable;
4306 break;
4307 case RTL_GIGA_MAC_VER_27:
4308 case RTL_GIGA_MAC_VER_28:
4309 ops->disable = r8168dp_hw_jumbo_disable;
4310 ops->enable = r8168dp_hw_jumbo_enable;
4311 break;
4312 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4313 case RTL_GIGA_MAC_VER_32:
4314 case RTL_GIGA_MAC_VER_33:
4315 case RTL_GIGA_MAC_VER_34:
4316 ops->disable = r8168e_hw_jumbo_disable;
4317 ops->enable = r8168e_hw_jumbo_enable;
4318 break;
4319
4320 /*
4321 * No action needed for jumbo frames with 8169.
4322 * No jumbo for 810x at all.
4323 */
Hayes Wangc5583862012-07-02 17:23:22 +08004324 case RTL_GIGA_MAC_VER_40:
4325 case RTL_GIGA_MAC_VER_41:
Francois Romieud58d46b2011-05-03 16:38:29 +02004326 default:
4327 ops->disable = NULL;
4328 ops->enable = NULL;
4329 break;
4330 }
4331}
4332
Francois Romieuffc46952012-07-06 14:19:23 +02004333DECLARE_RTL_COND(rtl_chipcmd_cond)
4334{
4335 void __iomem *ioaddr = tp->mmio_addr;
4336
4337 return RTL_R8(ChipCmd) & CmdReset;
4338}
4339
Francois Romieu6f43adc2011-04-29 15:05:51 +02004340static void rtl_hw_reset(struct rtl8169_private *tp)
4341{
4342 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu6f43adc2011-04-29 15:05:51 +02004343
Francois Romieu6f43adc2011-04-29 15:05:51 +02004344 RTL_W8(ChipCmd, CmdReset);
4345
Francois Romieuffc46952012-07-06 14:19:23 +02004346 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004347}
4348
Francois Romieub6ffd972011-06-17 17:00:05 +02004349static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4350{
4351 struct rtl_fw *rtl_fw;
4352 const char *name;
4353 int rc = -ENOMEM;
4354
4355 name = rtl_lookup_firmware_name(tp);
4356 if (!name)
4357 goto out_no_firmware;
4358
4359 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4360 if (!rtl_fw)
4361 goto err_warn;
4362
4363 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4364 if (rc < 0)
4365 goto err_free;
4366
Francois Romieufd112f22011-06-18 00:10:29 +02004367 rc = rtl_check_firmware(tp, rtl_fw);
4368 if (rc < 0)
4369 goto err_release_firmware;
4370
Francois Romieub6ffd972011-06-17 17:00:05 +02004371 tp->rtl_fw = rtl_fw;
4372out:
4373 return;
4374
Francois Romieufd112f22011-06-18 00:10:29 +02004375err_release_firmware:
4376 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004377err_free:
4378 kfree(rtl_fw);
4379err_warn:
4380 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4381 name, rc);
4382out_no_firmware:
4383 tp->rtl_fw = NULL;
4384 goto out;
4385}
4386
François Romieu953a12c2011-04-24 17:38:48 +02004387static void rtl_request_firmware(struct rtl8169_private *tp)
4388{
Francois Romieub6ffd972011-06-17 17:00:05 +02004389 if (IS_ERR(tp->rtl_fw))
4390 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004391}
4392
Hayes Wang92fc43b2011-07-06 15:58:03 +08004393static void rtl_rx_close(struct rtl8169_private *tp)
4394{
4395 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004396
Francois Romieu1687b562011-07-19 17:21:29 +02004397 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004398}
4399
Francois Romieuffc46952012-07-06 14:19:23 +02004400DECLARE_RTL_COND(rtl_npq_cond)
4401{
4402 void __iomem *ioaddr = tp->mmio_addr;
4403
4404 return RTL_R8(TxPoll) & NPQ;
4405}
4406
4407DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4408{
4409 void __iomem *ioaddr = tp->mmio_addr;
4410
4411 return RTL_R32(TxConfig) & TXCFG_EMPTY;
4412}
4413
françois romieue6de30d2011-01-03 15:08:37 +00004414static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415{
françois romieue6de30d2011-01-03 15:08:37 +00004416 void __iomem *ioaddr = tp->mmio_addr;
4417
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004419 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
Hayes Wang92fc43b2011-07-06 15:58:03 +08004421 rtl_rx_close(tp);
4422
Hayes Wang5d2e1952011-02-22 17:26:22 +08004423 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004424 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4425 tp->mac_version == RTL_GIGA_MAC_VER_31) {
Francois Romieuffc46952012-07-06 14:19:23 +02004426 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Hayes Wangc2218922011-09-06 16:55:18 +08004427 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4428 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
Hayes Wang7e18dca2012-03-30 14:33:02 +08004429 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004430 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
Hayes Wangc5583862012-07-02 17:23:22 +08004431 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
4432 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004433 tp->mac_version == RTL_GIGA_MAC_VER_38) {
David S. Miller8decf862011-09-22 03:23:13 -04004434 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02004435 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004436 } else {
4437 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4438 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004439 }
4440
Hayes Wang92fc43b2011-07-06 15:58:03 +08004441 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442}
4443
Francois Romieu7f796d832007-06-11 23:04:41 +02004444static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004445{
4446 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004447
4448 /* Set DMA burst size and Interframe Gap Time */
4449 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4450 (InterFrameGap << TxInterFrameGapShift));
4451}
4452
Francois Romieu07ce4062007-02-23 23:36:39 +01004453static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454{
4455 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
Francois Romieu07ce4062007-02-23 23:36:39 +01004457 tp->hw_start(dev);
4458
Francois Romieuda78dbf2012-01-26 14:18:23 +01004459 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004460}
4461
Francois Romieu7f796d832007-06-11 23:04:41 +02004462static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4463 void __iomem *ioaddr)
4464{
4465 /*
4466 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4467 * register to be written before TxDescAddrLow to work.
4468 * Switching from MMIO to I/O access fixes the issue as well.
4469 */
4470 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004471 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004472 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004473 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004474}
4475
4476static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4477{
4478 u16 cmd;
4479
4480 cmd = RTL_R16(CPlusCmd);
4481 RTL_W16(CPlusCmd, cmd);
4482 return cmd;
4483}
4484
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004485static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004486{
4487 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004488 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004489}
4490
Francois Romieu6dccd162007-02-13 23:38:05 +01004491static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4492{
Francois Romieu37441002011-06-17 22:58:54 +02004493 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004494 u32 mac_version;
4495 u32 clk;
4496 u32 val;
4497 } cfg2_info [] = {
4498 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4499 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4500 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4501 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004502 };
4503 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004504 unsigned int i;
4505 u32 clk;
4506
4507 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004508 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004509 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4510 RTL_W32(0x7c, p->val);
4511 break;
4512 }
4513 }
4514}
4515
Francois Romieue6b763e2012-03-08 09:35:39 +01004516static void rtl_set_rx_mode(struct net_device *dev)
4517{
4518 struct rtl8169_private *tp = netdev_priv(dev);
4519 void __iomem *ioaddr = tp->mmio_addr;
4520 u32 mc_filter[2]; /* Multicast hash filter */
4521 int rx_mode;
4522 u32 tmp = 0;
4523
4524 if (dev->flags & IFF_PROMISC) {
4525 /* Unconditionally log net taps. */
4526 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4527 rx_mode =
4528 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4529 AcceptAllPhys;
4530 mc_filter[1] = mc_filter[0] = 0xffffffff;
4531 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4532 (dev->flags & IFF_ALLMULTI)) {
4533 /* Too many to filter perfectly -- accept all multicasts. */
4534 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4535 mc_filter[1] = mc_filter[0] = 0xffffffff;
4536 } else {
4537 struct netdev_hw_addr *ha;
4538
4539 rx_mode = AcceptBroadcast | AcceptMyPhys;
4540 mc_filter[1] = mc_filter[0] = 0;
4541 netdev_for_each_mc_addr(ha, dev) {
4542 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4543 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4544 rx_mode |= AcceptMulticast;
4545 }
4546 }
4547
4548 if (dev->features & NETIF_F_RXALL)
4549 rx_mode |= (AcceptErr | AcceptRunt);
4550
4551 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4552
4553 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4554 u32 data = mc_filter[0];
4555
4556 mc_filter[0] = swab32(mc_filter[1]);
4557 mc_filter[1] = swab32(data);
4558 }
4559
Nathan Walp04817762012-11-01 12:08:47 +00004560 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
4561 mc_filter[1] = mc_filter[0] = 0xffffffff;
4562
Francois Romieue6b763e2012-03-08 09:35:39 +01004563 RTL_W32(MAR0 + 4, mc_filter[1]);
4564 RTL_W32(MAR0 + 0, mc_filter[0]);
4565
4566 RTL_W32(RxConfig, tmp);
4567}
4568
Francois Romieu07ce4062007-02-23 23:36:39 +01004569static void rtl_hw_start_8169(struct net_device *dev)
4570{
4571 struct rtl8169_private *tp = netdev_priv(dev);
4572 void __iomem *ioaddr = tp->mmio_addr;
4573 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004574
Francois Romieu9cb427b2006-11-02 00:10:16 +01004575 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4576 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4577 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4578 }
4579
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004581 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4582 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4583 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4584 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004585 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4586
Hayes Wange542a222011-07-06 15:58:04 +08004587 rtl_init_rxcfg(tp);
4588
françois romieuf0298f82011-01-03 15:07:42 +00004589 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004591 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592
Francois Romieucecb5fd2011-04-01 10:21:07 +02004593 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4594 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4595 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4596 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004597 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
Francois Romieu7f796d832007-06-11 23:04:41 +02004599 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004600
Francois Romieucecb5fd2011-04-01 10:21:07 +02004601 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4602 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004603 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004605 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 }
4607
Francois Romieubcf0bf92006-07-26 23:14:13 +02004608 RTL_W16(CPlusCmd, tp->cp_cmd);
4609
Francois Romieu6dccd162007-02-13 23:38:05 +01004610 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4611
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 /*
4613 * Undocumented corner. Supposedly:
4614 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4615 */
4616 RTL_W16(IntrMitigate, 0x0000);
4617
Francois Romieu7f796d832007-06-11 23:04:41 +02004618 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004619
Francois Romieucecb5fd2011-04-01 10:21:07 +02004620 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4621 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4622 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4623 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004624 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4625 rtl_set_rx_tx_config_registers(tp);
4626 }
4627
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004629
4630 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4631 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
4633 RTL_W32(RxMissed, 0);
4634
Francois Romieu07ce4062007-02-23 23:36:39 +01004635 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636
4637 /* no early-rx interrupts */
4638 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004639}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004641static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
4642{
4643 if (tp->csi_ops.write)
Francois Romieu52989f02012-07-06 13:37:00 +02004644 tp->csi_ops.write(tp, addr, value);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004645}
4646
4647static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
4648{
Francois Romieu52989f02012-07-06 13:37:00 +02004649 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004650}
4651
4652static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004653{
4654 u32 csi;
4655
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004656 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4657 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00004658}
4659
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004660static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004661{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004662 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00004663}
4664
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004665static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00004666{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004667 rtl_csi_access_enable(tp, 0x27000000);
4668}
4669
Francois Romieuffc46952012-07-06 14:19:23 +02004670DECLARE_RTL_COND(rtl_csiar_cond)
4671{
4672 void __iomem *ioaddr = tp->mmio_addr;
4673
4674 return RTL_R32(CSIAR) & CSIAR_FLAG;
4675}
4676
Francois Romieu52989f02012-07-06 13:37:00 +02004677static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004678{
Francois Romieu52989f02012-07-06 13:37:00 +02004679 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004680
4681 RTL_W32(CSIDR, value);
4682 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4683 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4684
Francois Romieuffc46952012-07-06 14:19:23 +02004685 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004686}
4687
Francois Romieu52989f02012-07-06 13:37:00 +02004688static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004689{
Francois Romieu52989f02012-07-06 13:37:00 +02004690 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004691
4692 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
4693 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4694
Francois Romieuffc46952012-07-06 14:19:23 +02004695 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4696 RTL_R32(CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004697}
4698
Francois Romieu52989f02012-07-06 13:37:00 +02004699static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wang7e18dca2012-03-30 14:33:02 +08004700{
Francois Romieu52989f02012-07-06 13:37:00 +02004701 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004702
4703 RTL_W32(CSIDR, value);
4704 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4705 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
4706 CSIAR_FUNC_NIC);
4707
Francois Romieuffc46952012-07-06 14:19:23 +02004708 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004709}
4710
Francois Romieu52989f02012-07-06 13:37:00 +02004711static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wang7e18dca2012-03-30 14:33:02 +08004712{
Francois Romieu52989f02012-07-06 13:37:00 +02004713 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004714
4715 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
4716 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4717
Francois Romieuffc46952012-07-06 14:19:23 +02004718 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4719 RTL_R32(CSIDR) : ~0;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004720}
4721
Bill Pembertonbaf63292012-12-03 09:23:28 -05004722static void rtl_init_csi_ops(struct rtl8169_private *tp)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004723{
4724 struct csi_ops *ops = &tp->csi_ops;
4725
4726 switch (tp->mac_version) {
4727 case RTL_GIGA_MAC_VER_01:
4728 case RTL_GIGA_MAC_VER_02:
4729 case RTL_GIGA_MAC_VER_03:
4730 case RTL_GIGA_MAC_VER_04:
4731 case RTL_GIGA_MAC_VER_05:
4732 case RTL_GIGA_MAC_VER_06:
4733 case RTL_GIGA_MAC_VER_10:
4734 case RTL_GIGA_MAC_VER_11:
4735 case RTL_GIGA_MAC_VER_12:
4736 case RTL_GIGA_MAC_VER_13:
4737 case RTL_GIGA_MAC_VER_14:
4738 case RTL_GIGA_MAC_VER_15:
4739 case RTL_GIGA_MAC_VER_16:
4740 case RTL_GIGA_MAC_VER_17:
4741 ops->write = NULL;
4742 ops->read = NULL;
4743 break;
4744
Hayes Wang7e18dca2012-03-30 14:33:02 +08004745 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004746 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004747 ops->write = r8402_csi_write;
4748 ops->read = r8402_csi_read;
4749 break;
4750
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004751 default:
4752 ops->write = r8169_csi_write;
4753 ops->read = r8169_csi_read;
4754 break;
4755 }
Francois Romieudacf8152008-08-02 20:44:13 +02004756}
4757
4758struct ephy_info {
4759 unsigned int offset;
4760 u16 mask;
4761 u16 bits;
4762};
4763
Francois Romieufdf6fc02012-07-06 22:40:38 +02004764static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
4765 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004766{
4767 u16 w;
4768
4769 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02004770 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4771 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02004772 e++;
4773 }
4774}
4775
Francois Romieub726e492008-06-28 12:22:59 +02004776static void rtl_disable_clock_request(struct pci_dev *pdev)
4777{
Jiang Liu7d7903b2012-07-24 17:20:16 +08004778 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
4779 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02004780}
4781
françois romieue6de30d2011-01-03 15:08:37 +00004782static void rtl_enable_clock_request(struct pci_dev *pdev)
4783{
Jiang Liu7d7903b2012-07-24 17:20:16 +08004784 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
4785 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00004786}
4787
Francois Romieub726e492008-06-28 12:22:59 +02004788#define R8168_CPCMD_QUIRK_MASK (\
4789 EnableBist | \
4790 Mac_dbgo_oe | \
4791 Force_half_dup | \
4792 Force_rxflow_en | \
4793 Force_txflow_en | \
4794 Cxpl_dbg_sel | \
4795 ASF | \
4796 PktCntrDisable | \
4797 Mac_dbgo_sel)
4798
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004799static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004800{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004801 void __iomem *ioaddr = tp->mmio_addr;
4802 struct pci_dev *pdev = tp->pci_dev;
4803
Francois Romieub726e492008-06-28 12:22:59 +02004804 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4805
4806 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4807
françois romieufaf1e782013-02-27 13:01:57 +00004808 if (tp->dev->mtu <= ETH_DATA_LEN) {
4809 rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
4810 PCI_EXP_DEVCTL_NOSNOOP_EN);
4811 }
Francois Romieu219a1e92008-06-28 11:58:39 +02004812}
4813
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004814static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004815{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004816 void __iomem *ioaddr = tp->mmio_addr;
4817
4818 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004819
françois romieuf0298f82011-01-03 15:07:42 +00004820 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004821
4822 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004823}
4824
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004825static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004826{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004827 void __iomem *ioaddr = tp->mmio_addr;
4828 struct pci_dev *pdev = tp->pci_dev;
4829
Francois Romieub726e492008-06-28 12:22:59 +02004830 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4831
4832 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4833
françois romieufaf1e782013-02-27 13:01:57 +00004834 if (tp->dev->mtu <= ETH_DATA_LEN)
4835 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004836
4837 rtl_disable_clock_request(pdev);
4838
4839 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004840}
4841
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004842static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004843{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004844 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004845 { 0x01, 0, 0x0001 },
4846 { 0x02, 0x0800, 0x1000 },
4847 { 0x03, 0, 0x0042 },
4848 { 0x06, 0x0080, 0x0000 },
4849 { 0x07, 0, 0x2000 }
4850 };
4851
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004852 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004853
Francois Romieufdf6fc02012-07-06 22:40:38 +02004854 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02004855
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004856 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004857}
4858
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004859static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02004860{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004861 void __iomem *ioaddr = tp->mmio_addr;
4862 struct pci_dev *pdev = tp->pci_dev;
4863
4864 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004865
4866 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4867
françois romieufaf1e782013-02-27 13:01:57 +00004868 if (tp->dev->mtu <= ETH_DATA_LEN)
4869 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieuef3386f2008-06-29 12:24:30 +02004870
4871 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4872}
4873
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004874static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004875{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004876 void __iomem *ioaddr = tp->mmio_addr;
4877 struct pci_dev *pdev = tp->pci_dev;
4878
4879 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004880
4881 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4882
4883 /* Magic. */
4884 RTL_W8(DBG_REG, 0x20);
4885
françois romieuf0298f82011-01-03 15:07:42 +00004886 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004887
françois romieufaf1e782013-02-27 13:01:57 +00004888 if (tp->dev->mtu <= ETH_DATA_LEN)
4889 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004890
4891 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4892}
4893
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004894static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004895{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004896 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004897 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004898 { 0x02, 0x0800, 0x1000 },
4899 { 0x03, 0, 0x0002 },
4900 { 0x06, 0x0080, 0x0000 }
4901 };
4902
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004903 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004904
4905 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4906
Francois Romieufdf6fc02012-07-06 22:40:38 +02004907 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02004908
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004909 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004910}
4911
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004912static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004913{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004914 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004915 { 0x01, 0, 0x0001 },
4916 { 0x03, 0x0400, 0x0220 }
4917 };
4918
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004919 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004920
Francois Romieufdf6fc02012-07-06 22:40:38 +02004921 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02004922
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004923 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004924}
4925
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004926static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02004927{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004928 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004929}
4930
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004931static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02004932{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004933 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004934
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004935 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004936}
4937
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004938static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02004939{
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_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004944
4945 rtl_disable_clock_request(pdev);
4946
françois romieuf0298f82011-01-03 15:07:42 +00004947 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004948
françois romieufaf1e782013-02-27 13:01:57 +00004949 if (tp->dev->mtu <= ETH_DATA_LEN)
4950 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu5b538df2008-07-20 16:22:45 +02004951
4952 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4953}
4954
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004955static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00004956{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004957 void __iomem *ioaddr = tp->mmio_addr;
4958 struct pci_dev *pdev = tp->pci_dev;
4959
4960 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004961
françois romieufaf1e782013-02-27 13:01:57 +00004962 if (tp->dev->mtu <= ETH_DATA_LEN)
4963 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang4804b3b2011-03-21 01:50:29 +00004964
4965 RTL_W8(MaxTxPacketSize, TxPacketMax);
4966
4967 rtl_disable_clock_request(pdev);
4968}
4969
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004970static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004971{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004972 void __iomem *ioaddr = tp->mmio_addr;
4973 struct pci_dev *pdev = tp->pci_dev;
françois romieue6de30d2011-01-03 15:08:37 +00004974 static const struct ephy_info e_info_8168d_4[] = {
4975 { 0x0b, ~0, 0x48 },
4976 { 0x19, 0x20, 0x50 },
4977 { 0x0c, ~0, 0x20 }
4978 };
4979 int i;
4980
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004981 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004982
4983 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4984
4985 RTL_W8(MaxTxPacketSize, TxPacketMax);
4986
4987 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4988 const struct ephy_info *e = e_info_8168d_4 + i;
4989 u16 w;
4990
Francois Romieufdf6fc02012-07-06 22:40:38 +02004991 w = rtl_ephy_read(tp, e->offset);
4992 rtl_ephy_write(tp, 0x03, (w & e->mask) | e->bits);
françois romieue6de30d2011-01-03 15:08:37 +00004993 }
4994
4995 rtl_enable_clock_request(pdev);
4996}
4997
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004998static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00004999{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005000 void __iomem *ioaddr = tp->mmio_addr;
5001 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08005002 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00005003 { 0x00, 0x0200, 0x0100 },
5004 { 0x00, 0x0000, 0x0004 },
5005 { 0x06, 0x0002, 0x0001 },
5006 { 0x06, 0x0000, 0x0030 },
5007 { 0x07, 0x0000, 0x2000 },
5008 { 0x00, 0x0000, 0x0020 },
5009 { 0x03, 0x5800, 0x2000 },
5010 { 0x03, 0x0000, 0x0001 },
5011 { 0x01, 0x0800, 0x1000 },
5012 { 0x07, 0x0000, 0x4000 },
5013 { 0x1e, 0x0000, 0x2000 },
5014 { 0x19, 0xffff, 0xfe6c },
5015 { 0x0a, 0x0000, 0x0040 }
5016 };
5017
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005018 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005019
Francois Romieufdf6fc02012-07-06 22:40:38 +02005020 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00005021
françois romieufaf1e782013-02-27 13:01:57 +00005022 if (tp->dev->mtu <= ETH_DATA_LEN)
5023 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang01dc7fe2011-03-21 01:50:28 +00005024
5025 RTL_W8(MaxTxPacketSize, TxPacketMax);
5026
5027 rtl_disable_clock_request(pdev);
5028
5029 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02005030 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
5031 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00005032
Francois Romieucecb5fd2011-04-01 10:21:07 +02005033 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00005034}
5035
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005036static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08005037{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005038 void __iomem *ioaddr = tp->mmio_addr;
5039 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08005040 static const struct ephy_info e_info_8168e_2[] = {
5041 { 0x09, 0x0000, 0x0080 },
5042 { 0x19, 0x0000, 0x0224 }
5043 };
5044
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005045 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005046
Francois Romieufdf6fc02012-07-06 22:40:38 +02005047 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08005048
françois romieufaf1e782013-02-27 13:01:57 +00005049 if (tp->dev->mtu <= ETH_DATA_LEN)
5050 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Hayes Wang70090422011-07-06 15:58:06 +08005051
Francois Romieufdf6fc02012-07-06 22:40:38 +02005052 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5053 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5054 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5055 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5056 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5057 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
5058 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5059 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08005060
Hayes Wang3090bd92011-09-06 16:55:15 +08005061 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005062
Francois Romieu4521e1a92012-11-01 16:46:28 +00005063 rtl_disable_clock_request(pdev);
5064
Hayes Wang70090422011-07-06 15:58:06 +08005065 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5066 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5067
5068 /* Adjust EEE LED frequency */
5069 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5070
5071 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5072 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005073 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wang70090422011-07-06 15:58:06 +08005074}
5075
Hayes Wang5f886e02012-03-30 14:33:03 +08005076static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005077{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005078 void __iomem *ioaddr = tp->mmio_addr;
5079 struct pci_dev *pdev = tp->pci_dev;
Hayes Wangc2218922011-09-06 16:55:18 +08005080
Hayes Wang5f886e02012-03-30 14:33:03 +08005081 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005082
5083 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5084
Francois Romieufdf6fc02012-07-06 22:40:38 +02005085 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5086 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5087 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5088 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5089 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5090 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5091 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5092 rtl_w1w0_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5093 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5094 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08005095
5096 RTL_W8(MaxTxPacketSize, EarlySize);
5097
Francois Romieu4521e1a92012-11-01 16:46:28 +00005098 rtl_disable_clock_request(pdev);
5099
Hayes Wangc2218922011-09-06 16:55:18 +08005100 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5101 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
Hayes Wangc2218922011-09-06 16:55:18 +08005102 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005103 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
5104 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08005105}
5106
Hayes Wang5f886e02012-03-30 14:33:03 +08005107static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5108{
5109 void __iomem *ioaddr = tp->mmio_addr;
5110 static const struct ephy_info e_info_8168f_1[] = {
5111 { 0x06, 0x00c0, 0x0020 },
5112 { 0x08, 0x0001, 0x0002 },
5113 { 0x09, 0x0000, 0x0080 },
5114 { 0x19, 0x0000, 0x0224 }
5115 };
5116
5117 rtl_hw_start_8168f(tp);
5118
Francois Romieufdf6fc02012-07-06 22:40:38 +02005119 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08005120
Francois Romieufdf6fc02012-07-06 22:40:38 +02005121 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08005122
5123 /* Adjust EEE LED frequency */
5124 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5125}
5126
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005127static void rtl_hw_start_8411(struct rtl8169_private *tp)
5128{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005129 static const struct ephy_info e_info_8168f_1[] = {
5130 { 0x06, 0x00c0, 0x0020 },
5131 { 0x0f, 0xffff, 0x5200 },
5132 { 0x1e, 0x0000, 0x4000 },
5133 { 0x19, 0x0000, 0x0224 }
5134 };
5135
5136 rtl_hw_start_8168f(tp);
5137
Francois Romieufdf6fc02012-07-06 22:40:38 +02005138 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005139
Francois Romieufdf6fc02012-07-06 22:40:38 +02005140 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005141}
5142
Hayes Wangc5583862012-07-02 17:23:22 +08005143static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5144{
5145 void __iomem *ioaddr = tp->mmio_addr;
5146 struct pci_dev *pdev = tp->pci_dev;
5147
hayeswangbeb330a2013-04-01 22:23:39 +00005148 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5149
Hayes Wangc5583862012-07-02 17:23:22 +08005150 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5151 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5152 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5153 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5154
5155 rtl_csi_access_enable_1(tp);
5156
5157 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5158
5159 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5160 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00005161 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08005162
5163 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005164 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08005165 RTL_W8(MaxTxPacketSize, EarlySize);
5166
5167 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5168 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5169
5170 /* Adjust EEE LED frequency */
5171 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5172
hayeswangbeb330a2013-04-01 22:23:39 +00005173 rtl_w1w0_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5174 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08005175}
5176
Francois Romieu07ce4062007-02-23 23:36:39 +01005177static void rtl_hw_start_8168(struct net_device *dev)
5178{
Francois Romieu2dd99532007-06-11 23:22:52 +02005179 struct rtl8169_private *tp = netdev_priv(dev);
5180 void __iomem *ioaddr = tp->mmio_addr;
5181
5182 RTL_W8(Cfg9346, Cfg9346_Unlock);
5183
françois romieuf0298f82011-01-03 15:07:42 +00005184 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02005185
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005186 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02005187
Francois Romieu0e485152007-02-20 00:00:26 +01005188 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02005189
5190 RTL_W16(CPlusCmd, tp->cp_cmd);
5191
Francois Romieu0e485152007-02-20 00:00:26 +01005192 RTL_W16(IntrMitigate, 0x5151);
5193
5194 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00005195 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005196 tp->event_slow |= RxFIFOOver | PCSTimeout;
5197 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01005198 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005199
5200 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5201
Francois Romieub8363902008-06-01 12:31:57 +02005202 rtl_set_rx_mode(dev);
5203
5204 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
5205 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02005206
5207 RTL_R8(IntrMask);
5208
Francois Romieu219a1e92008-06-28 11:58:39 +02005209 switch (tp->mac_version) {
5210 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005211 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005212 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005213
5214 case RTL_GIGA_MAC_VER_12:
5215 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005216 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005217 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005218
5219 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005220 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005221 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005222
5223 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005224 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005225 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005226
5227 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005228 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005229 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005230
Francois Romieu197ff762008-06-28 13:16:02 +02005231 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005232 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005233 break;
Francois Romieu197ff762008-06-28 13:16:02 +02005234
Francois Romieu6fb07052008-06-29 11:54:28 +02005235 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005236 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005237 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02005238
Francois Romieuef3386f2008-06-29 12:24:30 +02005239 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005240 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005241 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02005242
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005243 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005244 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005245 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005246
Francois Romieu5b538df2008-07-20 16:22:45 +02005247 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00005248 case RTL_GIGA_MAC_VER_26:
5249 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005250 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005251 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02005252
françois romieue6de30d2011-01-03 15:08:37 +00005253 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005254 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005255 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02005256
hayeswang4804b3b2011-03-21 01:50:29 +00005257 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005258 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005259 break;
5260
hayeswang01dc7fe2011-03-21 01:50:28 +00005261 case RTL_GIGA_MAC_VER_32:
5262 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005263 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005264 break;
5265 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005266 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005267 break;
françois romieue6de30d2011-01-03 15:08:37 +00005268
Hayes Wangc2218922011-09-06 16:55:18 +08005269 case RTL_GIGA_MAC_VER_35:
5270 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005271 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005272 break;
5273
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005274 case RTL_GIGA_MAC_VER_38:
5275 rtl_hw_start_8411(tp);
5276 break;
5277
Hayes Wangc5583862012-07-02 17:23:22 +08005278 case RTL_GIGA_MAC_VER_40:
5279 case RTL_GIGA_MAC_VER_41:
5280 rtl_hw_start_8168g_1(tp);
5281 break;
5282
Francois Romieu219a1e92008-06-28 11:58:39 +02005283 default:
5284 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
5285 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00005286 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005287 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005288
Francois Romieu0e485152007-02-20 00:00:26 +01005289 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5290
Francois Romieub8363902008-06-01 12:31:57 +02005291 RTL_W8(Cfg9346, Cfg9346_Lock);
5292
Francois Romieu2dd99532007-06-11 23:22:52 +02005293 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005294}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295
Francois Romieu2857ffb2008-08-02 21:08:49 +02005296#define R810X_CPCMD_QUIRK_MASK (\
5297 EnableBist | \
5298 Mac_dbgo_oe | \
5299 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00005300 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02005301 Force_txflow_en | \
5302 Cxpl_dbg_sel | \
5303 ASF | \
5304 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005305 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005306
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005307static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005308{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005309 void __iomem *ioaddr = tp->mmio_addr;
5310 struct pci_dev *pdev = tp->pci_dev;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005311 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02005312 { 0x01, 0, 0x6e65 },
5313 { 0x02, 0, 0x091f },
5314 { 0x03, 0, 0xc2f9 },
5315 { 0x06, 0, 0xafb5 },
5316 { 0x07, 0, 0x0e00 },
5317 { 0x19, 0, 0xec80 },
5318 { 0x01, 0, 0x2e65 },
5319 { 0x01, 0, 0x6e65 }
5320 };
5321 u8 cfg1;
5322
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005323 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005324
5325 RTL_W8(DBG_REG, FIX_NAK_1);
5326
5327 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5328
5329 RTL_W8(Config1,
5330 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5331 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5332
5333 cfg1 = RTL_R8(Config1);
5334 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5335 RTL_W8(Config1, cfg1 & ~LEDS0);
5336
Francois Romieufdf6fc02012-07-06 22:40:38 +02005337 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02005338}
5339
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005340static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005341{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005342 void __iomem *ioaddr = tp->mmio_addr;
5343 struct pci_dev *pdev = tp->pci_dev;
5344
5345 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005346
5347 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5348
5349 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5350 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005351}
5352
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005353static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005354{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005355 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005356
Francois Romieufdf6fc02012-07-06 22:40:38 +02005357 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005358}
5359
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005360static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005361{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005362 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005363 static const struct ephy_info e_info_8105e_1[] = {
5364 { 0x07, 0, 0x4000 },
5365 { 0x19, 0, 0x0200 },
5366 { 0x19, 0, 0x0020 },
5367 { 0x1e, 0, 0x2000 },
5368 { 0x03, 0, 0x0001 },
5369 { 0x19, 0, 0x0100 },
5370 { 0x19, 0, 0x0004 },
5371 { 0x0a, 0, 0x0020 }
5372 };
5373
Francois Romieucecb5fd2011-04-01 10:21:07 +02005374 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005375 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5376
Francois Romieucecb5fd2011-04-01 10:21:07 +02005377 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005378 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5379
5380 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08005381 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005382
Francois Romieufdf6fc02012-07-06 22:40:38 +02005383 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
Hayes Wang5a5e4442011-02-22 17:26:21 +08005384}
5385
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005386static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005387{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005388 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005389 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005390}
5391
Hayes Wang7e18dca2012-03-30 14:33:02 +08005392static void rtl_hw_start_8402(struct rtl8169_private *tp)
5393{
5394 void __iomem *ioaddr = tp->mmio_addr;
5395 static const struct ephy_info e_info_8402[] = {
5396 { 0x19, 0xffff, 0xff64 },
5397 { 0x1e, 0, 0x4000 }
5398 };
5399
5400 rtl_csi_access_enable_2(tp);
5401
5402 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5403 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5404
5405 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5406 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5407
Francois Romieufdf6fc02012-07-06 22:40:38 +02005408 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08005409
5410 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5411
Francois Romieufdf6fc02012-07-06 22:40:38 +02005412 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5413 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
5414 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5415 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5416 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5417 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5418 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005419}
5420
Hayes Wang5598bfe2012-07-02 17:23:21 +08005421static void rtl_hw_start_8106(struct rtl8169_private *tp)
5422{
5423 void __iomem *ioaddr = tp->mmio_addr;
5424
5425 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5426 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5427
Francois Romieu4521e1a92012-11-01 16:46:28 +00005428 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005429 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
5430 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
5431}
5432
Francois Romieu07ce4062007-02-23 23:36:39 +01005433static void rtl_hw_start_8101(struct net_device *dev)
5434{
Francois Romieucdf1a602007-06-11 23:29:50 +02005435 struct rtl8169_private *tp = netdev_priv(dev);
5436 void __iomem *ioaddr = tp->mmio_addr;
5437 struct pci_dev *pdev = tp->pci_dev;
5438
Francois Romieuda78dbf2012-01-26 14:18:23 +01005439 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5440 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00005441
Francois Romieucecb5fd2011-04-01 10:21:07 +02005442 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08005443 tp->mac_version == RTL_GIGA_MAC_VER_16)
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06005444 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
5445 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02005446
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005447 RTL_W8(Cfg9346, Cfg9346_Unlock);
5448
Francois Romieu2857ffb2008-08-02 21:08:49 +02005449 switch (tp->mac_version) {
5450 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005451 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005452 break;
5453
5454 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005455 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005456 break;
5457
5458 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005459 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005460 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005461
5462 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005463 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005464 break;
5465 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005466 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005467 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005468
5469 case RTL_GIGA_MAC_VER_37:
5470 rtl_hw_start_8402(tp);
5471 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08005472
5473 case RTL_GIGA_MAC_VER_39:
5474 rtl_hw_start_8106(tp);
5475 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02005476 }
5477
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005478 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02005479
françois romieuf0298f82011-01-03 15:07:42 +00005480 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02005481
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005482 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02005483
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005484 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02005485 RTL_W16(CPlusCmd, tp->cp_cmd);
5486
5487 RTL_W16(IntrMitigate, 0x0000);
5488
5489 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5490
5491 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5492 rtl_set_rx_tx_config_registers(tp);
5493
Francois Romieucdf1a602007-06-11 23:29:50 +02005494 RTL_R8(IntrMask);
5495
Francois Romieucdf1a602007-06-11 23:29:50 +02005496 rtl_set_rx_mode(dev);
5497
5498 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499}
5500
5501static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5502{
Francois Romieud58d46b2011-05-03 16:38:29 +02005503 struct rtl8169_private *tp = netdev_priv(dev);
5504
5505 if (new_mtu < ETH_ZLEN ||
5506 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 return -EINVAL;
5508
Francois Romieud58d46b2011-05-03 16:38:29 +02005509 if (new_mtu > ETH_DATA_LEN)
5510 rtl_hw_jumbo_enable(tp);
5511 else
5512 rtl_hw_jumbo_disable(tp);
5513
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005515 netdev_update_features(dev);
5516
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005517 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518}
5519
5520static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5521{
Al Viro95e09182007-12-22 18:55:39 +00005522 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5524}
5525
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005526static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5527 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005529 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005530 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005531
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005532 kfree(*data_buff);
5533 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 rtl8169_make_unusable_by_asic(desc);
5535}
5536
5537static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5538{
5539 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5540
5541 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5542}
5543
5544static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5545 u32 rx_buf_sz)
5546{
5547 desc->addr = cpu_to_le64(mapping);
5548 wmb();
5549 rtl8169_mark_to_asic(desc, rx_buf_sz);
5550}
5551
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005552static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005554 return (void *)ALIGN((long)data, 16);
5555}
5556
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005557static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5558 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005559{
5560 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005562 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005563 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005564 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005566 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5567 if (!data)
5568 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005569
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005570 if (rtl8169_align(data) != data) {
5571 kfree(data);
5572 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5573 if (!data)
5574 return NULL;
5575 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005576
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005577 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005578 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005579 if (unlikely(dma_mapping_error(d, mapping))) {
5580 if (net_ratelimit())
5581 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005582 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584
5585 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005586 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005587
5588err_out:
5589 kfree(data);
5590 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591}
5592
5593static void rtl8169_rx_clear(struct rtl8169_private *tp)
5594{
Francois Romieu07d3f512007-02-21 22:40:46 +01005595 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596
5597 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005598 if (tp->Rx_databuff[i]) {
5599 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 tp->RxDescArray + i);
5601 }
5602 }
5603}
5604
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005605static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005607 desc->opts1 |= cpu_to_le32(RingEnd);
5608}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005609
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005610static int rtl8169_rx_fill(struct rtl8169_private *tp)
5611{
5612 unsigned int i;
5613
5614 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005615 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005616
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005617 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005619
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005620 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005621 if (!data) {
5622 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005623 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005624 }
5625 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005628 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5629 return 0;
5630
5631err_out:
5632 rtl8169_rx_clear(tp);
5633 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634}
5635
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636static int rtl8169_init_ring(struct net_device *dev)
5637{
5638 struct rtl8169_private *tp = netdev_priv(dev);
5639
5640 rtl8169_init_ring_indexes(tp);
5641
5642 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005643 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005645 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646}
5647
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005648static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 struct TxDesc *desc)
5650{
5651 unsigned int len = tx_skb->len;
5652
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005653 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5654
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 desc->opts1 = 0x00;
5656 desc->opts2 = 0x00;
5657 desc->addr = 0x00;
5658 tx_skb->len = 0;
5659}
5660
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005661static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5662 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663{
5664 unsigned int i;
5665
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005666 for (i = 0; i < n; i++) {
5667 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 struct ring_info *tx_skb = tp->tx_skb + entry;
5669 unsigned int len = tx_skb->len;
5670
5671 if (len) {
5672 struct sk_buff *skb = tx_skb->skb;
5673
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005674 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 tp->TxDescArray + entry);
5676 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005677 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 dev_kfree_skb(skb);
5679 tx_skb->skb = NULL;
5680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 }
5682 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005683}
5684
5685static void rtl8169_tx_clear(struct rtl8169_private *tp)
5686{
5687 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 tp->cur_tx = tp->dirty_tx = 0;
5689}
5690
Francois Romieu4422bcd2012-01-26 11:23:32 +01005691static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692{
David Howellsc4028952006-11-22 14:57:56 +00005693 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005694 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695
Francois Romieuda78dbf2012-01-26 14:18:23 +01005696 napi_disable(&tp->napi);
5697 netif_stop_queue(dev);
5698 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699
françois romieuc7c2c392011-12-04 20:30:52 +00005700 rtl8169_hw_reset(tp);
5701
Francois Romieu56de4142011-03-15 17:29:31 +01005702 for (i = 0; i < NUM_RX_DESC; i++)
5703 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5704
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005706 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707
Francois Romieuda78dbf2012-01-26 14:18:23 +01005708 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01005709 rtl_hw_start(dev);
5710 netif_wake_queue(dev);
5711 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712}
5713
5714static void rtl8169_tx_timeout(struct net_device *dev)
5715{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005716 struct rtl8169_private *tp = netdev_priv(dev);
5717
5718 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719}
5720
5721static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005722 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723{
5724 struct skb_shared_info *info = skb_shinfo(skb);
5725 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005726 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005727 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728
5729 entry = tp->cur_tx;
5730 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005731 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 dma_addr_t mapping;
5733 u32 status, len;
5734 void *addr;
5735
5736 entry = (entry + 1) % NUM_TX_DESC;
5737
5738 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005739 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005740 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005741 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005742 if (unlikely(dma_mapping_error(d, mapping))) {
5743 if (net_ratelimit())
5744 netif_err(tp, drv, tp->dev,
5745 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005746 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748
Francois Romieucecb5fd2011-04-01 10:21:07 +02005749 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005750 status = opts[0] | len |
5751 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752
5753 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005754 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 txd->addr = cpu_to_le64(mapping);
5756
5757 tp->tx_skb[entry].len = len;
5758 }
5759
5760 if (cur_frag) {
5761 tp->tx_skb[entry].skb = skb;
5762 txd->opts1 |= cpu_to_le32(LastFrag);
5763 }
5764
5765 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005766
5767err_out:
5768 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5769 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770}
5771
Francois Romieu2b7b4312011-04-18 22:53:24 -07005772static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5773 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005775 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005776 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005777 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778
Francois Romieu2b7b4312011-04-18 22:53:24 -07005779 if (mss) {
5780 opts[0] |= TD_LSO;
5781 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5782 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005783 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784
5785 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005786 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005788 opts[offset] |= info->checksum.udp;
5789 else
5790 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792}
5793
Stephen Hemminger613573252009-08-31 19:50:58 +00005794static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5795 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796{
5797 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005798 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 struct TxDesc *txd = tp->TxDescArray + entry;
5800 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005801 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 dma_addr_t mapping;
5803 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005804 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005805 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005806
Julien Ducourthial477206a2012-05-09 00:00:06 +02005807 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005808 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005809 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 }
5811
5812 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005813 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005815 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005816 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005817 if (unlikely(dma_mapping_error(d, mapping))) {
5818 if (net_ratelimit())
5819 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005820 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822
5823 tp->tx_skb[entry].len = len;
5824 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825
Kirill Smelkov810f4892012-11-10 21:11:02 +04005826 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
Francois Romieu2b7b4312011-04-18 22:53:24 -07005827 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005828
Francois Romieu2b7b4312011-04-18 22:53:24 -07005829 rtl8169_tso_csum(tp, skb, opts);
5830
5831 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005832 if (frags < 0)
5833 goto err_dma_1;
5834 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005835 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005836 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005837 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005838 tp->tx_skb[entry].skb = skb;
5839 }
5840
Francois Romieu2b7b4312011-04-18 22:53:24 -07005841 txd->opts2 = cpu_to_le32(opts[1]);
5842
Richard Cochran5047fb52012-03-10 07:29:42 +00005843 skb_tx_timestamp(skb);
5844
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 wmb();
5846
Francois Romieucecb5fd2011-04-01 10:21:07 +02005847 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005848 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 txd->opts1 = cpu_to_le32(status);
5850
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 tp->cur_tx += frags + 1;
5852
David Dillow4c020a92010-03-03 16:33:10 +00005853 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854
Francois Romieucecb5fd2011-04-01 10:21:07 +02005855 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856
Francois Romieuda78dbf2012-01-26 14:18:23 +01005857 mmiowb();
5858
Julien Ducourthial477206a2012-05-09 00:00:06 +02005859 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01005860 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5861 * not miss a ring update when it notices a stopped queue.
5862 */
5863 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01005865 /* Sync with rtl_tx:
5866 * - publish queue status and cur_tx ring index (write barrier)
5867 * - refresh dirty_tx ring index (read barrier).
5868 * May the current thread have a pessimistic view of the ring
5869 * status and forget to wake up queue, a racing rtl_tx thread
5870 * can't.
5871 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005872 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02005873 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 netif_wake_queue(dev);
5875 }
5876
Stephen Hemminger613573252009-08-31 19:50:58 +00005877 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005879err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005880 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005881err_dma_0:
5882 dev_kfree_skb(skb);
5883 dev->stats.tx_dropped++;
5884 return NETDEV_TX_OK;
5885
5886err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005888 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005889 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890}
5891
5892static void rtl8169_pcierr_interrupt(struct net_device *dev)
5893{
5894 struct rtl8169_private *tp = netdev_priv(dev);
5895 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 u16 pci_status, pci_cmd;
5897
5898 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5899 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5900
Joe Perchesbf82c182010-02-09 11:49:50 +00005901 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5902 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903
5904 /*
5905 * The recovery sequence below admits a very elaborated explanation:
5906 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005907 * - I did not see what else could be done;
5908 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 *
5910 * Feel free to adjust to your needs.
5911 */
Francois Romieua27993f2006-12-18 00:04:19 +01005912 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005913 pci_cmd &= ~PCI_COMMAND_PARITY;
5914 else
5915 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5916
5917 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918
5919 pci_write_config_word(pdev, PCI_STATUS,
5920 pci_status & (PCI_STATUS_DETECTED_PARITY |
5921 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5922 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5923
5924 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00005925 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005926 void __iomem *ioaddr = tp->mmio_addr;
5927
Joe Perchesbf82c182010-02-09 11:49:50 +00005928 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 tp->cp_cmd &= ~PCIDAC;
5930 RTL_W16(CPlusCmd, tp->cp_cmd);
5931 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932 }
5933
françois romieue6de30d2011-01-03 15:08:37 +00005934 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005935
Francois Romieu98ddf982012-01-31 10:47:34 +01005936 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937}
5938
Francois Romieuda78dbf2012-01-26 14:18:23 +01005939static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940{
5941 unsigned int dirty_tx, tx_left;
5942
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 dirty_tx = tp->dirty_tx;
5944 smp_rmb();
5945 tx_left = tp->cur_tx - dirty_tx;
5946
5947 while (tx_left > 0) {
5948 unsigned int entry = dirty_tx % NUM_TX_DESC;
5949 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 u32 status;
5951
5952 rmb();
5953 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5954 if (status & DescOwn)
5955 break;
5956
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005957 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5958 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 if (status & LastFrag) {
Francois Romieu17bcb682012-07-23 22:55:55 +02005960 u64_stats_update_begin(&tp->tx_stats.syncp);
5961 tp->tx_stats.packets++;
5962 tp->tx_stats.bytes += tx_skb->skb->len;
5963 u64_stats_update_end(&tp->tx_stats.syncp);
5964 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 tx_skb->skb = NULL;
5966 }
5967 dirty_tx++;
5968 tx_left--;
5969 }
5970
5971 if (tp->dirty_tx != dirty_tx) {
5972 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005973 /* Sync with rtl8169_start_xmit:
5974 * - publish dirty_tx ring index (write barrier)
5975 * - refresh cur_tx ring index and queue status (read barrier)
5976 * May the current thread miss the stopped queue condition,
5977 * a racing xmit thread can only have a right view of the
5978 * ring status.
5979 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005980 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02005982 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 netif_wake_queue(dev);
5984 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005985 /*
5986 * 8168 hack: TxPoll requests are lost when the Tx packets are
5987 * too close. Let's kick an extra TxPoll request when a burst
5988 * of start_xmit activity is detected (if it is not detected,
5989 * it is slow enough). -- FR
5990 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005991 if (tp->cur_tx != dirty_tx) {
5992 void __iomem *ioaddr = tp->mmio_addr;
5993
Francois Romieud78ae2d2007-08-26 20:08:19 +02005994 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 }
5997}
5998
Francois Romieu126fa4b2005-05-12 20:09:17 -04005999static inline int rtl8169_fragmented_frame(u32 status)
6000{
6001 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
6002}
6003
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006004static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 u32 status = opts1 & RxProtoMask;
6007
6008 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00006009 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 skb->ip_summed = CHECKSUM_UNNECESSARY;
6011 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006012 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013}
6014
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006015static struct sk_buff *rtl8169_try_rx_copy(void *data,
6016 struct rtl8169_private *tp,
6017 int pkt_size,
6018 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02006020 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006021 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006023 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006024 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006025 prefetch(data);
6026 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
6027 if (skb)
6028 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006029 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6030
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006031 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032}
6033
Francois Romieuda78dbf2012-01-26 14:18:23 +01006034static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035{
6036 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006037 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040
Timo Teräs9fba0812013-01-15 21:01:24 +00006041 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006043 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 u32 status;
6045
6046 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04006047 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048
6049 if (status & DescOwn)
6050 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02006051 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006052 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6053 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006054 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02006056 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02006058 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02006059 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006060 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006061 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02006062 }
Ben Greear6bbe0212012-02-10 15:04:33 +00006063 if ((status & (RxRUNT | RxCRC)) &&
6064 !(status & (RxRWT | RxFOVF)) &&
6065 (dev->features & NETIF_F_RXALL))
6066 goto process_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006068 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00006069 dma_addr_t addr;
6070 int pkt_size;
6071
6072process_pkt:
6073 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00006074 if (likely(!(dev->features & NETIF_F_RXFCS)))
6075 pkt_size = (status & 0x00003fff) - 4;
6076 else
6077 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078
Francois Romieu126fa4b2005-05-12 20:09:17 -04006079 /*
6080 * The driver does not support incoming fragmented
6081 * frames. They are seen as a symptom of over-mtu
6082 * sized frames.
6083 */
6084 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02006085 dev->stats.rx_dropped++;
6086 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00006087 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006088 }
6089
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006090 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6091 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006092 if (!skb) {
6093 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00006094 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 }
6096
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006097 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 skb_put(skb, pkt_size);
6099 skb->protocol = eth_type_trans(skb, dev);
6100
Francois Romieu7a8fc772011-03-01 17:18:33 +01006101 rtl8169_rx_vlan_tag(desc, skb);
6102
Francois Romieu56de4142011-03-15 17:29:31 +01006103 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104
Junchang Wang8027aa22012-03-04 23:30:32 +01006105 u64_stats_update_begin(&tp->rx_stats.syncp);
6106 tp->rx_stats.packets++;
6107 tp->rx_stats.bytes += pkt_size;
6108 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 }
françois romieuce11ff52013-01-24 13:30:06 +00006110release_descriptor:
6111 desc->opts2 = 0;
6112 wmb();
6113 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114 }
6115
6116 count = cur_rx - tp->cur_rx;
6117 tp->cur_rx = cur_rx;
6118
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 return count;
6120}
6121
Francois Romieu07d3f512007-02-21 22:40:46 +01006122static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123{
Francois Romieu07d3f512007-02-21 22:40:46 +01006124 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01006127 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128
Francois Romieu9085cdfa2012-01-26 12:59:08 +01006129 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006130 if (status && status != 0xffff) {
6131 status &= RTL_EVENT_NAPI | tp->event_slow;
6132 if (status) {
6133 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00006134
Francois Romieuda78dbf2012-01-26 14:18:23 +01006135 rtl_irq_disable(tp);
6136 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139 return IRQ_RETVAL(handled);
6140}
6141
Francois Romieuda78dbf2012-01-26 14:18:23 +01006142/*
6143 * Workqueue context.
6144 */
6145static void rtl_slow_event_work(struct rtl8169_private *tp)
6146{
6147 struct net_device *dev = tp->dev;
6148 u16 status;
6149
6150 status = rtl_get_events(tp) & tp->event_slow;
6151 rtl_ack_events(tp, status);
6152
6153 if (unlikely(status & RxFIFOOver)) {
6154 switch (tp->mac_version) {
6155 /* Work around for rx fifo overflow */
6156 case RTL_GIGA_MAC_VER_11:
6157 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01006158 /* XXX - Hack alert. See rtl_task(). */
6159 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006160 default:
6161 break;
6162 }
6163 }
6164
6165 if (unlikely(status & SYSErr))
6166 rtl8169_pcierr_interrupt(dev);
6167
6168 if (status & LinkChg)
6169 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
6170
françois romieu7dbb4912012-06-09 10:53:16 +00006171 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006172}
6173
Francois Romieu4422bcd2012-01-26 11:23:32 +01006174static void rtl_task(struct work_struct *work)
6175{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006176 static const struct {
6177 int bitnr;
6178 void (*action)(struct rtl8169_private *);
6179 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01006180 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006181 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
6182 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
6183 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
6184 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01006185 struct rtl8169_private *tp =
6186 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006187 struct net_device *dev = tp->dev;
6188 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01006189
Francois Romieuda78dbf2012-01-26 14:18:23 +01006190 rtl_lock_work(tp);
6191
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006192 if (!netif_running(dev) ||
6193 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01006194 goto out_unlock;
6195
6196 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
6197 bool pending;
6198
Francois Romieuda78dbf2012-01-26 14:18:23 +01006199 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006200 if (pending)
6201 rtl_work[i].action(tp);
6202 }
6203
6204out_unlock:
6205 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01006206}
6207
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006208static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006210 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
6211 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006212 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
6213 int work_done= 0;
6214 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215
Francois Romieuda78dbf2012-01-26 14:18:23 +01006216 status = rtl_get_events(tp);
6217 rtl_ack_events(tp, status & ~tp->event_slow);
6218
6219 if (status & RTL_EVENT_NAPI_RX)
6220 work_done = rtl_rx(dev, tp, (u32) budget);
6221
6222 if (status & RTL_EVENT_NAPI_TX)
6223 rtl_tx(dev, tp);
6224
6225 if (status & tp->event_slow) {
6226 enable_mask &= ~tp->event_slow;
6227
6228 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
6229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006231 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006232 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00006233
Francois Romieuda78dbf2012-01-26 14:18:23 +01006234 rtl_irq_enable(tp, enable_mask);
6235 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236 }
6237
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006238 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240
Francois Romieu523a6092008-09-10 22:28:56 +02006241static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
6242{
6243 struct rtl8169_private *tp = netdev_priv(dev);
6244
6245 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6246 return;
6247
6248 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
6249 RTL_W32(RxMissed, 0);
6250}
6251
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252static void rtl8169_down(struct net_device *dev)
6253{
6254 struct rtl8169_private *tp = netdev_priv(dev);
6255 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256
Francois Romieu4876cc12011-03-11 21:07:11 +01006257 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01006259 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006260 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261
Hayes Wang92fc43b2011-07-06 15:58:03 +08006262 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006263 /*
6264 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01006265 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6266 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006267 */
Francois Romieu523a6092008-09-10 22:28:56 +02006268 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006271 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 rtl8169_tx_clear(tp);
6274
6275 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00006276
6277 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278}
6279
6280static int rtl8169_close(struct net_device *dev)
6281{
6282 struct rtl8169_private *tp = netdev_priv(dev);
6283 struct pci_dev *pdev = tp->pci_dev;
6284
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006285 pm_runtime_get_sync(&pdev->dev);
6286
Francois Romieucecb5fd2011-04-01 10:21:07 +02006287 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08006288 rtl8169_update_counters(dev);
6289
Francois Romieuda78dbf2012-01-26 14:18:23 +01006290 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006291 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006292
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006294 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006296 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00006298 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6299 tp->RxPhyAddr);
6300 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6301 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 tp->TxDescArray = NULL;
6303 tp->RxDescArray = NULL;
6304
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006305 pm_runtime_put_sync(&pdev->dev);
6306
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307 return 0;
6308}
6309
Francois Romieudc1c00c2012-03-08 10:06:18 +01006310#ifdef CONFIG_NET_POLL_CONTROLLER
6311static void rtl8169_netpoll(struct net_device *dev)
6312{
6313 struct rtl8169_private *tp = netdev_priv(dev);
6314
6315 rtl8169_interrupt(tp->pci_dev->irq, dev);
6316}
6317#endif
6318
Francois Romieudf43ac72012-03-08 09:48:40 +01006319static int rtl_open(struct net_device *dev)
6320{
6321 struct rtl8169_private *tp = netdev_priv(dev);
6322 void __iomem *ioaddr = tp->mmio_addr;
6323 struct pci_dev *pdev = tp->pci_dev;
6324 int retval = -ENOMEM;
6325
6326 pm_runtime_get_sync(&pdev->dev);
6327
6328 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02006329 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01006330 * dma_alloc_coherent provides more.
6331 */
6332 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6333 &tp->TxPhyAddr, GFP_KERNEL);
6334 if (!tp->TxDescArray)
6335 goto err_pm_runtime_put;
6336
6337 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6338 &tp->RxPhyAddr, GFP_KERNEL);
6339 if (!tp->RxDescArray)
6340 goto err_free_tx_0;
6341
6342 retval = rtl8169_init_ring(dev);
6343 if (retval < 0)
6344 goto err_free_rx_1;
6345
6346 INIT_WORK(&tp->wk.work, rtl_task);
6347
6348 smp_mb();
6349
6350 rtl_request_firmware(tp);
6351
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006352 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01006353 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
6354 dev->name, dev);
6355 if (retval < 0)
6356 goto err_release_fw_2;
6357
6358 rtl_lock_work(tp);
6359
6360 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6361
6362 napi_enable(&tp->napi);
6363
6364 rtl8169_init_phy(dev, tp);
6365
6366 __rtl8169_set_features(dev, dev->features);
6367
6368 rtl_pll_power_up(tp);
6369
6370 rtl_hw_start(dev);
6371
6372 netif_start_queue(dev);
6373
6374 rtl_unlock_work(tp);
6375
6376 tp->saved_wolopts = 0;
6377 pm_runtime_put_noidle(&pdev->dev);
6378
6379 rtl8169_check_link_status(dev, tp, ioaddr);
6380out:
6381 return retval;
6382
6383err_release_fw_2:
6384 rtl_release_firmware(tp);
6385 rtl8169_rx_clear(tp);
6386err_free_rx_1:
6387 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6388 tp->RxPhyAddr);
6389 tp->RxDescArray = NULL;
6390err_free_tx_0:
6391 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6392 tp->TxPhyAddr);
6393 tp->TxDescArray = NULL;
6394err_pm_runtime_put:
6395 pm_runtime_put_noidle(&pdev->dev);
6396 goto out;
6397}
6398
Junchang Wang8027aa22012-03-04 23:30:32 +01006399static struct rtnl_link_stats64 *
6400rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401{
6402 struct rtl8169_private *tp = netdev_priv(dev);
6403 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01006404 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405
Francois Romieuda78dbf2012-01-26 14:18:23 +01006406 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02006407 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02006408
Junchang Wang8027aa22012-03-04 23:30:32 +01006409 do {
6410 start = u64_stats_fetch_begin_bh(&tp->rx_stats.syncp);
6411 stats->rx_packets = tp->rx_stats.packets;
6412 stats->rx_bytes = tp->rx_stats.bytes;
6413 } while (u64_stats_fetch_retry_bh(&tp->rx_stats.syncp, start));
6414
6415
6416 do {
6417 start = u64_stats_fetch_begin_bh(&tp->tx_stats.syncp);
6418 stats->tx_packets = tp->tx_stats.packets;
6419 stats->tx_bytes = tp->tx_stats.bytes;
6420 } while (u64_stats_fetch_retry_bh(&tp->tx_stats.syncp, start));
6421
6422 stats->rx_dropped = dev->stats.rx_dropped;
6423 stats->tx_dropped = dev->stats.tx_dropped;
6424 stats->rx_length_errors = dev->stats.rx_length_errors;
6425 stats->rx_errors = dev->stats.rx_errors;
6426 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6427 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6428 stats->rx_missed_errors = dev->stats.rx_missed_errors;
6429
6430 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431}
6432
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006433static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006434{
françois romieu065c27c2011-01-03 15:08:12 +00006435 struct rtl8169_private *tp = netdev_priv(dev);
6436
Francois Romieu5d06a992006-02-23 00:47:58 +01006437 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006438 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006439
6440 netif_device_detach(dev);
6441 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006442
6443 rtl_lock_work(tp);
6444 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006445 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006446 rtl_unlock_work(tp);
6447
6448 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006449}
Francois Romieu5d06a992006-02-23 00:47:58 +01006450
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006451#ifdef CONFIG_PM
6452
6453static int rtl8169_suspend(struct device *device)
6454{
6455 struct pci_dev *pdev = to_pci_dev(device);
6456 struct net_device *dev = pci_get_drvdata(pdev);
6457
6458 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02006459
Francois Romieu5d06a992006-02-23 00:47:58 +01006460 return 0;
6461}
6462
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006463static void __rtl8169_resume(struct net_device *dev)
6464{
françois romieu065c27c2011-01-03 15:08:12 +00006465 struct rtl8169_private *tp = netdev_priv(dev);
6466
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006467 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006468
6469 rtl_pll_power_up(tp);
6470
Artem Savkovcff4c162012-04-03 10:29:11 +00006471 rtl_lock_work(tp);
6472 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006473 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00006474 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006475
Francois Romieu98ddf982012-01-31 10:47:34 +01006476 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006477}
6478
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006479static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006480{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006481 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01006482 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006483 struct rtl8169_private *tp = netdev_priv(dev);
6484
6485 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01006486
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006487 if (netif_running(dev))
6488 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006489
Francois Romieu5d06a992006-02-23 00:47:58 +01006490 return 0;
6491}
6492
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006493static int rtl8169_runtime_suspend(struct device *device)
6494{
6495 struct pci_dev *pdev = to_pci_dev(device);
6496 struct net_device *dev = pci_get_drvdata(pdev);
6497 struct rtl8169_private *tp = netdev_priv(dev);
6498
6499 if (!tp->TxDescArray)
6500 return 0;
6501
Francois Romieuda78dbf2012-01-26 14:18:23 +01006502 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006503 tp->saved_wolopts = __rtl8169_get_wol(tp);
6504 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006505 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006506
6507 rtl8169_net_suspend(dev);
6508
6509 return 0;
6510}
6511
6512static int rtl8169_runtime_resume(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
6518 if (!tp->TxDescArray)
6519 return 0;
6520
Francois Romieuda78dbf2012-01-26 14:18:23 +01006521 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006522 __rtl8169_set_wol(tp, tp->saved_wolopts);
6523 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006524 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006525
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006526 rtl8169_init_phy(dev, tp);
6527
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006528 __rtl8169_resume(dev);
6529
6530 return 0;
6531}
6532
6533static int rtl8169_runtime_idle(struct device *device)
6534{
6535 struct pci_dev *pdev = to_pci_dev(device);
6536 struct net_device *dev = pci_get_drvdata(pdev);
6537 struct rtl8169_private *tp = netdev_priv(dev);
6538
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00006539 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006540}
6541
Alexey Dobriyan47145212009-12-14 18:00:08 -08006542static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006543 .suspend = rtl8169_suspend,
6544 .resume = rtl8169_resume,
6545 .freeze = rtl8169_suspend,
6546 .thaw = rtl8169_resume,
6547 .poweroff = rtl8169_suspend,
6548 .restore = rtl8169_resume,
6549 .runtime_suspend = rtl8169_runtime_suspend,
6550 .runtime_resume = rtl8169_runtime_resume,
6551 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006552};
6553
6554#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6555
6556#else /* !CONFIG_PM */
6557
6558#define RTL8169_PM_OPS NULL
6559
6560#endif /* !CONFIG_PM */
6561
David S. Miller1805b2f2011-10-24 18:18:09 -04006562static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6563{
6564 void __iomem *ioaddr = tp->mmio_addr;
6565
6566 /* WoL fails with 8168b when the receiver is disabled. */
6567 switch (tp->mac_version) {
6568 case RTL_GIGA_MAC_VER_11:
6569 case RTL_GIGA_MAC_VER_12:
6570 case RTL_GIGA_MAC_VER_17:
6571 pci_clear_master(tp->pci_dev);
6572
6573 RTL_W8(ChipCmd, CmdRxEnb);
6574 /* PCI commit */
6575 RTL_R8(ChipCmd);
6576 break;
6577 default:
6578 break;
6579 }
6580}
6581
Francois Romieu1765f952008-09-13 17:21:40 +02006582static void rtl_shutdown(struct pci_dev *pdev)
6583{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006584 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006585 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00006586 struct device *d = &pdev->dev;
6587
6588 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02006589
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006590 rtl8169_net_suspend(dev);
6591
Francois Romieucecb5fd2011-04-01 10:21:07 +02006592 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006593 rtl_rar_set(tp, dev->perm_addr);
6594
Hayes Wang92fc43b2011-07-06 15:58:03 +08006595 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006596
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006597 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006598 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6599 rtl_wol_suspend_quirk(tp);
6600 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006601 }
6602
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006603 pci_wake_from_d3(pdev, true);
6604 pci_set_power_state(pdev, PCI_D3hot);
6605 }
françois romieu2a15cd22012-03-06 01:14:12 +00006606
6607 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006608}
Francois Romieu5d06a992006-02-23 00:47:58 +01006609
Bill Pembertonbaf63292012-12-03 09:23:28 -05006610static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01006611{
6612 struct net_device *dev = pci_get_drvdata(pdev);
6613 struct rtl8169_private *tp = netdev_priv(dev);
6614
6615 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6616 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6617 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6618 rtl8168_driver_stop(tp);
6619 }
6620
6621 cancel_work_sync(&tp->wk.work);
6622
Devendra Nagaad1be8d2012-05-31 01:51:20 +00006623 netif_napi_del(&tp->napi);
6624
Francois Romieue27566e2012-03-08 09:54:01 +01006625 unregister_netdev(dev);
6626
6627 rtl_release_firmware(tp);
6628
6629 if (pci_dev_run_wake(pdev))
6630 pm_runtime_get_noresume(&pdev->dev);
6631
6632 /* restore original MAC address */
6633 rtl_rar_set(tp, dev->perm_addr);
6634
6635 rtl_disable_msi(pdev, tp);
6636 rtl8169_release_board(pdev, dev, tp->mmio_addr);
6637 pci_set_drvdata(pdev, NULL);
6638}
6639
Francois Romieufa9c3852012-03-08 10:01:50 +01006640static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01006641 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01006642 .ndo_stop = rtl8169_close,
6643 .ndo_get_stats64 = rtl8169_get_stats64,
6644 .ndo_start_xmit = rtl8169_start_xmit,
6645 .ndo_tx_timeout = rtl8169_tx_timeout,
6646 .ndo_validate_addr = eth_validate_addr,
6647 .ndo_change_mtu = rtl8169_change_mtu,
6648 .ndo_fix_features = rtl8169_fix_features,
6649 .ndo_set_features = rtl8169_set_features,
6650 .ndo_set_mac_address = rtl_set_mac_address,
6651 .ndo_do_ioctl = rtl8169_ioctl,
6652 .ndo_set_rx_mode = rtl_set_rx_mode,
6653#ifdef CONFIG_NET_POLL_CONTROLLER
6654 .ndo_poll_controller = rtl8169_netpoll,
6655#endif
6656
6657};
6658
Francois Romieu31fa8b12012-03-08 10:09:40 +01006659static const struct rtl_cfg_info {
6660 void (*hw_start)(struct net_device *);
6661 unsigned int region;
6662 unsigned int align;
6663 u16 event_slow;
6664 unsigned features;
6665 u8 default_ver;
6666} rtl_cfg_infos [] = {
6667 [RTL_CFG_0] = {
6668 .hw_start = rtl_hw_start_8169,
6669 .region = 1,
6670 .align = 0,
6671 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
6672 .features = RTL_FEATURE_GMII,
6673 .default_ver = RTL_GIGA_MAC_VER_01,
6674 },
6675 [RTL_CFG_1] = {
6676 .hw_start = rtl_hw_start_8168,
6677 .region = 2,
6678 .align = 8,
6679 .event_slow = SYSErr | LinkChg | RxOverflow,
6680 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
6681 .default_ver = RTL_GIGA_MAC_VER_11,
6682 },
6683 [RTL_CFG_2] = {
6684 .hw_start = rtl_hw_start_8101,
6685 .region = 2,
6686 .align = 8,
6687 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
6688 PCSTimeout,
6689 .features = RTL_FEATURE_MSI,
6690 .default_ver = RTL_GIGA_MAC_VER_13,
6691 }
6692};
6693
6694/* Cfg9346_Unlock assumed. */
6695static unsigned rtl_try_msi(struct rtl8169_private *tp,
6696 const struct rtl_cfg_info *cfg)
6697{
6698 void __iomem *ioaddr = tp->mmio_addr;
6699 unsigned msi = 0;
6700 u8 cfg2;
6701
6702 cfg2 = RTL_R8(Config2) & ~MSIEnable;
6703 if (cfg->features & RTL_FEATURE_MSI) {
6704 if (pci_enable_msi(tp->pci_dev)) {
6705 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
6706 } else {
6707 cfg2 |= MSIEnable;
6708 msi = RTL_FEATURE_MSI;
6709 }
6710 }
6711 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6712 RTL_W8(Config2, cfg2);
6713 return msi;
6714}
6715
Hayes Wangc5583862012-07-02 17:23:22 +08006716DECLARE_RTL_COND(rtl_link_list_ready_cond)
6717{
6718 void __iomem *ioaddr = tp->mmio_addr;
6719
6720 return RTL_R8(MCU) & LINK_LIST_RDY;
6721}
6722
6723DECLARE_RTL_COND(rtl_rxtx_empty_cond)
6724{
6725 void __iomem *ioaddr = tp->mmio_addr;
6726
6727 return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
6728}
6729
Bill Pembertonbaf63292012-12-03 09:23:28 -05006730static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006731{
6732 void __iomem *ioaddr = tp->mmio_addr;
6733 u32 data;
6734
6735 tp->ocp_base = OCP_STD_PHY_BASE;
6736
6737 RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
6738
6739 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
6740 return;
6741
6742 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
6743 return;
6744
6745 RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
6746 msleep(1);
6747 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6748
Hayes Wang5f8bcce2012-07-10 08:47:05 +02006749 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08006750 data &= ~(1 << 14);
6751 r8168_mac_ocp_write(tp, 0xe8de, data);
6752
6753 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
6754 return;
6755
Hayes Wang5f8bcce2012-07-10 08:47:05 +02006756 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08006757 data |= (1 << 15);
6758 r8168_mac_ocp_write(tp, 0xe8de, data);
6759
6760 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
6761 return;
6762}
6763
Bill Pembertonbaf63292012-12-03 09:23:28 -05006764static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006765{
6766 switch (tp->mac_version) {
6767 case RTL_GIGA_MAC_VER_40:
6768 case RTL_GIGA_MAC_VER_41:
6769 rtl_hw_init_8168g(tp);
6770 break;
6771
6772 default:
6773 break;
6774 }
6775}
6776
Bill Pembertonbaf63292012-12-03 09:23:28 -05006777static int
Francois Romieu3b6cf252012-03-08 09:59:04 +01006778rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6779{
6780 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
6781 const unsigned int region = cfg->region;
6782 struct rtl8169_private *tp;
6783 struct mii_if_info *mii;
6784 struct net_device *dev;
6785 void __iomem *ioaddr;
6786 int chipset, i;
6787 int rc;
6788
6789 if (netif_msg_drv(&debug)) {
6790 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
6791 MODULENAME, RTL8169_VERSION);
6792 }
6793
6794 dev = alloc_etherdev(sizeof (*tp));
6795 if (!dev) {
6796 rc = -ENOMEM;
6797 goto out;
6798 }
6799
6800 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01006801 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006802 tp = netdev_priv(dev);
6803 tp->dev = dev;
6804 tp->pci_dev = pdev;
6805 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
6806
6807 mii = &tp->mii;
6808 mii->dev = dev;
6809 mii->mdio_read = rtl_mdio_read;
6810 mii->mdio_write = rtl_mdio_write;
6811 mii->phy_id_mask = 0x1f;
6812 mii->reg_num_mask = 0x1f;
6813 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
6814
6815 /* disable ASPM completely as that cause random device stop working
6816 * problems as well as full system hangs for some PCIe devices users */
6817 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6818 PCIE_LINK_STATE_CLKPM);
6819
6820 /* enable device (incl. PCI PM wakeup and hotplug setup) */
6821 rc = pci_enable_device(pdev);
6822 if (rc < 0) {
6823 netif_err(tp, probe, dev, "enable failure\n");
6824 goto err_out_free_dev_1;
6825 }
6826
6827 if (pci_set_mwi(pdev) < 0)
6828 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
6829
6830 /* make sure PCI base addr 1 is MMIO */
6831 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
6832 netif_err(tp, probe, dev,
6833 "region #%d not an MMIO resource, aborting\n",
6834 region);
6835 rc = -ENODEV;
6836 goto err_out_mwi_2;
6837 }
6838
6839 /* check for weird/broken PCI region reporting */
6840 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
6841 netif_err(tp, probe, dev,
6842 "Invalid PCI region size(s), aborting\n");
6843 rc = -ENODEV;
6844 goto err_out_mwi_2;
6845 }
6846
6847 rc = pci_request_regions(pdev, MODULENAME);
6848 if (rc < 0) {
6849 netif_err(tp, probe, dev, "could not request regions\n");
6850 goto err_out_mwi_2;
6851 }
6852
6853 tp->cp_cmd = RxChkSum;
6854
6855 if ((sizeof(dma_addr_t) > 4) &&
6856 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
6857 tp->cp_cmd |= PCIDAC;
6858 dev->features |= NETIF_F_HIGHDMA;
6859 } else {
6860 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6861 if (rc < 0) {
6862 netif_err(tp, probe, dev, "DMA configuration failed\n");
6863 goto err_out_free_res_3;
6864 }
6865 }
6866
6867 /* ioremap MMIO region */
6868 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
6869 if (!ioaddr) {
6870 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
6871 rc = -EIO;
6872 goto err_out_free_res_3;
6873 }
6874 tp->mmio_addr = ioaddr;
6875
6876 if (!pci_is_pcie(pdev))
6877 netif_info(tp, probe, dev, "not PCI Express\n");
6878
6879 /* Identify chip attached to board */
6880 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
6881
6882 rtl_init_rxcfg(tp);
6883
6884 rtl_irq_disable(tp);
6885
Hayes Wangc5583862012-07-02 17:23:22 +08006886 rtl_hw_initialize(tp);
6887
Francois Romieu3b6cf252012-03-08 09:59:04 +01006888 rtl_hw_reset(tp);
6889
6890 rtl_ack_events(tp, 0xffff);
6891
6892 pci_set_master(pdev);
6893
6894 /*
6895 * Pretend we are using VLANs; This bypasses a nasty bug where
6896 * Interrupts stop flowing on high load on 8110SCd controllers.
6897 */
6898 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6899 tp->cp_cmd |= RxVlan;
6900
6901 rtl_init_mdio_ops(tp);
6902 rtl_init_pll_power_ops(tp);
6903 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006904 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006905
6906 rtl8169_print_mac_version(tp);
6907
6908 chipset = tp->mac_version;
6909 tp->txd_version = rtl_chip_infos[chipset].txd_version;
6910
6911 RTL_W8(Cfg9346, Cfg9346_Unlock);
6912 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
6913 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
6914 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
6915 tp->features |= RTL_FEATURE_WOL;
6916 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
6917 tp->features |= RTL_FEATURE_WOL;
6918 tp->features |= rtl_try_msi(tp, cfg);
6919 RTL_W8(Cfg9346, Cfg9346_Lock);
6920
6921 if (rtl_tbi_enabled(tp)) {
6922 tp->set_speed = rtl8169_set_speed_tbi;
6923 tp->get_settings = rtl8169_gset_tbi;
6924 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
6925 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
6926 tp->link_ok = rtl8169_tbi_link_ok;
6927 tp->do_ioctl = rtl_tbi_ioctl;
6928 } else {
6929 tp->set_speed = rtl8169_set_speed_xmii;
6930 tp->get_settings = rtl8169_gset_xmii;
6931 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
6932 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
6933 tp->link_ok = rtl8169_xmii_link_ok;
6934 tp->do_ioctl = rtl_xmii_ioctl;
6935 }
6936
6937 mutex_init(&tp->wk.mutex);
6938
6939 /* Get MAC address */
6940 for (i = 0; i < ETH_ALEN; i++)
6941 dev->dev_addr[i] = RTL_R8(MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006942
6943 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
6944 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006945
6946 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
6947
6948 /* don't enable SG, IP_CSUM and TSO by default - it might not work
6949 * properly for all devices */
6950 dev->features |= NETIF_F_RXCSUM |
6951 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6952
6953 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6954 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6955 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6956 NETIF_F_HIGHDMA;
6957
6958 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6959 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
6960 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
6961
6962 dev->hw_features |= NETIF_F_RXALL;
6963 dev->hw_features |= NETIF_F_RXFCS;
6964
6965 tp->hw_start = cfg->hw_start;
6966 tp->event_slow = cfg->event_slow;
6967
6968 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
6969 ~(RxBOVF | RxFOVF) : ~0;
6970
6971 init_timer(&tp->timer);
6972 tp->timer.data = (unsigned long) dev;
6973 tp->timer.function = rtl8169_phy_timer;
6974
6975 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
6976
6977 rc = register_netdev(dev);
6978 if (rc < 0)
6979 goto err_out_msi_4;
6980
6981 pci_set_drvdata(pdev, dev);
6982
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006983 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
6984 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
6985 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006986 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
6987 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
6988 "tx checksumming: %s]\n",
6989 rtl_chip_infos[chipset].jumbo_max,
6990 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
6991 }
6992
6993 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6994 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6995 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6996 rtl8168_driver_start(tp);
6997 }
6998
6999 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
7000
7001 if (pci_dev_run_wake(pdev))
7002 pm_runtime_put_noidle(&pdev->dev);
7003
7004 netif_carrier_off(dev);
7005
7006out:
7007 return rc;
7008
7009err_out_msi_4:
Devendra Nagaad1be8d2012-05-31 01:51:20 +00007010 netif_napi_del(&tp->napi);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007011 rtl_disable_msi(pdev, tp);
7012 iounmap(ioaddr);
7013err_out_free_res_3:
7014 pci_release_regions(pdev);
7015err_out_mwi_2:
7016 pci_clear_mwi(pdev);
7017 pci_disable_device(pdev);
7018err_out_free_dev_1:
7019 free_netdev(dev);
7020 goto out;
7021}
7022
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023static struct pci_driver rtl8169_pci_driver = {
7024 .name = MODULENAME,
7025 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01007026 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05007027 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02007028 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007029 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030};
7031
Devendra Naga3eeb7da2012-10-26 09:27:42 +00007032module_pci_driver(rtl8169_pci_driver);