blob: e5a0bdbfbb2b7311c611df4d8ada990becbab743 [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>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000024#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000026#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000027#include <linux/firmware.h>
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +000028#include <linux/pci-aspm.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040029#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <asm/io.h>
32#include <asm/irq.h>
33
Francois Romieu865c6522008-05-11 14:51:00 +020034#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define MODULENAME "r8169"
36#define PFX MODULENAME ": "
37
françois romieubca03d52011-01-03 15:07:31 +000038#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
39#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000040#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
41#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080042#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080043#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
44#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080045#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
Hayes Wang7e18dca2012-03-30 14:33:02 +080046#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
Hayes Wangb3d7b2f2012-03-30 14:48:06 +080047#define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
hayeswang45dd95c2013-07-08 17:09:01 +080048#define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
Hayes Wang5598bfe2012-07-02 17:23:21 +080049#define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
hayeswang58152cd2013-04-01 22:23:42 +000050#define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
hayeswangbeb330a2013-04-01 22:23:39 +000051#define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
hayeswang57538c42013-04-01 22:23:40 +000052#define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
françois romieubca03d52011-01-03 15:07:31 +000053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#ifdef RTL8169_DEBUG
55#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020056 if (!(expr)) { \
57 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070058 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020059 }
Joe Perches06fa7352007-10-18 21:15:00 +020060#define dprintk(fmt, args...) \
61 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#else
63#define assert(expr) do {} while (0)
64#define dprintk(fmt, args...) do {} while (0)
65#endif /* RTL8169_DEBUG */
66
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020067#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070068 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020069
Julien Ducourthial477206a2012-05-09 00:00:06 +020070#define TX_SLOTS_AVAIL(tp) \
71 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
72
73/* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
74#define TX_FRAGS_READY_FOR(tp,nr_frags) \
75 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
78 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050079static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Francois Romieu9c14cea2008-07-05 00:21:15 +020081#define MAX_READ_REQUEST_SHIFT 12
Michal Schmidtaee77e42012-09-09 13:55:26 +000082#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
84
85#define R8169_REGS_SIZE 256
86#define R8169_NAPI_WEIGHT 64
87#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000088#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
90#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
91
92#define RTL8169_TX_TIMEOUT (6*HZ)
93#define RTL8169_PHY_TIMEOUT (10*HZ)
94
95/* write/read MMIO register */
96#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
97#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
98#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
99#define RTL_R8(reg) readb (ioaddr + (reg))
100#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +0000101#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200104 RTL_GIGA_MAC_VER_01 = 0,
105 RTL_GIGA_MAC_VER_02,
106 RTL_GIGA_MAC_VER_03,
107 RTL_GIGA_MAC_VER_04,
108 RTL_GIGA_MAC_VER_05,
109 RTL_GIGA_MAC_VER_06,
110 RTL_GIGA_MAC_VER_07,
111 RTL_GIGA_MAC_VER_08,
112 RTL_GIGA_MAC_VER_09,
113 RTL_GIGA_MAC_VER_10,
114 RTL_GIGA_MAC_VER_11,
115 RTL_GIGA_MAC_VER_12,
116 RTL_GIGA_MAC_VER_13,
117 RTL_GIGA_MAC_VER_14,
118 RTL_GIGA_MAC_VER_15,
119 RTL_GIGA_MAC_VER_16,
120 RTL_GIGA_MAC_VER_17,
121 RTL_GIGA_MAC_VER_18,
122 RTL_GIGA_MAC_VER_19,
123 RTL_GIGA_MAC_VER_20,
124 RTL_GIGA_MAC_VER_21,
125 RTL_GIGA_MAC_VER_22,
126 RTL_GIGA_MAC_VER_23,
127 RTL_GIGA_MAC_VER_24,
128 RTL_GIGA_MAC_VER_25,
129 RTL_GIGA_MAC_VER_26,
130 RTL_GIGA_MAC_VER_27,
131 RTL_GIGA_MAC_VER_28,
132 RTL_GIGA_MAC_VER_29,
133 RTL_GIGA_MAC_VER_30,
134 RTL_GIGA_MAC_VER_31,
135 RTL_GIGA_MAC_VER_32,
136 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800137 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800138 RTL_GIGA_MAC_VER_35,
139 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800140 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800141 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800142 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800143 RTL_GIGA_MAC_VER_40,
144 RTL_GIGA_MAC_VER_41,
hayeswang57538c42013-04-01 22:23:40 +0000145 RTL_GIGA_MAC_VER_42,
hayeswang58152cd2013-04-01 22:23:42 +0000146 RTL_GIGA_MAC_VER_43,
hayeswang45dd95c2013-07-08 17:09:01 +0800147 RTL_GIGA_MAC_VER_44,
Francois Romieu85bffe62011-04-27 08:22:39 +0200148 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149};
150
Francois Romieu2b7b4312011-04-18 22:53:24 -0700151enum rtl_tx_desc_version {
152 RTL_TD_0 = 0,
153 RTL_TD_1 = 1,
154};
155
Francois Romieud58d46b2011-05-03 16:38:29 +0200156#define JUMBO_1K ETH_DATA_LEN
157#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
158#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
159#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
160#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
161
162#define _R(NAME,TD,FW,SZ,B) { \
163 .name = NAME, \
164 .txd_version = TD, \
165 .fw_name = FW, \
166 .jumbo_max = SZ, \
167 .jumbo_tx_csum = B \
168}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800170static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700172 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200173 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200174 u16 jumbo_max;
175 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200176} rtl_chip_infos[] = {
177 /* PCI devices. */
178 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200179 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200180 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200181 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200182 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200183 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200184 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200185 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200186 [RTL_GIGA_MAC_VER_05] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200187 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200188 [RTL_GIGA_MAC_VER_06] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200189 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200190 /* PCI-E devices. */
191 [RTL_GIGA_MAC_VER_07] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200192 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200193 [RTL_GIGA_MAC_VER_08] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200194 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200195 [RTL_GIGA_MAC_VER_09] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200196 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200197 [RTL_GIGA_MAC_VER_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200198 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200199 [RTL_GIGA_MAC_VER_11] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200200 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200201 [RTL_GIGA_MAC_VER_12] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200202 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200203 [RTL_GIGA_MAC_VER_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200204 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200205 [RTL_GIGA_MAC_VER_14] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200206 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200207 [RTL_GIGA_MAC_VER_15] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200208 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200209 [RTL_GIGA_MAC_VER_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200210 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200211 [RTL_GIGA_MAC_VER_17] =
hayeswangf75761b2014-03-11 15:11:59 +0800212 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200213 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200214 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200215 [RTL_GIGA_MAC_VER_19] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200216 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200217 [RTL_GIGA_MAC_VER_20] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200218 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200219 [RTL_GIGA_MAC_VER_21] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200220 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200221 [RTL_GIGA_MAC_VER_22] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200222 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200223 [RTL_GIGA_MAC_VER_23] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200224 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200225 [RTL_GIGA_MAC_VER_24] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200226 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200227 [RTL_GIGA_MAC_VER_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200228 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
229 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200230 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200231 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
232 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200233 [RTL_GIGA_MAC_VER_27] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200234 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200235 [RTL_GIGA_MAC_VER_28] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200236 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200237 [RTL_GIGA_MAC_VER_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200238 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
239 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200240 [RTL_GIGA_MAC_VER_30] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200241 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
242 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200243 [RTL_GIGA_MAC_VER_31] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200244 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200245 [RTL_GIGA_MAC_VER_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200246 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
247 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200248 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200249 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
250 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800251 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200252 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
253 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800254 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200255 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
256 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800257 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200258 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
259 JUMBO_9K, false),
Hayes Wang7e18dca2012-03-30 14:33:02 +0800260 [RTL_GIGA_MAC_VER_37] =
261 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
262 JUMBO_1K, true),
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800263 [RTL_GIGA_MAC_VER_38] =
264 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
265 JUMBO_9K, false),
Hayes Wang5598bfe2012-07-02 17:23:21 +0800266 [RTL_GIGA_MAC_VER_39] =
267 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1,
268 JUMBO_1K, true),
Hayes Wangc5583862012-07-02 17:23:22 +0800269 [RTL_GIGA_MAC_VER_40] =
hayeswangbeb330a2013-04-01 22:23:39 +0000270 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2,
Hayes Wangc5583862012-07-02 17:23:22 +0800271 JUMBO_9K, false),
272 [RTL_GIGA_MAC_VER_41] =
273 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K, false),
hayeswang57538c42013-04-01 22:23:40 +0000274 [RTL_GIGA_MAC_VER_42] =
275 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3,
276 JUMBO_9K, false),
hayeswang58152cd2013-04-01 22:23:42 +0000277 [RTL_GIGA_MAC_VER_43] =
278 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2,
279 JUMBO_1K, true),
hayeswang45dd95c2013-07-08 17:09:01 +0800280 [RTL_GIGA_MAC_VER_44] =
281 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2,
282 JUMBO_9K, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283};
284#undef _R
285
Francois Romieubcf0bf92006-07-26 23:14:13 +0200286enum cfg_version {
287 RTL_CFG_0 = 0x00,
288 RTL_CFG_1,
289 RTL_CFG_2
290};
291
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000292static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200293 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200294 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200295 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100296 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200297 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
Francois Romieu2a35cfa2012-08-31 23:06:17 +0200298 { PCI_VENDOR_ID_DLINK, 0x4300,
299 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200300 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000301 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200302 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200303 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
304 { PCI_VENDOR_ID_LINKSYS, 0x1032,
305 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100306 { 0x0001, 0x8168,
307 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 {0,},
309};
310
311MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
312
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000313static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700314static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200315static struct {
316 u32 msg_enable;
317} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Francois Romieu07d3f512007-02-21 22:40:46 +0100319enum rtl_registers {
320 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100321 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100322 MAR0 = 8, /* Multicast filter. */
323 CounterAddrLow = 0x10,
324 CounterAddrHigh = 0x14,
325 TxDescStartAddrLow = 0x20,
326 TxDescStartAddrHigh = 0x24,
327 TxHDescStartAddrLow = 0x28,
328 TxHDescStartAddrHigh = 0x2c,
329 FLASH = 0x30,
330 ERSR = 0x36,
331 ChipCmd = 0x37,
332 TxPoll = 0x38,
333 IntrMask = 0x3c,
334 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700335
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800336 TxConfig = 0x40,
337#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
338#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
339
340 RxConfig = 0x44,
341#define RX128_INT_EN (1 << 15) /* 8111c and later */
342#define RX_MULTI_EN (1 << 14) /* 8111c only */
343#define RXCFG_FIFO_SHIFT 13
344 /* No threshold before first PCI xfer */
345#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000346#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800347#define RXCFG_DMA_SHIFT 8
348 /* Unlimited maximum PCI burst. */
349#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700350
Francois Romieu07d3f512007-02-21 22:40:46 +0100351 RxMissed = 0x4c,
352 Cfg9346 = 0x50,
353 Config0 = 0x51,
354 Config1 = 0x52,
355 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200356#define PME_SIGNAL (1 << 5) /* 8168c and later */
357
Francois Romieu07d3f512007-02-21 22:40:46 +0100358 Config3 = 0x54,
359 Config4 = 0x55,
360 Config5 = 0x56,
361 MultiIntr = 0x5c,
362 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100363 PHYstatus = 0x6c,
364 RxMaxSize = 0xda,
365 CPlusCmd = 0xe0,
366 IntrMitigate = 0xe2,
367 RxDescAddrLow = 0xe4,
368 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000369 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
370
371#define NoEarlyTx 0x3f /* Max value : no early transmit. */
372
373 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
374
375#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800376#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000377
Francois Romieu07d3f512007-02-21 22:40:46 +0100378 FuncEvent = 0xf0,
379 FuncEventMask = 0xf4,
380 FuncPresetState = 0xf8,
381 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382};
383
Francois Romieuf162a5d2008-06-01 22:37:49 +0200384enum rtl8110_registers {
385 TBICSR = 0x64,
386 TBI_ANAR = 0x68,
387 TBI_LPAR = 0x6a,
388};
389
390enum rtl8168_8101_registers {
391 CSIDR = 0x64,
392 CSIAR = 0x68,
393#define CSIAR_FLAG 0x80000000
394#define CSIAR_WRITE_CMD 0x80000000
395#define CSIAR_BYTE_ENABLE 0x0f
396#define CSIAR_BYTE_ENABLE_SHIFT 12
397#define CSIAR_ADDR_MASK 0x0fff
Hayes Wang7e18dca2012-03-30 14:33:02 +0800398#define CSIAR_FUNC_CARD 0x00000000
399#define CSIAR_FUNC_SDIO 0x00010000
400#define CSIAR_FUNC_NIC 0x00020000
hayeswang45dd95c2013-07-08 17:09:01 +0800401#define CSIAR_FUNC_NIC2 0x00010000
françois romieu065c27c2011-01-03 15:08:12 +0000402 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200403 EPHYAR = 0x80,
404#define EPHYAR_FLAG 0x80000000
405#define EPHYAR_WRITE_CMD 0x80000000
406#define EPHYAR_REG_MASK 0x1f
407#define EPHYAR_REG_SHIFT 16
408#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800409 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800410#define PFM_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200411 DBG_REG = 0xd1,
412#define FIX_NAK_1 (1 << 4)
413#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800414 TWSI = 0xd2,
415 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800416#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800417#define TX_EMPTY (1 << 5)
418#define RX_EMPTY (1 << 4)
419#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800420#define EN_NDP (1 << 3)
421#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800422#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000423 EFUSEAR = 0xdc,
424#define EFUSEAR_FLAG 0x80000000
425#define EFUSEAR_WRITE_CMD 0x80000000
426#define EFUSEAR_READ_CMD 0x00000000
427#define EFUSEAR_REG_MASK 0x03ff
428#define EFUSEAR_REG_SHIFT 8
429#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200430};
431
françois romieuc0e45c12011-01-03 15:08:04 +0000432enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800433 LED_FREQ = 0x1a,
434 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000435 ERIDR = 0x70,
436 ERIAR = 0x74,
437#define ERIAR_FLAG 0x80000000
438#define ERIAR_WRITE_CMD 0x80000000
439#define ERIAR_READ_CMD 0x00000000
440#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000441#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800442#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
443#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
444#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
445#define ERIAR_MASK_SHIFT 12
446#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
447#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800448#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800449#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000450 EPHY_RXER_NUM = 0x7c,
451 OCPDR = 0xb0, /* OCP GPHY access */
452#define OCPDR_WRITE_CMD 0x80000000
453#define OCPDR_READ_CMD 0x00000000
454#define OCPDR_REG_MASK 0x7f
455#define OCPDR_GPHY_REG_SHIFT 16
456#define OCPDR_DATA_MASK 0xffff
457 OCPAR = 0xb4,
458#define OCPAR_FLAG 0x80000000
459#define OCPAR_GPHY_WRITE_CMD 0x8000f060
460#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800461 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000462 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
463 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200464#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800465#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800466#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800467#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800468#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000469};
470
Francois Romieu07d3f512007-02-21 22:40:46 +0100471enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100473 SYSErr = 0x8000,
474 PCSTimeout = 0x4000,
475 SWInt = 0x0100,
476 TxDescUnavail = 0x0080,
477 RxFIFOOver = 0x0040,
478 LinkChg = 0x0020,
479 RxOverflow = 0x0010,
480 TxErr = 0x0008,
481 TxOK = 0x0004,
482 RxErr = 0x0002,
483 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400486 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200487 RxFOVF = (1 << 23),
488 RxRWT = (1 << 22),
489 RxRES = (1 << 21),
490 RxRUNT = (1 << 20),
491 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800494 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100495 CmdReset = 0x10,
496 CmdRxEnb = 0x08,
497 CmdTxEnb = 0x04,
498 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Francois Romieu275391a2007-02-23 23:50:28 +0100500 /* TXPoll register p.5 */
501 HPQ = 0x80, /* Poll cmd on the high prio queue */
502 NPQ = 0x40, /* Poll cmd on the low prio queue */
503 FSWInt = 0x01, /* Forced software interrupt */
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100506 Cfg9346_Lock = 0x00,
507 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100510 AcceptErr = 0x20,
511 AcceptRunt = 0x10,
512 AcceptBroadcast = 0x08,
513 AcceptMulticast = 0x04,
514 AcceptMyPhys = 0x02,
515 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200516#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /* TxConfigBits */
519 TxInterFrameGapShift = 24,
520 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
521
Francois Romieu5d06a992006-02-23 00:47:58 +0100522 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200523 LEDS1 = (1 << 7),
524 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200525 Speed_down = (1 << 4),
526 MEMMAP = (1 << 3),
527 IOMAP = (1 << 2),
528 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100529 PMEnable = (1 << 0), /* Power Management Enable */
530
Francois Romieu6dccd162007-02-13 23:38:05 +0100531 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000532 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000533 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100534 PCI_Clock_66MHz = 0x01,
535 PCI_Clock_33MHz = 0x00,
536
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100537 /* Config3 register p.25 */
538 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
539 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200540 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200541 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100542
Francois Romieud58d46b2011-05-03 16:38:29 +0200543 /* Config4 register */
544 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
545
Francois Romieu5d06a992006-02-23 00:47:58 +0100546 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100547 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
548 MWF = (1 << 5), /* Accept Multicast wakeup frame */
549 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200550 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100551 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100552 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000553 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /* TBICSR p.28 */
556 TBIReset = 0x80000000,
557 TBILoopback = 0x40000000,
558 TBINwEnable = 0x20000000,
559 TBINwRestart = 0x10000000,
560 TBILinkOk = 0x02000000,
561 TBINwComplete = 0x01000000,
562
563 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200564 EnableBist = (1 << 15), // 8168 8101
565 Mac_dbgo_oe = (1 << 14), // 8168 8101
566 Normal_mode = (1 << 13), // unused
567 Force_half_dup = (1 << 12), // 8168 8101
568 Force_rxflow_en = (1 << 11), // 8168 8101
569 Force_txflow_en = (1 << 10), // 8168 8101
570 Cxpl_dbg_sel = (1 << 9), // 8168 8101
571 ASF = (1 << 8), // 8168 8101
572 PktCntrDisable = (1 << 7), // 8168 8101
573 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 RxVlan = (1 << 6),
575 RxChkSum = (1 << 5),
576 PCIDAC = (1 << 4),
577 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100578 INTT_0 = 0x0000, // 8168
579 INTT_1 = 0x0001, // 8168
580 INTT_2 = 0x0002, // 8168
581 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100584 TBI_Enable = 0x80,
585 TxFlowCtrl = 0x40,
586 RxFlowCtrl = 0x20,
587 _1000bpsF = 0x10,
588 _100bps = 0x08,
589 _10bps = 0x04,
590 LinkStatus = 0x02,
591 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100594 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200595
596 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100597 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598};
599
Francois Romieu2b7b4312011-04-18 22:53:24 -0700600enum rtl_desc_bit {
601 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
603 RingEnd = (1 << 30), /* End of descriptor ring */
604 FirstFrag = (1 << 29), /* First segment of a packet */
605 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700606};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Francois Romieu2b7b4312011-04-18 22:53:24 -0700608/* Generic case. */
609enum rtl_tx_desc_bit {
610 /* First doubleword. */
611 TD_LSO = (1 << 27), /* Large Send Offload */
612#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Francois Romieu2b7b4312011-04-18 22:53:24 -0700614 /* Second doubleword. */
615 TxVlanTag = (1 << 17), /* Add VLAN tag */
616};
617
618/* 8169, 8168b and 810x except 8102e. */
619enum rtl_tx_desc_bit_0 {
620 /* First doubleword. */
621#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
622 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
623 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
624 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
625};
626
627/* 8102e, 8168c and beyond. */
628enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800629 /* First doubleword. */
630 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
631#define GTTCPHO_SHIFT 18
632
Francois Romieu2b7b4312011-04-18 22:53:24 -0700633 /* Second doubleword. */
634#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
635 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
636 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
637 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
638};
639
Francois Romieu2b7b4312011-04-18 22:53:24 -0700640enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 /* Rx private */
642 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
643 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
644
645#define RxProtoUDP (PID1)
646#define RxProtoTCP (PID0)
647#define RxProtoIP (PID1 | PID0)
648#define RxProtoMask RxProtoIP
649
650 IPFail = (1 << 16), /* IP checksum failed */
651 UDPFail = (1 << 15), /* UDP/IP checksum failed */
652 TCPFail = (1 << 14), /* TCP/IP checksum failed */
653 RxVlanTag = (1 << 16), /* VLAN tag available */
654};
655
656#define RsvdMask 0x3fffc000
657
658struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200659 __le32 opts1;
660 __le32 opts2;
661 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662};
663
664struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200665 __le32 opts1;
666 __le32 opts2;
667 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668};
669
670struct ring_info {
671 struct sk_buff *skb;
672 u32 len;
673 u8 __pad[sizeof(void *) - sizeof(u32)];
674};
675
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200676enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200677 RTL_FEATURE_WOL = (1 << 0),
678 RTL_FEATURE_MSI = (1 << 1),
679 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200680};
681
Ivan Vecera355423d2009-02-06 21:49:57 -0800682struct rtl8169_counters {
683 __le64 tx_packets;
684 __le64 rx_packets;
685 __le64 tx_errors;
686 __le32 rx_errors;
687 __le16 rx_missed;
688 __le16 align_errors;
689 __le32 tx_one_collision;
690 __le32 tx_multi_collision;
691 __le64 rx_unicast;
692 __le64 rx_broadcast;
693 __le32 rx_multicast;
694 __le16 tx_aborted;
695 __le16 tx_underun;
696};
697
Francois Romieuda78dbf2012-01-26 14:18:23 +0100698enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100699 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100700 RTL_FLAG_TASK_SLOW_PENDING,
701 RTL_FLAG_TASK_RESET_PENDING,
702 RTL_FLAG_TASK_PHY_PENDING,
703 RTL_FLAG_MAX
704};
705
Junchang Wang8027aa22012-03-04 23:30:32 +0100706struct rtl8169_stats {
707 u64 packets;
708 u64 bytes;
709 struct u64_stats_sync syncp;
710};
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712struct rtl8169_private {
713 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200714 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000715 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700716 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200717 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700718 u16 txd_version;
719 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
721 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100723 struct rtl8169_stats rx_stats;
724 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
726 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
727 dma_addr_t TxPhyAddr;
728 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000729 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 struct timer_list timer;
732 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100733
734 u16 event_slow;
françois romieuc0e45c12011-01-03 15:08:04 +0000735
736 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200737 void (*write)(struct rtl8169_private *, int, int);
738 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000739 } mdio_ops;
740
françois romieu065c27c2011-01-03 15:08:12 +0000741 struct pll_power_ops {
742 void (*down)(struct rtl8169_private *);
743 void (*up)(struct rtl8169_private *);
744 } pll_power_ops;
745
Francois Romieud58d46b2011-05-03 16:38:29 +0200746 struct jumbo_ops {
747 void (*enable)(struct rtl8169_private *);
748 void (*disable)(struct rtl8169_private *);
749 } jumbo_ops;
750
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800751 struct csi_ops {
Francois Romieu52989f02012-07-06 13:37:00 +0200752 void (*write)(struct rtl8169_private *, int, int);
753 u32 (*read)(struct rtl8169_private *, int);
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800754 } csi_ops;
755
Oliver Neukum54405cd2011-01-06 21:55:13 +0100756 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200757 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000758 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100759 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000760 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800762 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
hayeswang5888d3f2014-07-11 16:25:56 +0800763 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100764
765 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100766 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
767 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100768 struct work_struct work;
769 } wk;
770
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200771 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200772
773 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800774 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000775 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400776 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000777
Francois Romieub6ffd972011-06-17 17:00:05 +0200778 struct rtl_fw {
779 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200780
781#define RTL_VER_SIZE 32
782
783 char version[RTL_VER_SIZE];
784
785 struct rtl_fw_phy_action {
786 __le32 *code;
787 size_t size;
788 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200789 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300790#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800791
792 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793};
794
Ralf Baechle979b6c12005-06-13 14:30:40 -0700795MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700798MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200799module_param_named(debug, debug.msg_enable, int, 0);
800MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801MODULE_LICENSE("GPL");
802MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000803MODULE_FIRMWARE(FIRMWARE_8168D_1);
804MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000805MODULE_FIRMWARE(FIRMWARE_8168E_1);
806MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400807MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800808MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800809MODULE_FIRMWARE(FIRMWARE_8168F_1);
810MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800811MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800812MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800813MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800814MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000815MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000816MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000817MODULE_FIRMWARE(FIRMWARE_8168G_3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Francois Romieuda78dbf2012-01-26 14:18:23 +0100819static void rtl_lock_work(struct rtl8169_private *tp)
820{
821 mutex_lock(&tp->wk.mutex);
822}
823
824static void rtl_unlock_work(struct rtl8169_private *tp)
825{
826 mutex_unlock(&tp->wk.mutex);
827}
828
Francois Romieud58d46b2011-05-03 16:38:29 +0200829static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
830{
Jiang Liu7d7903b2012-07-24 17:20:16 +0800831 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
832 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200833}
834
Francois Romieuffc46952012-07-06 14:19:23 +0200835struct rtl_cond {
836 bool (*check)(struct rtl8169_private *);
837 const char *msg;
838};
839
840static void rtl_udelay(unsigned int d)
841{
842 udelay(d);
843}
844
845static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
846 void (*delay)(unsigned int), unsigned int d, int n,
847 bool high)
848{
849 int i;
850
851 for (i = 0; i < n; i++) {
852 delay(d);
853 if (c->check(tp) == high)
854 return true;
855 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200856 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
857 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200858 return false;
859}
860
861static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
862 const struct rtl_cond *c,
863 unsigned int d, int n)
864{
865 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
866}
867
868static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
869 const struct rtl_cond *c,
870 unsigned int d, int n)
871{
872 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
873}
874
875static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
876 const struct rtl_cond *c,
877 unsigned int d, int n)
878{
879 return rtl_loop_wait(tp, c, msleep, d, n, true);
880}
881
882static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
883 const struct rtl_cond *c,
884 unsigned int d, int n)
885{
886 return rtl_loop_wait(tp, c, msleep, d, n, false);
887}
888
889#define DECLARE_RTL_COND(name) \
890static bool name ## _check(struct rtl8169_private *); \
891 \
892static const struct rtl_cond name = { \
893 .check = name ## _check, \
894 .msg = #name \
895}; \
896 \
897static bool name ## _check(struct rtl8169_private *tp)
898
899DECLARE_RTL_COND(rtl_ocpar_cond)
900{
901 void __iomem *ioaddr = tp->mmio_addr;
902
903 return RTL_R32(OCPAR) & OCPAR_FLAG;
904}
905
françois romieub646d902011-01-03 15:08:21 +0000906static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
907{
908 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000909
910 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Francois Romieuffc46952012-07-06 14:19:23 +0200911
912 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
913 RTL_R32(OCPDR) : ~0;
françois romieub646d902011-01-03 15:08:21 +0000914}
915
916static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
917{
918 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000919
920 RTL_W32(OCPDR, data);
921 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Francois Romieuffc46952012-07-06 14:19:23 +0200922
923 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
924}
925
926DECLARE_RTL_COND(rtl_eriar_cond)
927{
928 void __iomem *ioaddr = tp->mmio_addr;
929
930 return RTL_R32(ERIAR) & ERIAR_FLAG;
françois romieub646d902011-01-03 15:08:21 +0000931}
932
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800933static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000934{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800935 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000936
937 RTL_W8(ERIDR, cmd);
938 RTL_W32(ERIAR, 0x800010e8);
939 msleep(2);
Francois Romieuffc46952012-07-06 14:19:23 +0200940
941 if (!rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 5))
942 return;
françois romieub646d902011-01-03 15:08:21 +0000943
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800944 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000945}
946
947#define OOB_CMD_RESET 0x00
948#define OOB_CMD_DRIVER_START 0x05
949#define OOB_CMD_DRIVER_STOP 0x06
950
Francois Romieucecb5fd2011-04-01 10:21:07 +0200951static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
952{
953 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
954}
955
Francois Romieuffc46952012-07-06 14:19:23 +0200956DECLARE_RTL_COND(rtl_ocp_read_cond)
françois romieub646d902011-01-03 15:08:21 +0000957{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200958 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000959
Francois Romieucecb5fd2011-04-01 10:21:07 +0200960 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000961
Francois Romieuffc46952012-07-06 14:19:23 +0200962 return ocp_read(tp, 0x0f, reg) & 0x00000800;
963}
964
965static void rtl8168_driver_start(struct rtl8169_private *tp)
966{
967 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
968
969 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
françois romieub646d902011-01-03 15:08:21 +0000970}
971
972static void rtl8168_driver_stop(struct rtl8169_private *tp)
973{
françois romieub646d902011-01-03 15:08:21 +0000974 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
975
Francois Romieuffc46952012-07-06 14:19:23 +0200976 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
françois romieub646d902011-01-03 15:08:21 +0000977}
978
hayeswang4804b3b2011-03-21 01:50:29 +0000979static int r8168dp_check_dash(struct rtl8169_private *tp)
980{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200981 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000982
Francois Romieucecb5fd2011-04-01 10:21:07 +0200983 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000984}
françois romieub646d902011-01-03 15:08:21 +0000985
Hayes Wangc5583862012-07-02 17:23:22 +0800986static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
987{
988 if (reg & 0xffff0001) {
989 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
990 return true;
991 }
992 return false;
993}
994
995DECLARE_RTL_COND(rtl_ocp_gphy_cond)
996{
997 void __iomem *ioaddr = tp->mmio_addr;
998
999 return RTL_R32(GPHY_OCP) & OCPAR_FLAG;
1000}
1001
1002static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1003{
1004 void __iomem *ioaddr = tp->mmio_addr;
1005
1006 if (rtl_ocp_reg_failure(tp, reg))
1007 return;
1008
1009 RTL_W32(GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
1010
1011 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
1012}
1013
1014static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
1015{
1016 void __iomem *ioaddr = tp->mmio_addr;
1017
1018 if (rtl_ocp_reg_failure(tp, reg))
1019 return 0;
1020
1021 RTL_W32(GPHY_OCP, reg << 15);
1022
1023 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
1024 (RTL_R32(GPHY_OCP) & 0xffff) : ~0;
1025}
1026
Hayes Wangc5583862012-07-02 17:23:22 +08001027static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1028{
1029 void __iomem *ioaddr = tp->mmio_addr;
1030
1031 if (rtl_ocp_reg_failure(tp, reg))
1032 return;
1033
1034 RTL_W32(OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +08001035}
1036
1037static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1038{
1039 void __iomem *ioaddr = tp->mmio_addr;
1040
1041 if (rtl_ocp_reg_failure(tp, reg))
1042 return 0;
1043
1044 RTL_W32(OCPDR, reg << 15);
1045
Hayes Wang3a83ad12012-07-11 20:31:56 +08001046 return RTL_R32(OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +08001047}
1048
1049#define OCP_STD_PHY_BASE 0xa400
1050
1051static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1052{
1053 if (reg == 0x1f) {
1054 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1055 return;
1056 }
1057
1058 if (tp->ocp_base != OCP_STD_PHY_BASE)
1059 reg -= 0x10;
1060
1061 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1062}
1063
1064static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1065{
1066 if (tp->ocp_base != OCP_STD_PHY_BASE)
1067 reg -= 0x10;
1068
1069 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1070}
1071
hayeswangeee37862013-04-01 22:23:38 +00001072static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1073{
1074 if (reg == 0x1f) {
1075 tp->ocp_base = value << 4;
1076 return;
1077 }
1078
1079 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1080}
1081
1082static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1083{
1084 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1085}
1086
Francois Romieuffc46952012-07-06 14:19:23 +02001087DECLARE_RTL_COND(rtl_phyar_cond)
1088{
1089 void __iomem *ioaddr = tp->mmio_addr;
1090
1091 return RTL_R32(PHYAR) & 0x80000000;
1092}
1093
Francois Romieu24192212012-07-06 20:19:42 +02001094static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Francois Romieu24192212012-07-06 20:19:42 +02001096 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Francois Romieu24192212012-07-06 20:19:42 +02001098 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Francois Romieuffc46952012-07-06 14:19:23 +02001100 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -07001101 /*
Timo Teräs81a95f02010-06-09 17:31:48 -07001102 * According to hardware specs a 20us delay is required after write
1103 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -07001104 */
Timo Teräs81a95f02010-06-09 17:31:48 -07001105 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
Francois Romieu24192212012-07-06 20:19:42 +02001108static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Francois Romieu24192212012-07-06 20:19:42 +02001110 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieuffc46952012-07-06 14:19:23 +02001111 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Francois Romieu24192212012-07-06 20:19:42 +02001113 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Francois Romieuffc46952012-07-06 14:19:23 +02001115 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1116 RTL_R32(PHYAR) & 0xffff : ~0;
1117
Timo Teräs81a95f02010-06-09 17:31:48 -07001118 /*
1119 * According to hardware specs a 20us delay is required after read
1120 * complete indication, but before sending next command.
1121 */
1122 udelay(20);
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return value;
1125}
1126
Francois Romieu24192212012-07-06 20:19:42 +02001127static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001128{
Francois Romieu24192212012-07-06 20:19:42 +02001129 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001130
Francois Romieu24192212012-07-06 20:19:42 +02001131 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
françois romieuc0e45c12011-01-03 15:08:04 +00001132 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
1133 RTL_W32(EPHY_RXER_NUM, 0);
1134
Francois Romieuffc46952012-07-06 14:19:23 +02001135 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001136}
1137
Francois Romieu24192212012-07-06 20:19:42 +02001138static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001139{
Francois Romieu24192212012-07-06 20:19:42 +02001140 r8168dp_1_mdio_access(tp, reg,
1141 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001142}
1143
Francois Romieu24192212012-07-06 20:19:42 +02001144static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001145{
Francois Romieu24192212012-07-06 20:19:42 +02001146 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001147
Francois Romieu24192212012-07-06 20:19:42 +02001148 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001149
1150 mdelay(1);
1151 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1152 RTL_W32(EPHY_RXER_NUM, 0);
1153
Francois Romieuffc46952012-07-06 14:19:23 +02001154 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1155 RTL_R32(OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001156}
1157
françois romieue6de30d2011-01-03 15:08:37 +00001158#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1159
1160static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1161{
1162 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1163}
1164
1165static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1166{
1167 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1168}
1169
Francois Romieu24192212012-07-06 20:19:42 +02001170static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001171{
Francois Romieu24192212012-07-06 20:19:42 +02001172 void __iomem *ioaddr = tp->mmio_addr;
1173
françois romieue6de30d2011-01-03 15:08:37 +00001174 r8168dp_2_mdio_start(ioaddr);
1175
Francois Romieu24192212012-07-06 20:19:42 +02001176 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001177
1178 r8168dp_2_mdio_stop(ioaddr);
1179}
1180
Francois Romieu24192212012-07-06 20:19:42 +02001181static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001182{
Francois Romieu24192212012-07-06 20:19:42 +02001183 void __iomem *ioaddr = tp->mmio_addr;
françois romieue6de30d2011-01-03 15:08:37 +00001184 int value;
1185
1186 r8168dp_2_mdio_start(ioaddr);
1187
Francois Romieu24192212012-07-06 20:19:42 +02001188 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001189
1190 r8168dp_2_mdio_stop(ioaddr);
1191
1192 return value;
1193}
1194
françois romieu4da19632011-01-03 15:07:55 +00001195static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001196{
Francois Romieu24192212012-07-06 20:19:42 +02001197 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001198}
1199
françois romieu4da19632011-01-03 15:07:55 +00001200static int rtl_readphy(struct rtl8169_private *tp, int location)
1201{
Francois Romieu24192212012-07-06 20:19:42 +02001202 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001203}
1204
1205static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1206{
1207 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1208}
1209
1210static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001211{
1212 int val;
1213
françois romieu4da19632011-01-03 15:07:55 +00001214 val = rtl_readphy(tp, reg_addr);
1215 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001216}
1217
Francois Romieuccdffb92008-07-26 14:26:06 +02001218static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1219 int val)
1220{
1221 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001222
françois romieu4da19632011-01-03 15:07:55 +00001223 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001224}
1225
1226static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1227{
1228 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001229
françois romieu4da19632011-01-03 15:07:55 +00001230 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001231}
1232
Francois Romieuffc46952012-07-06 14:19:23 +02001233DECLARE_RTL_COND(rtl_ephyar_cond)
1234{
1235 void __iomem *ioaddr = tp->mmio_addr;
1236
1237 return RTL_R32(EPHYAR) & EPHYAR_FLAG;
1238}
1239
Francois Romieufdf6fc02012-07-06 22:40:38 +02001240static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001241{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001242 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001243
1244 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1245 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1246
Francois Romieuffc46952012-07-06 14:19:23 +02001247 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1248
1249 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001250}
1251
Francois Romieufdf6fc02012-07-06 22:40:38 +02001252static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001253{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001254 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001255
1256 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1257
Francois Romieuffc46952012-07-06 14:19:23 +02001258 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1259 RTL_R32(EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001260}
1261
Francois Romieufdf6fc02012-07-06 22:40:38 +02001262static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1263 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001264{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001265 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001266
1267 BUG_ON((addr & 3) || (mask == 0));
1268 RTL_W32(ERIDR, val);
1269 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1270
Francois Romieuffc46952012-07-06 14:19:23 +02001271 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001272}
1273
Francois Romieufdf6fc02012-07-06 22:40:38 +02001274static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001275{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001276 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001277
1278 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1279
Francois Romieuffc46952012-07-06 14:19:23 +02001280 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1281 RTL_R32(ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001282}
1283
Francois Romieufdf6fc02012-07-06 22:40:38 +02001284static void rtl_w1w0_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
1285 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001286{
1287 u32 val;
1288
Francois Romieufdf6fc02012-07-06 22:40:38 +02001289 val = rtl_eri_read(tp, addr, type);
1290 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001291}
1292
françois romieuc28aa382011-08-02 03:53:43 +00001293struct exgmac_reg {
1294 u16 addr;
1295 u16 mask;
1296 u32 val;
1297};
1298
Francois Romieufdf6fc02012-07-06 22:40:38 +02001299static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001300 const struct exgmac_reg *r, int len)
1301{
1302 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001303 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001304 r++;
1305 }
1306}
1307
Francois Romieuffc46952012-07-06 14:19:23 +02001308DECLARE_RTL_COND(rtl_efusear_cond)
1309{
1310 void __iomem *ioaddr = tp->mmio_addr;
1311
1312 return RTL_R32(EFUSEAR) & EFUSEAR_FLAG;
1313}
1314
Francois Romieufdf6fc02012-07-06 22:40:38 +02001315static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001316{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001317 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00001318
1319 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1320
Francois Romieuffc46952012-07-06 14:19:23 +02001321 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1322 RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001323}
1324
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001325static u16 rtl_get_events(struct rtl8169_private *tp)
1326{
1327 void __iomem *ioaddr = tp->mmio_addr;
1328
1329 return RTL_R16(IntrStatus);
1330}
1331
1332static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1333{
1334 void __iomem *ioaddr = tp->mmio_addr;
1335
1336 RTL_W16(IntrStatus, bits);
1337 mmiowb();
1338}
1339
1340static void rtl_irq_disable(struct rtl8169_private *tp)
1341{
1342 void __iomem *ioaddr = tp->mmio_addr;
1343
1344 RTL_W16(IntrMask, 0);
1345 mmiowb();
1346}
1347
Francois Romieu3e990ff2012-01-26 12:50:01 +01001348static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1349{
1350 void __iomem *ioaddr = tp->mmio_addr;
1351
1352 RTL_W16(IntrMask, bits);
1353}
1354
Francois Romieuda78dbf2012-01-26 14:18:23 +01001355#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1356#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1357#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1358
1359static void rtl_irq_enable_all(struct rtl8169_private *tp)
1360{
1361 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1362}
1363
françois romieu811fd302011-12-04 20:30:45 +00001364static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
françois romieu811fd302011-12-04 20:30:45 +00001366 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001368 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001369 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001370 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
françois romieu4da19632011-01-03 15:07:55 +00001373static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
françois romieu4da19632011-01-03 15:07:55 +00001375 void __iomem *ioaddr = tp->mmio_addr;
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return RTL_R32(TBICSR) & TBIReset;
1378}
1379
françois romieu4da19632011-01-03 15:07:55 +00001380static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
françois romieu4da19632011-01-03 15:07:55 +00001382 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
1384
1385static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1386{
1387 return RTL_R32(TBICSR) & TBILinkOk;
1388}
1389
1390static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1391{
1392 return RTL_R8(PHYstatus) & LinkStatus;
1393}
1394
françois romieu4da19632011-01-03 15:07:55 +00001395static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
françois romieu4da19632011-01-03 15:07:55 +00001397 void __iomem *ioaddr = tp->mmio_addr;
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1400}
1401
françois romieu4da19632011-01-03 15:07:55 +00001402static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
1404 unsigned int val;
1405
françois romieu4da19632011-01-03 15:07:55 +00001406 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1407 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
Hayes Wang70090422011-07-06 15:58:06 +08001410static void rtl_link_chg_patch(struct rtl8169_private *tp)
1411{
1412 void __iomem *ioaddr = tp->mmio_addr;
1413 struct net_device *dev = tp->dev;
1414
1415 if (!netif_running(dev))
1416 return;
1417
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001418 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1419 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Hayes Wang70090422011-07-06 15:58:06 +08001420 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001421 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1422 ERIAR_EXGMAC);
1423 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1424 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001425 } else if (RTL_R8(PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001426 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1427 ERIAR_EXGMAC);
1428 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1429 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001430 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001431 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1432 ERIAR_EXGMAC);
1433 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1434 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001435 }
1436 /* Reset packet filter */
Francois Romieufdf6fc02012-07-06 22:40:38 +02001437 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001438 ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02001439 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001440 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001441 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1442 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1443 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001444 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1445 ERIAR_EXGMAC);
1446 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1447 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001448 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001449 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1450 ERIAR_EXGMAC);
1451 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1452 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001453 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001454 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1455 if (RTL_R8(PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001456 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1457 ERIAR_EXGMAC);
1458 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1459 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001460 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001461 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1462 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001463 }
Hayes Wang70090422011-07-06 15:58:06 +08001464 }
1465}
1466
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001467static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001468 struct rtl8169_private *tp,
1469 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001472 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001473 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001474 if (pm)
1475 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001477 if (net_ratelimit())
1478 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001479 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001481 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001482 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001483 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001487static void rtl8169_check_link_status(struct net_device *dev,
1488 struct rtl8169_private *tp,
1489 void __iomem *ioaddr)
1490{
1491 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1492}
1493
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001494#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1495
1496static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1497{
1498 void __iomem *ioaddr = tp->mmio_addr;
1499 u8 options;
1500 u32 wolopts = 0;
1501
1502 options = RTL_R8(Config1);
1503 if (!(options & PMEnable))
1504 return 0;
1505
1506 options = RTL_R8(Config3);
1507 if (options & LinkUp)
1508 wolopts |= WAKE_PHY;
1509 if (options & MagicPacket)
1510 wolopts |= WAKE_MAGIC;
1511
1512 options = RTL_R8(Config5);
1513 if (options & UWF)
1514 wolopts |= WAKE_UCAST;
1515 if (options & BWF)
1516 wolopts |= WAKE_BCAST;
1517 if (options & MWF)
1518 wolopts |= WAKE_MCAST;
1519
1520 return wolopts;
1521}
1522
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001523static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1524{
1525 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001526
Francois Romieuda78dbf2012-01-26 14:18:23 +01001527 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001528
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001529 wol->supported = WAKE_ANY;
1530 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001531
Francois Romieuda78dbf2012-01-26 14:18:23 +01001532 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001533}
1534
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001535static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001536{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001537 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001538 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001539 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001540 u32 opt;
1541 u16 reg;
1542 u8 mask;
1543 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001544 { WAKE_PHY, Config3, LinkUp },
1545 { WAKE_MAGIC, Config3, MagicPacket },
1546 { WAKE_UCAST, Config5, UWF },
1547 { WAKE_BCAST, Config5, BWF },
1548 { WAKE_MCAST, Config5, MWF },
1549 { WAKE_ANY, Config5, LanWake }
1550 };
Francois Romieu851e6022012-04-17 11:10:11 +02001551 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001552
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001553 RTL_W8(Cfg9346, Cfg9346_Unlock);
1554
1555 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
Francois Romieu851e6022012-04-17 11:10:11 +02001556 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001557 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001558 options |= cfg[i].mask;
1559 RTL_W8(cfg[i].reg, options);
1560 }
1561
Francois Romieu851e6022012-04-17 11:10:11 +02001562 switch (tp->mac_version) {
1563 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1564 options = RTL_R8(Config1) & ~PMEnable;
1565 if (wolopts)
1566 options |= PMEnable;
1567 RTL_W8(Config1, options);
1568 break;
1569 default:
Francois Romieud387b422012-04-17 11:12:01 +02001570 options = RTL_R8(Config2) & ~PME_SIGNAL;
1571 if (wolopts)
1572 options |= PME_SIGNAL;
1573 RTL_W8(Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001574 break;
1575 }
1576
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001577 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001578}
1579
1580static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1581{
1582 struct rtl8169_private *tp = netdev_priv(dev);
1583
Francois Romieuda78dbf2012-01-26 14:18:23 +01001584 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001585
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001586 if (wol->wolopts)
1587 tp->features |= RTL_FEATURE_WOL;
1588 else
1589 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001590 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001591
1592 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001593
françois romieuea809072010-11-08 13:23:58 +00001594 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1595
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001596 return 0;
1597}
1598
Francois Romieu31bd2042011-04-26 18:58:59 +02001599static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1600{
Francois Romieu85bffe62011-04-27 08:22:39 +02001601 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001602}
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604static void rtl8169_get_drvinfo(struct net_device *dev,
1605 struct ethtool_drvinfo *info)
1606{
1607 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001608 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Rick Jones68aad782011-11-07 13:29:27 +00001610 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1611 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1612 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001613 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001614 if (!IS_ERR_OR_NULL(rtl_fw))
1615 strlcpy(info->fw_version, rtl_fw->version,
1616 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
1619static int rtl8169_get_regs_len(struct net_device *dev)
1620{
1621 return R8169_REGS_SIZE;
1622}
1623
1624static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001625 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
1627 struct rtl8169_private *tp = netdev_priv(dev);
1628 void __iomem *ioaddr = tp->mmio_addr;
1629 int ret = 0;
1630 u32 reg;
1631
1632 reg = RTL_R32(TBICSR);
1633 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1634 (duplex == DUPLEX_FULL)) {
1635 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1636 } else if (autoneg == AUTONEG_ENABLE)
1637 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1638 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001639 netif_warn(tp, link, dev,
1640 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 ret = -EOPNOTSUPP;
1642 }
1643
1644 return ret;
1645}
1646
1647static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001648 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
1650 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001651 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001652 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Hayes Wang716b50a2011-02-22 17:26:18 +08001654 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001657 int auto_nego;
1658
françois romieu4da19632011-01-03 15:07:55 +00001659 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001660 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1661 ADVERTISE_100HALF | ADVERTISE_100FULL);
1662
1663 if (adv & ADVERTISED_10baseT_Half)
1664 auto_nego |= ADVERTISE_10HALF;
1665 if (adv & ADVERTISED_10baseT_Full)
1666 auto_nego |= ADVERTISE_10FULL;
1667 if (adv & ADVERTISED_100baseT_Half)
1668 auto_nego |= ADVERTISE_100HALF;
1669 if (adv & ADVERTISED_100baseT_Full)
1670 auto_nego |= ADVERTISE_100FULL;
1671
françois romieu3577aa12009-05-19 10:46:48 +00001672 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1673
françois romieu4da19632011-01-03 15:07:55 +00001674 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001675 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1676
1677 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001678 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001679 if (adv & ADVERTISED_1000baseT_Half)
1680 giga_ctrl |= ADVERTISE_1000HALF;
1681 if (adv & ADVERTISED_1000baseT_Full)
1682 giga_ctrl |= ADVERTISE_1000FULL;
1683 } else if (adv & (ADVERTISED_1000baseT_Half |
1684 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001685 netif_info(tp, link, dev,
1686 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001687 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
françois romieu3577aa12009-05-19 10:46:48 +00001690 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001691
françois romieu4da19632011-01-03 15:07:55 +00001692 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1693 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001694 } else {
1695 giga_ctrl = 0;
1696
1697 if (speed == SPEED_10)
1698 bmcr = 0;
1699 else if (speed == SPEED_100)
1700 bmcr = BMCR_SPEED100;
1701 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001702 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001703
1704 if (duplex == DUPLEX_FULL)
1705 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001706 }
1707
françois romieu4da19632011-01-03 15:07:55 +00001708 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001709
Francois Romieucecb5fd2011-04-01 10:21:07 +02001710 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1711 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001712 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001713 rtl_writephy(tp, 0x17, 0x2138);
1714 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001715 } else {
françois romieu4da19632011-01-03 15:07:55 +00001716 rtl_writephy(tp, 0x17, 0x2108);
1717 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001718 }
1719 }
1720
Oliver Neukum54405cd2011-01-06 21:55:13 +01001721 rc = 0;
1722out:
1723 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
1726static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001727 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
1729 struct rtl8169_private *tp = netdev_priv(dev);
1730 int ret;
1731
Oliver Neukum54405cd2011-01-06 21:55:13 +01001732 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001733 if (ret < 0)
1734 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Francois Romieu4876cc12011-03-11 21:07:11 +01001736 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1737 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001739 }
1740out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 return ret;
1742}
1743
1744static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1745{
1746 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 int ret;
1748
Francois Romieu4876cc12011-03-11 21:07:11 +01001749 del_timer_sync(&tp->timer);
1750
Francois Romieuda78dbf2012-01-26 14:18:23 +01001751 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001752 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001753 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001754 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return ret;
1757}
1758
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001759static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1760 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
Francois Romieud58d46b2011-05-03 16:38:29 +02001762 struct rtl8169_private *tp = netdev_priv(dev);
1763
Francois Romieu2b7b4312011-04-18 22:53:24 -07001764 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001765 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Francois Romieud58d46b2011-05-03 16:38:29 +02001767 if (dev->mtu > JUMBO_1K &&
1768 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1769 features &= ~NETIF_F_IP_CSUM;
1770
Michał Mirosław350fb322011-04-08 06:35:56 +00001771 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
1773
Francois Romieuda78dbf2012-01-26 14:18:23 +01001774static void __rtl8169_set_features(struct net_device *dev,
1775 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
1777 struct rtl8169_private *tp = netdev_priv(dev);
Ben Greear6bbe0212012-02-10 15:04:33 +00001778 netdev_features_t changed = features ^ dev->features;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001779 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Patrick McHardyf6469682013-04-19 02:04:27 +00001781 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM |
1782 NETIF_F_HW_VLAN_CTAG_RX)))
Ben Greear6bbe0212012-02-10 15:04:33 +00001783 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Patrick McHardyf6469682013-04-19 02:04:27 +00001785 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX)) {
Ben Greear6bbe0212012-02-10 15:04:33 +00001786 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
Patrick McHardyf6469682013-04-19 02:04:27 +00001791 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
Ben Greear6bbe0212012-02-10 15:04:33 +00001792 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)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001831 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), 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);
Peter Wu15edae92013-08-21 23:17:11 +02001878 u32 __iomem *data = tp->mmio_addr;
1879 u32 *dw = p;
1880 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Francois Romieuda78dbf2012-01-26 14:18:23 +01001882 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02001883 for (i = 0; i < R8169_REGS_SIZE; i += 4)
1884 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001885 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886}
1887
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001888static u32 rtl8169_get_msglevel(struct net_device *dev)
1889{
1890 struct rtl8169_private *tp = netdev_priv(dev);
1891
1892 return tp->msg_enable;
1893}
1894
1895static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1896{
1897 struct rtl8169_private *tp = netdev_priv(dev);
1898
1899 tp->msg_enable = value;
1900}
1901
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001902static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1903 "tx_packets",
1904 "rx_packets",
1905 "tx_errors",
1906 "rx_errors",
1907 "rx_missed",
1908 "align_errors",
1909 "tx_single_collisions",
1910 "tx_multi_collisions",
1911 "unicast",
1912 "broadcast",
1913 "multicast",
1914 "tx_aborted",
1915 "tx_underrun",
1916};
1917
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001918static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001919{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001920 switch (sset) {
1921 case ETH_SS_STATS:
1922 return ARRAY_SIZE(rtl8169_gstrings);
1923 default:
1924 return -EOPNOTSUPP;
1925 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001926}
1927
Francois Romieuffc46952012-07-06 14:19:23 +02001928DECLARE_RTL_COND(rtl_counters_cond)
1929{
1930 void __iomem *ioaddr = tp->mmio_addr;
1931
1932 return RTL_R32(CounterAddrLow) & CounterDump;
1933}
1934
Ivan Vecera355423d2009-02-06 21:49:57 -08001935static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001936{
1937 struct rtl8169_private *tp = netdev_priv(dev);
1938 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001939 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001940 struct rtl8169_counters *counters;
1941 dma_addr_t paddr;
1942 u32 cmd;
1943
Ivan Vecera355423d2009-02-06 21:49:57 -08001944 /*
1945 * Some chips are unable to dump tally counters when the receiver
1946 * is disabled.
1947 */
1948 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1949 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001950
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001951 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001952 if (!counters)
1953 return;
1954
1955 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001956 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001957 RTL_W32(CounterAddrLow, cmd);
1958 RTL_W32(CounterAddrLow, cmd | CounterDump);
1959
Francois Romieuffc46952012-07-06 14:19:23 +02001960 if (rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000))
1961 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001962
1963 RTL_W32(CounterAddrLow, 0);
1964 RTL_W32(CounterAddrHigh, 0);
1965
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001966 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001967}
1968
Ivan Vecera355423d2009-02-06 21:49:57 -08001969static void rtl8169_get_ethtool_stats(struct net_device *dev,
1970 struct ethtool_stats *stats, u64 *data)
1971{
1972 struct rtl8169_private *tp = netdev_priv(dev);
1973
1974 ASSERT_RTNL();
1975
1976 rtl8169_update_counters(dev);
1977
1978 data[0] = le64_to_cpu(tp->counters.tx_packets);
1979 data[1] = le64_to_cpu(tp->counters.rx_packets);
1980 data[2] = le64_to_cpu(tp->counters.tx_errors);
1981 data[3] = le32_to_cpu(tp->counters.rx_errors);
1982 data[4] = le16_to_cpu(tp->counters.rx_missed);
1983 data[5] = le16_to_cpu(tp->counters.align_errors);
1984 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1985 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1986 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1987 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1988 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1989 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1990 data[12] = le16_to_cpu(tp->counters.tx_underun);
1991}
1992
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001993static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1994{
1995 switch(stringset) {
1996 case ETH_SS_STATS:
1997 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1998 break;
1999 }
2000}
2001
Jeff Garzik7282d492006-09-13 14:30:00 -04002002static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 .get_drvinfo = rtl8169_get_drvinfo,
2004 .get_regs_len = rtl8169_get_regs_len,
2005 .get_link = ethtool_op_get_link,
2006 .get_settings = rtl8169_get_settings,
2007 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002008 .get_msglevel = rtl8169_get_msglevel,
2009 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002011 .get_wol = rtl8169_get_wol,
2012 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002013 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002014 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002015 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002016 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017};
2018
Francois Romieu07d3f512007-02-21 22:40:46 +01002019static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02002020 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
Francois Romieu5d320a22011-05-08 17:47:36 +02002022 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01002023 /*
2024 * The driver currently handles the 8168Bf and the 8168Be identically
2025 * but they can be identified more specifically through the test below
2026 * if needed:
2027 *
2028 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002029 *
2030 * Same thing for the 8101Eb and the 8101Ec:
2031 *
2032 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002033 */
Francois Romieu37441002011-06-17 22:58:54 +02002034 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002036 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 int mac_version;
2038 } mac_info[] = {
Hayes Wangc5583862012-07-02 17:23:22 +08002039 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002040 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002041 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002042 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2043 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2044
Hayes Wangc2218922011-09-06 16:55:18 +08002045 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002046 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002047 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2048 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2049
hayeswang01dc7fe2011-03-21 01:50:28 +00002050 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002051 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002052 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2053 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2054 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2055
Francois Romieu5b538df2008-07-20 16:22:45 +02002056 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002057 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2058 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002059 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002060
françois romieue6de30d2011-01-03 15:08:37 +00002061 /* 8168DP family. */
2062 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2063 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002064 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002065
Francois Romieuef808d52008-06-29 13:10:54 +02002066 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07002067 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02002068 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002069 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002070 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002071 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2072 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002073 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02002074 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02002075 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002076
2077 /* 8168B family. */
2078 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2079 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2080 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2081 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2082
2083 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002084 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2085 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002086 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00002087 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002088 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2089 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2090 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002091 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2092 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2093 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2094 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2095 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2096 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002097 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002098 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002099 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002100 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2101 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002102 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2103 /* FIXME: where did these entries come from ? -- FR */
2104 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2105 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2106
2107 /* 8110 family. */
2108 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2109 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2110 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2111 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2112 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2113 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2114
Jean Delvaref21b75e2009-05-26 20:54:48 -07002115 /* Catch-all */
2116 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002117 };
2118 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 u32 reg;
2120
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002121 reg = RTL_R32(TxConfig);
2122 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 p++;
2124 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002125
2126 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2127 netif_notice(tp, probe, dev,
2128 "unknown MAC, using family default\n");
2129 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002130 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2131 tp->mac_version = tp->mii.supports_gmii ?
2132 RTL_GIGA_MAC_VER_42 :
2133 RTL_GIGA_MAC_VER_43;
Francois Romieu5d320a22011-05-08 17:47:36 +02002134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
2136
2137static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2138{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002139 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140}
2141
Francois Romieu867763c2007-08-17 18:21:58 +02002142struct phy_reg {
2143 u16 reg;
2144 u16 val;
2145};
2146
françois romieu4da19632011-01-03 15:07:55 +00002147static void rtl_writephy_batch(struct rtl8169_private *tp,
2148 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002149{
2150 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002151 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002152 regs++;
2153 }
2154}
2155
françois romieubca03d52011-01-03 15:07:31 +00002156#define PHY_READ 0x00000000
2157#define PHY_DATA_OR 0x10000000
2158#define PHY_DATA_AND 0x20000000
2159#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002160#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002161#define PHY_CLEAR_READCOUNT 0x70000000
2162#define PHY_WRITE 0x80000000
2163#define PHY_READCOUNT_EQ_SKIP 0x90000000
2164#define PHY_COMP_EQ_SKIPN 0xa0000000
2165#define PHY_COMP_NEQ_SKIPN 0xb0000000
2166#define PHY_WRITE_PREVIOUS 0xc0000000
2167#define PHY_SKIPN 0xd0000000
2168#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002169
Hayes Wang960aee62011-06-18 11:37:48 +02002170struct fw_info {
2171 u32 magic;
2172 char version[RTL_VER_SIZE];
2173 __le32 fw_start;
2174 __le32 fw_len;
2175 u8 chksum;
2176} __packed;
2177
Francois Romieu1c361ef2011-06-17 17:16:24 +02002178#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2179
2180static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002181{
Francois Romieub6ffd972011-06-17 17:00:05 +02002182 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002183 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002184 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2185 char *version = rtl_fw->version;
2186 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002187
Francois Romieu1c361ef2011-06-17 17:16:24 +02002188 if (fw->size < FW_OPCODE_SIZE)
2189 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002190
2191 if (!fw_info->magic) {
2192 size_t i, size, start;
2193 u8 checksum = 0;
2194
2195 if (fw->size < sizeof(*fw_info))
2196 goto out;
2197
2198 for (i = 0; i < fw->size; i++)
2199 checksum += fw->data[i];
2200 if (checksum != 0)
2201 goto out;
2202
2203 start = le32_to_cpu(fw_info->fw_start);
2204 if (start > fw->size)
2205 goto out;
2206
2207 size = le32_to_cpu(fw_info->fw_len);
2208 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2209 goto out;
2210
2211 memcpy(version, fw_info->version, RTL_VER_SIZE);
2212
2213 pa->code = (__le32 *)(fw->data + start);
2214 pa->size = size;
2215 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002216 if (fw->size % FW_OPCODE_SIZE)
2217 goto out;
2218
2219 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2220
2221 pa->code = (__le32 *)fw->data;
2222 pa->size = fw->size / FW_OPCODE_SIZE;
2223 }
2224 version[RTL_VER_SIZE - 1] = 0;
2225
2226 rc = true;
2227out:
2228 return rc;
2229}
2230
Francois Romieufd112f22011-06-18 00:10:29 +02002231static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2232 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002233{
Francois Romieufd112f22011-06-18 00:10:29 +02002234 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002235 size_t index;
2236
Francois Romieu1c361ef2011-06-17 17:16:24 +02002237 for (index = 0; index < pa->size; index++) {
2238 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002239 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002240
hayeswang42b82dc2011-01-10 02:07:25 +00002241 switch(action & 0xf0000000) {
2242 case PHY_READ:
2243 case PHY_DATA_OR:
2244 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002245 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002246 case PHY_CLEAR_READCOUNT:
2247 case PHY_WRITE:
2248 case PHY_WRITE_PREVIOUS:
2249 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002250 break;
2251
hayeswang42b82dc2011-01-10 02:07:25 +00002252 case PHY_BJMPN:
2253 if (regno > index) {
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_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002260 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002261 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002262 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002263 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002264 }
2265 break;
2266 case PHY_COMP_EQ_SKIPN:
2267 case PHY_COMP_NEQ_SKIPN:
2268 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002269 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002270 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002271 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002272 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002273 }
2274 break;
2275
hayeswang42b82dc2011-01-10 02:07:25 +00002276 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002277 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002278 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002279 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002280 }
2281 }
Francois Romieufd112f22011-06-18 00:10:29 +02002282 rc = true;
2283out:
2284 return rc;
2285}
françois romieubca03d52011-01-03 15:07:31 +00002286
Francois Romieufd112f22011-06-18 00:10:29 +02002287static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2288{
2289 struct net_device *dev = tp->dev;
2290 int rc = -EINVAL;
2291
2292 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2293 netif_err(tp, ifup, dev, "invalid firwmare\n");
2294 goto out;
2295 }
2296
2297 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2298 rc = 0;
2299out:
2300 return rc;
2301}
2302
2303static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2304{
2305 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002306 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002307 u32 predata, count;
2308 size_t index;
2309
2310 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002311 org.write = ops->write;
2312 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002313
Francois Romieu1c361ef2011-06-17 17:16:24 +02002314 for (index = 0; index < pa->size; ) {
2315 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002316 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002317 u32 regno = (action & 0x0fff0000) >> 16;
2318
2319 if (!action)
2320 break;
françois romieubca03d52011-01-03 15:07:31 +00002321
2322 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002323 case PHY_READ:
2324 predata = rtl_readphy(tp, regno);
2325 count++;
2326 index++;
françois romieubca03d52011-01-03 15:07:31 +00002327 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002328 case PHY_DATA_OR:
2329 predata |= data;
2330 index++;
2331 break;
2332 case PHY_DATA_AND:
2333 predata &= data;
2334 index++;
2335 break;
2336 case PHY_BJMPN:
2337 index -= regno;
2338 break;
hayeswangeee37862013-04-01 22:23:38 +00002339 case PHY_MDIO_CHG:
2340 if (data == 0) {
2341 ops->write = org.write;
2342 ops->read = org.read;
2343 } else if (data == 1) {
2344 ops->write = mac_mcu_write;
2345 ops->read = mac_mcu_read;
2346 }
2347
hayeswang42b82dc2011-01-10 02:07:25 +00002348 index++;
2349 break;
2350 case PHY_CLEAR_READCOUNT:
2351 count = 0;
2352 index++;
2353 break;
2354 case PHY_WRITE:
2355 rtl_writephy(tp, regno, data);
2356 index++;
2357 break;
2358 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002359 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002360 break;
2361 case PHY_COMP_EQ_SKIPN:
2362 if (predata == data)
2363 index += regno;
2364 index++;
2365 break;
2366 case PHY_COMP_NEQ_SKIPN:
2367 if (predata != data)
2368 index += regno;
2369 index++;
2370 break;
2371 case PHY_WRITE_PREVIOUS:
2372 rtl_writephy(tp, regno, predata);
2373 index++;
2374 break;
2375 case PHY_SKIPN:
2376 index += regno + 1;
2377 break;
2378 case PHY_DELAY_MS:
2379 mdelay(data);
2380 index++;
2381 break;
2382
françois romieubca03d52011-01-03 15:07:31 +00002383 default:
2384 BUG();
2385 }
2386 }
hayeswangeee37862013-04-01 22:23:38 +00002387
2388 ops->write = org.write;
2389 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002390}
2391
françois romieuf1e02ed2011-01-13 13:07:53 +00002392static void rtl_release_firmware(struct rtl8169_private *tp)
2393{
Francois Romieub6ffd972011-06-17 17:00:05 +02002394 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2395 release_firmware(tp->rtl_fw->fw);
2396 kfree(tp->rtl_fw);
2397 }
2398 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002399}
2400
François Romieu953a12c2011-04-24 17:38:48 +02002401static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002402{
Francois Romieub6ffd972011-06-17 17:00:05 +02002403 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002404
2405 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002406 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002407 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002408}
2409
2410static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2411{
2412 if (rtl_readphy(tp, reg) != val)
2413 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2414 else
2415 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002416}
2417
françois romieu4da19632011-01-03 15:07:55 +00002418static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002420 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002421 { 0x1f, 0x0001 },
2422 { 0x06, 0x006e },
2423 { 0x08, 0x0708 },
2424 { 0x15, 0x4000 },
2425 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
françois romieu0b9b5712009-08-10 19:44:56 +00002427 { 0x1f, 0x0001 },
2428 { 0x03, 0x00a1 },
2429 { 0x02, 0x0008 },
2430 { 0x01, 0x0120 },
2431 { 0x00, 0x1000 },
2432 { 0x04, 0x0800 },
2433 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
françois romieu0b9b5712009-08-10 19:44:56 +00002435 { 0x03, 0xff41 },
2436 { 0x02, 0xdf60 },
2437 { 0x01, 0x0140 },
2438 { 0x00, 0x0077 },
2439 { 0x04, 0x7800 },
2440 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
françois romieu0b9b5712009-08-10 19:44:56 +00002442 { 0x03, 0x802f },
2443 { 0x02, 0x4f02 },
2444 { 0x01, 0x0409 },
2445 { 0x00, 0xf0f9 },
2446 { 0x04, 0x9800 },
2447 { 0x04, 0x9000 },
2448
2449 { 0x03, 0xdf01 },
2450 { 0x02, 0xdf20 },
2451 { 0x01, 0xff95 },
2452 { 0x00, 0xba00 },
2453 { 0x04, 0xa800 },
2454 { 0x04, 0xa000 },
2455
2456 { 0x03, 0xff41 },
2457 { 0x02, 0xdf20 },
2458 { 0x01, 0x0140 },
2459 { 0x00, 0x00bb },
2460 { 0x04, 0xb800 },
2461 { 0x04, 0xb000 },
2462
2463 { 0x03, 0xdf41 },
2464 { 0x02, 0xdc60 },
2465 { 0x01, 0x6340 },
2466 { 0x00, 0x007d },
2467 { 0x04, 0xd800 },
2468 { 0x04, 0xd000 },
2469
2470 { 0x03, 0xdf01 },
2471 { 0x02, 0xdf20 },
2472 { 0x01, 0x100a },
2473 { 0x00, 0xa0ff },
2474 { 0x04, 0xf800 },
2475 { 0x04, 0xf000 },
2476
2477 { 0x1f, 0x0000 },
2478 { 0x0b, 0x0000 },
2479 { 0x00, 0x9200 }
2480 };
2481
françois romieu4da19632011-01-03 15:07:55 +00002482 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483}
2484
françois romieu4da19632011-01-03 15:07:55 +00002485static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002486{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002487 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002488 { 0x1f, 0x0002 },
2489 { 0x01, 0x90d0 },
2490 { 0x1f, 0x0000 }
2491 };
2492
françois romieu4da19632011-01-03 15:07:55 +00002493 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002494}
2495
françois romieu4da19632011-01-03 15:07:55 +00002496static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002497{
2498 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002499
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002500 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2501 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002502 return;
2503
françois romieu4da19632011-01-03 15:07:55 +00002504 rtl_writephy(tp, 0x1f, 0x0001);
2505 rtl_writephy(tp, 0x10, 0xf01b);
2506 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002507}
2508
françois romieu4da19632011-01-03 15:07:55 +00002509static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002510{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002511 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002512 { 0x1f, 0x0001 },
2513 { 0x04, 0x0000 },
2514 { 0x03, 0x00a1 },
2515 { 0x02, 0x0008 },
2516 { 0x01, 0x0120 },
2517 { 0x00, 0x1000 },
2518 { 0x04, 0x0800 },
2519 { 0x04, 0x9000 },
2520 { 0x03, 0x802f },
2521 { 0x02, 0x4f02 },
2522 { 0x01, 0x0409 },
2523 { 0x00, 0xf099 },
2524 { 0x04, 0x9800 },
2525 { 0x04, 0xa000 },
2526 { 0x03, 0xdf01 },
2527 { 0x02, 0xdf20 },
2528 { 0x01, 0xff95 },
2529 { 0x00, 0xba00 },
2530 { 0x04, 0xa800 },
2531 { 0x04, 0xf000 },
2532 { 0x03, 0xdf01 },
2533 { 0x02, 0xdf20 },
2534 { 0x01, 0x101a },
2535 { 0x00, 0xa0ff },
2536 { 0x04, 0xf800 },
2537 { 0x04, 0x0000 },
2538 { 0x1f, 0x0000 },
2539
2540 { 0x1f, 0x0001 },
2541 { 0x10, 0xf41b },
2542 { 0x14, 0xfb54 },
2543 { 0x18, 0xf5c7 },
2544 { 0x1f, 0x0000 },
2545
2546 { 0x1f, 0x0001 },
2547 { 0x17, 0x0cc0 },
2548 { 0x1f, 0x0000 }
2549 };
2550
françois romieu4da19632011-01-03 15:07:55 +00002551 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002552
françois romieu4da19632011-01-03 15:07:55 +00002553 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002554}
2555
françois romieu4da19632011-01-03 15:07:55 +00002556static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002557{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002558 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002559 { 0x1f, 0x0001 },
2560 { 0x04, 0x0000 },
2561 { 0x03, 0x00a1 },
2562 { 0x02, 0x0008 },
2563 { 0x01, 0x0120 },
2564 { 0x00, 0x1000 },
2565 { 0x04, 0x0800 },
2566 { 0x04, 0x9000 },
2567 { 0x03, 0x802f },
2568 { 0x02, 0x4f02 },
2569 { 0x01, 0x0409 },
2570 { 0x00, 0xf099 },
2571 { 0x04, 0x9800 },
2572 { 0x04, 0xa000 },
2573 { 0x03, 0xdf01 },
2574 { 0x02, 0xdf20 },
2575 { 0x01, 0xff95 },
2576 { 0x00, 0xba00 },
2577 { 0x04, 0xa800 },
2578 { 0x04, 0xf000 },
2579 { 0x03, 0xdf01 },
2580 { 0x02, 0xdf20 },
2581 { 0x01, 0x101a },
2582 { 0x00, 0xa0ff },
2583 { 0x04, 0xf800 },
2584 { 0x04, 0x0000 },
2585 { 0x1f, 0x0000 },
2586
2587 { 0x1f, 0x0001 },
2588 { 0x0b, 0x8480 },
2589 { 0x1f, 0x0000 },
2590
2591 { 0x1f, 0x0001 },
2592 { 0x18, 0x67c7 },
2593 { 0x04, 0x2000 },
2594 { 0x03, 0x002f },
2595 { 0x02, 0x4360 },
2596 { 0x01, 0x0109 },
2597 { 0x00, 0x3022 },
2598 { 0x04, 0x2800 },
2599 { 0x1f, 0x0000 },
2600
2601 { 0x1f, 0x0001 },
2602 { 0x17, 0x0cc0 },
2603 { 0x1f, 0x0000 }
2604 };
2605
françois romieu4da19632011-01-03 15:07:55 +00002606 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002607}
2608
françois romieu4da19632011-01-03 15:07:55 +00002609static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002610{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002611 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002612 { 0x10, 0xf41b },
2613 { 0x1f, 0x0000 }
2614 };
2615
françois romieu4da19632011-01-03 15:07:55 +00002616 rtl_writephy(tp, 0x1f, 0x0001);
2617 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002618
françois romieu4da19632011-01-03 15:07:55 +00002619 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002620}
2621
françois romieu4da19632011-01-03 15:07:55 +00002622static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002623{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002624 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002625 { 0x1f, 0x0001 },
2626 { 0x10, 0xf41b },
2627 { 0x1f, 0x0000 }
2628 };
2629
françois romieu4da19632011-01-03 15:07:55 +00002630 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002631}
2632
françois romieu4da19632011-01-03 15:07:55 +00002633static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002634{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002635 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002636 { 0x1f, 0x0000 },
2637 { 0x1d, 0x0f00 },
2638 { 0x1f, 0x0002 },
2639 { 0x0c, 0x1ec8 },
2640 { 0x1f, 0x0000 }
2641 };
2642
françois romieu4da19632011-01-03 15:07:55 +00002643 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002644}
2645
françois romieu4da19632011-01-03 15:07:55 +00002646static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002647{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002648 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002649 { 0x1f, 0x0001 },
2650 { 0x1d, 0x3d98 },
2651 { 0x1f, 0x0000 }
2652 };
2653
françois romieu4da19632011-01-03 15:07:55 +00002654 rtl_writephy(tp, 0x1f, 0x0000);
2655 rtl_patchphy(tp, 0x14, 1 << 5);
2656 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002657
françois romieu4da19632011-01-03 15:07:55 +00002658 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002659}
2660
françois romieu4da19632011-01-03 15:07:55 +00002661static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002662{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002663 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002664 { 0x1f, 0x0001 },
2665 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002666 { 0x1f, 0x0002 },
2667 { 0x00, 0x88d4 },
2668 { 0x01, 0x82b1 },
2669 { 0x03, 0x7002 },
2670 { 0x08, 0x9e30 },
2671 { 0x09, 0x01f0 },
2672 { 0x0a, 0x5500 },
2673 { 0x0c, 0x00c8 },
2674 { 0x1f, 0x0003 },
2675 { 0x12, 0xc096 },
2676 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002677 { 0x1f, 0x0000 },
2678 { 0x1f, 0x0000 },
2679 { 0x09, 0x2000 },
2680 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002681 };
2682
françois romieu4da19632011-01-03 15:07:55 +00002683 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002684
françois romieu4da19632011-01-03 15:07:55 +00002685 rtl_patchphy(tp, 0x14, 1 << 5);
2686 rtl_patchphy(tp, 0x0d, 1 << 5);
2687 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002688}
2689
françois romieu4da19632011-01-03 15:07:55 +00002690static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002691{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002692 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002693 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002694 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002695 { 0x03, 0x802f },
2696 { 0x02, 0x4f02 },
2697 { 0x01, 0x0409 },
2698 { 0x00, 0xf099 },
2699 { 0x04, 0x9800 },
2700 { 0x04, 0x9000 },
2701 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002702 { 0x1f, 0x0002 },
2703 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002704 { 0x06, 0x0761 },
2705 { 0x1f, 0x0003 },
2706 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002707 { 0x1f, 0x0000 }
2708 };
2709
françois romieu4da19632011-01-03 15:07:55 +00002710 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002711
françois romieu4da19632011-01-03 15:07:55 +00002712 rtl_patchphy(tp, 0x16, 1 << 0);
2713 rtl_patchphy(tp, 0x14, 1 << 5);
2714 rtl_patchphy(tp, 0x0d, 1 << 5);
2715 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002716}
2717
françois romieu4da19632011-01-03 15:07:55 +00002718static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002719{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002720 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002721 { 0x1f, 0x0001 },
2722 { 0x12, 0x2300 },
2723 { 0x1d, 0x3d98 },
2724 { 0x1f, 0x0002 },
2725 { 0x0c, 0x7eb8 },
2726 { 0x06, 0x5461 },
2727 { 0x1f, 0x0003 },
2728 { 0x16, 0x0f0a },
2729 { 0x1f, 0x0000 }
2730 };
2731
françois romieu4da19632011-01-03 15:07:55 +00002732 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002733
françois romieu4da19632011-01-03 15:07:55 +00002734 rtl_patchphy(tp, 0x16, 1 << 0);
2735 rtl_patchphy(tp, 0x14, 1 << 5);
2736 rtl_patchphy(tp, 0x0d, 1 << 5);
2737 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002738}
2739
françois romieu4da19632011-01-03 15:07:55 +00002740static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002741{
françois romieu4da19632011-01-03 15:07:55 +00002742 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002743}
2744
françois romieubca03d52011-01-03 15:07:31 +00002745static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002746{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002747 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002748 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002749 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002750 { 0x06, 0x4064 },
2751 { 0x07, 0x2863 },
2752 { 0x08, 0x059c },
2753 { 0x09, 0x26b4 },
2754 { 0x0a, 0x6a19 },
2755 { 0x0b, 0xdcc8 },
2756 { 0x10, 0xf06d },
2757 { 0x14, 0x7f68 },
2758 { 0x18, 0x7fd9 },
2759 { 0x1c, 0xf0ff },
2760 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002761 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002762 { 0x12, 0xf49f },
2763 { 0x13, 0x070b },
2764 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002765 { 0x14, 0x94c0 },
2766
2767 /*
2768 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002769 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002770 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002771 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002772 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002773 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002774 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002775 { 0x06, 0x5561 },
2776
2777 /*
2778 * Can not link to 1Gbps with bad cable
2779 * Decrease SNR threshold form 21.07dB to 19.04dB
2780 */
2781 { 0x1f, 0x0001 },
2782 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002783
2784 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002785 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002786 };
2787
françois romieu4da19632011-01-03 15:07:55 +00002788 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002789
françois romieubca03d52011-01-03 15:07:31 +00002790 /*
2791 * Rx Error Issue
2792 * Fine Tune Switching regulator parameter
2793 */
françois romieu4da19632011-01-03 15:07:55 +00002794 rtl_writephy(tp, 0x1f, 0x0002);
2795 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2796 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002797
Francois Romieufdf6fc02012-07-06 22:40:38 +02002798 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002799 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002800 { 0x1f, 0x0002 },
2801 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002802 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002803 { 0x05, 0x8330 },
2804 { 0x06, 0x669a },
2805 { 0x1f, 0x0002 }
2806 };
2807 int val;
2808
françois romieu4da19632011-01-03 15:07:55 +00002809 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002810
françois romieu4da19632011-01-03 15:07:55 +00002811 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002812
2813 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002814 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002815 0x0065, 0x0066, 0x0067, 0x0068,
2816 0x0069, 0x006a, 0x006b, 0x006c
2817 };
2818 int i;
2819
françois romieu4da19632011-01-03 15:07:55 +00002820 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002821
2822 val &= 0xff00;
2823 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002824 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002825 }
2826 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002827 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002828 { 0x1f, 0x0002 },
2829 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002830 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002831 { 0x05, 0x8330 },
2832 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002833 };
2834
françois romieu4da19632011-01-03 15:07:55 +00002835 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002836 }
2837
françois romieubca03d52011-01-03 15:07:31 +00002838 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002839 rtl_writephy(tp, 0x1f, 0x0002);
2840 rtl_patchphy(tp, 0x0d, 0x0300);
2841 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002842
françois romieubca03d52011-01-03 15:07:31 +00002843 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002844 rtl_writephy(tp, 0x1f, 0x0002);
2845 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2846 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002847
françois romieu4da19632011-01-03 15:07:55 +00002848 rtl_writephy(tp, 0x1f, 0x0005);
2849 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002850
2851 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002852
françois romieu4da19632011-01-03 15:07:55 +00002853 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002854}
2855
françois romieubca03d52011-01-03 15:07:31 +00002856static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002857{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002858 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002859 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002860 { 0x1f, 0x0001 },
2861 { 0x06, 0x4064 },
2862 { 0x07, 0x2863 },
2863 { 0x08, 0x059c },
2864 { 0x09, 0x26b4 },
2865 { 0x0a, 0x6a19 },
2866 { 0x0b, 0xdcc8 },
2867 { 0x10, 0xf06d },
2868 { 0x14, 0x7f68 },
2869 { 0x18, 0x7fd9 },
2870 { 0x1c, 0xf0ff },
2871 { 0x1d, 0x3d9c },
2872 { 0x1f, 0x0003 },
2873 { 0x12, 0xf49f },
2874 { 0x13, 0x070b },
2875 { 0x1a, 0x05ad },
2876 { 0x14, 0x94c0 },
2877
françois romieubca03d52011-01-03 15:07:31 +00002878 /*
2879 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002880 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002881 */
françois romieudaf9df62009-10-07 12:44:20 +00002882 { 0x1f, 0x0002 },
2883 { 0x06, 0x5561 },
2884 { 0x1f, 0x0005 },
2885 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002886 { 0x06, 0x5561 },
2887
2888 /*
2889 * Can not link to 1Gbps with bad cable
2890 * Decrease SNR threshold form 21.07dB to 19.04dB
2891 */
2892 { 0x1f, 0x0001 },
2893 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002894
2895 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002896 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002897 };
2898
françois romieu4da19632011-01-03 15:07:55 +00002899 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002900
Francois Romieufdf6fc02012-07-06 22:40:38 +02002901 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002902 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002903 { 0x1f, 0x0002 },
2904 { 0x05, 0x669a },
2905 { 0x1f, 0x0005 },
2906 { 0x05, 0x8330 },
2907 { 0x06, 0x669a },
2908
2909 { 0x1f, 0x0002 }
2910 };
2911 int val;
2912
françois romieu4da19632011-01-03 15:07:55 +00002913 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002914
françois romieu4da19632011-01-03 15:07:55 +00002915 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002916 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002917 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002918 0x0065, 0x0066, 0x0067, 0x0068,
2919 0x0069, 0x006a, 0x006b, 0x006c
2920 };
2921 int i;
2922
françois romieu4da19632011-01-03 15:07:55 +00002923 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002924
2925 val &= 0xff00;
2926 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002927 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002928 }
2929 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002930 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002931 { 0x1f, 0x0002 },
2932 { 0x05, 0x2642 },
2933 { 0x1f, 0x0005 },
2934 { 0x05, 0x8330 },
2935 { 0x06, 0x2642 }
2936 };
2937
françois romieu4da19632011-01-03 15:07:55 +00002938 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002939 }
2940
françois romieubca03d52011-01-03 15:07:31 +00002941 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002942 rtl_writephy(tp, 0x1f, 0x0002);
2943 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2944 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002945
françois romieubca03d52011-01-03 15:07:31 +00002946 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002947 rtl_writephy(tp, 0x1f, 0x0002);
2948 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002949
françois romieu4da19632011-01-03 15:07:55 +00002950 rtl_writephy(tp, 0x1f, 0x0005);
2951 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002952
2953 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002954
françois romieu4da19632011-01-03 15:07:55 +00002955 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002956}
2957
françois romieu4da19632011-01-03 15:07:55 +00002958static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002959{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002960 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002961 { 0x1f, 0x0002 },
2962 { 0x10, 0x0008 },
2963 { 0x0d, 0x006c },
2964
2965 { 0x1f, 0x0000 },
2966 { 0x0d, 0xf880 },
2967
2968 { 0x1f, 0x0001 },
2969 { 0x17, 0x0cc0 },
2970
2971 { 0x1f, 0x0001 },
2972 { 0x0b, 0xa4d8 },
2973 { 0x09, 0x281c },
2974 { 0x07, 0x2883 },
2975 { 0x0a, 0x6b35 },
2976 { 0x1d, 0x3da4 },
2977 { 0x1c, 0xeffd },
2978 { 0x14, 0x7f52 },
2979 { 0x18, 0x7fc6 },
2980 { 0x08, 0x0601 },
2981 { 0x06, 0x4063 },
2982 { 0x10, 0xf074 },
2983 { 0x1f, 0x0003 },
2984 { 0x13, 0x0789 },
2985 { 0x12, 0xf4bd },
2986 { 0x1a, 0x04fd },
2987 { 0x14, 0x84b0 },
2988 { 0x1f, 0x0000 },
2989 { 0x00, 0x9200 },
2990
2991 { 0x1f, 0x0005 },
2992 { 0x01, 0x0340 },
2993 { 0x1f, 0x0001 },
2994 { 0x04, 0x4000 },
2995 { 0x03, 0x1d21 },
2996 { 0x02, 0x0c32 },
2997 { 0x01, 0x0200 },
2998 { 0x00, 0x5554 },
2999 { 0x04, 0x4800 },
3000 { 0x04, 0x4000 },
3001 { 0x04, 0xf000 },
3002 { 0x03, 0xdf01 },
3003 { 0x02, 0xdf20 },
3004 { 0x01, 0x101a },
3005 { 0x00, 0xa0ff },
3006 { 0x04, 0xf800 },
3007 { 0x04, 0xf000 },
3008 { 0x1f, 0x0000 },
3009
3010 { 0x1f, 0x0007 },
3011 { 0x1e, 0x0023 },
3012 { 0x16, 0x0000 },
3013 { 0x1f, 0x0000 }
3014 };
3015
françois romieu4da19632011-01-03 15:07:55 +00003016 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003017}
3018
françois romieue6de30d2011-01-03 15:08:37 +00003019static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3020{
3021 static const struct phy_reg phy_reg_init[] = {
3022 { 0x1f, 0x0001 },
3023 { 0x17, 0x0cc0 },
3024
3025 { 0x1f, 0x0007 },
3026 { 0x1e, 0x002d },
3027 { 0x18, 0x0040 },
3028 { 0x1f, 0x0000 }
3029 };
3030
3031 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3032 rtl_patchphy(tp, 0x0d, 1 << 5);
3033}
3034
Hayes Wang70090422011-07-06 15:58:06 +08003035static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003036{
3037 static const struct phy_reg phy_reg_init[] = {
3038 /* Enable Delay cap */
3039 { 0x1f, 0x0005 },
3040 { 0x05, 0x8b80 },
3041 { 0x06, 0xc896 },
3042 { 0x1f, 0x0000 },
3043
3044 /* Channel estimation fine tune */
3045 { 0x1f, 0x0001 },
3046 { 0x0b, 0x6c20 },
3047 { 0x07, 0x2872 },
3048 { 0x1c, 0xefff },
3049 { 0x1f, 0x0003 },
3050 { 0x14, 0x6420 },
3051 { 0x1f, 0x0000 },
3052
3053 /* Update PFM & 10M TX idle timer */
3054 { 0x1f, 0x0007 },
3055 { 0x1e, 0x002f },
3056 { 0x15, 0x1919 },
3057 { 0x1f, 0x0000 },
3058
3059 { 0x1f, 0x0007 },
3060 { 0x1e, 0x00ac },
3061 { 0x18, 0x0006 },
3062 { 0x1f, 0x0000 }
3063 };
3064
Francois Romieu15ecd032011-04-27 13:52:22 -07003065 rtl_apply_firmware(tp);
3066
hayeswang01dc7fe2011-03-21 01:50:28 +00003067 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3068
3069 /* DCO enable for 10M IDLE Power */
3070 rtl_writephy(tp, 0x1f, 0x0007);
3071 rtl_writephy(tp, 0x1e, 0x0023);
3072 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
3073 rtl_writephy(tp, 0x1f, 0x0000);
3074
3075 /* For impedance matching */
3076 rtl_writephy(tp, 0x1f, 0x0002);
3077 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003078 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003079
3080 /* PHY auto speed down */
3081 rtl_writephy(tp, 0x1f, 0x0007);
3082 rtl_writephy(tp, 0x1e, 0x002d);
3083 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
3084 rtl_writephy(tp, 0x1f, 0x0000);
3085 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3086
3087 rtl_writephy(tp, 0x1f, 0x0005);
3088 rtl_writephy(tp, 0x05, 0x8b86);
3089 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3090 rtl_writephy(tp, 0x1f, 0x0000);
3091
3092 rtl_writephy(tp, 0x1f, 0x0005);
3093 rtl_writephy(tp, 0x05, 0x8b85);
3094 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3095 rtl_writephy(tp, 0x1f, 0x0007);
3096 rtl_writephy(tp, 0x1e, 0x0020);
3097 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
3098 rtl_writephy(tp, 0x1f, 0x0006);
3099 rtl_writephy(tp, 0x00, 0x5a00);
3100 rtl_writephy(tp, 0x1f, 0x0000);
3101 rtl_writephy(tp, 0x0d, 0x0007);
3102 rtl_writephy(tp, 0x0e, 0x003c);
3103 rtl_writephy(tp, 0x0d, 0x4007);
3104 rtl_writephy(tp, 0x0e, 0x0000);
3105 rtl_writephy(tp, 0x0d, 0x0000);
3106}
3107
françois romieu9ecb9aa2012-12-07 11:20:21 +00003108static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3109{
3110 const u16 w[] = {
3111 addr[0] | (addr[1] << 8),
3112 addr[2] | (addr[3] << 8),
3113 addr[4] | (addr[5] << 8)
3114 };
3115 const struct exgmac_reg e[] = {
3116 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3117 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3118 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3119 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3120 };
3121
3122 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3123}
3124
Hayes Wang70090422011-07-06 15:58:06 +08003125static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3126{
3127 static const struct phy_reg phy_reg_init[] = {
3128 /* Enable Delay cap */
3129 { 0x1f, 0x0004 },
3130 { 0x1f, 0x0007 },
3131 { 0x1e, 0x00ac },
3132 { 0x18, 0x0006 },
3133 { 0x1f, 0x0002 },
3134 { 0x1f, 0x0000 },
3135 { 0x1f, 0x0000 },
3136
3137 /* Channel estimation fine tune */
3138 { 0x1f, 0x0003 },
3139 { 0x09, 0xa20f },
3140 { 0x1f, 0x0000 },
3141 { 0x1f, 0x0000 },
3142
3143 /* Green Setting */
3144 { 0x1f, 0x0005 },
3145 { 0x05, 0x8b5b },
3146 { 0x06, 0x9222 },
3147 { 0x05, 0x8b6d },
3148 { 0x06, 0x8000 },
3149 { 0x05, 0x8b76 },
3150 { 0x06, 0x8000 },
3151 { 0x1f, 0x0000 }
3152 };
3153
3154 rtl_apply_firmware(tp);
3155
3156 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3157
3158 /* For 4-corner performance improve */
3159 rtl_writephy(tp, 0x1f, 0x0005);
3160 rtl_writephy(tp, 0x05, 0x8b80);
3161 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
3162 rtl_writephy(tp, 0x1f, 0x0000);
3163
3164 /* PHY auto speed down */
3165 rtl_writephy(tp, 0x1f, 0x0004);
3166 rtl_writephy(tp, 0x1f, 0x0007);
3167 rtl_writephy(tp, 0x1e, 0x002d);
3168 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3169 rtl_writephy(tp, 0x1f, 0x0002);
3170 rtl_writephy(tp, 0x1f, 0x0000);
3171 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3172
3173 /* improve 10M EEE waveform */
3174 rtl_writephy(tp, 0x1f, 0x0005);
3175 rtl_writephy(tp, 0x05, 0x8b86);
3176 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3177 rtl_writephy(tp, 0x1f, 0x0000);
3178
3179 /* Improve 2-pair detection performance */
3180 rtl_writephy(tp, 0x1f, 0x0005);
3181 rtl_writephy(tp, 0x05, 0x8b85);
3182 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3183 rtl_writephy(tp, 0x1f, 0x0000);
3184
3185 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003186 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003187 rtl_writephy(tp, 0x1f, 0x0005);
3188 rtl_writephy(tp, 0x05, 0x8b85);
3189 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3190 rtl_writephy(tp, 0x1f, 0x0004);
3191 rtl_writephy(tp, 0x1f, 0x0007);
3192 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04003193 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003194 rtl_writephy(tp, 0x1f, 0x0002);
3195 rtl_writephy(tp, 0x1f, 0x0000);
3196 rtl_writephy(tp, 0x0d, 0x0007);
3197 rtl_writephy(tp, 0x0e, 0x003c);
3198 rtl_writephy(tp, 0x0d, 0x4007);
3199 rtl_writephy(tp, 0x0e, 0x0000);
3200 rtl_writephy(tp, 0x0d, 0x0000);
3201
3202 /* Green feature */
3203 rtl_writephy(tp, 0x1f, 0x0003);
3204 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3205 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3206 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003207
françois romieu9ecb9aa2012-12-07 11:20:21 +00003208 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3209 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003210}
3211
Hayes Wang5f886e02012-03-30 14:33:03 +08003212static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3213{
3214 /* For 4-corner performance improve */
3215 rtl_writephy(tp, 0x1f, 0x0005);
3216 rtl_writephy(tp, 0x05, 0x8b80);
3217 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3218 rtl_writephy(tp, 0x1f, 0x0000);
3219
3220 /* PHY auto speed down */
3221 rtl_writephy(tp, 0x1f, 0x0007);
3222 rtl_writephy(tp, 0x1e, 0x002d);
3223 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3224 rtl_writephy(tp, 0x1f, 0x0000);
3225 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3226
3227 /* Improve 10M EEE waveform */
3228 rtl_writephy(tp, 0x1f, 0x0005);
3229 rtl_writephy(tp, 0x05, 0x8b86);
3230 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3231 rtl_writephy(tp, 0x1f, 0x0000);
3232}
3233
Hayes Wangc2218922011-09-06 16:55:18 +08003234static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3235{
3236 static const struct phy_reg phy_reg_init[] = {
3237 /* Channel estimation fine tune */
3238 { 0x1f, 0x0003 },
3239 { 0x09, 0xa20f },
3240 { 0x1f, 0x0000 },
3241
3242 /* Modify green table for giga & fnet */
3243 { 0x1f, 0x0005 },
3244 { 0x05, 0x8b55 },
3245 { 0x06, 0x0000 },
3246 { 0x05, 0x8b5e },
3247 { 0x06, 0x0000 },
3248 { 0x05, 0x8b67 },
3249 { 0x06, 0x0000 },
3250 { 0x05, 0x8b70 },
3251 { 0x06, 0x0000 },
3252 { 0x1f, 0x0000 },
3253 { 0x1f, 0x0007 },
3254 { 0x1e, 0x0078 },
3255 { 0x17, 0x0000 },
3256 { 0x19, 0x00fb },
3257 { 0x1f, 0x0000 },
3258
3259 /* Modify green table for 10M */
3260 { 0x1f, 0x0005 },
3261 { 0x05, 0x8b79 },
3262 { 0x06, 0xaa00 },
3263 { 0x1f, 0x0000 },
3264
3265 /* Disable hiimpedance detection (RTCT) */
3266 { 0x1f, 0x0003 },
3267 { 0x01, 0x328a },
3268 { 0x1f, 0x0000 }
3269 };
3270
3271 rtl_apply_firmware(tp);
3272
3273 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3274
Hayes Wang5f886e02012-03-30 14:33:03 +08003275 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003276
3277 /* Improve 2-pair detection performance */
3278 rtl_writephy(tp, 0x1f, 0x0005);
3279 rtl_writephy(tp, 0x05, 0x8b85);
3280 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3281 rtl_writephy(tp, 0x1f, 0x0000);
3282}
3283
3284static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3285{
3286 rtl_apply_firmware(tp);
3287
Hayes Wang5f886e02012-03-30 14:33:03 +08003288 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003289}
3290
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003291static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3292{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003293 static const struct phy_reg phy_reg_init[] = {
3294 /* Channel estimation fine tune */
3295 { 0x1f, 0x0003 },
3296 { 0x09, 0xa20f },
3297 { 0x1f, 0x0000 },
3298
3299 /* Modify green table for giga & fnet */
3300 { 0x1f, 0x0005 },
3301 { 0x05, 0x8b55 },
3302 { 0x06, 0x0000 },
3303 { 0x05, 0x8b5e },
3304 { 0x06, 0x0000 },
3305 { 0x05, 0x8b67 },
3306 { 0x06, 0x0000 },
3307 { 0x05, 0x8b70 },
3308 { 0x06, 0x0000 },
3309 { 0x1f, 0x0000 },
3310 { 0x1f, 0x0007 },
3311 { 0x1e, 0x0078 },
3312 { 0x17, 0x0000 },
3313 { 0x19, 0x00aa },
3314 { 0x1f, 0x0000 },
3315
3316 /* Modify green table for 10M */
3317 { 0x1f, 0x0005 },
3318 { 0x05, 0x8b79 },
3319 { 0x06, 0xaa00 },
3320 { 0x1f, 0x0000 },
3321
3322 /* Disable hiimpedance detection (RTCT) */
3323 { 0x1f, 0x0003 },
3324 { 0x01, 0x328a },
3325 { 0x1f, 0x0000 }
3326 };
3327
3328
3329 rtl_apply_firmware(tp);
3330
3331 rtl8168f_hw_phy_config(tp);
3332
3333 /* Improve 2-pair detection performance */
3334 rtl_writephy(tp, 0x1f, 0x0005);
3335 rtl_writephy(tp, 0x05, 0x8b85);
3336 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3337 rtl_writephy(tp, 0x1f, 0x0000);
3338
3339 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3340
3341 /* Modify green table for giga */
3342 rtl_writephy(tp, 0x1f, 0x0005);
3343 rtl_writephy(tp, 0x05, 0x8b54);
3344 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3345 rtl_writephy(tp, 0x05, 0x8b5d);
3346 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3347 rtl_writephy(tp, 0x05, 0x8a7c);
3348 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3349 rtl_writephy(tp, 0x05, 0x8a7f);
3350 rtl_w1w0_phy(tp, 0x06, 0x0100, 0x0000);
3351 rtl_writephy(tp, 0x05, 0x8a82);
3352 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3353 rtl_writephy(tp, 0x05, 0x8a85);
3354 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3355 rtl_writephy(tp, 0x05, 0x8a88);
3356 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3357 rtl_writephy(tp, 0x1f, 0x0000);
3358
3359 /* uc same-seed solution */
3360 rtl_writephy(tp, 0x1f, 0x0005);
3361 rtl_writephy(tp, 0x05, 0x8b85);
3362 rtl_w1w0_phy(tp, 0x06, 0x8000, 0x0000);
3363 rtl_writephy(tp, 0x1f, 0x0000);
3364
3365 /* eee setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003366 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003367 rtl_writephy(tp, 0x1f, 0x0005);
3368 rtl_writephy(tp, 0x05, 0x8b85);
3369 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3370 rtl_writephy(tp, 0x1f, 0x0004);
3371 rtl_writephy(tp, 0x1f, 0x0007);
3372 rtl_writephy(tp, 0x1e, 0x0020);
3373 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
3374 rtl_writephy(tp, 0x1f, 0x0000);
3375 rtl_writephy(tp, 0x0d, 0x0007);
3376 rtl_writephy(tp, 0x0e, 0x003c);
3377 rtl_writephy(tp, 0x0d, 0x4007);
3378 rtl_writephy(tp, 0x0e, 0x0000);
3379 rtl_writephy(tp, 0x0d, 0x0000);
3380
3381 /* Green feature */
3382 rtl_writephy(tp, 0x1f, 0x0003);
3383 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3384 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3385 rtl_writephy(tp, 0x1f, 0x0000);
3386}
3387
Hayes Wangc5583862012-07-02 17:23:22 +08003388static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3389{
Hayes Wangc5583862012-07-02 17:23:22 +08003390 rtl_apply_firmware(tp);
3391
hayeswang41f44d12013-04-01 22:23:36 +00003392 rtl_writephy(tp, 0x1f, 0x0a46);
3393 if (rtl_readphy(tp, 0x10) & 0x0100) {
3394 rtl_writephy(tp, 0x1f, 0x0bcc);
3395 rtl_w1w0_phy(tp, 0x12, 0x0000, 0x8000);
3396 } else {
3397 rtl_writephy(tp, 0x1f, 0x0bcc);
3398 rtl_w1w0_phy(tp, 0x12, 0x8000, 0x0000);
3399 }
Hayes Wangc5583862012-07-02 17:23:22 +08003400
hayeswang41f44d12013-04-01 22:23:36 +00003401 rtl_writephy(tp, 0x1f, 0x0a46);
3402 if (rtl_readphy(tp, 0x13) & 0x0100) {
3403 rtl_writephy(tp, 0x1f, 0x0c41);
3404 rtl_w1w0_phy(tp, 0x15, 0x0002, 0x0000);
3405 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003406 rtl_writephy(tp, 0x1f, 0x0c41);
3407 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003408 }
Hayes Wangc5583862012-07-02 17:23:22 +08003409
hayeswang41f44d12013-04-01 22:23:36 +00003410 /* Enable PHY auto speed down */
3411 rtl_writephy(tp, 0x1f, 0x0a44);
3412 rtl_w1w0_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003413
hayeswangfe7524c2013-04-01 22:23:37 +00003414 rtl_writephy(tp, 0x1f, 0x0bcc);
3415 rtl_w1w0_phy(tp, 0x14, 0x0100, 0x0000);
3416 rtl_writephy(tp, 0x1f, 0x0a44);
3417 rtl_w1w0_phy(tp, 0x11, 0x00c0, 0x0000);
3418 rtl_writephy(tp, 0x1f, 0x0a43);
3419 rtl_writephy(tp, 0x13, 0x8084);
3420 rtl_w1w0_phy(tp, 0x14, 0x0000, 0x6000);
3421 rtl_w1w0_phy(tp, 0x10, 0x1003, 0x0000);
3422
hayeswang41f44d12013-04-01 22:23:36 +00003423 /* EEE auto-fallback function */
3424 rtl_writephy(tp, 0x1f, 0x0a4b);
3425 rtl_w1w0_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003426
hayeswang41f44d12013-04-01 22:23:36 +00003427 /* Enable UC LPF tune function */
3428 rtl_writephy(tp, 0x1f, 0x0a43);
3429 rtl_writephy(tp, 0x13, 0x8012);
3430 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3431
3432 rtl_writephy(tp, 0x1f, 0x0c42);
3433 rtl_w1w0_phy(tp, 0x11, 0x4000, 0x2000);
3434
hayeswangfe7524c2013-04-01 22:23:37 +00003435 /* Improve SWR Efficiency */
3436 rtl_writephy(tp, 0x1f, 0x0bcd);
3437 rtl_writephy(tp, 0x14, 0x5065);
3438 rtl_writephy(tp, 0x14, 0xd065);
3439 rtl_writephy(tp, 0x1f, 0x0bc8);
3440 rtl_writephy(tp, 0x11, 0x5655);
3441 rtl_writephy(tp, 0x1f, 0x0bcd);
3442 rtl_writephy(tp, 0x14, 0x1065);
3443 rtl_writephy(tp, 0x14, 0x9065);
3444 rtl_writephy(tp, 0x14, 0x1065);
3445
David Chang1bac1072013-11-27 15:48:36 +08003446 /* Check ALDPS bit, disable it if enabled */
3447 rtl_writephy(tp, 0x1f, 0x0a43);
3448 if (rtl_readphy(tp, 0x10) & 0x0004)
3449 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0004);
3450
hayeswang41f44d12013-04-01 22:23:36 +00003451 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003452}
3453
hayeswang57538c42013-04-01 22:23:40 +00003454static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3455{
3456 rtl_apply_firmware(tp);
3457}
3458
françois romieu4da19632011-01-03 15:07:55 +00003459static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003460{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003461 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003462 { 0x1f, 0x0003 },
3463 { 0x08, 0x441d },
3464 { 0x01, 0x9100 },
3465 { 0x1f, 0x0000 }
3466 };
3467
françois romieu4da19632011-01-03 15:07:55 +00003468 rtl_writephy(tp, 0x1f, 0x0000);
3469 rtl_patchphy(tp, 0x11, 1 << 12);
3470 rtl_patchphy(tp, 0x19, 1 << 13);
3471 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003472
françois romieu4da19632011-01-03 15:07:55 +00003473 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003474}
3475
Hayes Wang5a5e4442011-02-22 17:26:21 +08003476static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3477{
3478 static const struct phy_reg phy_reg_init[] = {
3479 { 0x1f, 0x0005 },
3480 { 0x1a, 0x0000 },
3481 { 0x1f, 0x0000 },
3482
3483 { 0x1f, 0x0004 },
3484 { 0x1c, 0x0000 },
3485 { 0x1f, 0x0000 },
3486
3487 { 0x1f, 0x0001 },
3488 { 0x15, 0x7701 },
3489 { 0x1f, 0x0000 }
3490 };
3491
3492 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003493 rtl_writephy(tp, 0x1f, 0x0000);
3494 rtl_writephy(tp, 0x18, 0x0310);
3495 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003496
François Romieu953a12c2011-04-24 17:38:48 +02003497 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003498
3499 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3500}
3501
Hayes Wang7e18dca2012-03-30 14:33:02 +08003502static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3503{
Hayes Wang7e18dca2012-03-30 14:33:02 +08003504 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01003505 rtl_writephy(tp, 0x1f, 0x0000);
3506 rtl_writephy(tp, 0x18, 0x0310);
3507 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003508
3509 rtl_apply_firmware(tp);
3510
3511 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003512 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003513 rtl_writephy(tp, 0x1f, 0x0004);
3514 rtl_writephy(tp, 0x10, 0x401f);
3515 rtl_writephy(tp, 0x19, 0x7030);
3516 rtl_writephy(tp, 0x1f, 0x0000);
3517}
3518
Hayes Wang5598bfe2012-07-02 17:23:21 +08003519static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3520{
Hayes Wang5598bfe2012-07-02 17:23:21 +08003521 static const struct phy_reg phy_reg_init[] = {
3522 { 0x1f, 0x0004 },
3523 { 0x10, 0xc07f },
3524 { 0x19, 0x7030 },
3525 { 0x1f, 0x0000 }
3526 };
3527
3528 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003529 rtl_writephy(tp, 0x1f, 0x0000);
3530 rtl_writephy(tp, 0x18, 0x0310);
3531 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003532
3533 rtl_apply_firmware(tp);
3534
Francois Romieufdf6fc02012-07-06 22:40:38 +02003535 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003536 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3537
Francois Romieufdf6fc02012-07-06 22:40:38 +02003538 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003539}
3540
Francois Romieu5615d9f2007-08-17 17:50:46 +02003541static void rtl_hw_phy_config(struct net_device *dev)
3542{
3543 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003544
3545 rtl8169_print_mac_version(tp);
3546
3547 switch (tp->mac_version) {
3548 case RTL_GIGA_MAC_VER_01:
3549 break;
3550 case RTL_GIGA_MAC_VER_02:
3551 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003552 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003553 break;
3554 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003555 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003556 break;
françois romieu2e9558562009-08-10 19:44:19 +00003557 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003558 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003559 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003560 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003561 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003562 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003563 case RTL_GIGA_MAC_VER_07:
3564 case RTL_GIGA_MAC_VER_08:
3565 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003566 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003567 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003568 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003569 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003570 break;
3571 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003572 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003573 break;
3574 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003575 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003576 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003577 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003578 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003579 break;
3580 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003581 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003582 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003583 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003584 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003585 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003586 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003587 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003588 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003589 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003590 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003591 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003592 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003593 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003594 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003595 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003596 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003597 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003598 break;
3599 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003600 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003601 break;
3602 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003603 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003604 break;
françois romieue6de30d2011-01-03 15:08:37 +00003605 case RTL_GIGA_MAC_VER_28:
3606 rtl8168d_4_hw_phy_config(tp);
3607 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003608 case RTL_GIGA_MAC_VER_29:
3609 case RTL_GIGA_MAC_VER_30:
3610 rtl8105e_hw_phy_config(tp);
3611 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003612 case RTL_GIGA_MAC_VER_31:
3613 /* None. */
3614 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003615 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003616 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003617 rtl8168e_1_hw_phy_config(tp);
3618 break;
3619 case RTL_GIGA_MAC_VER_34:
3620 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003621 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003622 case RTL_GIGA_MAC_VER_35:
3623 rtl8168f_1_hw_phy_config(tp);
3624 break;
3625 case RTL_GIGA_MAC_VER_36:
3626 rtl8168f_2_hw_phy_config(tp);
3627 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003628
Hayes Wang7e18dca2012-03-30 14:33:02 +08003629 case RTL_GIGA_MAC_VER_37:
3630 rtl8402_hw_phy_config(tp);
3631 break;
3632
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003633 case RTL_GIGA_MAC_VER_38:
3634 rtl8411_hw_phy_config(tp);
3635 break;
3636
Hayes Wang5598bfe2012-07-02 17:23:21 +08003637 case RTL_GIGA_MAC_VER_39:
3638 rtl8106e_hw_phy_config(tp);
3639 break;
3640
Hayes Wangc5583862012-07-02 17:23:22 +08003641 case RTL_GIGA_MAC_VER_40:
3642 rtl8168g_1_hw_phy_config(tp);
3643 break;
hayeswang57538c42013-04-01 22:23:40 +00003644 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00003645 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08003646 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00003647 rtl8168g_2_hw_phy_config(tp);
3648 break;
Hayes Wangc5583862012-07-02 17:23:22 +08003649
3650 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02003651 default:
3652 break;
3653 }
3654}
3655
Francois Romieuda78dbf2012-01-26 14:18:23 +01003656static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 struct timer_list *timer = &tp->timer;
3659 void __iomem *ioaddr = tp->mmio_addr;
3660 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3661
Francois Romieubcf0bf92006-07-26 23:14:13 +02003662 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
françois romieu4da19632011-01-03 15:07:55 +00003664 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003665 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 * A busy loop could burn quite a few cycles on nowadays CPU.
3667 * Let's delay the execution of the timer for a few ticks.
3668 */
3669 timeout = HZ/10;
3670 goto out_mod_timer;
3671 }
3672
3673 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01003674 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675
Lekensteyn9bb8eeb2013-08-02 10:36:55 +02003676 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
françois romieu4da19632011-01-03 15:07:55 +00003678 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679
3680out_mod_timer:
3681 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003682}
3683
3684static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3685{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003686 if (!test_and_set_bit(flag, tp->wk.flags))
3687 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003688}
3689
3690static void rtl8169_phy_timer(unsigned long __opaque)
3691{
3692 struct net_device *dev = (struct net_device *)__opaque;
3693 struct rtl8169_private *tp = netdev_priv(dev);
3694
Francois Romieu98ddf982012-01-31 10:47:34 +01003695 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696}
3697
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3699 void __iomem *ioaddr)
3700{
3701 iounmap(ioaddr);
3702 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003703 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 pci_disable_device(pdev);
3705 free_netdev(dev);
3706}
3707
Francois Romieuffc46952012-07-06 14:19:23 +02003708DECLARE_RTL_COND(rtl_phy_reset_cond)
3709{
3710 return tp->phy_reset_pending(tp);
3711}
3712
Francois Romieubf793292006-11-01 00:53:05 +01003713static void rtl8169_phy_reset(struct net_device *dev,
3714 struct rtl8169_private *tp)
3715{
françois romieu4da19632011-01-03 15:07:55 +00003716 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02003717 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01003718}
3719
David S. Miller8decf862011-09-22 03:23:13 -04003720static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3721{
3722 void __iomem *ioaddr = tp->mmio_addr;
3723
3724 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3725 (RTL_R8(PHYstatus) & TBI_Enable);
3726}
3727
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003728static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003730 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003731
Francois Romieu5615d9f2007-08-17 17:50:46 +02003732 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003733
Marcus Sundberg773328942008-07-10 21:28:08 +02003734 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3735 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3736 RTL_W8(0x82, 0x01);
3737 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003738
Francois Romieu6dccd162007-02-13 23:38:05 +01003739 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3740
3741 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3742 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003743
Francois Romieubcf0bf92006-07-26 23:14:13 +02003744 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003745 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3746 RTL_W8(0x82, 0x01);
3747 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003748 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003749 }
3750
Francois Romieubf793292006-11-01 00:53:05 +01003751 rtl8169_phy_reset(dev, tp);
3752
Oliver Neukum54405cd2011-01-06 21:55:13 +01003753 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003754 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3755 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3756 (tp->mii.supports_gmii ?
3757 ADVERTISED_1000baseT_Half |
3758 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003759
David S. Miller8decf862011-09-22 03:23:13 -04003760 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003761 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003762}
3763
Francois Romieu773d2022007-01-31 23:47:43 +01003764static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3765{
3766 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu773d2022007-01-31 23:47:43 +01003767
Francois Romieuda78dbf2012-01-26 14:18:23 +01003768 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003769
3770 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003771
françois romieu9ecb9aa2012-12-07 11:20:21 +00003772 RTL_W32(MAC4, addr[4] | addr[5] << 8);
françois romieu908ba2b2010-04-26 11:42:58 +00003773 RTL_R32(MAC4);
3774
françois romieu9ecb9aa2012-12-07 11:20:21 +00003775 RTL_W32(MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
françois romieu908ba2b2010-04-26 11:42:58 +00003776 RTL_R32(MAC0);
3777
françois romieu9ecb9aa2012-12-07 11:20:21 +00003778 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
3779 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00003780
Francois Romieu773d2022007-01-31 23:47:43 +01003781 RTL_W8(Cfg9346, Cfg9346_Lock);
3782
Francois Romieuda78dbf2012-01-26 14:18:23 +01003783 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003784}
3785
3786static int rtl_set_mac_address(struct net_device *dev, void *p)
3787{
3788 struct rtl8169_private *tp = netdev_priv(dev);
3789 struct sockaddr *addr = p;
3790
3791 if (!is_valid_ether_addr(addr->sa_data))
3792 return -EADDRNOTAVAIL;
3793
3794 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3795
3796 rtl_rar_set(tp, dev->dev_addr);
3797
3798 return 0;
3799}
3800
Francois Romieu5f787a12006-08-17 13:02:36 +02003801static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3802{
3803 struct rtl8169_private *tp = netdev_priv(dev);
3804 struct mii_ioctl_data *data = if_mii(ifr);
3805
Francois Romieu8b4ab282008-11-19 22:05:25 -08003806 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3807}
Francois Romieu5f787a12006-08-17 13:02:36 +02003808
Francois Romieucecb5fd2011-04-01 10:21:07 +02003809static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3810 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003811{
Francois Romieu5f787a12006-08-17 13:02:36 +02003812 switch (cmd) {
3813 case SIOCGMIIPHY:
3814 data->phy_id = 32; /* Internal PHY */
3815 return 0;
3816
3817 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003818 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003819 return 0;
3820
3821 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003822 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003823 return 0;
3824 }
3825 return -EOPNOTSUPP;
3826}
3827
Francois Romieu8b4ab282008-11-19 22:05:25 -08003828static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3829{
3830 return -EOPNOTSUPP;
3831}
3832
Francois Romieufbac58f2007-10-04 22:51:38 +02003833static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3834{
3835 if (tp->features & RTL_FEATURE_MSI) {
3836 pci_disable_msi(pdev);
3837 tp->features &= ~RTL_FEATURE_MSI;
3838 }
3839}
3840
Bill Pembertonbaf63292012-12-03 09:23:28 -05003841static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00003842{
3843 struct mdio_ops *ops = &tp->mdio_ops;
3844
3845 switch (tp->mac_version) {
3846 case RTL_GIGA_MAC_VER_27:
3847 ops->write = r8168dp_1_mdio_write;
3848 ops->read = r8168dp_1_mdio_read;
3849 break;
françois romieue6de30d2011-01-03 15:08:37 +00003850 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003851 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003852 ops->write = r8168dp_2_mdio_write;
3853 ops->read = r8168dp_2_mdio_read;
3854 break;
Hayes Wangc5583862012-07-02 17:23:22 +08003855 case RTL_GIGA_MAC_VER_40:
3856 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00003857 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00003858 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08003859 case RTL_GIGA_MAC_VER_44:
Hayes Wangc5583862012-07-02 17:23:22 +08003860 ops->write = r8168g_mdio_write;
3861 ops->read = r8168g_mdio_read;
3862 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003863 default:
3864 ops->write = r8169_mdio_write;
3865 ops->read = r8169_mdio_read;
3866 break;
3867 }
3868}
3869
hayeswange2409d82013-03-31 17:02:04 +00003870static void rtl_speed_down(struct rtl8169_private *tp)
3871{
3872 u32 adv;
3873 int lpa;
3874
3875 rtl_writephy(tp, 0x1f, 0x0000);
3876 lpa = rtl_readphy(tp, MII_LPA);
3877
3878 if (lpa & (LPA_10HALF | LPA_10FULL))
3879 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
3880 else if (lpa & (LPA_100HALF | LPA_100FULL))
3881 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3882 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
3883 else
3884 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3885 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3886 (tp->mii.supports_gmii ?
3887 ADVERTISED_1000baseT_Half |
3888 ADVERTISED_1000baseT_Full : 0);
3889
3890 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
3891 adv);
3892}
3893
David S. Miller1805b2f2011-10-24 18:18:09 -04003894static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3895{
3896 void __iomem *ioaddr = tp->mmio_addr;
3897
3898 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00003899 case RTL_GIGA_MAC_VER_25:
3900 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04003901 case RTL_GIGA_MAC_VER_29:
3902 case RTL_GIGA_MAC_VER_30:
3903 case RTL_GIGA_MAC_VER_32:
3904 case RTL_GIGA_MAC_VER_33:
3905 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08003906 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003907 case RTL_GIGA_MAC_VER_38:
Hayes Wang5598bfe2012-07-02 17:23:21 +08003908 case RTL_GIGA_MAC_VER_39:
Hayes Wangc5583862012-07-02 17:23:22 +08003909 case RTL_GIGA_MAC_VER_40:
3910 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00003911 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00003912 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08003913 case RTL_GIGA_MAC_VER_44:
David S. Miller1805b2f2011-10-24 18:18:09 -04003914 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3915 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3916 break;
3917 default:
3918 break;
3919 }
3920}
3921
3922static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3923{
3924 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3925 return false;
3926
hayeswange2409d82013-03-31 17:02:04 +00003927 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04003928 rtl_wol_suspend_quirk(tp);
3929
3930 return true;
3931}
3932
françois romieu065c27c2011-01-03 15:08:12 +00003933static void r810x_phy_power_down(struct rtl8169_private *tp)
3934{
3935 rtl_writephy(tp, 0x1f, 0x0000);
3936 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3937}
3938
3939static void r810x_phy_power_up(struct rtl8169_private *tp)
3940{
3941 rtl_writephy(tp, 0x1f, 0x0000);
3942 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3943}
3944
3945static void r810x_pll_power_down(struct rtl8169_private *tp)
3946{
Hayes Wang00042992012-03-30 14:33:00 +08003947 void __iomem *ioaddr = tp->mmio_addr;
3948
David S. Miller1805b2f2011-10-24 18:18:09 -04003949 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003950 return;
françois romieu065c27c2011-01-03 15:08:12 +00003951
3952 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003953
3954 switch (tp->mac_version) {
3955 case RTL_GIGA_MAC_VER_07:
3956 case RTL_GIGA_MAC_VER_08:
3957 case RTL_GIGA_MAC_VER_09:
3958 case RTL_GIGA_MAC_VER_10:
3959 case RTL_GIGA_MAC_VER_13:
3960 case RTL_GIGA_MAC_VER_16:
3961 break;
3962 default:
3963 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3964 break;
3965 }
françois romieu065c27c2011-01-03 15:08:12 +00003966}
3967
3968static void r810x_pll_power_up(struct rtl8169_private *tp)
3969{
Hayes Wang00042992012-03-30 14:33:00 +08003970 void __iomem *ioaddr = tp->mmio_addr;
3971
françois romieu065c27c2011-01-03 15:08:12 +00003972 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003973
3974 switch (tp->mac_version) {
3975 case RTL_GIGA_MAC_VER_07:
3976 case RTL_GIGA_MAC_VER_08:
3977 case RTL_GIGA_MAC_VER_09:
3978 case RTL_GIGA_MAC_VER_10:
3979 case RTL_GIGA_MAC_VER_13:
3980 case RTL_GIGA_MAC_VER_16:
3981 break;
3982 default:
3983 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3984 break;
3985 }
françois romieu065c27c2011-01-03 15:08:12 +00003986}
3987
3988static void r8168_phy_power_up(struct rtl8169_private *tp)
3989{
3990 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003991 switch (tp->mac_version) {
3992 case RTL_GIGA_MAC_VER_11:
3993 case RTL_GIGA_MAC_VER_12:
3994 case RTL_GIGA_MAC_VER_17:
3995 case RTL_GIGA_MAC_VER_18:
3996 case RTL_GIGA_MAC_VER_19:
3997 case RTL_GIGA_MAC_VER_20:
3998 case RTL_GIGA_MAC_VER_21:
3999 case RTL_GIGA_MAC_VER_22:
4000 case RTL_GIGA_MAC_VER_23:
4001 case RTL_GIGA_MAC_VER_24:
4002 case RTL_GIGA_MAC_VER_25:
4003 case RTL_GIGA_MAC_VER_26:
4004 case RTL_GIGA_MAC_VER_27:
4005 case RTL_GIGA_MAC_VER_28:
4006 case RTL_GIGA_MAC_VER_31:
4007 rtl_writephy(tp, 0x0e, 0x0000);
4008 break;
4009 default:
4010 break;
4011 }
françois romieu065c27c2011-01-03 15:08:12 +00004012 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4013}
4014
4015static void r8168_phy_power_down(struct rtl8169_private *tp)
4016{
4017 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004018 switch (tp->mac_version) {
4019 case RTL_GIGA_MAC_VER_32:
4020 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004021 case RTL_GIGA_MAC_VER_40:
4022 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004023 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4024 break;
4025
4026 case RTL_GIGA_MAC_VER_11:
4027 case RTL_GIGA_MAC_VER_12:
4028 case RTL_GIGA_MAC_VER_17:
4029 case RTL_GIGA_MAC_VER_18:
4030 case RTL_GIGA_MAC_VER_19:
4031 case RTL_GIGA_MAC_VER_20:
4032 case RTL_GIGA_MAC_VER_21:
4033 case RTL_GIGA_MAC_VER_22:
4034 case RTL_GIGA_MAC_VER_23:
4035 case RTL_GIGA_MAC_VER_24:
4036 case RTL_GIGA_MAC_VER_25:
4037 case RTL_GIGA_MAC_VER_26:
4038 case RTL_GIGA_MAC_VER_27:
4039 case RTL_GIGA_MAC_VER_28:
4040 case RTL_GIGA_MAC_VER_31:
4041 rtl_writephy(tp, 0x0e, 0x0200);
4042 default:
4043 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4044 break;
4045 }
françois romieu065c27c2011-01-03 15:08:12 +00004046}
4047
4048static void r8168_pll_power_down(struct rtl8169_private *tp)
4049{
4050 void __iomem *ioaddr = tp->mmio_addr;
4051
Francois Romieucecb5fd2011-04-01 10:21:07 +02004052 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4053 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4054 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00004055 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00004056 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08004057 }
françois romieu065c27c2011-01-03 15:08:12 +00004058
Francois Romieucecb5fd2011-04-01 10:21:07 +02004059 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4060 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00004061 (RTL_R16(CPlusCmd) & ASF)) {
4062 return;
4063 }
4064
hayeswang01dc7fe2011-03-21 01:50:28 +00004065 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4066 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004067 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004068
David S. Miller1805b2f2011-10-24 18:18:09 -04004069 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004070 return;
françois romieu065c27c2011-01-03 15:08:12 +00004071
4072 r8168_phy_power_down(tp);
4073
4074 switch (tp->mac_version) {
4075 case RTL_GIGA_MAC_VER_25:
4076 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004077 case RTL_GIGA_MAC_VER_27:
4078 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004079 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004080 case RTL_GIGA_MAC_VER_32:
4081 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00004082 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4083 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004084 case RTL_GIGA_MAC_VER_40:
4085 case RTL_GIGA_MAC_VER_41:
4086 rtl_w1w0_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
4087 0xfc000000, ERIAR_EXGMAC);
4088 break;
françois romieu065c27c2011-01-03 15:08:12 +00004089 }
4090}
4091
4092static void r8168_pll_power_up(struct rtl8169_private *tp)
4093{
4094 void __iomem *ioaddr = tp->mmio_addr;
4095
françois romieu065c27c2011-01-03 15:08:12 +00004096 switch (tp->mac_version) {
4097 case RTL_GIGA_MAC_VER_25:
4098 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004099 case RTL_GIGA_MAC_VER_27:
4100 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004101 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004102 case RTL_GIGA_MAC_VER_32:
4103 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00004104 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4105 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004106 case RTL_GIGA_MAC_VER_40:
4107 case RTL_GIGA_MAC_VER_41:
4108 rtl_w1w0_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
4109 0x00000000, ERIAR_EXGMAC);
4110 break;
françois romieu065c27c2011-01-03 15:08:12 +00004111 }
4112
4113 r8168_phy_power_up(tp);
4114}
4115
Francois Romieud58d46b2011-05-03 16:38:29 +02004116static void rtl_generic_op(struct rtl8169_private *tp,
4117 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00004118{
4119 if (op)
4120 op(tp);
4121}
4122
4123static void rtl_pll_power_down(struct rtl8169_private *tp)
4124{
Francois Romieud58d46b2011-05-03 16:38:29 +02004125 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00004126}
4127
4128static void rtl_pll_power_up(struct rtl8169_private *tp)
4129{
Francois Romieud58d46b2011-05-03 16:38:29 +02004130 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00004131}
4132
Bill Pembertonbaf63292012-12-03 09:23:28 -05004133static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00004134{
4135 struct pll_power_ops *ops = &tp->pll_power_ops;
4136
4137 switch (tp->mac_version) {
4138 case RTL_GIGA_MAC_VER_07:
4139 case RTL_GIGA_MAC_VER_08:
4140 case RTL_GIGA_MAC_VER_09:
4141 case RTL_GIGA_MAC_VER_10:
4142 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08004143 case RTL_GIGA_MAC_VER_29:
4144 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004145 case RTL_GIGA_MAC_VER_37:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004146 case RTL_GIGA_MAC_VER_39:
hayeswang58152cd2013-04-01 22:23:42 +00004147 case RTL_GIGA_MAC_VER_43:
françois romieu065c27c2011-01-03 15:08:12 +00004148 ops->down = r810x_pll_power_down;
4149 ops->up = r810x_pll_power_up;
4150 break;
4151
4152 case RTL_GIGA_MAC_VER_11:
4153 case RTL_GIGA_MAC_VER_12:
4154 case RTL_GIGA_MAC_VER_17:
4155 case RTL_GIGA_MAC_VER_18:
4156 case RTL_GIGA_MAC_VER_19:
4157 case RTL_GIGA_MAC_VER_20:
4158 case RTL_GIGA_MAC_VER_21:
4159 case RTL_GIGA_MAC_VER_22:
4160 case RTL_GIGA_MAC_VER_23:
4161 case RTL_GIGA_MAC_VER_24:
4162 case RTL_GIGA_MAC_VER_25:
4163 case RTL_GIGA_MAC_VER_26:
4164 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00004165 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004166 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004167 case RTL_GIGA_MAC_VER_32:
4168 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004169 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08004170 case RTL_GIGA_MAC_VER_35:
4171 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004172 case RTL_GIGA_MAC_VER_38:
Hayes Wangc5583862012-07-02 17:23:22 +08004173 case RTL_GIGA_MAC_VER_40:
4174 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004175 case RTL_GIGA_MAC_VER_42:
hayeswang45dd95c2013-07-08 17:09:01 +08004176 case RTL_GIGA_MAC_VER_44:
françois romieu065c27c2011-01-03 15:08:12 +00004177 ops->down = r8168_pll_power_down;
4178 ops->up = r8168_pll_power_up;
4179 break;
4180
4181 default:
4182 ops->down = NULL;
4183 ops->up = NULL;
4184 break;
4185 }
4186}
4187
Hayes Wange542a222011-07-06 15:58:04 +08004188static void rtl_init_rxcfg(struct rtl8169_private *tp)
4189{
4190 void __iomem *ioaddr = tp->mmio_addr;
4191
4192 switch (tp->mac_version) {
4193 case RTL_GIGA_MAC_VER_01:
4194 case RTL_GIGA_MAC_VER_02:
4195 case RTL_GIGA_MAC_VER_03:
4196 case RTL_GIGA_MAC_VER_04:
4197 case RTL_GIGA_MAC_VER_05:
4198 case RTL_GIGA_MAC_VER_06:
4199 case RTL_GIGA_MAC_VER_10:
4200 case RTL_GIGA_MAC_VER_11:
4201 case RTL_GIGA_MAC_VER_12:
4202 case RTL_GIGA_MAC_VER_13:
4203 case RTL_GIGA_MAC_VER_14:
4204 case RTL_GIGA_MAC_VER_15:
4205 case RTL_GIGA_MAC_VER_16:
4206 case RTL_GIGA_MAC_VER_17:
4207 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
4208 break;
4209 case RTL_GIGA_MAC_VER_18:
4210 case RTL_GIGA_MAC_VER_19:
4211 case RTL_GIGA_MAC_VER_20:
4212 case RTL_GIGA_MAC_VER_21:
4213 case RTL_GIGA_MAC_VER_22:
4214 case RTL_GIGA_MAC_VER_23:
4215 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00004216 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02004217 case RTL_GIGA_MAC_VER_35:
Hayes Wange542a222011-07-06 15:58:04 +08004218 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
4219 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004220 case RTL_GIGA_MAC_VER_40:
4221 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004222 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004223 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004224 case RTL_GIGA_MAC_VER_44:
hayeswangbeb330a2013-04-01 22:23:39 +00004225 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST | RX_EARLY_OFF);
4226 break;
Hayes Wange542a222011-07-06 15:58:04 +08004227 default:
4228 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
4229 break;
4230 }
4231}
4232
Hayes Wang92fc43b2011-07-06 15:58:03 +08004233static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4234{
Timo Teräs9fba0812013-01-15 21:01:24 +00004235 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004236}
4237
Francois Romieud58d46b2011-05-03 16:38:29 +02004238static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4239{
françois romieu9c5028e2012-03-02 04:43:14 +00004240 void __iomem *ioaddr = tp->mmio_addr;
4241
4242 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004243 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00004244 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004245}
4246
4247static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4248{
françois romieu9c5028e2012-03-02 04:43:14 +00004249 void __iomem *ioaddr = tp->mmio_addr;
4250
4251 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004252 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00004253 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004254}
4255
4256static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4257{
4258 void __iomem *ioaddr = tp->mmio_addr;
4259
4260 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4261 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
4262 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
4263}
4264
4265static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4266{
4267 void __iomem *ioaddr = tp->mmio_addr;
4268
4269 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4270 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
4271 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
4272}
4273
4274static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4275{
4276 void __iomem *ioaddr = tp->mmio_addr;
4277
4278 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4279}
4280
4281static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4282{
4283 void __iomem *ioaddr = tp->mmio_addr;
4284
4285 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4286}
4287
4288static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4289{
4290 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02004291
4292 RTL_W8(MaxTxPacketSize, 0x3f);
4293 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4294 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01004295 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02004296}
4297
4298static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4299{
4300 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02004301
4302 RTL_W8(MaxTxPacketSize, 0x0c);
4303 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4304 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01004305 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02004306}
4307
4308static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4309{
4310 rtl_tx_performance_tweak(tp->pci_dev,
4311 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4312}
4313
4314static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4315{
4316 rtl_tx_performance_tweak(tp->pci_dev,
4317 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4318}
4319
4320static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4321{
4322 void __iomem *ioaddr = tp->mmio_addr;
4323
4324 r8168b_0_hw_jumbo_enable(tp);
4325
4326 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
4327}
4328
4329static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4330{
4331 void __iomem *ioaddr = tp->mmio_addr;
4332
4333 r8168b_0_hw_jumbo_disable(tp);
4334
4335 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4336}
4337
Bill Pembertonbaf63292012-12-03 09:23:28 -05004338static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02004339{
4340 struct jumbo_ops *ops = &tp->jumbo_ops;
4341
4342 switch (tp->mac_version) {
4343 case RTL_GIGA_MAC_VER_11:
4344 ops->disable = r8168b_0_hw_jumbo_disable;
4345 ops->enable = r8168b_0_hw_jumbo_enable;
4346 break;
4347 case RTL_GIGA_MAC_VER_12:
4348 case RTL_GIGA_MAC_VER_17:
4349 ops->disable = r8168b_1_hw_jumbo_disable;
4350 ops->enable = r8168b_1_hw_jumbo_enable;
4351 break;
4352 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4353 case RTL_GIGA_MAC_VER_19:
4354 case RTL_GIGA_MAC_VER_20:
4355 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4356 case RTL_GIGA_MAC_VER_22:
4357 case RTL_GIGA_MAC_VER_23:
4358 case RTL_GIGA_MAC_VER_24:
4359 case RTL_GIGA_MAC_VER_25:
4360 case RTL_GIGA_MAC_VER_26:
4361 ops->disable = r8168c_hw_jumbo_disable;
4362 ops->enable = r8168c_hw_jumbo_enable;
4363 break;
4364 case RTL_GIGA_MAC_VER_27:
4365 case RTL_GIGA_MAC_VER_28:
4366 ops->disable = r8168dp_hw_jumbo_disable;
4367 ops->enable = r8168dp_hw_jumbo_enable;
4368 break;
4369 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4370 case RTL_GIGA_MAC_VER_32:
4371 case RTL_GIGA_MAC_VER_33:
4372 case RTL_GIGA_MAC_VER_34:
4373 ops->disable = r8168e_hw_jumbo_disable;
4374 ops->enable = r8168e_hw_jumbo_enable;
4375 break;
4376
4377 /*
4378 * No action needed for jumbo frames with 8169.
4379 * No jumbo for 810x at all.
4380 */
Hayes Wangc5583862012-07-02 17:23:22 +08004381 case RTL_GIGA_MAC_VER_40:
4382 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004383 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004384 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004385 case RTL_GIGA_MAC_VER_44:
Francois Romieud58d46b2011-05-03 16:38:29 +02004386 default:
4387 ops->disable = NULL;
4388 ops->enable = NULL;
4389 break;
4390 }
4391}
4392
Francois Romieuffc46952012-07-06 14:19:23 +02004393DECLARE_RTL_COND(rtl_chipcmd_cond)
4394{
4395 void __iomem *ioaddr = tp->mmio_addr;
4396
4397 return RTL_R8(ChipCmd) & CmdReset;
4398}
4399
Francois Romieu6f43adc2011-04-29 15:05:51 +02004400static void rtl_hw_reset(struct rtl8169_private *tp)
4401{
4402 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu6f43adc2011-04-29 15:05:51 +02004403
Francois Romieu6f43adc2011-04-29 15:05:51 +02004404 RTL_W8(ChipCmd, CmdReset);
4405
Francois Romieuffc46952012-07-06 14:19:23 +02004406 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004407}
4408
Francois Romieub6ffd972011-06-17 17:00:05 +02004409static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4410{
4411 struct rtl_fw *rtl_fw;
4412 const char *name;
4413 int rc = -ENOMEM;
4414
4415 name = rtl_lookup_firmware_name(tp);
4416 if (!name)
4417 goto out_no_firmware;
4418
4419 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4420 if (!rtl_fw)
4421 goto err_warn;
4422
4423 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4424 if (rc < 0)
4425 goto err_free;
4426
Francois Romieufd112f22011-06-18 00:10:29 +02004427 rc = rtl_check_firmware(tp, rtl_fw);
4428 if (rc < 0)
4429 goto err_release_firmware;
4430
Francois Romieub6ffd972011-06-17 17:00:05 +02004431 tp->rtl_fw = rtl_fw;
4432out:
4433 return;
4434
Francois Romieufd112f22011-06-18 00:10:29 +02004435err_release_firmware:
4436 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004437err_free:
4438 kfree(rtl_fw);
4439err_warn:
4440 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4441 name, rc);
4442out_no_firmware:
4443 tp->rtl_fw = NULL;
4444 goto out;
4445}
4446
François Romieu953a12c2011-04-24 17:38:48 +02004447static void rtl_request_firmware(struct rtl8169_private *tp)
4448{
Francois Romieub6ffd972011-06-17 17:00:05 +02004449 if (IS_ERR(tp->rtl_fw))
4450 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004451}
4452
Hayes Wang92fc43b2011-07-06 15:58:03 +08004453static void rtl_rx_close(struct rtl8169_private *tp)
4454{
4455 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004456
Francois Romieu1687b562011-07-19 17:21:29 +02004457 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004458}
4459
Francois Romieuffc46952012-07-06 14:19:23 +02004460DECLARE_RTL_COND(rtl_npq_cond)
4461{
4462 void __iomem *ioaddr = tp->mmio_addr;
4463
4464 return RTL_R8(TxPoll) & NPQ;
4465}
4466
4467DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4468{
4469 void __iomem *ioaddr = tp->mmio_addr;
4470
4471 return RTL_R32(TxConfig) & TXCFG_EMPTY;
4472}
4473
françois romieue6de30d2011-01-03 15:08:37 +00004474static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475{
françois romieue6de30d2011-01-03 15:08:37 +00004476 void __iomem *ioaddr = tp->mmio_addr;
4477
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004479 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480
Hayes Wang92fc43b2011-07-06 15:58:03 +08004481 rtl_rx_close(tp);
4482
Hayes Wang5d2e1952011-02-22 17:26:22 +08004483 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004484 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4485 tp->mac_version == RTL_GIGA_MAC_VER_31) {
Francois Romieuffc46952012-07-06 14:19:23 +02004486 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Hayes Wangc2218922011-09-06 16:55:18 +08004487 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4488 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
Hayes Wang7e18dca2012-03-30 14:33:02 +08004489 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004490 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
Hayes Wangc5583862012-07-02 17:23:22 +08004491 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
4492 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
hayeswang57538c42013-04-01 22:23:40 +00004493 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
hayeswang58152cd2013-04-01 22:23:42 +00004494 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
hayeswang45dd95c2013-07-08 17:09:01 +08004495 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004496 tp->mac_version == RTL_GIGA_MAC_VER_38) {
David S. Miller8decf862011-09-22 03:23:13 -04004497 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02004498 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004499 } else {
4500 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4501 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004502 }
4503
Hayes Wang92fc43b2011-07-06 15:58:03 +08004504 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505}
4506
Francois Romieu7f796d832007-06-11 23:04:41 +02004507static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004508{
4509 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004510
4511 /* Set DMA burst size and Interframe Gap Time */
4512 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4513 (InterFrameGap << TxInterFrameGapShift));
4514}
4515
Francois Romieu07ce4062007-02-23 23:36:39 +01004516static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517{
4518 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519
Francois Romieu07ce4062007-02-23 23:36:39 +01004520 tp->hw_start(dev);
4521
Francois Romieuda78dbf2012-01-26 14:18:23 +01004522 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004523}
4524
Francois Romieu7f796d832007-06-11 23:04:41 +02004525static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4526 void __iomem *ioaddr)
4527{
4528 /*
4529 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4530 * register to be written before TxDescAddrLow to work.
4531 * Switching from MMIO to I/O access fixes the issue as well.
4532 */
4533 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004534 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004535 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004536 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004537}
4538
4539static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4540{
4541 u16 cmd;
4542
4543 cmd = RTL_R16(CPlusCmd);
4544 RTL_W16(CPlusCmd, cmd);
4545 return cmd;
4546}
4547
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004548static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004549{
4550 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004551 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004552}
4553
Francois Romieu6dccd162007-02-13 23:38:05 +01004554static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4555{
Francois Romieu37441002011-06-17 22:58:54 +02004556 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004557 u32 mac_version;
4558 u32 clk;
4559 u32 val;
4560 } cfg2_info [] = {
4561 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4562 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4563 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4564 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004565 };
4566 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004567 unsigned int i;
4568 u32 clk;
4569
4570 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004571 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004572 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4573 RTL_W32(0x7c, p->val);
4574 break;
4575 }
4576 }
4577}
4578
Francois Romieue6b763e2012-03-08 09:35:39 +01004579static void rtl_set_rx_mode(struct net_device *dev)
4580{
4581 struct rtl8169_private *tp = netdev_priv(dev);
4582 void __iomem *ioaddr = tp->mmio_addr;
4583 u32 mc_filter[2]; /* Multicast hash filter */
4584 int rx_mode;
4585 u32 tmp = 0;
4586
4587 if (dev->flags & IFF_PROMISC) {
4588 /* Unconditionally log net taps. */
4589 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4590 rx_mode =
4591 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4592 AcceptAllPhys;
4593 mc_filter[1] = mc_filter[0] = 0xffffffff;
4594 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4595 (dev->flags & IFF_ALLMULTI)) {
4596 /* Too many to filter perfectly -- accept all multicasts. */
4597 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4598 mc_filter[1] = mc_filter[0] = 0xffffffff;
4599 } else {
4600 struct netdev_hw_addr *ha;
4601
4602 rx_mode = AcceptBroadcast | AcceptMyPhys;
4603 mc_filter[1] = mc_filter[0] = 0;
4604 netdev_for_each_mc_addr(ha, dev) {
4605 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4606 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4607 rx_mode |= AcceptMulticast;
4608 }
4609 }
4610
4611 if (dev->features & NETIF_F_RXALL)
4612 rx_mode |= (AcceptErr | AcceptRunt);
4613
4614 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4615
4616 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4617 u32 data = mc_filter[0];
4618
4619 mc_filter[0] = swab32(mc_filter[1]);
4620 mc_filter[1] = swab32(data);
4621 }
4622
Nathan Walp04817762012-11-01 12:08:47 +00004623 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
4624 mc_filter[1] = mc_filter[0] = 0xffffffff;
4625
Francois Romieue6b763e2012-03-08 09:35:39 +01004626 RTL_W32(MAR0 + 4, mc_filter[1]);
4627 RTL_W32(MAR0 + 0, mc_filter[0]);
4628
4629 RTL_W32(RxConfig, tmp);
4630}
4631
Francois Romieu07ce4062007-02-23 23:36:39 +01004632static void rtl_hw_start_8169(struct net_device *dev)
4633{
4634 struct rtl8169_private *tp = netdev_priv(dev);
4635 void __iomem *ioaddr = tp->mmio_addr;
4636 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004637
Francois Romieu9cb427b2006-11-02 00:10:16 +01004638 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4639 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4640 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4641 }
4642
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004644 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4645 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4646 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4647 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004648 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4649
Hayes Wange542a222011-07-06 15:58:04 +08004650 rtl_init_rxcfg(tp);
4651
françois romieuf0298f82011-01-03 15:07:42 +00004652 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004654 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655
Francois Romieucecb5fd2011-04-01 10:21:07 +02004656 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4657 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4658 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4659 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004660 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
Francois Romieu7f796d832007-06-11 23:04:41 +02004662 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004663
Francois Romieucecb5fd2011-04-01 10:21:07 +02004664 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4665 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004666 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004668 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 }
4670
Francois Romieubcf0bf92006-07-26 23:14:13 +02004671 RTL_W16(CPlusCmd, tp->cp_cmd);
4672
Francois Romieu6dccd162007-02-13 23:38:05 +01004673 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4674
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 /*
4676 * Undocumented corner. Supposedly:
4677 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4678 */
4679 RTL_W16(IntrMitigate, 0x0000);
4680
Francois Romieu7f796d832007-06-11 23:04:41 +02004681 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004682
Francois Romieucecb5fd2011-04-01 10:21:07 +02004683 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4684 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4685 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4686 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004687 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4688 rtl_set_rx_tx_config_registers(tp);
4689 }
4690
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004692
4693 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4694 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695
4696 RTL_W32(RxMissed, 0);
4697
Francois Romieu07ce4062007-02-23 23:36:39 +01004698 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699
4700 /* no early-rx interrupts */
4701 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004702}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004704static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
4705{
4706 if (tp->csi_ops.write)
Francois Romieu52989f02012-07-06 13:37:00 +02004707 tp->csi_ops.write(tp, addr, value);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004708}
4709
4710static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
4711{
Francois Romieu52989f02012-07-06 13:37:00 +02004712 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004713}
4714
4715static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004716{
4717 u32 csi;
4718
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004719 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4720 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00004721}
4722
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004723static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004724{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004725 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00004726}
4727
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004728static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00004729{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004730 rtl_csi_access_enable(tp, 0x27000000);
4731}
4732
Francois Romieuffc46952012-07-06 14:19:23 +02004733DECLARE_RTL_COND(rtl_csiar_cond)
4734{
4735 void __iomem *ioaddr = tp->mmio_addr;
4736
4737 return RTL_R32(CSIAR) & CSIAR_FLAG;
4738}
4739
Francois Romieu52989f02012-07-06 13:37:00 +02004740static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004741{
Francois Romieu52989f02012-07-06 13:37:00 +02004742 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004743
4744 RTL_W32(CSIDR, value);
4745 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4746 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4747
Francois Romieuffc46952012-07-06 14:19:23 +02004748 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004749}
4750
Francois Romieu52989f02012-07-06 13:37:00 +02004751static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004752{
Francois Romieu52989f02012-07-06 13:37:00 +02004753 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004754
4755 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
4756 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4757
Francois Romieuffc46952012-07-06 14:19:23 +02004758 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4759 RTL_R32(CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004760}
4761
Francois Romieu52989f02012-07-06 13:37:00 +02004762static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wang7e18dca2012-03-30 14:33:02 +08004763{
Francois Romieu52989f02012-07-06 13:37:00 +02004764 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004765
4766 RTL_W32(CSIDR, value);
4767 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4768 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
4769 CSIAR_FUNC_NIC);
4770
Francois Romieuffc46952012-07-06 14:19:23 +02004771 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004772}
4773
Francois Romieu52989f02012-07-06 13:37:00 +02004774static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wang7e18dca2012-03-30 14:33:02 +08004775{
Francois Romieu52989f02012-07-06 13:37:00 +02004776 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004777
4778 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
4779 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4780
Francois Romieuffc46952012-07-06 14:19:23 +02004781 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4782 RTL_R32(CSIDR) : ~0;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004783}
4784
hayeswang45dd95c2013-07-08 17:09:01 +08004785static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
4786{
4787 void __iomem *ioaddr = tp->mmio_addr;
4788
4789 RTL_W32(CSIDR, value);
4790 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4791 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
4792 CSIAR_FUNC_NIC2);
4793
4794 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
4795}
4796
4797static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
4798{
4799 void __iomem *ioaddr = tp->mmio_addr;
4800
4801 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
4802 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4803
4804 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4805 RTL_R32(CSIDR) : ~0;
4806}
4807
Bill Pembertonbaf63292012-12-03 09:23:28 -05004808static void rtl_init_csi_ops(struct rtl8169_private *tp)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004809{
4810 struct csi_ops *ops = &tp->csi_ops;
4811
4812 switch (tp->mac_version) {
4813 case RTL_GIGA_MAC_VER_01:
4814 case RTL_GIGA_MAC_VER_02:
4815 case RTL_GIGA_MAC_VER_03:
4816 case RTL_GIGA_MAC_VER_04:
4817 case RTL_GIGA_MAC_VER_05:
4818 case RTL_GIGA_MAC_VER_06:
4819 case RTL_GIGA_MAC_VER_10:
4820 case RTL_GIGA_MAC_VER_11:
4821 case RTL_GIGA_MAC_VER_12:
4822 case RTL_GIGA_MAC_VER_13:
4823 case RTL_GIGA_MAC_VER_14:
4824 case RTL_GIGA_MAC_VER_15:
4825 case RTL_GIGA_MAC_VER_16:
4826 case RTL_GIGA_MAC_VER_17:
4827 ops->write = NULL;
4828 ops->read = NULL;
4829 break;
4830
Hayes Wang7e18dca2012-03-30 14:33:02 +08004831 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004832 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004833 ops->write = r8402_csi_write;
4834 ops->read = r8402_csi_read;
4835 break;
4836
hayeswang45dd95c2013-07-08 17:09:01 +08004837 case RTL_GIGA_MAC_VER_44:
4838 ops->write = r8411_csi_write;
4839 ops->read = r8411_csi_read;
4840 break;
4841
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004842 default:
4843 ops->write = r8169_csi_write;
4844 ops->read = r8169_csi_read;
4845 break;
4846 }
Francois Romieudacf8152008-08-02 20:44:13 +02004847}
4848
4849struct ephy_info {
4850 unsigned int offset;
4851 u16 mask;
4852 u16 bits;
4853};
4854
Francois Romieufdf6fc02012-07-06 22:40:38 +02004855static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
4856 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004857{
4858 u16 w;
4859
4860 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02004861 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4862 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02004863 e++;
4864 }
4865}
4866
Francois Romieub726e492008-06-28 12:22:59 +02004867static void rtl_disable_clock_request(struct pci_dev *pdev)
4868{
Jiang Liu7d7903b2012-07-24 17:20:16 +08004869 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
4870 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02004871}
4872
françois romieue6de30d2011-01-03 15:08:37 +00004873static void rtl_enable_clock_request(struct pci_dev *pdev)
4874{
Jiang Liu7d7903b2012-07-24 17:20:16 +08004875 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
4876 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00004877}
4878
Francois Romieub726e492008-06-28 12:22:59 +02004879#define R8168_CPCMD_QUIRK_MASK (\
4880 EnableBist | \
4881 Mac_dbgo_oe | \
4882 Force_half_dup | \
4883 Force_rxflow_en | \
4884 Force_txflow_en | \
4885 Cxpl_dbg_sel | \
4886 ASF | \
4887 PktCntrDisable | \
4888 Mac_dbgo_sel)
4889
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004890static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004891{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004892 void __iomem *ioaddr = tp->mmio_addr;
4893 struct pci_dev *pdev = tp->pci_dev;
4894
Francois Romieub726e492008-06-28 12:22:59 +02004895 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4896
4897 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4898
françois romieufaf1e782013-02-27 13:01:57 +00004899 if (tp->dev->mtu <= ETH_DATA_LEN) {
4900 rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
4901 PCI_EXP_DEVCTL_NOSNOOP_EN);
4902 }
Francois Romieu219a1e92008-06-28 11:58:39 +02004903}
4904
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004905static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004906{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004907 void __iomem *ioaddr = tp->mmio_addr;
4908
4909 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004910
françois romieuf0298f82011-01-03 15:07:42 +00004911 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004912
4913 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004914}
4915
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004916static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004917{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004918 void __iomem *ioaddr = tp->mmio_addr;
4919 struct pci_dev *pdev = tp->pci_dev;
4920
Francois Romieub726e492008-06-28 12:22:59 +02004921 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4922
4923 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4924
françois romieufaf1e782013-02-27 13:01:57 +00004925 if (tp->dev->mtu <= ETH_DATA_LEN)
4926 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004927
4928 rtl_disable_clock_request(pdev);
4929
4930 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004931}
4932
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004933static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004934{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004935 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004936 { 0x01, 0, 0x0001 },
4937 { 0x02, 0x0800, 0x1000 },
4938 { 0x03, 0, 0x0042 },
4939 { 0x06, 0x0080, 0x0000 },
4940 { 0x07, 0, 0x2000 }
4941 };
4942
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004943 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004944
Francois Romieufdf6fc02012-07-06 22:40:38 +02004945 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02004946
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004947 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004948}
4949
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004950static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02004951{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004952 void __iomem *ioaddr = tp->mmio_addr;
4953 struct pci_dev *pdev = tp->pci_dev;
4954
4955 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004956
4957 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4958
françois romieufaf1e782013-02-27 13:01:57 +00004959 if (tp->dev->mtu <= ETH_DATA_LEN)
4960 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieuef3386f2008-06-29 12:24:30 +02004961
4962 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4963}
4964
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004965static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004966{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004967 void __iomem *ioaddr = tp->mmio_addr;
4968 struct pci_dev *pdev = tp->pci_dev;
4969
4970 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004971
4972 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4973
4974 /* Magic. */
4975 RTL_W8(DBG_REG, 0x20);
4976
françois romieuf0298f82011-01-03 15:07:42 +00004977 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004978
françois romieufaf1e782013-02-27 13:01:57 +00004979 if (tp->dev->mtu <= ETH_DATA_LEN)
4980 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004981
4982 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4983}
4984
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004985static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004986{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004987 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004988 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004989 { 0x02, 0x0800, 0x1000 },
4990 { 0x03, 0, 0x0002 },
4991 { 0x06, 0x0080, 0x0000 }
4992 };
4993
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004994 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004995
4996 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4997
Francois Romieufdf6fc02012-07-06 22:40:38 +02004998 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02004999
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005000 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005001}
5002
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005003static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005004{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005005 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005006 { 0x01, 0, 0x0001 },
5007 { 0x03, 0x0400, 0x0220 }
5008 };
5009
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005010 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005011
Francois Romieufdf6fc02012-07-06 22:40:38 +02005012 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02005013
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005014 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005015}
5016
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005017static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02005018{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005019 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02005020}
5021
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005022static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02005023{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005024 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005025
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005026 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005027}
5028
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005029static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02005030{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005031 void __iomem *ioaddr = tp->mmio_addr;
5032 struct pci_dev *pdev = tp->pci_dev;
5033
5034 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005035
5036 rtl_disable_clock_request(pdev);
5037
françois romieuf0298f82011-01-03 15:07:42 +00005038 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02005039
françois romieufaf1e782013-02-27 13:01:57 +00005040 if (tp->dev->mtu <= ETH_DATA_LEN)
5041 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu5b538df2008-07-20 16:22:45 +02005042
5043 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5044}
5045
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005046static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00005047{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005048 void __iomem *ioaddr = tp->mmio_addr;
5049 struct pci_dev *pdev = tp->pci_dev;
5050
5051 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005052
françois romieufaf1e782013-02-27 13:01:57 +00005053 if (tp->dev->mtu <= ETH_DATA_LEN)
5054 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang4804b3b2011-03-21 01:50:29 +00005055
5056 RTL_W8(MaxTxPacketSize, TxPacketMax);
5057
5058 rtl_disable_clock_request(pdev);
5059}
5060
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005061static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005062{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005063 void __iomem *ioaddr = tp->mmio_addr;
5064 struct pci_dev *pdev = tp->pci_dev;
françois romieue6de30d2011-01-03 15:08:37 +00005065 static const struct ephy_info e_info_8168d_4[] = {
5066 { 0x0b, ~0, 0x48 },
5067 { 0x19, 0x20, 0x50 },
5068 { 0x0c, ~0, 0x20 }
5069 };
5070 int i;
5071
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005072 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005073
5074 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5075
5076 RTL_W8(MaxTxPacketSize, TxPacketMax);
5077
5078 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
5079 const struct ephy_info *e = e_info_8168d_4 + i;
5080 u16 w;
5081
Francois Romieufdf6fc02012-07-06 22:40:38 +02005082 w = rtl_ephy_read(tp, e->offset);
5083 rtl_ephy_write(tp, 0x03, (w & e->mask) | e->bits);
françois romieue6de30d2011-01-03 15:08:37 +00005084 }
5085
5086 rtl_enable_clock_request(pdev);
5087}
5088
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005089static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00005090{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005091 void __iomem *ioaddr = tp->mmio_addr;
5092 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08005093 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00005094 { 0x00, 0x0200, 0x0100 },
5095 { 0x00, 0x0000, 0x0004 },
5096 { 0x06, 0x0002, 0x0001 },
5097 { 0x06, 0x0000, 0x0030 },
5098 { 0x07, 0x0000, 0x2000 },
5099 { 0x00, 0x0000, 0x0020 },
5100 { 0x03, 0x5800, 0x2000 },
5101 { 0x03, 0x0000, 0x0001 },
5102 { 0x01, 0x0800, 0x1000 },
5103 { 0x07, 0x0000, 0x4000 },
5104 { 0x1e, 0x0000, 0x2000 },
5105 { 0x19, 0xffff, 0xfe6c },
5106 { 0x0a, 0x0000, 0x0040 }
5107 };
5108
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005109 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005110
Francois Romieufdf6fc02012-07-06 22:40:38 +02005111 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00005112
françois romieufaf1e782013-02-27 13:01:57 +00005113 if (tp->dev->mtu <= ETH_DATA_LEN)
5114 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang01dc7fe2011-03-21 01:50:28 +00005115
5116 RTL_W8(MaxTxPacketSize, TxPacketMax);
5117
5118 rtl_disable_clock_request(pdev);
5119
5120 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02005121 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
5122 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00005123
Francois Romieucecb5fd2011-04-01 10:21:07 +02005124 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00005125}
5126
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005127static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08005128{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005129 void __iomem *ioaddr = tp->mmio_addr;
5130 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08005131 static const struct ephy_info e_info_8168e_2[] = {
5132 { 0x09, 0x0000, 0x0080 },
5133 { 0x19, 0x0000, 0x0224 }
5134 };
5135
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005136 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005137
Francois Romieufdf6fc02012-07-06 22:40:38 +02005138 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08005139
françois romieufaf1e782013-02-27 13:01:57 +00005140 if (tp->dev->mtu <= ETH_DATA_LEN)
5141 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Hayes Wang70090422011-07-06 15:58:06 +08005142
Francois Romieufdf6fc02012-07-06 22:40:38 +02005143 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5144 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5145 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5146 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5147 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5148 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
5149 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5150 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08005151
Hayes Wang3090bd92011-09-06 16:55:15 +08005152 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005153
Francois Romieu4521e1a92012-11-01 16:46:28 +00005154 rtl_disable_clock_request(pdev);
5155
Hayes Wang70090422011-07-06 15:58:06 +08005156 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5157 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5158
5159 /* Adjust EEE LED frequency */
5160 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5161
5162 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5163 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005164 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wang70090422011-07-06 15:58:06 +08005165}
5166
Hayes Wang5f886e02012-03-30 14:33:03 +08005167static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005168{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005169 void __iomem *ioaddr = tp->mmio_addr;
5170 struct pci_dev *pdev = tp->pci_dev;
Hayes Wangc2218922011-09-06 16:55:18 +08005171
Hayes Wang5f886e02012-03-30 14:33:03 +08005172 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005173
5174 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5175
Francois Romieufdf6fc02012-07-06 22:40:38 +02005176 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5177 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5178 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5179 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5180 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5181 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5182 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5183 rtl_w1w0_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5184 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5185 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08005186
5187 RTL_W8(MaxTxPacketSize, EarlySize);
5188
Francois Romieu4521e1a92012-11-01 16:46:28 +00005189 rtl_disable_clock_request(pdev);
5190
Hayes Wangc2218922011-09-06 16:55:18 +08005191 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5192 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
Hayes Wangc2218922011-09-06 16:55:18 +08005193 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005194 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
5195 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08005196}
5197
Hayes Wang5f886e02012-03-30 14:33:03 +08005198static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5199{
5200 void __iomem *ioaddr = tp->mmio_addr;
5201 static const struct ephy_info e_info_8168f_1[] = {
5202 { 0x06, 0x00c0, 0x0020 },
5203 { 0x08, 0x0001, 0x0002 },
5204 { 0x09, 0x0000, 0x0080 },
5205 { 0x19, 0x0000, 0x0224 }
5206 };
5207
5208 rtl_hw_start_8168f(tp);
5209
Francois Romieufdf6fc02012-07-06 22:40:38 +02005210 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08005211
Francois Romieufdf6fc02012-07-06 22:40:38 +02005212 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08005213
5214 /* Adjust EEE LED frequency */
5215 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5216}
5217
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005218static void rtl_hw_start_8411(struct rtl8169_private *tp)
5219{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005220 static const struct ephy_info e_info_8168f_1[] = {
5221 { 0x06, 0x00c0, 0x0020 },
5222 { 0x0f, 0xffff, 0x5200 },
5223 { 0x1e, 0x0000, 0x4000 },
5224 { 0x19, 0x0000, 0x0224 }
5225 };
5226
5227 rtl_hw_start_8168f(tp);
5228
Francois Romieufdf6fc02012-07-06 22:40:38 +02005229 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005230
Francois Romieufdf6fc02012-07-06 22:40:38 +02005231 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005232}
5233
Hayes Wangc5583862012-07-02 17:23:22 +08005234static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5235{
5236 void __iomem *ioaddr = tp->mmio_addr;
5237 struct pci_dev *pdev = tp->pci_dev;
5238
hayeswangbeb330a2013-04-01 22:23:39 +00005239 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5240
Hayes Wangc5583862012-07-02 17:23:22 +08005241 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5242 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5243 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5244 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5245
5246 rtl_csi_access_enable_1(tp);
5247
5248 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5249
5250 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5251 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00005252 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08005253
5254 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005255 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08005256 RTL_W8(MaxTxPacketSize, EarlySize);
5257
5258 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5259 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5260
5261 /* Adjust EEE LED frequency */
5262 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5263
hayeswangbeb330a2013-04-01 22:23:39 +00005264 rtl_w1w0_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5265 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08005266}
5267
hayeswang57538c42013-04-01 22:23:40 +00005268static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5269{
5270 void __iomem *ioaddr = tp->mmio_addr;
5271 static const struct ephy_info e_info_8168g_2[] = {
5272 { 0x00, 0x0000, 0x0008 },
5273 { 0x0c, 0x3df0, 0x0200 },
5274 { 0x19, 0xffff, 0xfc00 },
5275 { 0x1e, 0xffff, 0x20eb }
5276 };
5277
5278 rtl_hw_start_8168g_1(tp);
5279
5280 /* disable aspm and clock request before access ephy */
5281 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
5282 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
5283 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
5284}
5285
hayeswang45dd95c2013-07-08 17:09:01 +08005286static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5287{
5288 void __iomem *ioaddr = tp->mmio_addr;
5289 static const struct ephy_info e_info_8411_2[] = {
5290 { 0x00, 0x0000, 0x0008 },
5291 { 0x0c, 0x3df0, 0x0200 },
5292 { 0x0f, 0xffff, 0x5200 },
5293 { 0x19, 0x0020, 0x0000 },
5294 { 0x1e, 0x0000, 0x2000 }
5295 };
5296
5297 rtl_hw_start_8168g_1(tp);
5298
5299 /* disable aspm and clock request before access ephy */
5300 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
5301 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
5302 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
5303}
5304
Francois Romieu07ce4062007-02-23 23:36:39 +01005305static void rtl_hw_start_8168(struct net_device *dev)
5306{
Francois Romieu2dd99532007-06-11 23:22:52 +02005307 struct rtl8169_private *tp = netdev_priv(dev);
5308 void __iomem *ioaddr = tp->mmio_addr;
5309
5310 RTL_W8(Cfg9346, Cfg9346_Unlock);
5311
françois romieuf0298f82011-01-03 15:07:42 +00005312 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02005313
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005314 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02005315
Francois Romieu0e485152007-02-20 00:00:26 +01005316 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02005317
5318 RTL_W16(CPlusCmd, tp->cp_cmd);
5319
Francois Romieu0e485152007-02-20 00:00:26 +01005320 RTL_W16(IntrMitigate, 0x5151);
5321
5322 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00005323 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005324 tp->event_slow |= RxFIFOOver | PCSTimeout;
5325 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01005326 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005327
5328 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5329
hayeswang1a964642013-04-01 22:23:41 +00005330 rtl_set_rx_tx_config_registers(tp);
Francois Romieu2dd99532007-06-11 23:22:52 +02005331
5332 RTL_R8(IntrMask);
5333
Francois Romieu219a1e92008-06-28 11:58:39 +02005334 switch (tp->mac_version) {
5335 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005336 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005337 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005338
5339 case RTL_GIGA_MAC_VER_12:
5340 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005341 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005342 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005343
5344 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005345 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005346 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005347
5348 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005349 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005350 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005351
5352 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005353 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005354 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005355
Francois Romieu197ff762008-06-28 13:16:02 +02005356 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005357 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005358 break;
Francois Romieu197ff762008-06-28 13:16:02 +02005359
Francois Romieu6fb07052008-06-29 11:54:28 +02005360 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005361 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005362 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02005363
Francois Romieuef3386f2008-06-29 12:24:30 +02005364 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005365 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005366 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02005367
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005368 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005369 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005370 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005371
Francois Romieu5b538df2008-07-20 16:22:45 +02005372 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00005373 case RTL_GIGA_MAC_VER_26:
5374 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005375 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005376 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02005377
françois romieue6de30d2011-01-03 15:08:37 +00005378 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005379 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005380 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02005381
hayeswang4804b3b2011-03-21 01:50:29 +00005382 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005383 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005384 break;
5385
hayeswang01dc7fe2011-03-21 01:50:28 +00005386 case RTL_GIGA_MAC_VER_32:
5387 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005388 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005389 break;
5390 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005391 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005392 break;
françois romieue6de30d2011-01-03 15:08:37 +00005393
Hayes Wangc2218922011-09-06 16:55:18 +08005394 case RTL_GIGA_MAC_VER_35:
5395 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005396 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005397 break;
5398
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005399 case RTL_GIGA_MAC_VER_38:
5400 rtl_hw_start_8411(tp);
5401 break;
5402
Hayes Wangc5583862012-07-02 17:23:22 +08005403 case RTL_GIGA_MAC_VER_40:
5404 case RTL_GIGA_MAC_VER_41:
5405 rtl_hw_start_8168g_1(tp);
5406 break;
hayeswang57538c42013-04-01 22:23:40 +00005407 case RTL_GIGA_MAC_VER_42:
5408 rtl_hw_start_8168g_2(tp);
5409 break;
Hayes Wangc5583862012-07-02 17:23:22 +08005410
hayeswang45dd95c2013-07-08 17:09:01 +08005411 case RTL_GIGA_MAC_VER_44:
5412 rtl_hw_start_8411_2(tp);
5413 break;
5414
Francois Romieu219a1e92008-06-28 11:58:39 +02005415 default:
5416 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
5417 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00005418 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005419 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005420
hayeswang1a964642013-04-01 22:23:41 +00005421 RTL_W8(Cfg9346, Cfg9346_Lock);
5422
Francois Romieu0e485152007-02-20 00:00:26 +01005423 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5424
hayeswang1a964642013-04-01 22:23:41 +00005425 rtl_set_rx_mode(dev);
Francois Romieub8363902008-06-01 12:31:57 +02005426
Francois Romieu2dd99532007-06-11 23:22:52 +02005427 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005428}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429
Francois Romieu2857ffb2008-08-02 21:08:49 +02005430#define R810X_CPCMD_QUIRK_MASK (\
5431 EnableBist | \
5432 Mac_dbgo_oe | \
5433 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00005434 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02005435 Force_txflow_en | \
5436 Cxpl_dbg_sel | \
5437 ASF | \
5438 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005439 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005440
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005441static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005442{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005443 void __iomem *ioaddr = tp->mmio_addr;
5444 struct pci_dev *pdev = tp->pci_dev;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005445 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02005446 { 0x01, 0, 0x6e65 },
5447 { 0x02, 0, 0x091f },
5448 { 0x03, 0, 0xc2f9 },
5449 { 0x06, 0, 0xafb5 },
5450 { 0x07, 0, 0x0e00 },
5451 { 0x19, 0, 0xec80 },
5452 { 0x01, 0, 0x2e65 },
5453 { 0x01, 0, 0x6e65 }
5454 };
5455 u8 cfg1;
5456
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005457 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005458
5459 RTL_W8(DBG_REG, FIX_NAK_1);
5460
5461 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5462
5463 RTL_W8(Config1,
5464 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5465 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5466
5467 cfg1 = RTL_R8(Config1);
5468 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5469 RTL_W8(Config1, cfg1 & ~LEDS0);
5470
Francois Romieufdf6fc02012-07-06 22:40:38 +02005471 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02005472}
5473
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005474static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005475{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005476 void __iomem *ioaddr = tp->mmio_addr;
5477 struct pci_dev *pdev = tp->pci_dev;
5478
5479 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005480
5481 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5482
5483 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5484 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005485}
5486
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005487static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005488{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005489 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005490
Francois Romieufdf6fc02012-07-06 22:40:38 +02005491 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005492}
5493
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005494static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005495{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005496 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005497 static const struct ephy_info e_info_8105e_1[] = {
5498 { 0x07, 0, 0x4000 },
5499 { 0x19, 0, 0x0200 },
5500 { 0x19, 0, 0x0020 },
5501 { 0x1e, 0, 0x2000 },
5502 { 0x03, 0, 0x0001 },
5503 { 0x19, 0, 0x0100 },
5504 { 0x19, 0, 0x0004 },
5505 { 0x0a, 0, 0x0020 }
5506 };
5507
Francois Romieucecb5fd2011-04-01 10:21:07 +02005508 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005509 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5510
Francois Romieucecb5fd2011-04-01 10:21:07 +02005511 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005512 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5513
5514 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08005515 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005516
Francois Romieufdf6fc02012-07-06 22:40:38 +02005517 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
Hayes Wang5a5e4442011-02-22 17:26:21 +08005518}
5519
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005520static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005521{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005522 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005523 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005524}
5525
Hayes Wang7e18dca2012-03-30 14:33:02 +08005526static void rtl_hw_start_8402(struct rtl8169_private *tp)
5527{
5528 void __iomem *ioaddr = tp->mmio_addr;
5529 static const struct ephy_info e_info_8402[] = {
5530 { 0x19, 0xffff, 0xff64 },
5531 { 0x1e, 0, 0x4000 }
5532 };
5533
5534 rtl_csi_access_enable_2(tp);
5535
5536 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5537 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5538
5539 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5540 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5541
Francois Romieufdf6fc02012-07-06 22:40:38 +02005542 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08005543
5544 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5545
Francois Romieufdf6fc02012-07-06 22:40:38 +02005546 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5547 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
5548 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5549 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5550 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5551 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5552 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005553}
5554
Hayes Wang5598bfe2012-07-02 17:23:21 +08005555static void rtl_hw_start_8106(struct rtl8169_private *tp)
5556{
5557 void __iomem *ioaddr = tp->mmio_addr;
5558
5559 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5560 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5561
Francois Romieu4521e1a92012-11-01 16:46:28 +00005562 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005563 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
5564 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
5565}
5566
Francois Romieu07ce4062007-02-23 23:36:39 +01005567static void rtl_hw_start_8101(struct net_device *dev)
5568{
Francois Romieucdf1a602007-06-11 23:29:50 +02005569 struct rtl8169_private *tp = netdev_priv(dev);
5570 void __iomem *ioaddr = tp->mmio_addr;
5571 struct pci_dev *pdev = tp->pci_dev;
5572
Francois Romieuda78dbf2012-01-26 14:18:23 +01005573 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5574 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00005575
Francois Romieucecb5fd2011-04-01 10:21:07 +02005576 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08005577 tp->mac_version == RTL_GIGA_MAC_VER_16)
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06005578 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
5579 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02005580
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005581 RTL_W8(Cfg9346, Cfg9346_Unlock);
5582
hayeswang1a964642013-04-01 22:23:41 +00005583 RTL_W8(MaxTxPacketSize, TxPacketMax);
5584
5585 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
5586
5587 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
5588 RTL_W16(CPlusCmd, tp->cp_cmd);
5589
5590 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5591
5592 rtl_set_rx_tx_config_registers(tp);
5593
Francois Romieu2857ffb2008-08-02 21:08:49 +02005594 switch (tp->mac_version) {
5595 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005596 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005597 break;
5598
5599 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005600 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005601 break;
5602
5603 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005604 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005605 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005606
5607 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005608 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005609 break;
5610 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005611 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005612 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005613
5614 case RTL_GIGA_MAC_VER_37:
5615 rtl_hw_start_8402(tp);
5616 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08005617
5618 case RTL_GIGA_MAC_VER_39:
5619 rtl_hw_start_8106(tp);
5620 break;
hayeswang58152cd2013-04-01 22:23:42 +00005621 case RTL_GIGA_MAC_VER_43:
5622 rtl_hw_start_8168g_2(tp);
5623 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02005624 }
5625
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005626 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02005627
Francois Romieucdf1a602007-06-11 23:29:50 +02005628 RTL_W16(IntrMitigate, 0x0000);
5629
Francois Romieucdf1a602007-06-11 23:29:50 +02005630 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieucdf1a602007-06-11 23:29:50 +02005631
Francois Romieucdf1a602007-06-11 23:29:50 +02005632 rtl_set_rx_mode(dev);
5633
hayeswang1a964642013-04-01 22:23:41 +00005634 RTL_R8(IntrMask);
5635
Francois Romieucdf1a602007-06-11 23:29:50 +02005636 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637}
5638
5639static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5640{
Francois Romieud58d46b2011-05-03 16:38:29 +02005641 struct rtl8169_private *tp = netdev_priv(dev);
5642
5643 if (new_mtu < ETH_ZLEN ||
5644 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 return -EINVAL;
5646
Francois Romieud58d46b2011-05-03 16:38:29 +02005647 if (new_mtu > ETH_DATA_LEN)
5648 rtl_hw_jumbo_enable(tp);
5649 else
5650 rtl_hw_jumbo_disable(tp);
5651
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005653 netdev_update_features(dev);
5654
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005655 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656}
5657
5658static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5659{
Al Viro95e09182007-12-22 18:55:39 +00005660 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5662}
5663
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005664static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5665 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005667 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005668 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005669
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005670 kfree(*data_buff);
5671 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 rtl8169_make_unusable_by_asic(desc);
5673}
5674
5675static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5676{
5677 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5678
5679 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5680}
5681
5682static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5683 u32 rx_buf_sz)
5684{
5685 desc->addr = cpu_to_le64(mapping);
5686 wmb();
5687 rtl8169_mark_to_asic(desc, rx_buf_sz);
5688}
5689
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005690static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005692 return (void *)ALIGN((long)data, 16);
5693}
5694
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005695static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5696 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005697{
5698 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005700 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005701 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005702 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005704 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5705 if (!data)
5706 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005707
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005708 if (rtl8169_align(data) != data) {
5709 kfree(data);
5710 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5711 if (!data)
5712 return NULL;
5713 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005714
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005715 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005716 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005717 if (unlikely(dma_mapping_error(d, mapping))) {
5718 if (net_ratelimit())
5719 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005720 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722
5723 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005724 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005725
5726err_out:
5727 kfree(data);
5728 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729}
5730
5731static void rtl8169_rx_clear(struct rtl8169_private *tp)
5732{
Francois Romieu07d3f512007-02-21 22:40:46 +01005733 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734
5735 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005736 if (tp->Rx_databuff[i]) {
5737 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 tp->RxDescArray + i);
5739 }
5740 }
5741}
5742
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005743static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005745 desc->opts1 |= cpu_to_le32(RingEnd);
5746}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005747
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005748static int rtl8169_rx_fill(struct rtl8169_private *tp)
5749{
5750 unsigned int i;
5751
5752 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005753 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005754
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005755 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005757
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005758 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005759 if (!data) {
5760 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005761 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005762 }
5763 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005766 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5767 return 0;
5768
5769err_out:
5770 rtl8169_rx_clear(tp);
5771 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772}
5773
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774static int rtl8169_init_ring(struct net_device *dev)
5775{
5776 struct rtl8169_private *tp = netdev_priv(dev);
5777
5778 rtl8169_init_ring_indexes(tp);
5779
5780 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005781 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005783 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784}
5785
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005786static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 struct TxDesc *desc)
5788{
5789 unsigned int len = tx_skb->len;
5790
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005791 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5792
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 desc->opts1 = 0x00;
5794 desc->opts2 = 0x00;
5795 desc->addr = 0x00;
5796 tx_skb->len = 0;
5797}
5798
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005799static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5800 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801{
5802 unsigned int i;
5803
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005804 for (i = 0; i < n; i++) {
5805 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 struct ring_info *tx_skb = tp->tx_skb + entry;
5807 unsigned int len = tx_skb->len;
5808
5809 if (len) {
5810 struct sk_buff *skb = tx_skb->skb;
5811
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005812 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 tp->TxDescArray + entry);
5814 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005815 tp->dev->stats.tx_dropped++;
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07005816 dev_kfree_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 tx_skb->skb = NULL;
5818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 }
5820 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005821}
5822
5823static void rtl8169_tx_clear(struct rtl8169_private *tp)
5824{
5825 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 tp->cur_tx = tp->dirty_tx = 0;
5827}
5828
Francois Romieu4422bcd2012-01-26 11:23:32 +01005829static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830{
David Howellsc4028952006-11-22 14:57:56 +00005831 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005832 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833
Francois Romieuda78dbf2012-01-26 14:18:23 +01005834 napi_disable(&tp->napi);
5835 netif_stop_queue(dev);
5836 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837
françois romieuc7c2c392011-12-04 20:30:52 +00005838 rtl8169_hw_reset(tp);
5839
Francois Romieu56de4142011-03-15 17:29:31 +01005840 for (i = 0; i < NUM_RX_DESC; i++)
5841 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5842
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005844 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845
Francois Romieuda78dbf2012-01-26 14:18:23 +01005846 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01005847 rtl_hw_start(dev);
5848 netif_wake_queue(dev);
5849 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850}
5851
5852static void rtl8169_tx_timeout(struct net_device *dev)
5853{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005854 struct rtl8169_private *tp = netdev_priv(dev);
5855
5856 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857}
5858
5859static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005860 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861{
5862 struct skb_shared_info *info = skb_shinfo(skb);
5863 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005864 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005865 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866
5867 entry = tp->cur_tx;
5868 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005869 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 dma_addr_t mapping;
5871 u32 status, len;
5872 void *addr;
5873
5874 entry = (entry + 1) % NUM_TX_DESC;
5875
5876 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005877 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005878 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005879 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005880 if (unlikely(dma_mapping_error(d, mapping))) {
5881 if (net_ratelimit())
5882 netif_err(tp, drv, tp->dev,
5883 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005884 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886
Francois Romieucecb5fd2011-04-01 10:21:07 +02005887 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005888 status = opts[0] | len |
5889 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890
5891 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005892 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 txd->addr = cpu_to_le64(mapping);
5894
5895 tp->tx_skb[entry].len = len;
5896 }
5897
5898 if (cur_frag) {
5899 tp->tx_skb[entry].skb = skb;
5900 txd->opts1 |= cpu_to_le32(LastFrag);
5901 }
5902
5903 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005904
5905err_out:
5906 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5907 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908}
5909
françois romieub423e9a2013-05-18 01:24:46 +00005910static bool rtl_skb_pad(struct sk_buff *skb)
5911{
5912 if (skb_padto(skb, ETH_ZLEN))
5913 return false;
5914 skb_put(skb, ETH_ZLEN - skb->len);
5915 return true;
5916}
5917
5918static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
5919{
5920 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
5921}
5922
hayeswang5888d3f2014-07-11 16:25:56 +08005923static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
5924 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925{
Michał Mirosław350fb322011-04-08 06:35:56 +00005926 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927
Francois Romieu2b7b4312011-04-18 22:53:24 -07005928 if (mss) {
5929 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08005930 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
5931 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5932 const struct iphdr *ip = ip_hdr(skb);
5933
5934 if (ip->protocol == IPPROTO_TCP)
5935 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
5936 else if (ip->protocol == IPPROTO_UDP)
5937 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
5938 else
5939 WARN_ON_ONCE(1);
5940 }
5941
5942 return true;
5943}
5944
5945static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
5946 struct sk_buff *skb, u32 *opts)
5947{
hayeswangbdfa4ed2014-07-11 16:25:57 +08005948 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08005949 u32 mss = skb_shinfo(skb)->gso_size;
5950
5951 if (mss) {
hayeswangbdfa4ed2014-07-11 16:25:57 +08005952 opts[0] |= TD1_GTSENV4;
5953 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08005954 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005955 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005956 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957
françois romieub423e9a2013-05-18 01:24:46 +00005958 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
5959 return skb_checksum_help(skb) == 0 && rtl_skb_pad(skb);
5960
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 if (ip->protocol == IPPROTO_TCP)
hayeswang5888d3f2014-07-11 16:25:56 +08005962 opts[1] |= TD1_IP_CS | TD1_TCP_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963 else if (ip->protocol == IPPROTO_UDP)
hayeswang5888d3f2014-07-11 16:25:56 +08005964 opts[1] |= TD1_IP_CS | TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005965 else
5966 WARN_ON_ONCE(1);
françois romieub423e9a2013-05-18 01:24:46 +00005967 } else {
5968 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
5969 return rtl_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 }
hayeswang5888d3f2014-07-11 16:25:56 +08005971
françois romieub423e9a2013-05-18 01:24:46 +00005972 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973}
5974
Stephen Hemminger613573252009-08-31 19:50:58 +00005975static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5976 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977{
5978 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005979 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 struct TxDesc *txd = tp->TxDescArray + entry;
5981 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005982 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 dma_addr_t mapping;
5984 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005985 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005986 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005987
Julien Ducourthial477206a2012-05-09 00:00:06 +02005988 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005989 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005990 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 }
5992
5993 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005994 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995
françois romieub423e9a2013-05-18 01:24:46 +00005996 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
5997 opts[0] = DescOwn;
5998
hayeswang5888d3f2014-07-11 16:25:56 +08005999 if (!tp->tso_csum(tp, skb, opts))
françois romieub423e9a2013-05-18 01:24:46 +00006000 goto err_update_stats;
6001
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006002 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006003 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006004 if (unlikely(dma_mapping_error(d, mapping))) {
6005 if (net_ratelimit())
6006 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006007 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009
6010 tp->tx_skb[entry].len = len;
6011 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012
Francois Romieu2b7b4312011-04-18 22:53:24 -07006013 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006014 if (frags < 0)
6015 goto err_dma_1;
6016 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07006017 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006018 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07006019 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006020 tp->tx_skb[entry].skb = skb;
6021 }
6022
Francois Romieu2b7b4312011-04-18 22:53:24 -07006023 txd->opts2 = cpu_to_le32(opts[1]);
6024
Richard Cochran5047fb52012-03-10 07:29:42 +00006025 skb_tx_timestamp(skb);
6026
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 wmb();
6028
Francois Romieucecb5fd2011-04-01 10:21:07 +02006029 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006030 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 txd->opts1 = cpu_to_le32(status);
6032
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 tp->cur_tx += frags + 1;
6034
David Dillow4c020a92010-03-03 16:33:10 +00006035 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036
Francois Romieucecb5fd2011-04-01 10:21:07 +02006037 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038
Francois Romieuda78dbf2012-01-26 14:18:23 +01006039 mmiowb();
6040
Julien Ducourthial477206a2012-05-09 00:00:06 +02006041 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01006042 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
6043 * not miss a ring update when it notices a stopped queue.
6044 */
6045 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01006047 /* Sync with rtl_tx:
6048 * - publish queue status and cur_tx ring index (write barrier)
6049 * - refresh dirty_tx ring index (read barrier).
6050 * May the current thread have a pessimistic view of the ring
6051 * status and forget to wake up queue, a racing rtl_tx thread
6052 * can't.
6053 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006054 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02006055 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 netif_wake_queue(dev);
6057 }
6058
Stephen Hemminger613573252009-08-31 19:50:58 +00006059 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006061err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006062 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006063err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006064 dev_kfree_skb_any(skb);
Stefan Badere5195c12013-04-26 13:49:32 +00006065err_update_stats:
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006066 dev->stats.tx_dropped++;
6067 return NETDEV_TX_OK;
6068
6069err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006071 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00006072 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073}
6074
6075static void rtl8169_pcierr_interrupt(struct net_device *dev)
6076{
6077 struct rtl8169_private *tp = netdev_priv(dev);
6078 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 u16 pci_status, pci_cmd;
6080
6081 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
6082 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
6083
Joe Perchesbf82c182010-02-09 11:49:50 +00006084 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
6085 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086
6087 /*
6088 * The recovery sequence below admits a very elaborated explanation:
6089 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01006090 * - I did not see what else could be done;
6091 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 *
6093 * Feel free to adjust to your needs.
6094 */
Francois Romieua27993f2006-12-18 00:04:19 +01006095 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01006096 pci_cmd &= ~PCI_COMMAND_PARITY;
6097 else
6098 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
6099
6100 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101
6102 pci_write_config_word(pdev, PCI_STATUS,
6103 pci_status & (PCI_STATUS_DETECTED_PARITY |
6104 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
6105 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
6106
6107 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00006108 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00006109 void __iomem *ioaddr = tp->mmio_addr;
6110
Joe Perchesbf82c182010-02-09 11:49:50 +00006111 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112 tp->cp_cmd &= ~PCIDAC;
6113 RTL_W16(CPlusCmd, tp->cp_cmd);
6114 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 }
6116
françois romieue6de30d2011-01-03 15:08:37 +00006117 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01006118
Francois Romieu98ddf982012-01-31 10:47:34 +01006119 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120}
6121
Francois Romieuda78dbf2012-01-26 14:18:23 +01006122static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123{
6124 unsigned int dirty_tx, tx_left;
6125
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126 dirty_tx = tp->dirty_tx;
6127 smp_rmb();
6128 tx_left = tp->cur_tx - dirty_tx;
6129
6130 while (tx_left > 0) {
6131 unsigned int entry = dirty_tx % NUM_TX_DESC;
6132 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 u32 status;
6134
6135 rmb();
6136 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
6137 if (status & DescOwn)
6138 break;
6139
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006140 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
6141 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 if (status & LastFrag) {
Francois Romieu17bcb682012-07-23 22:55:55 +02006143 u64_stats_update_begin(&tp->tx_stats.syncp);
6144 tp->tx_stats.packets++;
6145 tp->tx_stats.bytes += tx_skb->skb->len;
6146 u64_stats_update_end(&tp->tx_stats.syncp);
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006147 dev_kfree_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148 tx_skb->skb = NULL;
6149 }
6150 dirty_tx++;
6151 tx_left--;
6152 }
6153
6154 if (tp->dirty_tx != dirty_tx) {
6155 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01006156 /* Sync with rtl8169_start_xmit:
6157 * - publish dirty_tx ring index (write barrier)
6158 * - refresh cur_tx ring index and queue status (read barrier)
6159 * May the current thread miss the stopped queue condition,
6160 * a racing xmit thread can only have a right view of the
6161 * ring status.
6162 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006163 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02006165 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 netif_wake_queue(dev);
6167 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02006168 /*
6169 * 8168 hack: TxPoll requests are lost when the Tx packets are
6170 * too close. Let's kick an extra TxPoll request when a burst
6171 * of start_xmit activity is detected (if it is not detected,
6172 * it is slow enough). -- FR
6173 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006174 if (tp->cur_tx != dirty_tx) {
6175 void __iomem *ioaddr = tp->mmio_addr;
6176
Francois Romieud78ae2d2007-08-26 20:08:19 +02006177 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 }
6180}
6181
Francois Romieu126fa4b2005-05-12 20:09:17 -04006182static inline int rtl8169_fragmented_frame(u32 status)
6183{
6184 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
6185}
6186
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006187static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 u32 status = opts1 & RxProtoMask;
6190
6191 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00006192 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193 skb->ip_summed = CHECKSUM_UNNECESSARY;
6194 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006195 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196}
6197
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006198static struct sk_buff *rtl8169_try_rx_copy(void *data,
6199 struct rtl8169_private *tp,
6200 int pkt_size,
6201 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02006203 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006204 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006206 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006207 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006208 prefetch(data);
6209 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
6210 if (skb)
6211 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006212 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6213
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006214 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215}
6216
Francois Romieuda78dbf2012-01-26 14:18:23 +01006217static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218{
6219 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006220 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223
Timo Teräs9fba0812013-01-15 21:01:24 +00006224 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006226 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 u32 status;
6228
6229 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04006230 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231
6232 if (status & DescOwn)
6233 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02006234 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006235 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6236 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006237 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02006239 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02006241 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02006242 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006243 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006244 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02006245 }
Ben Greear6bbe0212012-02-10 15:04:33 +00006246 if ((status & (RxRUNT | RxCRC)) &&
6247 !(status & (RxRWT | RxFOVF)) &&
6248 (dev->features & NETIF_F_RXALL))
6249 goto process_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006251 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00006252 dma_addr_t addr;
6253 int pkt_size;
6254
6255process_pkt:
6256 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00006257 if (likely(!(dev->features & NETIF_F_RXFCS)))
6258 pkt_size = (status & 0x00003fff) - 4;
6259 else
6260 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261
Francois Romieu126fa4b2005-05-12 20:09:17 -04006262 /*
6263 * The driver does not support incoming fragmented
6264 * frames. They are seen as a symptom of over-mtu
6265 * sized frames.
6266 */
6267 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02006268 dev->stats.rx_dropped++;
6269 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00006270 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006271 }
6272
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006273 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6274 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006275 if (!skb) {
6276 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00006277 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278 }
6279
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006280 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 skb_put(skb, pkt_size);
6282 skb->protocol = eth_type_trans(skb, dev);
6283
Francois Romieu7a8fc772011-03-01 17:18:33 +01006284 rtl8169_rx_vlan_tag(desc, skb);
6285
Francois Romieu56de4142011-03-15 17:29:31 +01006286 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287
Junchang Wang8027aa22012-03-04 23:30:32 +01006288 u64_stats_update_begin(&tp->rx_stats.syncp);
6289 tp->rx_stats.packets++;
6290 tp->rx_stats.bytes += pkt_size;
6291 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292 }
françois romieuce11ff52013-01-24 13:30:06 +00006293release_descriptor:
6294 desc->opts2 = 0;
6295 wmb();
6296 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297 }
6298
6299 count = cur_rx - tp->cur_rx;
6300 tp->cur_rx = cur_rx;
6301
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 return count;
6303}
6304
Francois Romieu07d3f512007-02-21 22:40:46 +01006305static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306{
Francois Romieu07d3f512007-02-21 22:40:46 +01006307 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01006310 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311
Francois Romieu9085cdfa2012-01-26 12:59:08 +01006312 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006313 if (status && status != 0xffff) {
6314 status &= RTL_EVENT_NAPI | tp->event_slow;
6315 if (status) {
6316 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00006317
Francois Romieuda78dbf2012-01-26 14:18:23 +01006318 rtl_irq_disable(tp);
6319 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322 return IRQ_RETVAL(handled);
6323}
6324
Francois Romieuda78dbf2012-01-26 14:18:23 +01006325/*
6326 * Workqueue context.
6327 */
6328static void rtl_slow_event_work(struct rtl8169_private *tp)
6329{
6330 struct net_device *dev = tp->dev;
6331 u16 status;
6332
6333 status = rtl_get_events(tp) & tp->event_slow;
6334 rtl_ack_events(tp, status);
6335
6336 if (unlikely(status & RxFIFOOver)) {
6337 switch (tp->mac_version) {
6338 /* Work around for rx fifo overflow */
6339 case RTL_GIGA_MAC_VER_11:
6340 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01006341 /* XXX - Hack alert. See rtl_task(). */
6342 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006343 default:
6344 break;
6345 }
6346 }
6347
6348 if (unlikely(status & SYSErr))
6349 rtl8169_pcierr_interrupt(dev);
6350
6351 if (status & LinkChg)
6352 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
6353
françois romieu7dbb4912012-06-09 10:53:16 +00006354 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006355}
6356
Francois Romieu4422bcd2012-01-26 11:23:32 +01006357static void rtl_task(struct work_struct *work)
6358{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006359 static const struct {
6360 int bitnr;
6361 void (*action)(struct rtl8169_private *);
6362 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01006363 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006364 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
6365 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
6366 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
6367 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01006368 struct rtl8169_private *tp =
6369 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006370 struct net_device *dev = tp->dev;
6371 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01006372
Francois Romieuda78dbf2012-01-26 14:18:23 +01006373 rtl_lock_work(tp);
6374
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006375 if (!netif_running(dev) ||
6376 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01006377 goto out_unlock;
6378
6379 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
6380 bool pending;
6381
Francois Romieuda78dbf2012-01-26 14:18:23 +01006382 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006383 if (pending)
6384 rtl_work[i].action(tp);
6385 }
6386
6387out_unlock:
6388 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01006389}
6390
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006391static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006393 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
6394 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006395 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
6396 int work_done= 0;
6397 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398
Francois Romieuda78dbf2012-01-26 14:18:23 +01006399 status = rtl_get_events(tp);
6400 rtl_ack_events(tp, status & ~tp->event_slow);
6401
6402 if (status & RTL_EVENT_NAPI_RX)
6403 work_done = rtl_rx(dev, tp, (u32) budget);
6404
6405 if (status & RTL_EVENT_NAPI_TX)
6406 rtl_tx(dev, tp);
6407
6408 if (status & tp->event_slow) {
6409 enable_mask &= ~tp->event_slow;
6410
6411 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
6412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006414 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006415 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00006416
Francois Romieuda78dbf2012-01-26 14:18:23 +01006417 rtl_irq_enable(tp, enable_mask);
6418 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 }
6420
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006421 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423
Francois Romieu523a6092008-09-10 22:28:56 +02006424static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
6425{
6426 struct rtl8169_private *tp = netdev_priv(dev);
6427
6428 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6429 return;
6430
6431 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
6432 RTL_W32(RxMissed, 0);
6433}
6434
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435static void rtl8169_down(struct net_device *dev)
6436{
6437 struct rtl8169_private *tp = netdev_priv(dev);
6438 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439
Francois Romieu4876cc12011-03-11 21:07:11 +01006440 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01006442 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006443 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444
Hayes Wang92fc43b2011-07-06 15:58:03 +08006445 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006446 /*
6447 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01006448 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6449 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006450 */
Francois Romieu523a6092008-09-10 22:28:56 +02006451 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006454 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456 rtl8169_tx_clear(tp);
6457
6458 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00006459
6460 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461}
6462
6463static int rtl8169_close(struct net_device *dev)
6464{
6465 struct rtl8169_private *tp = netdev_priv(dev);
6466 struct pci_dev *pdev = tp->pci_dev;
6467
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006468 pm_runtime_get_sync(&pdev->dev);
6469
Francois Romieucecb5fd2011-04-01 10:21:07 +02006470 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08006471 rtl8169_update_counters(dev);
6472
Francois Romieuda78dbf2012-01-26 14:18:23 +01006473 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006474 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006475
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006477 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478
Lekensteyn4ea72442013-07-22 09:53:30 +02006479 cancel_work_sync(&tp->wk.work);
6480
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006481 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00006483 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6484 tp->RxPhyAddr);
6485 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6486 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487 tp->TxDescArray = NULL;
6488 tp->RxDescArray = NULL;
6489
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006490 pm_runtime_put_sync(&pdev->dev);
6491
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492 return 0;
6493}
6494
Francois Romieudc1c00c2012-03-08 10:06:18 +01006495#ifdef CONFIG_NET_POLL_CONTROLLER
6496static void rtl8169_netpoll(struct net_device *dev)
6497{
6498 struct rtl8169_private *tp = netdev_priv(dev);
6499
6500 rtl8169_interrupt(tp->pci_dev->irq, dev);
6501}
6502#endif
6503
Francois Romieudf43ac72012-03-08 09:48:40 +01006504static int rtl_open(struct net_device *dev)
6505{
6506 struct rtl8169_private *tp = netdev_priv(dev);
6507 void __iomem *ioaddr = tp->mmio_addr;
6508 struct pci_dev *pdev = tp->pci_dev;
6509 int retval = -ENOMEM;
6510
6511 pm_runtime_get_sync(&pdev->dev);
6512
6513 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02006514 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01006515 * dma_alloc_coherent provides more.
6516 */
6517 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6518 &tp->TxPhyAddr, GFP_KERNEL);
6519 if (!tp->TxDescArray)
6520 goto err_pm_runtime_put;
6521
6522 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6523 &tp->RxPhyAddr, GFP_KERNEL);
6524 if (!tp->RxDescArray)
6525 goto err_free_tx_0;
6526
6527 retval = rtl8169_init_ring(dev);
6528 if (retval < 0)
6529 goto err_free_rx_1;
6530
6531 INIT_WORK(&tp->wk.work, rtl_task);
6532
6533 smp_mb();
6534
6535 rtl_request_firmware(tp);
6536
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006537 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01006538 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
6539 dev->name, dev);
6540 if (retval < 0)
6541 goto err_release_fw_2;
6542
6543 rtl_lock_work(tp);
6544
6545 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6546
6547 napi_enable(&tp->napi);
6548
6549 rtl8169_init_phy(dev, tp);
6550
6551 __rtl8169_set_features(dev, dev->features);
6552
6553 rtl_pll_power_up(tp);
6554
6555 rtl_hw_start(dev);
6556
6557 netif_start_queue(dev);
6558
6559 rtl_unlock_work(tp);
6560
6561 tp->saved_wolopts = 0;
6562 pm_runtime_put_noidle(&pdev->dev);
6563
6564 rtl8169_check_link_status(dev, tp, ioaddr);
6565out:
6566 return retval;
6567
6568err_release_fw_2:
6569 rtl_release_firmware(tp);
6570 rtl8169_rx_clear(tp);
6571err_free_rx_1:
6572 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6573 tp->RxPhyAddr);
6574 tp->RxDescArray = NULL;
6575err_free_tx_0:
6576 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6577 tp->TxPhyAddr);
6578 tp->TxDescArray = NULL;
6579err_pm_runtime_put:
6580 pm_runtime_put_noidle(&pdev->dev);
6581 goto out;
6582}
6583
Junchang Wang8027aa22012-03-04 23:30:32 +01006584static struct rtnl_link_stats64 *
6585rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586{
6587 struct rtl8169_private *tp = netdev_priv(dev);
6588 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01006589 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590
Francois Romieuda78dbf2012-01-26 14:18:23 +01006591 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02006592 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02006593
Junchang Wang8027aa22012-03-04 23:30:32 +01006594 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07006595 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01006596 stats->rx_packets = tp->rx_stats.packets;
6597 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07006598 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01006599
6600
6601 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07006602 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01006603 stats->tx_packets = tp->tx_stats.packets;
6604 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07006605 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01006606
6607 stats->rx_dropped = dev->stats.rx_dropped;
6608 stats->tx_dropped = dev->stats.tx_dropped;
6609 stats->rx_length_errors = dev->stats.rx_length_errors;
6610 stats->rx_errors = dev->stats.rx_errors;
6611 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6612 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6613 stats->rx_missed_errors = dev->stats.rx_missed_errors;
6614
6615 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616}
6617
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006618static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006619{
françois romieu065c27c2011-01-03 15:08:12 +00006620 struct rtl8169_private *tp = netdev_priv(dev);
6621
Francois Romieu5d06a992006-02-23 00:47:58 +01006622 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006623 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006624
6625 netif_device_detach(dev);
6626 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006627
6628 rtl_lock_work(tp);
6629 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006630 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006631 rtl_unlock_work(tp);
6632
6633 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006634}
Francois Romieu5d06a992006-02-23 00:47:58 +01006635
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006636#ifdef CONFIG_PM
6637
6638static int rtl8169_suspend(struct device *device)
6639{
6640 struct pci_dev *pdev = to_pci_dev(device);
6641 struct net_device *dev = pci_get_drvdata(pdev);
6642
6643 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02006644
Francois Romieu5d06a992006-02-23 00:47:58 +01006645 return 0;
6646}
6647
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006648static void __rtl8169_resume(struct net_device *dev)
6649{
françois romieu065c27c2011-01-03 15:08:12 +00006650 struct rtl8169_private *tp = netdev_priv(dev);
6651
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006652 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006653
6654 rtl_pll_power_up(tp);
6655
Artem Savkovcff4c162012-04-03 10:29:11 +00006656 rtl_lock_work(tp);
6657 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006658 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00006659 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006660
Francois Romieu98ddf982012-01-31 10:47:34 +01006661 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006662}
6663
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006664static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006665{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006666 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01006667 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006668 struct rtl8169_private *tp = netdev_priv(dev);
6669
6670 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01006671
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006672 if (netif_running(dev))
6673 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006674
Francois Romieu5d06a992006-02-23 00:47:58 +01006675 return 0;
6676}
6677
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006678static int rtl8169_runtime_suspend(struct device *device)
6679{
6680 struct pci_dev *pdev = to_pci_dev(device);
6681 struct net_device *dev = pci_get_drvdata(pdev);
6682 struct rtl8169_private *tp = netdev_priv(dev);
6683
6684 if (!tp->TxDescArray)
6685 return 0;
6686
Francois Romieuda78dbf2012-01-26 14:18:23 +01006687 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006688 tp->saved_wolopts = __rtl8169_get_wol(tp);
6689 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006690 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006691
6692 rtl8169_net_suspend(dev);
6693
6694 return 0;
6695}
6696
6697static int rtl8169_runtime_resume(struct device *device)
6698{
6699 struct pci_dev *pdev = to_pci_dev(device);
6700 struct net_device *dev = pci_get_drvdata(pdev);
6701 struct rtl8169_private *tp = netdev_priv(dev);
6702
6703 if (!tp->TxDescArray)
6704 return 0;
6705
Francois Romieuda78dbf2012-01-26 14:18:23 +01006706 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006707 __rtl8169_set_wol(tp, tp->saved_wolopts);
6708 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006709 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006710
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006711 rtl8169_init_phy(dev, tp);
6712
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006713 __rtl8169_resume(dev);
6714
6715 return 0;
6716}
6717
6718static int rtl8169_runtime_idle(struct device *device)
6719{
6720 struct pci_dev *pdev = to_pci_dev(device);
6721 struct net_device *dev = pci_get_drvdata(pdev);
6722 struct rtl8169_private *tp = netdev_priv(dev);
6723
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00006724 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006725}
6726
Alexey Dobriyan47145212009-12-14 18:00:08 -08006727static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006728 .suspend = rtl8169_suspend,
6729 .resume = rtl8169_resume,
6730 .freeze = rtl8169_suspend,
6731 .thaw = rtl8169_resume,
6732 .poweroff = rtl8169_suspend,
6733 .restore = rtl8169_resume,
6734 .runtime_suspend = rtl8169_runtime_suspend,
6735 .runtime_resume = rtl8169_runtime_resume,
6736 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006737};
6738
6739#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6740
6741#else /* !CONFIG_PM */
6742
6743#define RTL8169_PM_OPS NULL
6744
6745#endif /* !CONFIG_PM */
6746
David S. Miller1805b2f2011-10-24 18:18:09 -04006747static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6748{
6749 void __iomem *ioaddr = tp->mmio_addr;
6750
6751 /* WoL fails with 8168b when the receiver is disabled. */
6752 switch (tp->mac_version) {
6753 case RTL_GIGA_MAC_VER_11:
6754 case RTL_GIGA_MAC_VER_12:
6755 case RTL_GIGA_MAC_VER_17:
6756 pci_clear_master(tp->pci_dev);
6757
6758 RTL_W8(ChipCmd, CmdRxEnb);
6759 /* PCI commit */
6760 RTL_R8(ChipCmd);
6761 break;
6762 default:
6763 break;
6764 }
6765}
6766
Francois Romieu1765f952008-09-13 17:21:40 +02006767static void rtl_shutdown(struct pci_dev *pdev)
6768{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006769 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006770 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00006771 struct device *d = &pdev->dev;
6772
6773 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02006774
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006775 rtl8169_net_suspend(dev);
6776
Francois Romieucecb5fd2011-04-01 10:21:07 +02006777 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006778 rtl_rar_set(tp, dev->perm_addr);
6779
Hayes Wang92fc43b2011-07-06 15:58:03 +08006780 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006781
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006782 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006783 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6784 rtl_wol_suspend_quirk(tp);
6785 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006786 }
6787
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006788 pci_wake_from_d3(pdev, true);
6789 pci_set_power_state(pdev, PCI_D3hot);
6790 }
françois romieu2a15cd22012-03-06 01:14:12 +00006791
6792 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006793}
Francois Romieu5d06a992006-02-23 00:47:58 +01006794
Bill Pembertonbaf63292012-12-03 09:23:28 -05006795static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01006796{
6797 struct net_device *dev = pci_get_drvdata(pdev);
6798 struct rtl8169_private *tp = netdev_priv(dev);
6799
6800 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6801 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6802 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6803 rtl8168_driver_stop(tp);
6804 }
6805
Devendra Nagaad1be8d2012-05-31 01:51:20 +00006806 netif_napi_del(&tp->napi);
6807
Francois Romieue27566e2012-03-08 09:54:01 +01006808 unregister_netdev(dev);
6809
6810 rtl_release_firmware(tp);
6811
6812 if (pci_dev_run_wake(pdev))
6813 pm_runtime_get_noresume(&pdev->dev);
6814
6815 /* restore original MAC address */
6816 rtl_rar_set(tp, dev->perm_addr);
6817
6818 rtl_disable_msi(pdev, tp);
6819 rtl8169_release_board(pdev, dev, tp->mmio_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01006820}
6821
Francois Romieufa9c3852012-03-08 10:01:50 +01006822static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01006823 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01006824 .ndo_stop = rtl8169_close,
6825 .ndo_get_stats64 = rtl8169_get_stats64,
6826 .ndo_start_xmit = rtl8169_start_xmit,
6827 .ndo_tx_timeout = rtl8169_tx_timeout,
6828 .ndo_validate_addr = eth_validate_addr,
6829 .ndo_change_mtu = rtl8169_change_mtu,
6830 .ndo_fix_features = rtl8169_fix_features,
6831 .ndo_set_features = rtl8169_set_features,
6832 .ndo_set_mac_address = rtl_set_mac_address,
6833 .ndo_do_ioctl = rtl8169_ioctl,
6834 .ndo_set_rx_mode = rtl_set_rx_mode,
6835#ifdef CONFIG_NET_POLL_CONTROLLER
6836 .ndo_poll_controller = rtl8169_netpoll,
6837#endif
6838
6839};
6840
Francois Romieu31fa8b12012-03-08 10:09:40 +01006841static const struct rtl_cfg_info {
6842 void (*hw_start)(struct net_device *);
6843 unsigned int region;
6844 unsigned int align;
6845 u16 event_slow;
6846 unsigned features;
6847 u8 default_ver;
6848} rtl_cfg_infos [] = {
6849 [RTL_CFG_0] = {
6850 .hw_start = rtl_hw_start_8169,
6851 .region = 1,
6852 .align = 0,
6853 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
6854 .features = RTL_FEATURE_GMII,
6855 .default_ver = RTL_GIGA_MAC_VER_01,
6856 },
6857 [RTL_CFG_1] = {
6858 .hw_start = rtl_hw_start_8168,
6859 .region = 2,
6860 .align = 8,
6861 .event_slow = SYSErr | LinkChg | RxOverflow,
6862 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
6863 .default_ver = RTL_GIGA_MAC_VER_11,
6864 },
6865 [RTL_CFG_2] = {
6866 .hw_start = rtl_hw_start_8101,
6867 .region = 2,
6868 .align = 8,
6869 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
6870 PCSTimeout,
6871 .features = RTL_FEATURE_MSI,
6872 .default_ver = RTL_GIGA_MAC_VER_13,
6873 }
6874};
6875
6876/* Cfg9346_Unlock assumed. */
6877static unsigned rtl_try_msi(struct rtl8169_private *tp,
6878 const struct rtl_cfg_info *cfg)
6879{
6880 void __iomem *ioaddr = tp->mmio_addr;
6881 unsigned msi = 0;
6882 u8 cfg2;
6883
6884 cfg2 = RTL_R8(Config2) & ~MSIEnable;
6885 if (cfg->features & RTL_FEATURE_MSI) {
6886 if (pci_enable_msi(tp->pci_dev)) {
6887 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
6888 } else {
6889 cfg2 |= MSIEnable;
6890 msi = RTL_FEATURE_MSI;
6891 }
6892 }
6893 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6894 RTL_W8(Config2, cfg2);
6895 return msi;
6896}
6897
Hayes Wangc5583862012-07-02 17:23:22 +08006898DECLARE_RTL_COND(rtl_link_list_ready_cond)
6899{
6900 void __iomem *ioaddr = tp->mmio_addr;
6901
6902 return RTL_R8(MCU) & LINK_LIST_RDY;
6903}
6904
6905DECLARE_RTL_COND(rtl_rxtx_empty_cond)
6906{
6907 void __iomem *ioaddr = tp->mmio_addr;
6908
6909 return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
6910}
6911
Bill Pembertonbaf63292012-12-03 09:23:28 -05006912static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006913{
6914 void __iomem *ioaddr = tp->mmio_addr;
6915 u32 data;
6916
6917 tp->ocp_base = OCP_STD_PHY_BASE;
6918
6919 RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
6920
6921 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
6922 return;
6923
6924 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
6925 return;
6926
6927 RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
6928 msleep(1);
6929 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6930
Hayes Wang5f8bcce2012-07-10 08:47:05 +02006931 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08006932 data &= ~(1 << 14);
6933 r8168_mac_ocp_write(tp, 0xe8de, data);
6934
6935 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
6936 return;
6937
Hayes Wang5f8bcce2012-07-10 08:47:05 +02006938 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08006939 data |= (1 << 15);
6940 r8168_mac_ocp_write(tp, 0xe8de, data);
6941
6942 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
6943 return;
6944}
6945
Bill Pembertonbaf63292012-12-03 09:23:28 -05006946static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006947{
6948 switch (tp->mac_version) {
6949 case RTL_GIGA_MAC_VER_40:
6950 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00006951 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00006952 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08006953 case RTL_GIGA_MAC_VER_44:
Hayes Wangc5583862012-07-02 17:23:22 +08006954 rtl_hw_init_8168g(tp);
6955 break;
6956
6957 default:
6958 break;
6959 }
6960}
6961
Bill Pembertonbaf63292012-12-03 09:23:28 -05006962static int
Francois Romieu3b6cf252012-03-08 09:59:04 +01006963rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6964{
6965 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
6966 const unsigned int region = cfg->region;
6967 struct rtl8169_private *tp;
6968 struct mii_if_info *mii;
6969 struct net_device *dev;
6970 void __iomem *ioaddr;
6971 int chipset, i;
6972 int rc;
6973
6974 if (netif_msg_drv(&debug)) {
6975 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
6976 MODULENAME, RTL8169_VERSION);
6977 }
6978
6979 dev = alloc_etherdev(sizeof (*tp));
6980 if (!dev) {
6981 rc = -ENOMEM;
6982 goto out;
6983 }
6984
6985 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01006986 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006987 tp = netdev_priv(dev);
6988 tp->dev = dev;
6989 tp->pci_dev = pdev;
6990 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
6991
6992 mii = &tp->mii;
6993 mii->dev = dev;
6994 mii->mdio_read = rtl_mdio_read;
6995 mii->mdio_write = rtl_mdio_write;
6996 mii->phy_id_mask = 0x1f;
6997 mii->reg_num_mask = 0x1f;
6998 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
6999
7000 /* disable ASPM completely as that cause random device stop working
7001 * problems as well as full system hangs for some PCIe devices users */
7002 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
7003 PCIE_LINK_STATE_CLKPM);
7004
7005 /* enable device (incl. PCI PM wakeup and hotplug setup) */
7006 rc = pci_enable_device(pdev);
7007 if (rc < 0) {
7008 netif_err(tp, probe, dev, "enable failure\n");
7009 goto err_out_free_dev_1;
7010 }
7011
7012 if (pci_set_mwi(pdev) < 0)
7013 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
7014
7015 /* make sure PCI base addr 1 is MMIO */
7016 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
7017 netif_err(tp, probe, dev,
7018 "region #%d not an MMIO resource, aborting\n",
7019 region);
7020 rc = -ENODEV;
7021 goto err_out_mwi_2;
7022 }
7023
7024 /* check for weird/broken PCI region reporting */
7025 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
7026 netif_err(tp, probe, dev,
7027 "Invalid PCI region size(s), aborting\n");
7028 rc = -ENODEV;
7029 goto err_out_mwi_2;
7030 }
7031
7032 rc = pci_request_regions(pdev, MODULENAME);
7033 if (rc < 0) {
7034 netif_err(tp, probe, dev, "could not request regions\n");
7035 goto err_out_mwi_2;
7036 }
7037
7038 tp->cp_cmd = RxChkSum;
7039
7040 if ((sizeof(dma_addr_t) > 4) &&
7041 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
7042 tp->cp_cmd |= PCIDAC;
7043 dev->features |= NETIF_F_HIGHDMA;
7044 } else {
7045 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7046 if (rc < 0) {
7047 netif_err(tp, probe, dev, "DMA configuration failed\n");
7048 goto err_out_free_res_3;
7049 }
7050 }
7051
7052 /* ioremap MMIO region */
7053 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
7054 if (!ioaddr) {
7055 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
7056 rc = -EIO;
7057 goto err_out_free_res_3;
7058 }
7059 tp->mmio_addr = ioaddr;
7060
7061 if (!pci_is_pcie(pdev))
7062 netif_info(tp, probe, dev, "not PCI Express\n");
7063
7064 /* Identify chip attached to board */
7065 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
7066
7067 rtl_init_rxcfg(tp);
7068
7069 rtl_irq_disable(tp);
7070
Hayes Wangc5583862012-07-02 17:23:22 +08007071 rtl_hw_initialize(tp);
7072
Francois Romieu3b6cf252012-03-08 09:59:04 +01007073 rtl_hw_reset(tp);
7074
7075 rtl_ack_events(tp, 0xffff);
7076
7077 pci_set_master(pdev);
7078
7079 /*
7080 * Pretend we are using VLANs; This bypasses a nasty bug where
7081 * Interrupts stop flowing on high load on 8110SCd controllers.
7082 */
7083 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
7084 tp->cp_cmd |= RxVlan;
7085
7086 rtl_init_mdio_ops(tp);
7087 rtl_init_pll_power_ops(tp);
7088 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08007089 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007090
7091 rtl8169_print_mac_version(tp);
7092
7093 chipset = tp->mac_version;
7094 tp->txd_version = rtl_chip_infos[chipset].txd_version;
7095
7096 RTL_W8(Cfg9346, Cfg9346_Unlock);
7097 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
Peter Wu8f9d5132013-08-17 11:00:02 +02007098 RTL_W8(Config5, RTL_R8(Config5) & (BWF | MWF | UWF | LanWake | PMEStatus));
Francois Romieu3b6cf252012-03-08 09:59:04 +01007099 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
7100 tp->features |= RTL_FEATURE_WOL;
7101 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
7102 tp->features |= RTL_FEATURE_WOL;
7103 tp->features |= rtl_try_msi(tp, cfg);
7104 RTL_W8(Cfg9346, Cfg9346_Lock);
7105
7106 if (rtl_tbi_enabled(tp)) {
7107 tp->set_speed = rtl8169_set_speed_tbi;
7108 tp->get_settings = rtl8169_gset_tbi;
7109 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
7110 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
7111 tp->link_ok = rtl8169_tbi_link_ok;
7112 tp->do_ioctl = rtl_tbi_ioctl;
7113 } else {
7114 tp->set_speed = rtl8169_set_speed_xmii;
7115 tp->get_settings = rtl8169_gset_xmii;
7116 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
7117 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
7118 tp->link_ok = rtl8169_xmii_link_ok;
7119 tp->do_ioctl = rtl_xmii_ioctl;
7120 }
7121
7122 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05007123 u64_stats_init(&tp->rx_stats.syncp);
7124 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007125
7126 /* Get MAC address */
7127 for (i = 0; i < ETH_ALEN; i++)
7128 dev->dev_addr[i] = RTL_R8(MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007129
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00007130 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007131 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007132
7133 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
7134
7135 /* don't enable SG, IP_CSUM and TSO by default - it might not work
7136 * properly for all devices */
7137 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00007138 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007139
7140 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00007141 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
7142 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007143 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7144 NETIF_F_HIGHDMA;
7145
7146 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
7147 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00007148 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007149
hayeswang5888d3f2014-07-11 16:25:56 +08007150 if (tp->txd_version == RTL_TD_0)
7151 tp->tso_csum = rtl8169_tso_csum_v1;
7152 else if (tp->txd_version == RTL_TD_1)
7153 tp->tso_csum = rtl8169_tso_csum_v2;
7154 else
7155 WARN_ON_ONCE(1);
7156
Francois Romieu3b6cf252012-03-08 09:59:04 +01007157 dev->hw_features |= NETIF_F_RXALL;
7158 dev->hw_features |= NETIF_F_RXFCS;
7159
7160 tp->hw_start = cfg->hw_start;
7161 tp->event_slow = cfg->event_slow;
7162
7163 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
7164 ~(RxBOVF | RxFOVF) : ~0;
7165
7166 init_timer(&tp->timer);
7167 tp->timer.data = (unsigned long) dev;
7168 tp->timer.function = rtl8169_phy_timer;
7169
7170 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
7171
7172 rc = register_netdev(dev);
7173 if (rc < 0)
7174 goto err_out_msi_4;
7175
7176 pci_set_drvdata(pdev, dev);
7177
Francois Romieu92a7c4e2012-03-10 10:42:12 +01007178 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
7179 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
7180 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007181 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
7182 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
7183 "tx checksumming: %s]\n",
7184 rtl_chip_infos[chipset].jumbo_max,
7185 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
7186 }
7187
7188 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
7189 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
7190 tp->mac_version == RTL_GIGA_MAC_VER_31) {
7191 rtl8168_driver_start(tp);
7192 }
7193
7194 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
7195
7196 if (pci_dev_run_wake(pdev))
7197 pm_runtime_put_noidle(&pdev->dev);
7198
7199 netif_carrier_off(dev);
7200
7201out:
7202 return rc;
7203
7204err_out_msi_4:
Devendra Nagaad1be8d2012-05-31 01:51:20 +00007205 netif_napi_del(&tp->napi);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007206 rtl_disable_msi(pdev, tp);
7207 iounmap(ioaddr);
7208err_out_free_res_3:
7209 pci_release_regions(pdev);
7210err_out_mwi_2:
7211 pci_clear_mwi(pdev);
7212 pci_disable_device(pdev);
7213err_out_free_dev_1:
7214 free_netdev(dev);
7215 goto out;
7216}
7217
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218static struct pci_driver rtl8169_pci_driver = {
7219 .name = MODULENAME,
7220 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01007221 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05007222 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02007223 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007224 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225};
7226
Devendra Naga3eeb7da2012-10-26 09:27:42 +00007227module_pci_driver(rtl8169_pci_driver);